1
0

GeneratorTest.php 13 KB

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