nextcloud/tests/lib/preview.php

148 lines
3.9 KiB
PHP
Raw Normal View History

<?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;
class Preview extends \PHPUnit_Framework_TestCase {
public function testIsPreviewDeleted() {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$x = 50;
$y = 50;
$preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
$preview->getPreview();
$fileinfo = $rootView->getFileInfo($samplefile);
$fileid = $fileinfo['fileid'];
$thumbcachefile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $x . '-' . $y . '.png';
$this->assertEquals($rootView->file_exists($thumbcachefile), true);
$preview->deletePreview();
$this->assertEquals($rootView->file_exists($thumbcachefile), false);
}
public function testAreAllPreviewsDeleted() {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$x = 50;
$y = 50;
$preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
$preview->getPreview();
$fileinfo = $rootView->getFileInfo($samplefile);
$fileid = $fileinfo['fileid'];
$thumbcachefolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/';
$this->assertEquals($rootView->is_dir($thumbcachefolder), true);
$preview->deleteAllPreviews();
$this->assertEquals($rootView->is_dir($thumbcachefolder), false);
}
public function testIsMaxSizeWorking() {
$user = $this->initFS();
$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);
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000);
$image = $preview->getPreview();
2013-07-11 15:41:09 +04:00
$this->assertEquals($image->width(), $maxX);
$this->assertEquals($image->height(), $maxY);
}
public function txtBlacklist() {
2013-09-25 12:20:40 +04:00
$txt = 'random text file';
$ics = file_get_contents(__DIR__ . '/../data/testcal.ics');
$vcf = file_get_contents(__DIR__ . '/../data/testcontact.vcf');
return array(
array('txt', $txt, false),
array('ics', $ics, true),
array('vcf', $vcf, true),
);
}
/**
* @dataProvider txtBlacklist
*/
2013-09-27 16:55:37 +04:00
public function testIsTransparent($extension, $data, $expectedResult) {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$x = 32;
$y = 32;
2013-09-27 16:55:37 +04:00
$sample = '/'.$user.'/files/test.'.$extension;
$rootView->file_put_contents($sample, $data);
2013-09-27 16:55:37 +04:00
$preview = new \OC\Preview($user, 'files/', 'test.'.$extension, $x, $y);
$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
}
private function initFS() {
2013-10-30 18:39:55 +04:00
// create a new user with his own filesystem view
// this gets called by each test in this test class
$user=uniqid();
\OC_User::setUserId($user);
\OC\Files\Filesystem::init($user, '/'.$user.'/files');
\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
return $user;
}
}