GeneratorTest.php 15 KB

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