GeneratorTest.php 15 KB

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