1
0

ImageManagerTest.php 11 KB

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