GeneratorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Preview;
  24. use OC\Preview\Generator;
  25. use OC\Preview\GeneratorHelper;
  26. use OCP\EventDispatcher\IEventDispatcher;
  27. use OCP\Files\File;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\Files\SimpleFS\ISimpleFolder;
  32. use OCP\IConfig;
  33. use OCP\IPreview;
  34. use OCP\Preview\BeforePreviewFetchedEvent;
  35. use OCP\Preview\IProviderV2;
  36. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  37. use Symfony\Component\EventDispatcher\GenericEvent;
  38. class GeneratorTest extends \Test\TestCase {
  39. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  40. private $config;
  41. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  42. private $previewManager;
  43. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  44. private $appData;
  45. /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */
  46. private $helper;
  47. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  48. private $eventDispatcher;
  49. /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
  50. private $legacyEventDispatcher;
  51. /** @var Generator */
  52. private $generator;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. $this->config = $this->createMock(IConfig::class);
  56. $this->previewManager = $this->createMock(IPreview::class);
  57. $this->appData = $this->createMock(IAppData::class);
  58. $this->helper = $this->createMock(GeneratorHelper::class);
  59. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  60. $this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class);
  61. $this->generator = new Generator(
  62. $this->config,
  63. $this->previewManager,
  64. $this->appData,
  65. $this->helper,
  66. $this->legacyEventDispatcher,
  67. $this->eventDispatcher
  68. );
  69. }
  70. public function testGetCachedPreview() {
  71. $file = $this->createMock(File::class);
  72. $file->method('isReadable')
  73. ->willReturn(true);
  74. $file->method('getMimeType')
  75. ->willReturn('myMimeType');
  76. $file->method('getId')
  77. ->willReturn(42);
  78. $this->previewManager->method('isMimeSupported')
  79. ->with($this->equalTo('myMimeType'))
  80. ->willReturn(true);
  81. $previewFolder = $this->createMock(ISimpleFolder::class);
  82. $this->appData->method('getFolder')
  83. ->with($this->equalTo(42))
  84. ->willReturn($previewFolder);
  85. $maxPreview = $this->createMock(ISimpleFile::class);
  86. $maxPreview->method('getName')
  87. ->willReturn('1000-1000-max.png');
  88. $maxPreview->method('getSize')->willReturn(1000);
  89. $maxPreview->method('getMimeType')
  90. ->willReturn('image/png');
  91. $previewFolder->method('getDirectoryListing')
  92. ->willReturn([$maxPreview]);
  93. $previewFile = $this->createMock(ISimpleFile::class);
  94. $previewFile->method('getSize')->willReturn(1000);
  95. $previewFolder->method('getFile')
  96. ->with($this->equalTo('256-256.png'))
  97. ->willReturn($previewFile);
  98. $this->legacyEventDispatcher->expects($this->once())
  99. ->method('dispatch')
  100. ->with(
  101. $this->equalTo(IPreview::EVENT),
  102. $this->callback(function (GenericEvent $event) use ($file) {
  103. return $event->getSubject() === $file &&
  104. $event->getArgument('width') === 100 &&
  105. $event->getArgument('height') === 100;
  106. })
  107. );
  108. $this->eventDispatcher->expects($this->once())
  109. ->method('dispatchTyped')
  110. ->with(new BeforePreviewFetchedEvent($file));
  111. $result = $this->generator->getPreview($file, 100, 100);
  112. $this->assertSame($previewFile, $result);
  113. }
  114. public function testGetNewPreview() {
  115. $file = $this->createMock(File::class);
  116. $file->method('isReadable')
  117. ->willReturn(true);
  118. $file->method('getMimeType')
  119. ->willReturn('myMimeType');
  120. $file->method('getId')
  121. ->willReturn(42);
  122. $this->previewManager->method('isMimeSupported')
  123. ->with($this->equalTo('myMimeType'))
  124. ->willReturn(true);
  125. $previewFolder = $this->createMock(ISimpleFolder::class);
  126. $this->appData->method('getFolder')
  127. ->with($this->equalTo(42))
  128. ->willThrowException(new NotFoundException());
  129. $this->appData->method('newFolder')
  130. ->with($this->equalTo(42))
  131. ->willReturn($previewFolder);
  132. $this->config->method('getSystemValue')
  133. ->willReturnCallback(function ($key, $default) {
  134. return $default;
  135. });
  136. $this->config->method('getSystemValueInt')
  137. ->willReturnCallback(function ($key, $default) {
  138. return $default;
  139. });
  140. $invalidProvider = $this->createMock(IProviderV2::class);
  141. $invalidProvider->method('isAvailable')
  142. ->willReturn(true);
  143. $unavailableProvider = $this->createMock(IProviderV2::class);
  144. $unavailableProvider->method('isAvailable')
  145. ->willReturn(false);
  146. $validProvider = $this->createMock(IProviderV2::class);
  147. $validProvider->method('isAvailable')
  148. ->with($file)
  149. ->willReturn(true);
  150. $this->previewManager->method('getProviders')
  151. ->willReturn([
  152. '/image\/png/' => ['wrongProvider'],
  153. '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'unavailableProvider', 'validProvider'],
  154. ]);
  155. $this->helper->method('getProvider')
  156. ->willReturnCallback(function ($provider) use ($invalidProvider, $validProvider, $unavailableProvider) {
  157. if ($provider === 'wrongProvider') {
  158. $this->fail('Wrongprovider should not be constructed!');
  159. } elseif ($provider === 'brokenProvider') {
  160. return false;
  161. } elseif ($provider === 'invalidProvider') {
  162. return $invalidProvider;
  163. } elseif ($provider === 'validProvider') {
  164. return $validProvider;
  165. } elseif ($provider === 'unavailableProvider') {
  166. return $unavailableProvider;
  167. }
  168. $this->fail('Unexpected provider requested');
  169. });
  170. $image = $this->createMock(\OC_Image::class);
  171. $image->method('width')->willReturn(2048);
  172. $image->method('height')->willReturn(2048);
  173. $image->method('valid')->willReturn(true);
  174. $image->method('dataMimeType')->willReturn('image/png');
  175. $this->helper->method('getThumbnail')
  176. ->willReturnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
  177. if ($provider === $validProvider) {
  178. return $image;
  179. } else {
  180. return false;
  181. }
  182. });
  183. $image->method('data')
  184. ->willReturn('my data');
  185. $maxPreview = $this->createMock(ISimpleFile::class);
  186. $maxPreview->method('getName')->willReturn('2048-2048-max.png');
  187. $maxPreview->method('getMimeType')->willReturn('image/png');
  188. $maxPreview->method('getSize')->willReturn(1000);
  189. $previewFile = $this->createMock(ISimpleFile::class);
  190. $previewFile->method('getSize')->willReturn(1000);
  191. $previewFolder->method('getDirectoryListing')
  192. ->willReturn([]);
  193. $previewFolder->method('newFile')
  194. ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
  195. if ($filename === '2048-2048-max.png') {
  196. return $maxPreview;
  197. } elseif ($filename === '256-256.png') {
  198. return $previewFile;
  199. }
  200. $this->fail('Unexpected file');
  201. });
  202. $maxPreview->expects($this->once())
  203. ->method('putContent')
  204. ->with($this->equalTo('my data'));
  205. $previewFolder->method('getFile')
  206. ->with($this->equalTo('256-256.png'))
  207. ->willThrowException(new NotFoundException());
  208. $image = $this->getMockImage(2048, 2048, 'my resized data');
  209. $this->helper->method('getImage')
  210. ->with($this->equalTo($maxPreview))
  211. ->willReturn($image);
  212. $previewFile->expects($this->once())
  213. ->method('putContent')
  214. ->with('my resized data');
  215. $this->legacyEventDispatcher->expects($this->once())
  216. ->method('dispatch')
  217. ->with(
  218. $this->equalTo(IPreview::EVENT),
  219. $this->callback(function (GenericEvent $event) use ($file) {
  220. return $event->getSubject() === $file &&
  221. $event->getArgument('width') === 100 &&
  222. $event->getArgument('height') === 100;
  223. })
  224. );
  225. $this->eventDispatcher->expects($this->once())
  226. ->method('dispatchTyped')
  227. ->with(new BeforePreviewFetchedEvent($file));
  228. $result = $this->generator->getPreview($file, 100, 100);
  229. $this->assertSame($previewFile, $result);
  230. }
  231. public function testInvalidMimeType() {
  232. $this->expectException(NotFoundException::class);
  233. $file = $this->createMock(File::class);
  234. $file->method('isReadable')
  235. ->willReturn(true);
  236. $file->method('getId')
  237. ->willReturn(42);
  238. $this->previewManager->method('isMimeSupported')
  239. ->with('invalidType')
  240. ->willReturn(false);
  241. $previewFolder = $this->createMock(ISimpleFolder::class);
  242. $this->appData->method('getFolder')
  243. ->with($this->equalTo(42))
  244. ->willReturn($previewFolder);
  245. $maxPreview = $this->createMock(ISimpleFile::class);
  246. $maxPreview->method('getName')
  247. ->willReturn('2048-2048-max.png');
  248. $maxPreview->method('getMimeType')
  249. ->willReturn('image/png');
  250. $previewFolder->method('getDirectoryListing')
  251. ->willReturn([$maxPreview]);
  252. $previewFolder->method('getFile')
  253. ->with($this->equalTo('1024-512-crop.png'))
  254. ->willThrowException(new NotFoundException());
  255. $this->legacyEventDispatcher->expects($this->once())
  256. ->method('dispatch')
  257. ->with(
  258. $this->equalTo(IPreview::EVENT),
  259. $this->callback(function (GenericEvent $event) use ($file) {
  260. return $event->getSubject() === $file &&
  261. $event->getArgument('width') === 1024 &&
  262. $event->getArgument('height') === 512 &&
  263. $event->getArgument('crop') === true &&
  264. $event->getArgument('mode') === IPreview::MODE_COVER;
  265. })
  266. );
  267. $this->eventDispatcher->expects($this->once())
  268. ->method('dispatchTyped')
  269. ->with(new BeforePreviewFetchedEvent($file));
  270. $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  271. }
  272. public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
  273. $file = $this->createMock(File::class);
  274. $file->method('isReadable')
  275. ->willReturn(true);
  276. $file->method('getId')
  277. ->willReturn(42);
  278. $previewFolder = $this->createMock(ISimpleFolder::class);
  279. $this->appData->method('getFolder')
  280. ->with($this->equalTo(42))
  281. ->willReturn($previewFolder);
  282. $maxPreview = $this->createMock(ISimpleFile::class);
  283. $maxPreview->method('getName')
  284. ->willReturn('2048-2048-max.png');
  285. $maxPreview->method('getSize')->willReturn(1000);
  286. $maxPreview->method('getMimeType')
  287. ->willReturn('image/png');
  288. $previewFolder->method('getDirectoryListing')
  289. ->willReturn([$maxPreview]);
  290. $preview = $this->createMock(ISimpleFile::class);
  291. $preview->method('getSize')->willReturn(1000);
  292. $previewFolder->method('getFile')
  293. ->with($this->equalTo('1024-512-crop.png'))
  294. ->willReturn($preview);
  295. $this->previewManager->expects($this->never())
  296. ->method('isMimeSupported');
  297. $this->legacyEventDispatcher->expects($this->once())
  298. ->method('dispatch')
  299. ->with(
  300. $this->equalTo(IPreview::EVENT),
  301. $this->callback(function (GenericEvent $event) use ($file) {
  302. return $event->getSubject() === $file &&
  303. $event->getArgument('width') === 1024 &&
  304. $event->getArgument('height') === 512 &&
  305. $event->getArgument('crop') === true &&
  306. $event->getArgument('mode') === IPreview::MODE_COVER;
  307. })
  308. );
  309. $this->eventDispatcher->expects($this->once())
  310. ->method('dispatchTyped')
  311. ->with(new BeforePreviewFetchedEvent($file));
  312. $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  313. $this->assertSame($preview, $result);
  314. }
  315. public function testNoProvider() {
  316. $file = $this->createMock(File::class);
  317. $file->method('isReadable')
  318. ->willReturn(true);
  319. $file->method('getMimeType')
  320. ->willReturn('myMimeType');
  321. $file->method('getId')
  322. ->willReturn(42);
  323. $previewFolder = $this->createMock(ISimpleFolder::class);
  324. $this->appData->method('getFolder')
  325. ->with($this->equalTo(42))
  326. ->willReturn($previewFolder);
  327. $previewFolder->method('getDirectoryListing')
  328. ->willReturn([]);
  329. $this->previewManager->method('getProviders')
  330. ->willReturn([]);
  331. $this->legacyEventDispatcher->expects($this->once())
  332. ->method('dispatch')
  333. ->with(
  334. $this->equalTo(IPreview::EVENT),
  335. $this->callback(function (GenericEvent $event) use ($file) {
  336. return $event->getSubject() === $file &&
  337. $event->getArgument('width') === 100 &&
  338. $event->getArgument('height') === 100;
  339. })
  340. );
  341. $this->eventDispatcher->expects($this->once())
  342. ->method('dispatchTyped')
  343. ->with(new BeforePreviewFetchedEvent($file));
  344. $this->expectException(NotFoundException::class);
  345. $this->generator->getPreview($file, 100, 100);
  346. }
  347. private function getMockImage($width, $height, $data = null) {
  348. $image = $this->createMock(\OC_Image::class);
  349. $image->method('height')->willReturn($width);
  350. $image->method('width')->willReturn($height);
  351. $image->method('valid')->willReturn(true);
  352. $image->method('dataMimeType')->willReturn('image/png');
  353. $image->method('data')->willReturn($data);
  354. $image->method('resizeCopy')->willReturnCallback(function ($size) use ($data) {
  355. return $this->getMockImage($size, $size, $data);
  356. });
  357. $image->method('preciseResizeCopy')->willReturnCallback(function ($width, $height) use ($data) {
  358. return $this->getMockImage($width, $height, $data);
  359. });
  360. $image->method('cropCopy')->willReturnCallback(function ($x, $y, $width, $height) use ($data) {
  361. return $this->getMockImage($width, $height, $data);
  362. });
  363. return $image;
  364. }
  365. public function dataSize() {
  366. return [
  367. [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
  368. [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
  369. [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  370. [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  371. [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
  372. [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
  373. [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
  374. [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
  375. [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
  376. [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
  377. [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
  378. [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
  379. [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  380. [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  381. [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
  382. [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
  383. [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
  384. [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
  385. //Test minimum size
  386. [2048, 1024, 32, 32, false, IPreview::MODE_FILL, 64, 32],
  387. [2048, 1024, 32, 32, false, IPreview::MODE_COVER, 64, 32],
  388. [2048, 1024, 32, 32, true, IPreview::MODE_FILL, 64, 64],
  389. [2048, 1024, 32, 32, true, IPreview::MODE_COVER, 64, 64],
  390. ];
  391. }
  392. /**
  393. * @dataProvider dataSize
  394. *
  395. * @param int $maxX
  396. * @param int $maxY
  397. * @param int $reqX
  398. * @param int $reqY
  399. * @param bool $crop
  400. * @param string $mode
  401. * @param int $expectedX
  402. * @param int $expectedY
  403. */
  404. public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) {
  405. $file = $this->createMock(File::class);
  406. $file->method('isReadable')
  407. ->willReturn(true);
  408. $file->method('getMimeType')
  409. ->willReturn('myMimeType');
  410. $file->method('getId')
  411. ->willReturn(42);
  412. $this->previewManager->method('isMimeSupported')
  413. ->with($this->equalTo('myMimeType'))
  414. ->willReturn(true);
  415. $previewFolder = $this->createMock(ISimpleFolder::class);
  416. $this->appData->method('getFolder')
  417. ->with($this->equalTo(42))
  418. ->willReturn($previewFolder);
  419. $maxPreview = $this->createMock(ISimpleFile::class);
  420. $maxPreview->method('getName')
  421. ->willReturn($maxX . '-' . $maxY . '-max.png');
  422. $maxPreview->method('getMimeType')
  423. ->willReturn('image/png');
  424. $maxPreview->method('getSize')->willReturn(1000);
  425. $previewFolder->method('getDirectoryListing')
  426. ->willReturn([$maxPreview]);
  427. $filename = $expectedX . '-' . $expectedY;
  428. if ($crop) {
  429. $filename .= '-crop';
  430. }
  431. $filename .= '.png';
  432. $previewFolder->method('getFile')
  433. ->with($this->equalTo($filename))
  434. ->willThrowException(new NotFoundException());
  435. $image = $this->getMockImage($maxX, $maxY);
  436. $this->helper->method('getImage')
  437. ->with($this->equalTo($maxPreview))
  438. ->willReturn($image);
  439. $preview = $this->createMock(ISimpleFile::class);
  440. $preview->method('getSize')->willReturn(1000);
  441. $previewFolder->method('newFile')
  442. ->with($this->equalTo($filename))
  443. ->willReturn($preview);
  444. $this->legacyEventDispatcher->expects($this->once())
  445. ->method('dispatch')
  446. ->with(
  447. $this->equalTo(IPreview::EVENT),
  448. $this->callback(function (GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) {
  449. return $event->getSubject() === $file &&
  450. $event->getArgument('width') === $reqX &&
  451. $event->getArgument('height') === $reqY &&
  452. $event->getArgument('crop') === $crop &&
  453. $event->getArgument('mode') === $mode;
  454. })
  455. );
  456. $this->eventDispatcher->expects($this->once())
  457. ->method('dispatchTyped')
  458. ->with(new BeforePreviewFetchedEvent($file));
  459. $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
  460. if ($expectedX === $maxX && $expectedY === $maxY) {
  461. $this->assertSame($maxPreview, $result);
  462. } else {
  463. $this->assertSame($preview, $result);
  464. }
  465. }
  466. public function testUnreadbleFile() {
  467. $file = $this->createMock(File::class);
  468. $file->method('isReadable')
  469. ->willReturn(false);
  470. $this->expectException(NotFoundException::class);
  471. $this->generator->getPreview($file, 100, 100, false);
  472. }
  473. }