ImageManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. ->withConsecutive(
  91. ['logo'],
  92. ['logo.png'],
  93. )->willReturnOnConsecutiveCalls(
  94. true,
  95. false,
  96. );
  97. $folder->expects($this->once())
  98. ->method('getFile')
  99. ->with('logo')
  100. ->willReturn($file);
  101. $newFile = $this->createMock(ISimpleFile::class);
  102. $folder->expects($this->once())
  103. ->method('newFile')
  104. ->with('logo.png')
  105. ->willReturn($newFile);
  106. $newFile->expects($this->once())
  107. ->method('putContent');
  108. $this->rootFolder->expects($this->once())
  109. ->method('getFolder')
  110. ->with('images')
  111. ->willReturn($folder);
  112. }
  113. }
  114. public function testGetImageUrl(): void {
  115. $this->checkImagick();
  116. $file = $this->createMock(ISimpleFile::class);
  117. $this->config->expects($this->exactly(2))
  118. ->method('getAppValue')
  119. ->withConsecutive(
  120. ['theming', 'cachebuster', '0'],
  121. ['theming', 'logoMime', '']
  122. )
  123. ->willReturn(0);
  124. $this->urlGenerator->expects($this->once())
  125. ->method('linkToRoute')
  126. ->willReturn('url-to-image');
  127. $this->assertEquals('url-to-image?v=0', $this->imageManager->getImageUrl('logo', false));
  128. }
  129. public function testGetImageUrlDefault(): void {
  130. $this->config->expects($this->exactly(2))
  131. ->method('getAppValue')
  132. ->withConsecutive(
  133. ['theming', 'cachebuster', '0'],
  134. ['theming', 'logoMime', '']
  135. )
  136. ->willReturnOnConsecutiveCalls(0, '');
  137. $this->urlGenerator->expects($this->once())
  138. ->method('imagePath')
  139. ->with('core', 'logo/logo.png')
  140. ->willReturn('logo/logo.png');
  141. $this->assertEquals('logo/logo.png?v=0', $this->imageManager->getImageUrl('logo'));
  142. }
  143. public function testGetImageUrlAbsolute(): void {
  144. $this->checkImagick();
  145. $file = $this->createMock(ISimpleFile::class);
  146. $this->config->expects($this->exactly(2))
  147. ->method('getAppValue')
  148. ->withConsecutive(
  149. ['theming', 'cachebuster', '0'],
  150. ['theming', 'logoMime', '']
  151. )
  152. ->willReturnOnConsecutiveCalls(0, 0);
  153. $this->urlGenerator->expects($this->any())
  154. ->method('getAbsoluteUrl')
  155. ->willReturn('url-to-image-absolute?v=0');
  156. $this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo', false));
  157. }
  158. public function testGetImage(): void {
  159. $this->checkImagick();
  160. $this->config->expects($this->once())
  161. ->method('getAppValue')->with('theming', 'logoMime', false)
  162. ->willReturn('png');
  163. $file = $this->createMock(ISimpleFile::class);
  164. $this->mockGetImage('logo', $file);
  165. $this->assertEquals($file, $this->imageManager->getImage('logo', false));
  166. }
  167. public function testGetImageUnset(): void {
  168. $this->expectException(\OCP\Files\NotFoundException::class);
  169. $this->config->expects($this->once())
  170. ->method('getAppValue')->with('theming', 'logoMime', false)
  171. ->willReturn(false);
  172. $this->imageManager->getImage('logo');
  173. }
  174. public function testGetCacheFolder(): void {
  175. $folder = $this->createMock(ISimpleFolder::class);
  176. $this->config->expects($this->once())
  177. ->method('getAppValue')
  178. ->with('theming', 'cachebuster', '0')
  179. ->willReturn('0');
  180. $this->rootFolder->expects($this->once())
  181. ->method('getFolder')
  182. ->with('0')
  183. ->willReturn($folder);
  184. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  185. }
  186. public function testGetCacheFolderCreate(): void {
  187. $folder = $this->createMock(ISimpleFolder::class);
  188. $this->config->expects($this->exactly(2))
  189. ->method('getAppValue')
  190. ->with('theming', 'cachebuster', '0')
  191. ->willReturn('0');
  192. $this->rootFolder->expects($this->exactly(2))
  193. ->method('getFolder')
  194. ->with('0')
  195. ->willReturnOnConsecutiveCalls(
  196. $this->throwException(new NotFoundException()),
  197. $folder,
  198. );
  199. $this->rootFolder->expects($this->once())
  200. ->method('newFolder')
  201. ->with('0')
  202. ->willReturn($folder);
  203. $this->rootFolder->expects($this->once())
  204. ->method('getDirectoryListing')
  205. ->willReturn([]);
  206. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  207. }
  208. public function testGetCachedImage(): void {
  209. $expected = $this->createMock(ISimpleFile::class);
  210. $folder = $this->setupCacheFolder();
  211. $folder->expects($this->once())
  212. ->method('getFile')
  213. ->with('filename')
  214. ->willReturn($expected);
  215. $this->assertEquals($expected, $this->imageManager->getCachedImage('filename'));
  216. }
  217. public function testGetCachedImageNotFound(): void {
  218. $this->expectException(\OCP\Files\NotFoundException::class);
  219. $folder = $this->setupCacheFolder();
  220. $folder->expects($this->once())
  221. ->method('getFile')
  222. ->with('filename')
  223. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  224. $image = $this->imageManager->getCachedImage('filename');
  225. }
  226. public function testSetCachedImage(): void {
  227. $folder = $this->setupCacheFolder();
  228. $file = $this->createMock(ISimpleFile::class);
  229. $folder->expects($this->once())
  230. ->method('fileExists')
  231. ->with('filename')
  232. ->willReturn(true);
  233. $folder->expects($this->once())
  234. ->method('getFile')
  235. ->with('filename')
  236. ->willReturn($file);
  237. $file->expects($this->once())
  238. ->method('putContent')
  239. ->with('filecontent');
  240. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  241. }
  242. public function testSetCachedImageCreate(): void {
  243. $folder = $this->setupCacheFolder();
  244. $file = $this->createMock(ISimpleFile::class);
  245. $folder->expects($this->once())
  246. ->method('fileExists')
  247. ->with('filename')
  248. ->willReturn(false);
  249. $folder->expects($this->once())
  250. ->method('newFile')
  251. ->with('filename')
  252. ->willReturn($file);
  253. $file->expects($this->once())
  254. ->method('putContent')
  255. ->with('filecontent');
  256. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  257. }
  258. private function setupCacheFolder() {
  259. $folder = $this->createMock(ISimpleFolder::class);
  260. $this->config->expects($this->once())
  261. ->method('getAppValue')
  262. ->with('theming', 'cachebuster', '0')
  263. ->willReturn('0');
  264. $this->rootFolder->expects($this->once())
  265. ->method('getFolder')
  266. ->with('0')
  267. ->willReturn($folder);
  268. return $folder;
  269. }
  270. public function testCleanup(): void {
  271. $folders = [
  272. $this->createMock(ISimpleFolder::class),
  273. $this->createMock(ISimpleFolder::class),
  274. $this->createMock(ISimpleFolder::class)
  275. ];
  276. foreach ($folders as $index => $folder) {
  277. $folder->expects($this->any())
  278. ->method('getName')
  279. ->willReturn("$index");
  280. }
  281. $folders[0]->expects($this->once())->method('delete');
  282. $folders[1]->expects($this->once())->method('delete');
  283. $folders[2]->expects($this->never())->method('delete');
  284. $this->config->expects($this->once())
  285. ->method('getAppValue')
  286. ->with('theming', 'cachebuster', '0')
  287. ->willReturn('2');
  288. $this->rootFolder->expects($this->once())
  289. ->method('getDirectoryListing')
  290. ->willReturn($folders);
  291. $this->rootFolder->expects($this->once())
  292. ->method('getFolder')
  293. ->with('2')
  294. ->willReturn($folders[2]);
  295. $this->imageManager->cleanup();
  296. }
  297. public function dataUpdateImage() {
  298. return [
  299. ['background', __DIR__ . '/../../../tests/data/testimage.png', true, false],
  300. ['background', __DIR__ . '/../../../tests/data/testimage.png', false, false],
  301. ['background', __DIR__ . '/../../../tests/data/testimage.jpg', true, false],
  302. ['background', __DIR__ . '/../../../tests/data/testimage.webp', true, false],
  303. ['background', __DIR__ . '/../../../tests/data/testimage-large.jpg', true, true],
  304. ['background', __DIR__ . '/../../../tests/data/testimage-wide.png', true, true],
  305. ['logo', __DIR__ . '/../../../tests/data/testimagelarge.svg', true, false],
  306. ];
  307. }
  308. /**
  309. * @dataProvider dataUpdateImage
  310. */
  311. public function testUpdateImage($key, $tmpFile, $folderExists, $shouldConvert): void {
  312. $file = $this->createMock(ISimpleFile::class);
  313. $folder = $this->createMock(ISimpleFolder::class);
  314. $oldFile = $this->createMock(ISimpleFile::class);
  315. $folder->expects($this->any())
  316. ->method('getFile')
  317. ->willReturn($oldFile);
  318. if ($folderExists) {
  319. $this->rootFolder
  320. ->expects($this->any())
  321. ->method('getFolder')
  322. ->with('images')
  323. ->willReturn($folder);
  324. } else {
  325. $this->rootFolder
  326. ->expects($this->any())
  327. ->method('getFolder')
  328. ->with('images')
  329. ->willThrowException(new NotFoundException());
  330. $this->rootFolder
  331. ->expects($this->any())
  332. ->method('newFolder')
  333. ->with('images')
  334. ->willReturn($folder);
  335. }
  336. $folder->expects($this->once())
  337. ->method('newFile')
  338. ->with($key)
  339. ->willReturn($file);
  340. if ($shouldConvert) {
  341. $this->tempManager->expects($this->once())
  342. ->method('getTemporaryFile')
  343. ->willReturn('/tmp/randomtempfile-theming');
  344. }
  345. $this->imageManager->updateImage($key, $tmpFile);
  346. }
  347. public function testUnsupportedImageType(): void {
  348. $this->expectException(\Exception::class);
  349. $this->expectExceptionMessage('Unsupported image type: text/plain');
  350. $file = $this->createMock(ISimpleFile::class);
  351. $folder = $this->createMock(ISimpleFolder::class);
  352. $oldFile = $this->createMock(ISimpleFile::class);
  353. $folder->expects($this->any())
  354. ->method('getFile')
  355. ->willReturn($oldFile);
  356. $this->rootFolder
  357. ->expects($this->any())
  358. ->method('getFolder')
  359. ->with('images')
  360. ->willReturn($folder);
  361. $folder->expects($this->once())
  362. ->method('newFile')
  363. ->with('favicon')
  364. ->willReturn($file);
  365. $this->imageManager->updateImage('favicon', __DIR__ . '/../../../tests/data/lorem.txt');
  366. }
  367. }