123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- <?php
- /**
- * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace Test\Preview;
- use OC\Preview\Generator;
- use OC\Preview\GeneratorHelper;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\File;
- use OCP\Files\IAppData;
- use OCP\Files\NotFoundException;
- use OCP\Files\SimpleFS\ISimpleFile;
- use OCP\Files\SimpleFS\ISimpleFolder;
- use OCP\IConfig;
- use OCP\IPreview;
- use OCP\Preview\BeforePreviewFetchedEvent;
- use OCP\Preview\IProviderV2;
- class GeneratorTest extends \Test\TestCase {
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $config;
- /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
- private $previewManager;
- /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
- private $appData;
- /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */
- private $helper;
- /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
- private $eventDispatcher;
- /** @var Generator */
- private $generator;
- protected function setUp(): void {
- parent::setUp();
- $this->config = $this->createMock(IConfig::class);
- $this->previewManager = $this->createMock(IPreview::class);
- $this->appData = $this->createMock(IAppData::class);
- $this->helper = $this->createMock(GeneratorHelper::class);
- $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
- $this->generator = new Generator(
- $this->config,
- $this->previewManager,
- $this->appData,
- $this->helper,
- $this->eventDispatcher
- );
- }
- public function testGetCachedPreview() {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getMimeType')
- ->willReturn('myMimeType');
- $file->method('getId')
- ->willReturn(42);
- $this->previewManager->method('isMimeSupported')
- ->with($this->equalTo('myMimeType'))
- ->willReturn(true);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $maxPreview = $this->createMock(ISimpleFile::class);
- $maxPreview->method('getName')
- ->willReturn('1000-1000-max.png');
- $maxPreview->method('getSize')->willReturn(1000);
- $maxPreview->method('getMimeType')
- ->willReturn('image/png');
- $previewFile = $this->createMock(ISimpleFile::class);
- $previewFile->method('getSize')->willReturn(1000);
- $previewFile->method('getName')->willReturn('256-256.png');
- $previewFolder->method('getDirectoryListing')
- ->willReturn([$maxPreview, $previewFile]);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL));
- $result = $this->generator->getPreview($file, 100, 100);
- $this->assertSame($previewFile, $result);
- }
- public function testGetNewPreview() {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getMimeType')
- ->willReturn('myMimeType');
- $file->method('getId')
- ->willReturn(42);
- $this->previewManager->method('isMimeSupported')
- ->with($this->equalTo('myMimeType'))
- ->willReturn(true);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willThrowException(new NotFoundException());
- $this->appData->method('newFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $this->config->method('getSystemValue')
- ->willReturnCallback(function ($key, $default) {
- return $default;
- });
- $this->config->method('getSystemValueInt')
- ->willReturnCallback(function ($key, $default) {
- return $default;
- });
- $invalidProvider = $this->createMock(IProviderV2::class);
- $invalidProvider->method('isAvailable')
- ->willReturn(true);
- $unavailableProvider = $this->createMock(IProviderV2::class);
- $unavailableProvider->method('isAvailable')
- ->willReturn(false);
- $validProvider = $this->createMock(IProviderV2::class);
- $validProvider->method('isAvailable')
- ->with($file)
- ->willReturn(true);
- $this->previewManager->method('getProviders')
- ->willReturn([
- '/image\/png/' => ['wrongProvider'],
- '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'unavailableProvider', 'validProvider'],
- ]);
- $this->helper->method('getProvider')
- ->willReturnCallback(function ($provider) use ($invalidProvider, $validProvider, $unavailableProvider) {
- if ($provider === 'wrongProvider') {
- $this->fail('Wrongprovider should not be constructed!');
- } elseif ($provider === 'brokenProvider') {
- return false;
- } elseif ($provider === 'invalidProvider') {
- return $invalidProvider;
- } elseif ($provider === 'validProvider') {
- return $validProvider;
- } elseif ($provider === 'unavailableProvider') {
- return $unavailableProvider;
- }
- $this->fail('Unexpected provider requested');
- });
- $image = $this->createMock(\OC_Image::class);
- $image->method('width')->willReturn(2048);
- $image->method('height')->willReturn(2048);
- $image->method('valid')->willReturn(true);
- $image->method('dataMimeType')->willReturn('image/png');
- $this->helper->method('getThumbnail')
- ->willReturnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
- if ($provider === $validProvider) {
- return $image;
- } else {
- return false;
- }
- });
- $image->method('data')
- ->willReturn('my data');
- $maxPreview = $this->createMock(ISimpleFile::class);
- $maxPreview->method('getName')->willReturn('2048-2048-max.png');
- $maxPreview->method('getMimeType')->willReturn('image/png');
- $maxPreview->method('getSize')->willReturn(1000);
- $previewFile = $this->createMock(ISimpleFile::class);
- $previewFile->method('getSize')->willReturn(1000);
- $previewFolder->method('getDirectoryListing')
- ->willReturn([]);
- $previewFolder->method('newFile')
- ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
- if ($filename === '2048-2048-max.png') {
- return $maxPreview;
- } elseif ($filename === '256-256.png') {
- return $previewFile;
- }
- $this->fail('Unexpected file');
- });
- $maxPreview->expects($this->once())
- ->method('putContent')
- ->with($this->equalTo('my data'));
- $previewFolder->method('getFile')
- ->with($this->equalTo('256-256.png'))
- ->willThrowException(new NotFoundException());
- $image = $this->getMockImage(2048, 2048, 'my resized data');
- $this->helper->method('getImage')
- ->with($this->equalTo($maxPreview))
- ->willReturn($image);
- $previewFile->expects($this->once())
- ->method('putContent')
- ->with('my resized data');
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL));
- $result = $this->generator->getPreview($file, 100, 100);
- $this->assertSame($previewFile, $result);
- }
- public function testInvalidMimeType() {
- $this->expectException(NotFoundException::class);
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getId')
- ->willReturn(42);
- $this->previewManager->method('isMimeSupported')
- ->with('invalidType')
- ->willReturn(false);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $maxPreview = $this->createMock(ISimpleFile::class);
- $maxPreview->method('getName')
- ->willReturn('2048-2048-max.png');
- $maxPreview->method('getMimeType')
- ->willReturn('image/png');
- $previewFolder->method('getDirectoryListing')
- ->willReturn([$maxPreview]);
- $previewFolder->method('getFile')
- ->with($this->equalTo('1024-512-crop.png'))
- ->willThrowException(new NotFoundException());
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER));
- $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
- }
- public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getId')
- ->willReturn(42);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $maxPreview = $this->createMock(ISimpleFile::class);
- $maxPreview->method('getName')
- ->willReturn('2048-2048-max.png');
- $maxPreview->method('getSize')->willReturn(1000);
- $maxPreview->method('getMimeType')
- ->willReturn('image/png');
- $preview = $this->createMock(ISimpleFile::class);
- $preview->method('getSize')->willReturn(1000);
- $preview->method('getName')->willReturn('1024-512-crop.png');
- $previewFolder->method('getDirectoryListing')
- ->willReturn([$maxPreview, $preview]);
- $this->previewManager->expects($this->never())
- ->method('isMimeSupported');
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER));
- $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
- $this->assertSame($preview, $result);
- }
- public function testNoProvider() {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getMimeType')
- ->willReturn('myMimeType');
- $file->method('getId')
- ->willReturn(42);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $previewFolder->method('getDirectoryListing')
- ->willReturn([]);
- $this->previewManager->method('getProviders')
- ->willReturn([]);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL));
- $this->expectException(NotFoundException::class);
- $this->generator->getPreview($file, 100, 100);
- }
- private function getMockImage($width, $height, $data = null) {
- $image = $this->createMock(\OC_Image::class);
- $image->method('height')->willReturn($width);
- $image->method('width')->willReturn($height);
- $image->method('valid')->willReturn(true);
- $image->method('dataMimeType')->willReturn('image/png');
- $image->method('data')->willReturn($data);
- $image->method('resizeCopy')->willReturnCallback(function ($size) use ($data) {
- return $this->getMockImage($size, $size, $data);
- });
- $image->method('preciseResizeCopy')->willReturnCallback(function ($width, $height) use ($data) {
- return $this->getMockImage($width, $height, $data);
- });
- $image->method('cropCopy')->willReturnCallback(function ($x, $y, $width, $height) use ($data) {
- return $this->getMockImage($width, $height, $data);
- });
- return $image;
- }
- public function dataSize() {
- return [
- [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
- [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
- [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
- [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
- [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
- [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
- [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
- [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
- [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
- [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
- [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
- [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
- [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
- [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
- [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
- [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
- [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
- [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
- //Test minimum size
- [2048, 1024, 32, 32, false, IPreview::MODE_FILL, 64, 32],
- [2048, 1024, 32, 32, false, IPreview::MODE_COVER, 64, 32],
- [2048, 1024, 32, 32, true, IPreview::MODE_FILL, 64, 64],
- [2048, 1024, 32, 32, true, IPreview::MODE_COVER, 64, 64],
- ];
- }
- /**
- * @dataProvider dataSize
- *
- * @param int $maxX
- * @param int $maxY
- * @param int $reqX
- * @param int $reqY
- * @param bool $crop
- * @param string $mode
- * @param int $expectedX
- * @param int $expectedY
- */
- public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(true);
- $file->method('getMimeType')
- ->willReturn('myMimeType');
- $file->method('getId')
- ->willReturn(42);
- $this->previewManager->method('isMimeSupported')
- ->with($this->equalTo('myMimeType'))
- ->willReturn(true);
- $previewFolder = $this->createMock(ISimpleFolder::class);
- $this->appData->method('getFolder')
- ->with($this->equalTo(42))
- ->willReturn($previewFolder);
- $maxPreview = $this->createMock(ISimpleFile::class);
- $maxPreview->method('getName')
- ->willReturn($maxX . '-' . $maxY . '-max.png');
- $maxPreview->method('getMimeType')
- ->willReturn('image/png');
- $maxPreview->method('getSize')->willReturn(1000);
- $previewFolder->method('getDirectoryListing')
- ->willReturn([$maxPreview]);
- $filename = $expectedX . '-' . $expectedY;
- if ($crop) {
- $filename .= '-crop';
- }
- $filename .= '.png';
- $previewFolder->method('getFile')
- ->with($this->equalTo($filename))
- ->willThrowException(new NotFoundException());
- $image = $this->getMockImage($maxX, $maxY);
- $this->helper->method('getImage')
- ->with($this->equalTo($maxPreview))
- ->willReturn($image);
- $preview = $this->createMock(ISimpleFile::class);
- $preview->method('getSize')->willReturn(1000);
- $previewFolder->method('newFile')
- ->with($this->equalTo($filename))
- ->willReturn($preview);
- $this->eventDispatcher->expects($this->once())
- ->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode));
- $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
- if ($expectedX === $maxX && $expectedY === $maxY) {
- $this->assertSame($maxPreview, $result);
- } else {
- $this->assertSame($preview, $result);
- }
- }
- public function testUnreadbleFile() {
- $file = $this->createMock(File::class);
- $file->method('isReadable')
- ->willReturn(false);
- $this->expectException(NotFoundException::class);
- $this->generator->getPreview($file, 100, 100, false);
- }
- }
|