GeneratorTest.php 14 KB

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