ImageManagerTest.php 13 KB

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