GeneratorTest.php 12 KB

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