GeneratorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Preview;
  7. use OC\Preview\Generator;
  8. use OC\Preview\GeneratorHelper;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use OCP\Files\File;
  11. use OCP\Files\IAppData;
  12. use OCP\Files\NotFoundException;
  13. use OCP\Files\SimpleFS\ISimpleFile;
  14. use OCP\Files\SimpleFS\ISimpleFolder;
  15. use OCP\IConfig;
  16. use OCP\IImage;
  17. use OCP\IPreview;
  18. use OCP\Preview\BeforePreviewFetchedEvent;
  19. use OCP\Preview\IProviderV2;
  20. class GeneratorTest extends \Test\TestCase {
  21. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  22. private $config;
  23. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  24. private $previewManager;
  25. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  26. private $appData;
  27. /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */
  28. private $helper;
  29. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  30. private $eventDispatcher;
  31. /** @var Generator */
  32. private $generator;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->previewManager = $this->createMock(IPreview::class);
  37. $this->appData = $this->createMock(IAppData::class);
  38. $this->helper = $this->createMock(GeneratorHelper::class);
  39. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  40. $this->generator = new Generator(
  41. $this->config,
  42. $this->previewManager,
  43. $this->appData,
  44. $this->helper,
  45. $this->eventDispatcher
  46. );
  47. }
  48. public function testGetCachedPreview(): void {
  49. $file = $this->createMock(File::class);
  50. $file->method('isReadable')
  51. ->willReturn(true);
  52. $file->method('getMimeType')
  53. ->willReturn('myMimeType');
  54. $file->method('getId')
  55. ->willReturn(42);
  56. $this->previewManager->method('isMimeSupported')
  57. ->with($this->equalTo('myMimeType'))
  58. ->willReturn(true);
  59. $previewFolder = $this->createMock(ISimpleFolder::class);
  60. $this->appData->method('getFolder')
  61. ->with($this->equalTo(42))
  62. ->willReturn($previewFolder);
  63. $maxPreview = $this->createMock(ISimpleFile::class);
  64. $maxPreview->method('getName')
  65. ->willReturn('1000-1000-max.png');
  66. $maxPreview->method('getSize')->willReturn(1000);
  67. $maxPreview->method('getMimeType')
  68. ->willReturn('image/png');
  69. $previewFile = $this->createMock(ISimpleFile::class);
  70. $previewFile->method('getSize')->willReturn(1000);
  71. $previewFile->method('getName')->willReturn('256-256.png');
  72. $previewFolder->method('getDirectoryListing')
  73. ->willReturn([$maxPreview, $previewFile]);
  74. $this->eventDispatcher->expects($this->once())
  75. ->method('dispatchTyped')
  76. ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));
  77. $result = $this->generator->getPreview($file, 100, 100);
  78. $this->assertSame($previewFile, $result);
  79. }
  80. public function testGetNewPreview(): void {
  81. $file = $this->createMock(File::class);
  82. $file->method('isReadable')
  83. ->willReturn(true);
  84. $file->method('getMimeType')
  85. ->willReturn('myMimeType');
  86. $file->method('getId')
  87. ->willReturn(42);
  88. $this->previewManager->method('isMimeSupported')
  89. ->with($this->equalTo('myMimeType'))
  90. ->willReturn(true);
  91. $previewFolder = $this->createMock(ISimpleFolder::class);
  92. $this->appData->method('getFolder')
  93. ->with($this->equalTo(42))
  94. ->willThrowException(new NotFoundException());
  95. $this->appData->method('newFolder')
  96. ->with($this->equalTo(42))
  97. ->willReturn($previewFolder);
  98. $this->config->method('getSystemValue')
  99. ->willReturnCallback(function ($key, $default) {
  100. return $default;
  101. });
  102. $this->config->method('getSystemValueInt')
  103. ->willReturnCallback(function ($key, $default) {
  104. return $default;
  105. });
  106. $invalidProvider = $this->createMock(IProviderV2::class);
  107. $invalidProvider->method('isAvailable')
  108. ->willReturn(true);
  109. $unavailableProvider = $this->createMock(IProviderV2::class);
  110. $unavailableProvider->method('isAvailable')
  111. ->willReturn(false);
  112. $validProvider = $this->createMock(IProviderV2::class);
  113. $validProvider->method('isAvailable')
  114. ->with($file)
  115. ->willReturn(true);
  116. $this->previewManager->method('getProviders')
  117. ->willReturn([
  118. '/image\/png/' => ['wrongProvider'],
  119. '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'unavailableProvider', 'validProvider'],
  120. ]);
  121. $this->helper->method('getProvider')
  122. ->willReturnCallback(function ($provider) use ($invalidProvider, $validProvider, $unavailableProvider) {
  123. if ($provider === 'wrongProvider') {
  124. $this->fail('Wrongprovider should not be constructed!');
  125. } elseif ($provider === 'brokenProvider') {
  126. return false;
  127. } elseif ($provider === 'invalidProvider') {
  128. return $invalidProvider;
  129. } elseif ($provider === 'validProvider') {
  130. return $validProvider;
  131. } elseif ($provider === 'unavailableProvider') {
  132. return $unavailableProvider;
  133. }
  134. $this->fail('Unexpected provider requested');
  135. });
  136. $image = $this->createMock(IImage::class);
  137. $image->method('width')->willReturn(2048);
  138. $image->method('height')->willReturn(2048);
  139. $image->method('valid')->willReturn(true);
  140. $image->method('dataMimeType')->willReturn('image/png');
  141. $this->helper->method('getThumbnail')
  142. ->willReturnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
  143. if ($provider === $validProvider) {
  144. return $image;
  145. } else {
  146. return false;
  147. }
  148. });
  149. $image->method('data')
  150. ->willReturn('my data');
  151. $maxPreview = $this->createMock(ISimpleFile::class);
  152. $maxPreview->method('getName')->willReturn('2048-2048-max.png');
  153. $maxPreview->method('getMimeType')->willReturn('image/png');
  154. $maxPreview->method('getSize')->willReturn(1000);
  155. $previewFile = $this->createMock(ISimpleFile::class);
  156. $previewFile->method('getSize')->willReturn(1000);
  157. $previewFolder->method('getDirectoryListing')
  158. ->willReturn([]);
  159. $previewFolder->method('newFile')
  160. ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
  161. if ($filename === '2048-2048-max.png') {
  162. return $maxPreview;
  163. } elseif ($filename === '256-256.png') {
  164. return $previewFile;
  165. }
  166. $this->fail('Unexpected file');
  167. });
  168. $maxPreview->expects($this->once())
  169. ->method('putContent')
  170. ->with($this->equalTo('my data'));
  171. $previewFolder->method('getFile')
  172. ->with($this->equalTo('256-256.png'))
  173. ->willThrowException(new NotFoundException());
  174. $image = $this->getMockImage(2048, 2048, 'my resized data');
  175. $this->helper->method('getImage')
  176. ->with($this->equalTo($maxPreview))
  177. ->willReturn($image);
  178. $previewFile->expects($this->once())
  179. ->method('putContent')
  180. ->with('my resized data');
  181. $this->eventDispatcher->expects($this->once())
  182. ->method('dispatchTyped')
  183. ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));
  184. $result = $this->generator->getPreview($file, 100, 100);
  185. $this->assertSame($previewFile, $result);
  186. }
  187. public function testInvalidMimeType(): void {
  188. $this->expectException(NotFoundException::class);
  189. $file = $this->createMock(File::class);
  190. $file->method('isReadable')
  191. ->willReturn(true);
  192. $file->method('getId')
  193. ->willReturn(42);
  194. $this->previewManager->method('isMimeSupported')
  195. ->with('invalidType')
  196. ->willReturn(false);
  197. $previewFolder = $this->createMock(ISimpleFolder::class);
  198. $this->appData->method('getFolder')
  199. ->with($this->equalTo(42))
  200. ->willReturn($previewFolder);
  201. $maxPreview = $this->createMock(ISimpleFile::class);
  202. $maxPreview->method('getName')
  203. ->willReturn('2048-2048-max.png');
  204. $maxPreview->method('getMimeType')
  205. ->willReturn('image/png');
  206. $previewFolder->method('getDirectoryListing')
  207. ->willReturn([$maxPreview]);
  208. $previewFolder->method('getFile')
  209. ->with($this->equalTo('1024-512-crop.png'))
  210. ->willThrowException(new NotFoundException());
  211. $this->eventDispatcher->expects($this->once())
  212. ->method('dispatchTyped')
  213. ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'));
  214. $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  215. }
  216. public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype(): void {
  217. $file = $this->createMock(File::class);
  218. $file->method('isReadable')
  219. ->willReturn(true);
  220. $file->method('getId')
  221. ->willReturn(42);
  222. $previewFolder = $this->createMock(ISimpleFolder::class);
  223. $this->appData->method('getFolder')
  224. ->with($this->equalTo(42))
  225. ->willReturn($previewFolder);
  226. $maxPreview = $this->createMock(ISimpleFile::class);
  227. $maxPreview->method('getName')
  228. ->willReturn('2048-2048-max.png');
  229. $maxPreview->method('getSize')->willReturn(1000);
  230. $maxPreview->method('getMimeType')
  231. ->willReturn('image/png');
  232. $preview = $this->createMock(ISimpleFile::class);
  233. $preview->method('getSize')->willReturn(1000);
  234. $preview->method('getName')->willReturn('1024-512-crop.png');
  235. $previewFolder->method('getDirectoryListing')
  236. ->willReturn([$maxPreview, $preview]);
  237. $this->previewManager->expects($this->never())
  238. ->method('isMimeSupported');
  239. $this->eventDispatcher->expects($this->once())
  240. ->method('dispatchTyped')
  241. ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'));
  242. $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  243. $this->assertSame($preview, $result);
  244. }
  245. public function testNoProvider(): void {
  246. $file = $this->createMock(File::class);
  247. $file->method('isReadable')
  248. ->willReturn(true);
  249. $file->method('getMimeType')
  250. ->willReturn('myMimeType');
  251. $file->method('getId')
  252. ->willReturn(42);
  253. $previewFolder = $this->createMock(ISimpleFolder::class);
  254. $this->appData->method('getFolder')
  255. ->with($this->equalTo(42))
  256. ->willReturn($previewFolder);
  257. $previewFolder->method('getDirectoryListing')
  258. ->willReturn([]);
  259. $this->previewManager->method('getProviders')
  260. ->willReturn([]);
  261. $this->eventDispatcher->expects($this->once())
  262. ->method('dispatchTyped')
  263. ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null));
  264. $this->expectException(NotFoundException::class);
  265. $this->generator->getPreview($file, 100, 100);
  266. }
  267. private function getMockImage($width, $height, $data = null) {
  268. $image = $this->createMock(IImage::class);
  269. $image->method('height')->willReturn($width);
  270. $image->method('width')->willReturn($height);
  271. $image->method('valid')->willReturn(true);
  272. $image->method('dataMimeType')->willReturn('image/png');
  273. $image->method('data')->willReturn($data);
  274. $image->method('resizeCopy')->willReturnCallback(function ($size) use ($data) {
  275. return $this->getMockImage($size, $size, $data);
  276. });
  277. $image->method('preciseResizeCopy')->willReturnCallback(function ($width, $height) use ($data) {
  278. return $this->getMockImage($width, $height, $data);
  279. });
  280. $image->method('cropCopy')->willReturnCallback(function ($x, $y, $width, $height) use ($data) {
  281. return $this->getMockImage($width, $height, $data);
  282. });
  283. return $image;
  284. }
  285. public function dataSize() {
  286. return [
  287. [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
  288. [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
  289. [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  290. [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  291. [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
  292. [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
  293. [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
  294. [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
  295. [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
  296. [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
  297. [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
  298. [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
  299. [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  300. [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  301. [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
  302. [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
  303. [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
  304. [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
  305. //Test minimum size
  306. [2048, 1024, 32, 32, false, IPreview::MODE_FILL, 64, 32],
  307. [2048, 1024, 32, 32, false, IPreview::MODE_COVER, 64, 32],
  308. [2048, 1024, 32, 32, true, IPreview::MODE_FILL, 64, 64],
  309. [2048, 1024, 32, 32, true, IPreview::MODE_COVER, 64, 64],
  310. ];
  311. }
  312. /**
  313. * @dataProvider dataSize
  314. *
  315. * @param int $maxX
  316. * @param int $maxY
  317. * @param int $reqX
  318. * @param int $reqY
  319. * @param bool $crop
  320. * @param string $mode
  321. * @param int $expectedX
  322. * @param int $expectedY
  323. */
  324. public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY): void {
  325. $file = $this->createMock(File::class);
  326. $file->method('isReadable')
  327. ->willReturn(true);
  328. $file->method('getMimeType')
  329. ->willReturn('myMimeType');
  330. $file->method('getId')
  331. ->willReturn(42);
  332. $this->previewManager->method('isMimeSupported')
  333. ->with($this->equalTo('myMimeType'))
  334. ->willReturn(true);
  335. $previewFolder = $this->createMock(ISimpleFolder::class);
  336. $this->appData->method('getFolder')
  337. ->with($this->equalTo(42))
  338. ->willReturn($previewFolder);
  339. $maxPreview = $this->createMock(ISimpleFile::class);
  340. $maxPreview->method('getName')
  341. ->willReturn($maxX . '-' . $maxY . '-max.png');
  342. $maxPreview->method('getMimeType')
  343. ->willReturn('image/png');
  344. $maxPreview->method('getSize')->willReturn(1000);
  345. $previewFolder->method('getDirectoryListing')
  346. ->willReturn([$maxPreview]);
  347. $filename = $expectedX . '-' . $expectedY;
  348. if ($crop) {
  349. $filename .= '-crop';
  350. }
  351. $filename .= '.png';
  352. $previewFolder->method('getFile')
  353. ->with($this->equalTo($filename))
  354. ->willThrowException(new NotFoundException());
  355. $image = $this->getMockImage($maxX, $maxY);
  356. $this->helper->method('getImage')
  357. ->with($this->equalTo($maxPreview))
  358. ->willReturn($image);
  359. $preview = $this->createMock(ISimpleFile::class);
  360. $preview->method('getSize')->willReturn(1000);
  361. $previewFolder->method('newFile')
  362. ->with($this->equalTo($filename))
  363. ->willReturn($preview);
  364. $this->eventDispatcher->expects($this->once())
  365. ->method('dispatchTyped')
  366. ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null));
  367. $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
  368. if ($expectedX === $maxX && $expectedY === $maxY) {
  369. $this->assertSame($maxPreview, $result);
  370. } else {
  371. $this->assertSame($preview, $result);
  372. }
  373. }
  374. public function testUnreadbleFile(): void {
  375. $file = $this->createMock(File::class);
  376. $file->method('isReadable')
  377. ->willReturn(false);
  378. $this->expectException(NotFoundException::class);
  379. $this->generator->getPreview($file, 100, 100, false);
  380. }
  381. }