GeneratorTest.php 14 KB

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