GeneratorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. $previewFile = $this->createMock(ISimpleFile::class);
  92. $previewFile->method('getSize')->willReturn(1000);
  93. $previewFile->method('getName')->willReturn('256-256.png');
  94. $previewFolder->method('getDirectoryListing')
  95. ->willReturn([$maxPreview, $previewFile]);
  96. $this->legacyEventDispatcher->expects($this->once())
  97. ->method('dispatch')
  98. ->with(
  99. $this->equalTo(IPreview::EVENT),
  100. $this->callback(function (GenericEvent $event) use ($file) {
  101. return $event->getSubject() === $file &&
  102. $event->getArgument('width') === 100 &&
  103. $event->getArgument('height') === 100;
  104. })
  105. );
  106. $this->eventDispatcher->expects($this->once())
  107. ->method('dispatchTyped')
  108. ->with(new BeforePreviewFetchedEvent($file));
  109. $result = $this->generator->getPreview($file, 100, 100);
  110. $this->assertSame($previewFile, $result);
  111. }
  112. public function testGetNewPreview() {
  113. $file = $this->createMock(File::class);
  114. $file->method('isReadable')
  115. ->willReturn(true);
  116. $file->method('getMimeType')
  117. ->willReturn('myMimeType');
  118. $file->method('getId')
  119. ->willReturn(42);
  120. $this->previewManager->method('isMimeSupported')
  121. ->with($this->equalTo('myMimeType'))
  122. ->willReturn(true);
  123. $previewFolder = $this->createMock(ISimpleFolder::class);
  124. $this->appData->method('getFolder')
  125. ->with($this->equalTo(42))
  126. ->willThrowException(new NotFoundException());
  127. $this->appData->method('newFolder')
  128. ->with($this->equalTo(42))
  129. ->willReturn($previewFolder);
  130. $this->config->method('getSystemValue')
  131. ->willReturnCallback(function ($key, $default) {
  132. return $default;
  133. });
  134. $this->config->method('getSystemValueInt')
  135. ->willReturnCallback(function ($key, $default) {
  136. return $default;
  137. });
  138. $invalidProvider = $this->createMock(IProviderV2::class);
  139. $invalidProvider->method('isAvailable')
  140. ->willReturn(true);
  141. $unavailableProvider = $this->createMock(IProviderV2::class);
  142. $unavailableProvider->method('isAvailable')
  143. ->willReturn(false);
  144. $validProvider = $this->createMock(IProviderV2::class);
  145. $validProvider->method('isAvailable')
  146. ->with($file)
  147. ->willReturn(true);
  148. $this->previewManager->method('getProviders')
  149. ->willReturn([
  150. '/image\/png/' => ['wrongProvider'],
  151. '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'unavailableProvider', 'validProvider'],
  152. ]);
  153. $this->helper->method('getProvider')
  154. ->willReturnCallback(function ($provider) use ($invalidProvider, $validProvider, $unavailableProvider) {
  155. if ($provider === 'wrongProvider') {
  156. $this->fail('Wrongprovider should not be constructed!');
  157. } elseif ($provider === 'brokenProvider') {
  158. return false;
  159. } elseif ($provider === 'invalidProvider') {
  160. return $invalidProvider;
  161. } elseif ($provider === 'validProvider') {
  162. return $validProvider;
  163. } elseif ($provider === 'unavailableProvider') {
  164. return $unavailableProvider;
  165. }
  166. $this->fail('Unexpected provider requested');
  167. });
  168. $image = $this->createMock(\OC_Image::class);
  169. $image->method('width')->willReturn(2048);
  170. $image->method('height')->willReturn(2048);
  171. $image->method('valid')->willReturn(true);
  172. $image->method('dataMimeType')->willReturn('image/png');
  173. $this->helper->method('getThumbnail')
  174. ->willReturnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
  175. if ($provider === $validProvider) {
  176. return $image;
  177. } else {
  178. return false;
  179. }
  180. });
  181. $image->method('data')
  182. ->willReturn('my data');
  183. $maxPreview = $this->createMock(ISimpleFile::class);
  184. $maxPreview->method('getName')->willReturn('2048-2048-max.png');
  185. $maxPreview->method('getMimeType')->willReturn('image/png');
  186. $maxPreview->method('getSize')->willReturn(1000);
  187. $previewFile = $this->createMock(ISimpleFile::class);
  188. $previewFile->method('getSize')->willReturn(1000);
  189. $previewFolder->method('getDirectoryListing')
  190. ->willReturn([]);
  191. $previewFolder->method('newFile')
  192. ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) {
  193. if ($filename === '2048-2048-max.png') {
  194. return $maxPreview;
  195. } elseif ($filename === '256-256.png') {
  196. return $previewFile;
  197. }
  198. $this->fail('Unexpected file');
  199. });
  200. $maxPreview->expects($this->once())
  201. ->method('putContent')
  202. ->with($this->equalTo('my data'));
  203. $previewFolder->method('getFile')
  204. ->with($this->equalTo('256-256.png'))
  205. ->willThrowException(new NotFoundException());
  206. $image = $this->getMockImage(2048, 2048, 'my resized data');
  207. $this->helper->method('getImage')
  208. ->with($this->equalTo($maxPreview))
  209. ->willReturn($image);
  210. $previewFile->expects($this->once())
  211. ->method('putContent')
  212. ->with('my resized data');
  213. $this->legacyEventDispatcher->expects($this->once())
  214. ->method('dispatch')
  215. ->with(
  216. $this->equalTo(IPreview::EVENT),
  217. $this->callback(function (GenericEvent $event) use ($file) {
  218. return $event->getSubject() === $file &&
  219. $event->getArgument('width') === 100 &&
  220. $event->getArgument('height') === 100;
  221. })
  222. );
  223. $this->eventDispatcher->expects($this->once())
  224. ->method('dispatchTyped')
  225. ->with(new BeforePreviewFetchedEvent($file));
  226. $result = $this->generator->getPreview($file, 100, 100);
  227. $this->assertSame($previewFile, $result);
  228. }
  229. public function testInvalidMimeType() {
  230. $this->expectException(NotFoundException::class);
  231. $file = $this->createMock(File::class);
  232. $file->method('isReadable')
  233. ->willReturn(true);
  234. $file->method('getId')
  235. ->willReturn(42);
  236. $this->previewManager->method('isMimeSupported')
  237. ->with('invalidType')
  238. ->willReturn(false);
  239. $previewFolder = $this->createMock(ISimpleFolder::class);
  240. $this->appData->method('getFolder')
  241. ->with($this->equalTo(42))
  242. ->willReturn($previewFolder);
  243. $maxPreview = $this->createMock(ISimpleFile::class);
  244. $maxPreview->method('getName')
  245. ->willReturn('2048-2048-max.png');
  246. $maxPreview->method('getMimeType')
  247. ->willReturn('image/png');
  248. $previewFolder->method('getDirectoryListing')
  249. ->willReturn([$maxPreview]);
  250. $previewFolder->method('getFile')
  251. ->with($this->equalTo('1024-512-crop.png'))
  252. ->willThrowException(new NotFoundException());
  253. $this->legacyEventDispatcher->expects($this->once())
  254. ->method('dispatch')
  255. ->with(
  256. $this->equalTo(IPreview::EVENT),
  257. $this->callback(function (GenericEvent $event) use ($file) {
  258. return $event->getSubject() === $file &&
  259. $event->getArgument('width') === 1024 &&
  260. $event->getArgument('height') === 512 &&
  261. $event->getArgument('crop') === true &&
  262. $event->getArgument('mode') === IPreview::MODE_COVER;
  263. })
  264. );
  265. $this->eventDispatcher->expects($this->once())
  266. ->method('dispatchTyped')
  267. ->with(new BeforePreviewFetchedEvent($file));
  268. $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  269. }
  270. public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() {
  271. $file = $this->createMock(File::class);
  272. $file->method('isReadable')
  273. ->willReturn(true);
  274. $file->method('getId')
  275. ->willReturn(42);
  276. $previewFolder = $this->createMock(ISimpleFolder::class);
  277. $this->appData->method('getFolder')
  278. ->with($this->equalTo(42))
  279. ->willReturn($previewFolder);
  280. $maxPreview = $this->createMock(ISimpleFile::class);
  281. $maxPreview->method('getName')
  282. ->willReturn('2048-2048-max.png');
  283. $maxPreview->method('getSize')->willReturn(1000);
  284. $maxPreview->method('getMimeType')
  285. ->willReturn('image/png');
  286. $preview = $this->createMock(ISimpleFile::class);
  287. $preview->method('getSize')->willReturn(1000);
  288. $preview->method('getName')->willReturn('1024-512-crop.png');
  289. $previewFolder->method('getDirectoryListing')
  290. ->willReturn([$maxPreview, $preview]);
  291. $this->previewManager->expects($this->never())
  292. ->method('isMimeSupported');
  293. $this->legacyEventDispatcher->expects($this->once())
  294. ->method('dispatch')
  295. ->with(
  296. $this->equalTo(IPreview::EVENT),
  297. $this->callback(function (GenericEvent $event) use ($file) {
  298. return $event->getSubject() === $file &&
  299. $event->getArgument('width') === 1024 &&
  300. $event->getArgument('height') === 512 &&
  301. $event->getArgument('crop') === true &&
  302. $event->getArgument('mode') === IPreview::MODE_COVER;
  303. })
  304. );
  305. $this->eventDispatcher->expects($this->once())
  306. ->method('dispatchTyped')
  307. ->with(new BeforePreviewFetchedEvent($file));
  308. $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType');
  309. $this->assertSame($preview, $result);
  310. }
  311. public function testNoProvider() {
  312. $file = $this->createMock(File::class);
  313. $file->method('isReadable')
  314. ->willReturn(true);
  315. $file->method('getMimeType')
  316. ->willReturn('myMimeType');
  317. $file->method('getId')
  318. ->willReturn(42);
  319. $previewFolder = $this->createMock(ISimpleFolder::class);
  320. $this->appData->method('getFolder')
  321. ->with($this->equalTo(42))
  322. ->willReturn($previewFolder);
  323. $previewFolder->method('getDirectoryListing')
  324. ->willReturn([]);
  325. $this->previewManager->method('getProviders')
  326. ->willReturn([]);
  327. $this->legacyEventDispatcher->expects($this->once())
  328. ->method('dispatch')
  329. ->with(
  330. $this->equalTo(IPreview::EVENT),
  331. $this->callback(function (GenericEvent $event) use ($file) {
  332. return $event->getSubject() === $file &&
  333. $event->getArgument('width') === 100 &&
  334. $event->getArgument('height') === 100;
  335. })
  336. );
  337. $this->eventDispatcher->expects($this->once())
  338. ->method('dispatchTyped')
  339. ->with(new BeforePreviewFetchedEvent($file));
  340. $this->expectException(NotFoundException::class);
  341. $this->generator->getPreview($file, 100, 100);
  342. }
  343. private function getMockImage($width, $height, $data = null) {
  344. $image = $this->createMock(\OC_Image::class);
  345. $image->method('height')->willReturn($width);
  346. $image->method('width')->willReturn($height);
  347. $image->method('valid')->willReturn(true);
  348. $image->method('dataMimeType')->willReturn('image/png');
  349. $image->method('data')->willReturn($data);
  350. $image->method('resizeCopy')->willReturnCallback(function ($size) use ($data) {
  351. return $this->getMockImage($size, $size, $data);
  352. });
  353. $image->method('preciseResizeCopy')->willReturnCallback(function ($width, $height) use ($data) {
  354. return $this->getMockImage($width, $height, $data);
  355. });
  356. $image->method('cropCopy')->willReturnCallback(function ($x, $y, $width, $height) use ($data) {
  357. return $this->getMockImage($width, $height, $data);
  358. });
  359. return $image;
  360. }
  361. public function dataSize() {
  362. return [
  363. [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
  364. [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
  365. [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  366. [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  367. [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
  368. [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
  369. [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
  370. [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
  371. [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
  372. [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
  373. [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
  374. [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
  375. [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 1024, 1024],
  376. [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 1024, 1024],
  377. [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
  378. [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
  379. [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
  380. [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
  381. //Test minimum size
  382. [2048, 1024, 32, 32, false, IPreview::MODE_FILL, 64, 32],
  383. [2048, 1024, 32, 32, false, IPreview::MODE_COVER, 64, 32],
  384. [2048, 1024, 32, 32, true, IPreview::MODE_FILL, 64, 64],
  385. [2048, 1024, 32, 32, true, IPreview::MODE_COVER, 64, 64],
  386. ];
  387. }
  388. /**
  389. * @dataProvider dataSize
  390. *
  391. * @param int $maxX
  392. * @param int $maxY
  393. * @param int $reqX
  394. * @param int $reqY
  395. * @param bool $crop
  396. * @param string $mode
  397. * @param int $expectedX
  398. * @param int $expectedY
  399. */
  400. public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) {
  401. $file = $this->createMock(File::class);
  402. $file->method('isReadable')
  403. ->willReturn(true);
  404. $file->method('getMimeType')
  405. ->willReturn('myMimeType');
  406. $file->method('getId')
  407. ->willReturn(42);
  408. $this->previewManager->method('isMimeSupported')
  409. ->with($this->equalTo('myMimeType'))
  410. ->willReturn(true);
  411. $previewFolder = $this->createMock(ISimpleFolder::class);
  412. $this->appData->method('getFolder')
  413. ->with($this->equalTo(42))
  414. ->willReturn($previewFolder);
  415. $maxPreview = $this->createMock(ISimpleFile::class);
  416. $maxPreview->method('getName')
  417. ->willReturn($maxX . '-' . $maxY . '-max.png');
  418. $maxPreview->method('getMimeType')
  419. ->willReturn('image/png');
  420. $maxPreview->method('getSize')->willReturn(1000);
  421. $previewFolder->method('getDirectoryListing')
  422. ->willReturn([$maxPreview]);
  423. $filename = $expectedX . '-' . $expectedY;
  424. if ($crop) {
  425. $filename .= '-crop';
  426. }
  427. $filename .= '.png';
  428. $previewFolder->method('getFile')
  429. ->with($this->equalTo($filename))
  430. ->willThrowException(new NotFoundException());
  431. $image = $this->getMockImage($maxX, $maxY);
  432. $this->helper->method('getImage')
  433. ->with($this->equalTo($maxPreview))
  434. ->willReturn($image);
  435. $preview = $this->createMock(ISimpleFile::class);
  436. $preview->method('getSize')->willReturn(1000);
  437. $previewFolder->method('newFile')
  438. ->with($this->equalTo($filename))
  439. ->willReturn($preview);
  440. $this->legacyEventDispatcher->expects($this->once())
  441. ->method('dispatch')
  442. ->with(
  443. $this->equalTo(IPreview::EVENT),
  444. $this->callback(function (GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) {
  445. return $event->getSubject() === $file &&
  446. $event->getArgument('width') === $reqX &&
  447. $event->getArgument('height') === $reqY &&
  448. $event->getArgument('crop') === $crop &&
  449. $event->getArgument('mode') === $mode;
  450. })
  451. );
  452. $this->eventDispatcher->expects($this->once())
  453. ->method('dispatchTyped')
  454. ->with(new BeforePreviewFetchedEvent($file));
  455. $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
  456. if ($expectedX === $maxX && $expectedY === $maxY) {
  457. $this->assertSame($maxPreview, $result);
  458. } else {
  459. $this->assertSame($preview, $result);
  460. }
  461. }
  462. public function testUnreadbleFile() {
  463. $file = $this->createMock(File::class);
  464. $file->method('isReadable')
  465. ->willReturn(false);
  466. $this->expectException(NotFoundException::class);
  467. $this->generator->getPreview($file, 100, 100, false);
  468. }
  469. }