2016-10-17 22:53:23 +03:00
|
|
|
<?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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
namespace Test\Preview;
|
|
|
|
|
|
|
|
use OC\Preview\Generator;
|
|
|
|
use OC\Preview\GeneratorHelper;
|
|
|
|
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;
|
2019-06-04 16:25:25 +03:00
|
|
|
use OCP\Preview\IProviderV2;
|
2016-11-10 16:04:59 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
/** @var Generator */
|
|
|
|
private $generator;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-10-17 22:53:23 +03:00
|
|
|
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);
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$this->generator = new Generator(
|
|
|
|
$this->config,
|
|
|
|
$this->previewManager,
|
|
|
|
$this->appData,
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->helper,
|
|
|
|
$this->eventDispatcher
|
2016-10-17 22:53:23 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCachedPreview() {
|
|
|
|
$file = $this->createMock(File::class);
|
2018-05-29 22:09:08 +03:00
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
$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');
|
2018-01-04 12:00:07 +03:00
|
|
|
$maxPreview->method('getMimeType')
|
|
|
|
->willReturn('image/png');
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$previewFolder->method('getDirectoryListing')
|
|
|
|
->willReturn([$maxPreview]);
|
|
|
|
|
|
|
|
$previewFile = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$previewFolder->method('getFile')
|
2018-10-31 15:40:41 +03:00
|
|
|
->with($this->equalTo('256-256.png'))
|
2016-10-17 22:53:23 +03:00
|
|
|
->willReturn($previewFile);
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->with(
|
|
|
|
$this->equalTo(IPreview::EVENT),
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->callback(function (GenericEvent $event) use ($file) {
|
2016-11-10 16:04:59 +03:00
|
|
|
return $event->getSubject() === $file &&
|
|
|
|
$event->getArgument('width') === 100 &&
|
|
|
|
$event->getArgument('height') === 100;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$result = $this->generator->getPreview($file, 100, 100);
|
|
|
|
$this->assertSame($previewFile, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNewPreview() {
|
|
|
|
$file = $this->createMock(File::class);
|
2018-05-29 22:09:08 +03:00
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
$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')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($key, $defult) {
|
2016-10-17 22:53:23 +03:00
|
|
|
return $defult;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-10-17 22:53:23 +03:00
|
|
|
|
2019-06-04 16:25:25 +03:00
|
|
|
$invalidProvider = $this->createMock(IProviderV2::class);
|
2018-08-16 11:45:51 +03:00
|
|
|
$invalidProvider->method('isAvailable')
|
|
|
|
->willReturn(true);
|
2019-06-04 16:25:25 +03:00
|
|
|
$unavailableProvider = $this->createMock(IProviderV2::class);
|
2018-08-16 11:45:51 +03:00
|
|
|
$unavailableProvider->method('isAvailable')
|
|
|
|
->willReturn(false);
|
2019-06-04 16:25:25 +03:00
|
|
|
$validProvider = $this->createMock(IProviderV2::class);
|
2018-08-16 11:45:51 +03:00
|
|
|
$validProvider->method('isAvailable')
|
|
|
|
->with($file)
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$this->previewManager->method('getProviders')
|
|
|
|
->willReturn([
|
|
|
|
'/image\/png/' => ['wrongProvider'],
|
2018-08-16 11:45:51 +03:00
|
|
|
'/myMimeType/' => ['brokenProvider', 'invalidProvider', 'unavailableProvider', 'validProvider'],
|
2016-10-17 22:53:23 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->helper->method('getProvider')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($provider) use ($invalidProvider, $validProvider, $unavailableProvider) {
|
2016-10-17 22:53:23 +03:00
|
|
|
if ($provider === 'wrongProvider') {
|
|
|
|
$this->fail('Wrongprovider should not be constructed!');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($provider === 'brokenProvider') {
|
2016-10-17 22:53:23 +03:00
|
|
|
return false;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($provider === 'invalidProvider') {
|
2016-10-17 22:53:23 +03:00
|
|
|
return $invalidProvider;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($provider === 'validProvider') {
|
2016-10-17 22:53:23 +03:00
|
|
|
return $validProvider;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($provider === 'unavailableProvider') {
|
2018-08-16 11:45:51 +03:00
|
|
|
return $unavailableProvider;
|
2016-10-17 22:53:23 +03:00
|
|
|
}
|
|
|
|
$this->fail('Unexpected provider requested');
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-10-17 22:53:23 +03:00
|
|
|
|
2020-07-27 17:52:44 +03:00
|
|
|
$image = $this->createMock(\OC_Image::class);
|
2016-10-17 22:53:23 +03:00
|
|
|
$image->method('width')->willReturn(2048);
|
|
|
|
$image->method('height')->willReturn(2048);
|
2017-05-02 14:23:02 +03:00
|
|
|
$image->method('valid')->willReturn(true);
|
2018-01-04 12:00:07 +03:00
|
|
|
$image->method('dataMimeType')->willReturn('image/png');
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$this->helper->method('getThumbnail')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
|
2016-10-17 22:53:23 +03:00
|
|
|
if ($provider === $validProvider) {
|
|
|
|
return $image;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$image->method('data')
|
|
|
|
->willReturn('my data');
|
|
|
|
|
|
|
|
$maxPreview = $this->createMock(ISimpleFile::class);
|
|
|
|
$maxPreview->method('getName')->willReturn('2048-2048-max.png');
|
2018-01-04 12:00:07 +03:00
|
|
|
$maxPreview->method('getMimeType')->willReturn('image/png');
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$previewFile = $this->createMock(ISimpleFile::class);
|
|
|
|
|
|
|
|
$previewFolder->method('getDirectoryListing')
|
|
|
|
->willReturn([]);
|
|
|
|
$previewFolder->method('newFile')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
|
2016-10-17 22:53:23 +03:00
|
|
|
if ($filename === '2048-2048-max.png') {
|
|
|
|
return $maxPreview;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($filename === '256-256.png') {
|
2016-10-17 22:53:23 +03:00
|
|
|
return $previewFile;
|
|
|
|
}
|
|
|
|
$this->fail('Unexpected file');
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$maxPreview->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with($this->equalTo('my data'));
|
|
|
|
|
|
|
|
$previewFolder->method('getFile')
|
2018-10-31 15:40:41 +03:00
|
|
|
->with($this->equalTo('256-256.png'))
|
2016-10-17 22:53:23 +03:00
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
|
2020-04-09 16:59:18 +03:00
|
|
|
$image = $this->getMockImage(2048, 2048, 'my resized data');
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->helper->method('getImage')
|
|
|
|
->with($this->equalTo($maxPreview))
|
|
|
|
->willReturn($image);
|
|
|
|
|
|
|
|
$previewFile->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with('my resized data');
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->with(
|
|
|
|
$this->equalTo(IPreview::EVENT),
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->callback(function (GenericEvent $event) use ($file) {
|
2016-11-10 16:04:59 +03:00
|
|
|
return $event->getSubject() === $file &&
|
|
|
|
$event->getArgument('width') === 100 &&
|
|
|
|
$event->getArgument('height') === 100;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$result = $this->generator->getPreview($file, 100, 100);
|
|
|
|
$this->assertSame($previewFile, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidMimeType() {
|
|
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
|
|
|
|
$file = $this->createMock(File::class);
|
2018-05-29 22:09:08 +03:00
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$this->previewManager->method('isMimeSupported')
|
|
|
|
->with('invalidType')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->with(
|
|
|
|
$this->equalTo(IPreview::EVENT),
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->callback(function (GenericEvent $event) use ($file) {
|
2016-11-10 16:04:59 +03:00
|
|
|
return $event->getSubject() === $file &&
|
|
|
|
$event->getArgument('width') === 0 &&
|
|
|
|
$event->getArgument('height') === 0 &&
|
|
|
|
$event->getArgument('crop') === true &&
|
|
|
|
$event->getArgument('mode') === IPreview::MODE_COVER;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoProvider() {
|
|
|
|
$file = $this->createMock(File::class);
|
2018-05-29 22:09:08 +03:00
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
$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);
|
|
|
|
|
|
|
|
$previewFolder->method('getDirectoryListing')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$this->previewManager->method('getProviders')
|
|
|
|
->willReturn([]);
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->with(
|
|
|
|
$this->equalTo(IPreview::EVENT),
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->callback(function (GenericEvent $event) use ($file) {
|
2016-11-10 16:04:59 +03:00
|
|
|
return $event->getSubject() === $file &&
|
|
|
|
$event->getArgument('width') === 100 &&
|
|
|
|
$event->getArgument('height') === 100;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
$this->generator->getPreview($file, 100, 100);
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:59:18 +03:00
|
|
|
private function getMockImage($width, $height, $data = null) {
|
2020-07-27 17:52:44 +03:00
|
|
|
$image = $this->createMock(\OC_Image::class);
|
2020-04-09 16:59:18 +03:00
|
|
|
$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);
|
|
|
|
|
2020-04-10 17:48:31 +03:00
|
|
|
$image->method('resizeCopy')->willReturnCallback(function ($size) use ($data) {
|
2020-04-09 16:59:18 +03:00
|
|
|
return $this->getMockImage($size, $size, $data);
|
|
|
|
});
|
2020-04-10 17:48:31 +03:00
|
|
|
$image->method('preciseResizeCopy')->willReturnCallback(function ($width, $height) use ($data) {
|
2020-04-09 16:59:18 +03:00
|
|
|
return $this->getMockImage($width, $height, $data);
|
|
|
|
});
|
2020-04-10 17:48:31 +03:00
|
|
|
$image->method('cropCopy')->willReturnCallback(function ($x, $y, $width, $height) use ($data) {
|
2020-04-09 16:59:18 +03:00
|
|
|
return $this->getMockImage($width, $height, $data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
public function dataSize() {
|
|
|
|
return [
|
|
|
|
[1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
|
|
|
|
[1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
|
2018-10-31 15:40:41 +03:00
|
|
|
[1024, 2048, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
|
|
|
|
[1024, 2048, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
[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],
|
2018-10-31 15:40:41 +03:00
|
|
|
[2048, 1024, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
|
|
|
|
[2048, 1024, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
[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],
|
2018-10-31 15:40:41 +03:00
|
|
|
|
|
|
|
//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],
|
2016-10-17 22:53:23 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2018-05-29 22:09:08 +03:00
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(true);
|
2016-10-17 22:53:23 +03:00
|
|
|
$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');
|
2018-01-04 12:00:07 +03:00
|
|
|
$maxPreview->method('getMimeType')
|
|
|
|
->willReturn('image/png');
|
2016-10-17 22:53:23 +03:00
|
|
|
|
|
|
|
$previewFolder->method('getDirectoryListing')
|
|
|
|
->willReturn([$maxPreview]);
|
|
|
|
|
|
|
|
$filename = $expectedX . '-' . $expectedY;
|
|
|
|
if ($crop) {
|
|
|
|
$filename .= '-crop';
|
|
|
|
}
|
|
|
|
$filename .= '.png';
|
|
|
|
$previewFolder->method('getFile')
|
|
|
|
->with($this->equalTo($filename))
|
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
|
2020-04-09 16:59:18 +03:00
|
|
|
$image = $this->getMockImage($maxX, $maxY);
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->helper->method('getImage')
|
|
|
|
->with($this->equalTo($maxPreview))
|
|
|
|
->willReturn($image);
|
|
|
|
|
|
|
|
$preview = $this->createMock(ISimpleFile::class);
|
|
|
|
$previewFolder->method('newFile')
|
|
|
|
->with($this->equalTo($filename))
|
|
|
|
->willReturn($preview);
|
|
|
|
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher->expects($this->once())
|
|
|
|
->method('dispatch')
|
|
|
|
->with(
|
|
|
|
$this->equalTo(IPreview::EVENT),
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->callback(function (GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) {
|
2016-11-10 16:04:59 +03:00
|
|
|
return $event->getSubject() === $file &&
|
|
|
|
$event->getArgument('width') === $reqX &&
|
|
|
|
$event->getArgument('height') === $reqY &&
|
|
|
|
$event->getArgument('crop') === $crop &&
|
|
|
|
$event->getArgument('mode') === $mode;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
|
2017-03-19 23:15:54 +03:00
|
|
|
if ($expectedX === $maxX && $expectedY === $maxY) {
|
|
|
|
$this->assertSame($maxPreview, $result);
|
|
|
|
} else {
|
|
|
|
$this->assertSame($preview, $result);
|
|
|
|
}
|
2016-10-17 22:53:23 +03:00
|
|
|
}
|
2018-05-29 22:09:08 +03:00
|
|
|
|
|
|
|
public function testUnreadbleFile() {
|
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
$file->method('isReadable')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
|
|
|
|
$this->generator->getPreview($file, 100, 100, false);
|
|
|
|
}
|
2016-10-17 22:53:23 +03:00
|
|
|
}
|