ImageManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests;
  7. use OCA\Theming\ImageManager;
  8. use OCA\Theming\Service\BackgroundService;
  9. use OCP\Files\IAppData;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\SimpleFS\ISimpleFile;
  12. use OCP\Files\SimpleFS\ISimpleFolder;
  13. use OCP\ICacheFactory;
  14. use OCP\IConfig;
  15. use OCP\ITempManager;
  16. use OCP\IURLGenerator;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. class ImageManagerTest extends TestCase {
  21. /** @var IConfig|MockObject */
  22. protected $config;
  23. /** @var IAppData|MockObject */
  24. protected $appData;
  25. /** @var ImageManager */
  26. protected $imageManager;
  27. /** @var IURLGenerator|MockObject */
  28. private $urlGenerator;
  29. /** @var ICacheFactory|MockObject */
  30. private $cacheFactory;
  31. /** @var LoggerInterface|MockObject */
  32. private $logger;
  33. /** @var ITempManager|MockObject */
  34. private $tempManager;
  35. /** @var ISimpleFolder|MockObject */
  36. private $rootFolder;
  37. /** @var BackgroundService|MockObject */
  38. private $backgroundService;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->appData = $this->createMock(IAppData::class);
  43. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  44. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  45. $this->logger = $this->createMock(LoggerInterface::class);
  46. $this->tempManager = $this->createMock(ITempManager::class);
  47. $this->rootFolder = $this->createMock(ISimpleFolder::class);
  48. $this->backgroundService = $this->createMock(BackgroundService::class);
  49. $this->imageManager = new ImageManager(
  50. $this->config,
  51. $this->appData,
  52. $this->urlGenerator,
  53. $this->cacheFactory,
  54. $this->logger,
  55. $this->tempManager,
  56. $this->backgroundService,
  57. );
  58. $this->appData
  59. ->expects($this->any())
  60. ->method('getFolder')
  61. ->with('global')
  62. ->willReturn($this->rootFolder);
  63. }
  64. private function checkImagick() {
  65. if (!extension_loaded('imagick')) {
  66. $this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
  67. }
  68. $checkImagick = new \Imagick();
  69. if (empty($checkImagick->queryFormats('SVG'))) {
  70. $this->markTestSkipped('No SVG provider present.');
  71. }
  72. if (empty($checkImagick->queryFormats('PNG'))) {
  73. $this->markTestSkipped('No PNG provider present.');
  74. }
  75. }
  76. public function mockGetImage($key, $file) {
  77. /** @var MockObject $folder */
  78. $folder = $this->createMock(ISimpleFolder::class);
  79. if ($file === null) {
  80. $folder->expects($this->once())
  81. ->method('getFile')
  82. ->with('logo')
  83. ->willThrowException(new NotFoundException());
  84. } else {
  85. $file->expects($this->once())
  86. ->method('getContent')
  87. ->willReturn(file_get_contents(__DIR__ . '/../../../tests/data/testimage.png'));
  88. $folder->expects($this->exactly(2))
  89. ->method('fileExists')
  90. ->willReturnMap([
  91. ['logo', true],
  92. ['logo.png', false],
  93. ]);
  94. $folder->expects($this->once())
  95. ->method('getFile')
  96. ->with('logo')
  97. ->willReturn($file);
  98. $newFile = $this->createMock(ISimpleFile::class);
  99. $folder->expects($this->once())
  100. ->method('newFile')
  101. ->with('logo.png')
  102. ->willReturn($newFile);
  103. $newFile->expects($this->once())
  104. ->method('putContent');
  105. $this->rootFolder->expects($this->once())
  106. ->method('getFolder')
  107. ->with('images')
  108. ->willReturn($folder);
  109. }
  110. }
  111. public function testGetImageUrl(): void {
  112. $this->checkImagick();
  113. $this->config->expects($this->exactly(2))
  114. ->method('getAppValue')
  115. ->willReturnMap([
  116. ['theming', 'cachebuster', '0', '0'],
  117. ['theming', 'logoMime', '', '0'],
  118. ]);
  119. $this->urlGenerator->expects($this->once())
  120. ->method('linkToRoute')
  121. ->willReturn('url-to-image');
  122. $this->assertEquals('url-to-image?v=0', $this->imageManager->getImageUrl('logo', false));
  123. }
  124. public function testGetImageUrlDefault(): void {
  125. $this->config->expects($this->exactly(2))
  126. ->method('getAppValue')
  127. ->willReturnMap([
  128. ['theming', 'cachebuster', '0', '0'],
  129. ['theming', 'logoMime', '', ''],
  130. ]);
  131. $this->urlGenerator->expects($this->once())
  132. ->method('imagePath')
  133. ->with('core', 'logo/logo.png')
  134. ->willReturn('logo/logo.png');
  135. $this->assertEquals('logo/logo.png?v=0', $this->imageManager->getImageUrl('logo'));
  136. }
  137. public function testGetImageUrlAbsolute(): void {
  138. $this->checkImagick();
  139. $this->config->expects($this->exactly(2))
  140. ->method('getAppValue')
  141. ->willReturnMap([
  142. ['theming', 'cachebuster', '0', '0'],
  143. ['theming', 'logoMime', '', ''],
  144. ]);
  145. $this->urlGenerator->expects($this->any())
  146. ->method('getAbsoluteUrl')
  147. ->willReturn('url-to-image-absolute?v=0');
  148. $this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo', false));
  149. }
  150. public function testGetImage(): void {
  151. $this->checkImagick();
  152. $this->config->expects($this->once())
  153. ->method('getAppValue')->with('theming', 'logoMime', false)
  154. ->willReturn('png');
  155. $file = $this->createMock(ISimpleFile::class);
  156. $this->mockGetImage('logo', $file);
  157. $this->assertEquals($file, $this->imageManager->getImage('logo', false));
  158. }
  159. public function testGetImageUnset(): void {
  160. $this->expectException(NotFoundException::class);
  161. $this->config->expects($this->once())
  162. ->method('getAppValue')->with('theming', 'logoMime', false)
  163. ->willReturn(false);
  164. $this->imageManager->getImage('logo');
  165. }
  166. public function testGetCacheFolder(): void {
  167. $folder = $this->createMock(ISimpleFolder::class);
  168. $this->config->expects($this->once())
  169. ->method('getAppValue')
  170. ->with('theming', 'cachebuster', '0')
  171. ->willReturn('0');
  172. $this->rootFolder->expects($this->once())
  173. ->method('getFolder')
  174. ->with('0')
  175. ->willReturn($folder);
  176. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  177. }
  178. public function testGetCacheFolderCreate(): void {
  179. $folder = $this->createMock(ISimpleFolder::class);
  180. $this->config->expects($this->exactly(2))
  181. ->method('getAppValue')
  182. ->with('theming', 'cachebuster', '0')
  183. ->willReturn('0');
  184. $this->rootFolder->expects($this->exactly(2))
  185. ->method('getFolder')
  186. ->with('0')
  187. ->willReturnOnConsecutiveCalls(
  188. $this->throwException(new NotFoundException()),
  189. $folder,
  190. );
  191. $this->rootFolder->expects($this->once())
  192. ->method('newFolder')
  193. ->with('0')
  194. ->willReturn($folder);
  195. $this->rootFolder->expects($this->once())
  196. ->method('getDirectoryListing')
  197. ->willReturn([]);
  198. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  199. }
  200. public function testGetCachedImage(): void {
  201. $expected = $this->createMock(ISimpleFile::class);
  202. $folder = $this->setupCacheFolder();
  203. $folder->expects($this->once())
  204. ->method('getFile')
  205. ->with('filename')
  206. ->willReturn($expected);
  207. $this->assertEquals($expected, $this->imageManager->getCachedImage('filename'));
  208. }
  209. public function testGetCachedImageNotFound(): void {
  210. $this->expectException(NotFoundException::class);
  211. $folder = $this->setupCacheFolder();
  212. $folder->expects($this->once())
  213. ->method('getFile')
  214. ->with('filename')
  215. ->will($this->throwException(new NotFoundException()));
  216. $image = $this->imageManager->getCachedImage('filename');
  217. }
  218. public function testSetCachedImage(): void {
  219. $folder = $this->setupCacheFolder();
  220. $file = $this->createMock(ISimpleFile::class);
  221. $folder->expects($this->once())
  222. ->method('fileExists')
  223. ->with('filename')
  224. ->willReturn(true);
  225. $folder->expects($this->once())
  226. ->method('getFile')
  227. ->with('filename')
  228. ->willReturn($file);
  229. $file->expects($this->once())
  230. ->method('putContent')
  231. ->with('filecontent');
  232. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  233. }
  234. public function testSetCachedImageCreate(): void {
  235. $folder = $this->setupCacheFolder();
  236. $file = $this->createMock(ISimpleFile::class);
  237. $folder->expects($this->once())
  238. ->method('fileExists')
  239. ->with('filename')
  240. ->willReturn(false);
  241. $folder->expects($this->once())
  242. ->method('newFile')
  243. ->with('filename')
  244. ->willReturn($file);
  245. $file->expects($this->once())
  246. ->method('putContent')
  247. ->with('filecontent');
  248. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  249. }
  250. private function setupCacheFolder() {
  251. $folder = $this->createMock(ISimpleFolder::class);
  252. $this->config->expects($this->once())
  253. ->method('getAppValue')
  254. ->with('theming', 'cachebuster', '0')
  255. ->willReturn('0');
  256. $this->rootFolder->expects($this->once())
  257. ->method('getFolder')
  258. ->with('0')
  259. ->willReturn($folder);
  260. return $folder;
  261. }
  262. public function testCleanup(): void {
  263. $folders = [
  264. $this->createMock(ISimpleFolder::class),
  265. $this->createMock(ISimpleFolder::class),
  266. $this->createMock(ISimpleFolder::class)
  267. ];
  268. foreach ($folders as $index => $folder) {
  269. $folder->expects($this->any())
  270. ->method('getName')
  271. ->willReturn("$index");
  272. }
  273. $folders[0]->expects($this->once())->method('delete');
  274. $folders[1]->expects($this->once())->method('delete');
  275. $folders[2]->expects($this->never())->method('delete');
  276. $this->config->expects($this->once())
  277. ->method('getAppValue')
  278. ->with('theming', 'cachebuster', '0')
  279. ->willReturn('2');
  280. $this->rootFolder->expects($this->once())
  281. ->method('getDirectoryListing')
  282. ->willReturn($folders);
  283. $this->rootFolder->expects($this->once())
  284. ->method('getFolder')
  285. ->with('2')
  286. ->willReturn($folders[2]);
  287. $this->imageManager->cleanup();
  288. }
  289. public function dataUpdateImage() {
  290. return [
  291. ['background', __DIR__ . '/../../../tests/data/testimage.png', true, false],
  292. ['background', __DIR__ . '/../../../tests/data/testimage.png', false, false],
  293. ['background', __DIR__ . '/../../../tests/data/testimage.jpg', true, false],
  294. ['background', __DIR__ . '/../../../tests/data/testimage.webp', true, false],
  295. ['background', __DIR__ . '/../../../tests/data/testimage-large.jpg', true, true],
  296. ['background', __DIR__ . '/../../../tests/data/testimage-wide.png', true, true],
  297. ['logo', __DIR__ . '/../../../tests/data/testimagelarge.svg', true, false],
  298. ];
  299. }
  300. /**
  301. * @dataProvider dataUpdateImage
  302. */
  303. public function testUpdateImage($key, $tmpFile, $folderExists, $shouldConvert): void {
  304. $file = $this->createMock(ISimpleFile::class);
  305. $folder = $this->createMock(ISimpleFolder::class);
  306. $oldFile = $this->createMock(ISimpleFile::class);
  307. $folder->expects($this->any())
  308. ->method('getFile')
  309. ->willReturn($oldFile);
  310. if ($folderExists) {
  311. $this->rootFolder
  312. ->expects($this->any())
  313. ->method('getFolder')
  314. ->with('images')
  315. ->willReturn($folder);
  316. } else {
  317. $this->rootFolder
  318. ->expects($this->any())
  319. ->method('getFolder')
  320. ->with('images')
  321. ->willThrowException(new NotFoundException());
  322. $this->rootFolder
  323. ->expects($this->any())
  324. ->method('newFolder')
  325. ->with('images')
  326. ->willReturn($folder);
  327. }
  328. $folder->expects($this->once())
  329. ->method('newFile')
  330. ->with($key)
  331. ->willReturn($file);
  332. if ($shouldConvert) {
  333. $this->tempManager->expects($this->once())
  334. ->method('getTemporaryFile')
  335. ->willReturn('/tmp/randomtempfile-theming');
  336. }
  337. $this->imageManager->updateImage($key, $tmpFile);
  338. }
  339. public function testUnsupportedImageType(): void {
  340. $this->expectException(\Exception::class);
  341. $this->expectExceptionMessage('Unsupported image type: text/plain');
  342. $file = $this->createMock(ISimpleFile::class);
  343. $folder = $this->createMock(ISimpleFolder::class);
  344. $oldFile = $this->createMock(ISimpleFile::class);
  345. $folder->expects($this->any())
  346. ->method('getFile')
  347. ->willReturn($oldFile);
  348. $this->rootFolder
  349. ->expects($this->any())
  350. ->method('getFolder')
  351. ->with('images')
  352. ->willReturn($folder);
  353. $folder->expects($this->once())
  354. ->method('newFile')
  355. ->with('favicon')
  356. ->willReturn($file);
  357. $this->imageManager->updateImage('favicon', __DIR__ . '/../../../tests/data/lorem.txt');
  358. }
  359. }