GeneratorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. $image->method('valid')->willReturn(true);
  144. $this->helper->method('getThumbnail')
  145. ->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
  146. if ($provider === $validProvider) {
  147. return $image;
  148. } else {
  149. return false;
  150. }
  151. }));
  152. $image->method('data')
  153. ->willReturn('my data');
  154. $maxPreview = $this->createMock(ISimpleFile::class);
  155. $maxPreview->method('getName')->willReturn('2048-2048-max.png');
  156. $previewFile = $this->createMock(ISimpleFile::class);
  157. $previewFolder->method('getDirectoryListing')
  158. ->willReturn([]);
  159. $previewFolder->method('newFile')
  160. ->will($this->returnCallback(function($filename) use ($maxPreview, $previewFile) {
  161. if ($filename === '2048-2048-max.png') {
  162. return $maxPreview;
  163. } else if ($filename === '128-128.png') {
  164. return $previewFile;
  165. }
  166. $this->fail('Unexpected file');
  167. }));
  168. $maxPreview->expects($this->once())
  169. ->method('putContent')
  170. ->with($this->equalTo('my data'));
  171. $previewFolder->method('getFile')
  172. ->with($this->equalTo('128-128.png'))
  173. ->willThrowException(new NotFoundException());
  174. $image = $this->createMock(IImage::class);
  175. $this->helper->method('getImage')
  176. ->with($this->equalTo($maxPreview))
  177. ->willReturn($image);
  178. $image->expects($this->once())
  179. ->method('resize')
  180. ->with(128);
  181. $image->method('data')
  182. ->willReturn('my resized data');
  183. $image->method('valid')->willReturn(true);
  184. $previewFile->expects($this->once())
  185. ->method('putContent')
  186. ->with('my resized data');
  187. $this->eventDispatcher->expects($this->once())
  188. ->method('dispatch')
  189. ->with(
  190. $this->equalTo(IPreview::EVENT),
  191. $this->callback(function(GenericEvent $event) use ($file) {
  192. return $event->getSubject() === $file &&
  193. $event->getArgument('width') === 100 &&
  194. $event->getArgument('height') === 100;
  195. })
  196. );
  197. $result = $this->generator->getPreview($file, 100, 100);
  198. $this->assertSame($previewFile, $result);
  199. }
  200. public function testInvalidMimeType() {
  201. $this->expectException(NotFoundException::class);
  202. $file = $this->createMock(File::class);
  203. $this->previewManager->method('isMimeSupported')
  204. ->with('invalidType')
  205. ->willReturn(false);
  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') === 0 &&
  213. $event->getArgument('height') === 0 &&
  214. $event->getArgument('crop') === true &&
  215. $event->getArgument('mode') === IPreview::MODE_COVER;
  216. })
  217. );
  218. $this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType');
  219. }
  220. public function testNoProvider() {
  221. $file = $this->createMock(File::class);
  222. $file->method('getMimeType')
  223. ->willReturn('myMimeType');
  224. $file->method('getId')
  225. ->willReturn(42);
  226. $this->previewManager->method('isMimeSupported')
  227. ->with($this->equalTo('myMimeType'))
  228. ->willReturn(true);
  229. $previewFolder = $this->createMock(ISimpleFolder::class);
  230. $this->appData->method('getFolder')
  231. ->with($this->equalTo(42))
  232. ->willReturn($previewFolder);
  233. $previewFolder->method('getDirectoryListing')
  234. ->willReturn([]);
  235. $this->previewManager->method('getProviders')
  236. ->willReturn([]);
  237. $this->eventDispatcher->expects($this->once())
  238. ->method('dispatch')
  239. ->with(
  240. $this->equalTo(IPreview::EVENT),
  241. $this->callback(function(GenericEvent $event) use ($file) {
  242. return $event->getSubject() === $file &&
  243. $event->getArgument('width') === 100 &&
  244. $event->getArgument('height') === 100;
  245. })
  246. );
  247. $this->expectException(NotFoundException::class);
  248. $this->generator->getPreview($file, 100, 100);
  249. }
  250. public function dataSize() {
  251. return [
  252. [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
  253. [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
  254. [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 512, 512],
  255. [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 512, 512],
  256. [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
  257. [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
  258. [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
  259. [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
  260. [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
  261. [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
  262. [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
  263. [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
  264. [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 512, 512],
  265. [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 512, 512],
  266. [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
  267. [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
  268. [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
  269. [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
  270. ];
  271. }
  272. /**
  273. * @dataProvider dataSize
  274. *
  275. * @param int $maxX
  276. * @param int $maxY
  277. * @param int $reqX
  278. * @param int $reqY
  279. * @param bool $crop
  280. * @param string $mode
  281. * @param int $expectedX
  282. * @param int $expectedY
  283. */
  284. public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) {
  285. $file = $this->createMock(File::class);
  286. $file->method('getMimeType')
  287. ->willReturn('myMimeType');
  288. $file->method('getId')
  289. ->willReturn(42);
  290. $this->previewManager->method('isMimeSupported')
  291. ->with($this->equalTo('myMimeType'))
  292. ->willReturn(true);
  293. $previewFolder = $this->createMock(ISimpleFolder::class);
  294. $this->appData->method('getFolder')
  295. ->with($this->equalTo(42))
  296. ->willReturn($previewFolder);
  297. $maxPreview = $this->createMock(ISimpleFile::class);
  298. $maxPreview->method('getName')
  299. ->willReturn($maxX . '-' . $maxY . '-max.png');
  300. $previewFolder->method('getDirectoryListing')
  301. ->willReturn([$maxPreview]);
  302. $filename = $expectedX . '-' . $expectedY;
  303. if ($crop) {
  304. $filename .= '-crop';
  305. }
  306. $filename .= '.png';
  307. $previewFolder->method('getFile')
  308. ->with($this->equalTo($filename))
  309. ->willThrowException(new NotFoundException());
  310. $image = $this->createMock(IImage::class);
  311. $this->helper->method('getImage')
  312. ->with($this->equalTo($maxPreview))
  313. ->willReturn($image);
  314. $image->method('height')->willReturn($maxY);
  315. $image->method('width')->willReturn($maxX);
  316. $image->method('valid')->willReturn(true);
  317. $preview = $this->createMock(ISimpleFile::class);
  318. $previewFolder->method('newFile')
  319. ->with($this->equalTo($filename))
  320. ->willReturn($preview);
  321. $this->eventDispatcher->expects($this->once())
  322. ->method('dispatch')
  323. ->with(
  324. $this->equalTo(IPreview::EVENT),
  325. $this->callback(function(GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) {
  326. return $event->getSubject() === $file &&
  327. $event->getArgument('width') === $reqX &&
  328. $event->getArgument('height') === $reqY &&
  329. $event->getArgument('crop') === $crop &&
  330. $event->getArgument('mode') === $mode;
  331. })
  332. );
  333. $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
  334. if ($expectedX === $maxX && $expectedY === $maxY) {
  335. $this->assertSame($maxPreview, $result);
  336. } else {
  337. $this->assertSame($preview, $result);
  338. }
  339. }
  340. }