2013-06-21 16:24:52 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Georg Ehrke <georg@ownCloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
2014-11-11 00:59:50 +03:00
|
|
|
class Preview extends TestCase {
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $user;
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $rootView;
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var \OC\Files\Storage\Storage */
|
|
|
|
private $originalStorage;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->originalStorage = \OC\Files\Filesystem::getStorage('/');
|
|
|
|
|
|
|
|
// create a new user with his own filesystem view
|
|
|
|
// this gets called by each test in this test class
|
|
|
|
$this->user = $this->getUniqueID();
|
|
|
|
\OC_User::setUserId($this->user);
|
|
|
|
\OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->rootView = new \OC\Files\View('');
|
|
|
|
$this->rootView->mkdir('/'.$this->user);
|
|
|
|
$this->rootView->mkdir('/'.$this->user.'/files');
|
|
|
|
}
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
protected function tearDown() {
|
|
|
|
\OC\Files\Filesystem::clearMounts();
|
|
|
|
\OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
public function testIsPreviewDeleted() {
|
|
|
|
|
|
|
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
|
|
|
|
|
|
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
2013-06-21 16:24:52 +04:00
|
|
|
|
|
|
|
$x = 50;
|
|
|
|
$y = 50;
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
|
2013-06-21 16:24:52 +04:00
|
|
|
$preview->getPreview();
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
2014-04-04 13:37:47 +04:00
|
|
|
$fileId = $fileInfo['fileid'];
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
|
2013-06-21 16:24:52 +04:00
|
|
|
|
|
|
|
$preview->deletePreview();
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
|
2013-06-21 16:24:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAreAllPreviewsDeleted() {
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
2013-06-21 16:24:52 +04:00
|
|
|
|
|
|
|
$x = 50;
|
|
|
|
$y = 50;
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
|
2013-06-21 16:24:52 +04:00
|
|
|
$preview->getPreview();
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
2014-04-04 13:37:47 +04:00
|
|
|
$fileId = $fileInfo['fileid'];
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$thumbCacheFolder = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/';
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true);
|
2013-06-21 16:24:52 +04:00
|
|
|
|
|
|
|
$preview->deleteAllPreviews();
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
|
2013-06-21 16:24:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsMaxSizeWorking() {
|
|
|
|
|
|
|
|
$maxX = 250;
|
|
|
|
$maxY = 250;
|
|
|
|
|
2013-07-11 15:39:10 +04:00
|
|
|
\OC_Config::setValue('preview_max_x', $maxX);
|
|
|
|
\OC_Config::setValue('preview_max_y', $maxY);
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
2013-06-21 16:24:52 +04:00
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', 1000, 1000);
|
2013-06-21 16:24:52 +04:00
|
|
|
$image = $preview->getPreview();
|
2013-07-11 15:41:09 +04:00
|
|
|
|
2013-06-21 16:24:52 +04:00
|
|
|
$this->assertEquals($image->width(), $maxX);
|
|
|
|
$this->assertEquals($image->height(), $maxY);
|
|
|
|
}
|
|
|
|
|
2013-09-27 13:33:37 +04:00
|
|
|
public function txtBlacklist() {
|
2013-09-25 12:20:40 +04:00
|
|
|
$txt = 'random text file';
|
|
|
|
|
2013-09-27 11:59:04 +04:00
|
|
|
return array(
|
2013-09-27 13:01:47 +04:00
|
|
|
array('txt', $txt, false),
|
2013-09-27 11:59:04 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-27 13:33:37 +04:00
|
|
|
* @dataProvider txtBlacklist
|
2013-09-27 11:59:04 +04:00
|
|
|
*/
|
2013-09-27 16:55:37 +04:00
|
|
|
public function testIsTransparent($extension, $data, $expectedResult) {
|
2013-09-27 13:01:47 +04:00
|
|
|
|
2013-09-27 11:59:04 +04:00
|
|
|
$x = 32;
|
|
|
|
$y = 32;
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
$sample = '/'.$this->user.'/files/test.'.$extension;
|
|
|
|
$this->rootView->file_put_contents($sample, $data);
|
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.'.$extension, $x, $y);
|
2013-09-27 11:59:04 +04:00
|
|
|
$image = $preview->getPreview();
|
|
|
|
$resource = $image->resource();
|
|
|
|
|
|
|
|
//http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
|
|
|
|
$colorIndex = imagecolorat($resource, 1, 1);
|
|
|
|
$colorInfo = imagecolorsforindex($resource, $colorIndex);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expectedResult,
|
|
|
|
$colorInfo['alpha'] === 127,
|
|
|
|
'Failed asserting that only previews for text files are transparent.'
|
|
|
|
);
|
2013-09-25 12:20:40 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 18:21:50 +04:00
|
|
|
public function testCreationFromCached() {
|
|
|
|
|
|
|
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
|
|
|
|
|
|
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
|
|
|
|
|
|
|
// create base preview
|
|
|
|
$x = 150;
|
|
|
|
$y = 150;
|
|
|
|
|
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
|
|
|
|
$preview->getPreview();
|
|
|
|
|
|
|
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
|
|
|
$fileId = $fileInfo['fileid'];
|
|
|
|
|
|
|
|
$thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
|
|
|
|
|
|
|
|
$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
|
|
|
|
|
|
|
|
|
|
|
|
// create smaller previews
|
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50);
|
|
|
|
$isCached = $preview->isCached($fileId);
|
|
|
|
|
|
|
|
$this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public function testScalingUp() {
|
|
|
|
|
|
|
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
|
|
|
|
|
|
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
|
|
|
|
|
|
|
// create base preview
|
|
|
|
$x = 150;
|
|
|
|
$y = 150;
|
|
|
|
|
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
|
|
|
|
$preview->getPreview();
|
|
|
|
|
|
|
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
|
|
|
$fileId = $fileInfo['fileid'];
|
|
|
|
|
|
|
|
$thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
|
|
|
|
|
|
|
|
$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
|
|
|
|
|
|
|
|
|
|
|
|
// create bigger previews - with scale up
|
|
|
|
$preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250);
|
|
|
|
$isCached = $preview->isCached($fileId);
|
|
|
|
|
|
|
|
$this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
|
|
|
|
}
|
|
|
|
*/
|
2013-09-27 13:33:37 +04:00
|
|
|
}
|