GeneratorTest.php 16 KB

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