ImageManagerTest.php 13 KB

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