ImageManagerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Haertl <jus@bitgrid.net>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Tests;
  25. use OCA\Theming\ImageManager;
  26. use OCA\Theming\ThemingDefaults;
  27. use OCP\Files\SimpleFS\ISimpleFile;
  28. use OCP\ICacheFactory;
  29. use OCP\IConfig;
  30. use OCP\ILogger;
  31. use OCP\IURLGenerator;
  32. use Test\TestCase;
  33. use OCP\Files\SimpleFS\ISimpleFolder;
  34. use OCP\Files\IAppData;
  35. use OCP\Files\NotFoundException;
  36. class ImageManagerTest extends TestCase {
  37. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $config;
  39. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $appData;
  41. /** @var ImageManager */
  42. protected $imageManager;
  43. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  44. private $urlGenerator;
  45. /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
  46. private $cacheFactory;
  47. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  48. private $logger;
  49. protected function setUp() {
  50. parent::setUp();
  51. $this->config = $this->createMock(IConfig::class);
  52. $this->appData = $this->createMock(IAppData::class);
  53. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  54. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  55. $this->logger = $this->createMock(ILogger::class);
  56. $this->imageManager = new ImageManager(
  57. $this->config,
  58. $this->appData,
  59. $this->urlGenerator,
  60. $this->cacheFactory,
  61. $this->logger
  62. );
  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 \PHPUnit_Framework_MockObject_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->at(0))
  89. ->method('fileExists')
  90. ->with('logo')
  91. ->willReturn(true);
  92. $folder->expects($this->at(1))
  93. ->method('fileExists')
  94. ->with('logo.png')
  95. ->willReturn(false);
  96. $folder->expects($this->at(2))
  97. ->method('getFile')
  98. ->with('logo')
  99. ->willReturn($file);
  100. $newFile = $this->createMock(ISimpleFile::class);
  101. $folder->expects($this->at(3))
  102. ->method('newFile')
  103. ->with('logo.png')
  104. ->willReturn($newFile);
  105. $newFile->expects($this->once())
  106. ->method('putContent');
  107. $this->appData->expects($this->once())
  108. ->method('getFolder')
  109. ->with('images')
  110. ->willReturn($folder);
  111. }
  112. }
  113. public function testGetImageUrl() {
  114. $this->checkImagick();
  115. $file = $this->createMock(ISimpleFile::class);
  116. $this->config->expects($this->exactly(2))
  117. ->method('getAppValue')
  118. ->withConsecutive(
  119. ['theming', 'cachebuster', '0'],
  120. ['theming', 'logoMime', '']
  121. )
  122. ->willReturn(0);
  123. $this->mockGetImage('logo', $file);
  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() {
  130. $this->config->expects($this->exactly(2))
  131. ->method('getAppValue')
  132. ->withConsecutive(
  133. ['theming', 'cachebuster', '0'],
  134. ['theming', 'logoMime', false]
  135. )
  136. ->willReturnOnConsecutiveCalls(0, false);
  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() {
  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. ->willReturn(0);
  153. $this->mockGetImage('logo', $file);
  154. $this->urlGenerator->expects($this->at(0))
  155. ->method('getBaseUrl')
  156. ->willReturn('baseurl');
  157. $this->urlGenerator->expects($this->at(1))
  158. ->method('getAbsoluteUrl')
  159. ->willReturn('url-to-image-absolute?v=0');
  160. $this->urlGenerator->expects($this->at(2))
  161. ->method('getAbsoluteUrl')
  162. ->willReturn('url-to-image-absolute?v=0');
  163. $this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo', false));
  164. }
  165. public function testGetImage() {
  166. $this->checkImagick();
  167. $this->config->expects($this->once())
  168. ->method('getAppValue')->with('theming', 'logoMime', false)
  169. ->willReturn('png');
  170. $file = $this->createMock(ISimpleFile::class);
  171. $this->mockGetImage('logo', $file);
  172. $this->assertEquals($file, $this->imageManager->getImage('logo', false));
  173. }
  174. /**
  175. * @expectedException OCP\Files\NotFoundException
  176. */
  177. public function testGetImageUnset() {
  178. $this->config->expects($this->once())
  179. ->method('getAppValue')->with('theming', 'logoMime', false)
  180. ->willReturn(false);
  181. $this->imageManager->getImage('logo');
  182. }
  183. public function testGetCacheFolder() {
  184. $folder = $this->createMock(ISimpleFolder::class);
  185. $this->config->expects($this->once())
  186. ->method('getAppValue')
  187. ->with('theming', 'cachebuster', '0')
  188. ->willReturn('0');
  189. $this->appData->expects($this->at(0))
  190. ->method('getFolder')
  191. ->with('0')
  192. ->willReturn($folder);
  193. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  194. }
  195. public function testGetCacheFolderCreate() {
  196. $folder = $this->createMock(ISimpleFolder::class);
  197. $this->config->expects($this->exactly(2))
  198. ->method('getAppValue')
  199. ->with('theming', 'cachebuster', '0')
  200. ->willReturn('0');
  201. $this->appData->expects($this->at(0))
  202. ->method('getFolder')
  203. ->willThrowException(new NotFoundException());
  204. $this->appData->expects($this->at(1))
  205. ->method('newFolder')
  206. ->with('0')
  207. ->willReturn($folder);
  208. $this->appData->expects($this->at(2))
  209. ->method('getFolder')
  210. ->with('0')
  211. ->willReturn($folder);
  212. $this->appData->expects($this->once())
  213. ->method('getDirectoryListing')
  214. ->willReturn([]);
  215. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  216. }
  217. public function testGetCachedImage() {
  218. $expected = $this->createMock(ISimpleFile::class);
  219. $folder = $this->setupCacheFolder();
  220. $folder->expects($this->once())
  221. ->method('getFile')
  222. ->with('filename')
  223. ->willReturn($expected);
  224. $this->assertEquals($expected, $this->imageManager->getCachedImage('filename'));
  225. }
  226. /**
  227. * @expectedException \OCP\Files\NotFoundException
  228. */
  229. public function testGetCachedImageNotFound() {
  230. $folder = $this->setupCacheFolder();
  231. $folder->expects($this->once())
  232. ->method('getFile')
  233. ->with('filename')
  234. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  235. $image = $this->imageManager->getCachedImage('filename');
  236. }
  237. public function testSetCachedImage() {
  238. $folder = $this->setupCacheFolder();
  239. $file = $this->createMock(ISimpleFile::class);
  240. $folder->expects($this->once())
  241. ->method('fileExists')
  242. ->with('filename')
  243. ->willReturn(true);
  244. $folder->expects($this->once())
  245. ->method('getFile')
  246. ->with('filename')
  247. ->willReturn($file);
  248. $file->expects($this->once())
  249. ->method('putContent')
  250. ->with('filecontent');
  251. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  252. }
  253. public function testSetCachedImageCreate() {
  254. $folder = $this->setupCacheFolder();
  255. $file = $this->createMock(ISimpleFile::class);
  256. $folder->expects($this->once())
  257. ->method('fileExists')
  258. ->with('filename')
  259. ->willReturn(false);
  260. $folder->expects($this->once())
  261. ->method('newFile')
  262. ->with('filename')
  263. ->willReturn($file);
  264. $file->expects($this->once())
  265. ->method('putContent')
  266. ->with('filecontent');
  267. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  268. }
  269. private function setupCacheFolder() {
  270. $folder = $this->createMock(ISimpleFolder::class);
  271. $this->config->expects($this->once())
  272. ->method('getAppValue')
  273. ->with('theming', 'cachebuster', '0')
  274. ->willReturn('0');
  275. $this->appData->expects($this->at(0))
  276. ->method('getFolder')
  277. ->with('0')
  278. ->willReturn($folder);
  279. return $folder;
  280. }
  281. public function testCleanup() {
  282. $folders = [
  283. $this->createMock(ISimpleFolder::class),
  284. $this->createMock(ISimpleFolder::class),
  285. $this->createMock(ISimpleFolder::class)
  286. ];
  287. foreach ($folders as $index=>$folder) {
  288. $folder->expects($this->any())
  289. ->method('getName')
  290. ->willReturn($index);
  291. }
  292. $folders[0]->expects($this->once())->method('delete');
  293. $folders[1]->expects($this->once())->method('delete');
  294. $folders[2]->expects($this->never())->method('delete');
  295. $this->config->expects($this->once())
  296. ->method('getAppValue')
  297. ->with('theming','cachebuster','0')
  298. ->willReturn('2');
  299. $this->appData->expects($this->once())
  300. ->method('getDirectoryListing')
  301. ->willReturn($folders);
  302. $this->appData->expects($this->once())
  303. ->method('getFolder')
  304. ->with('2')
  305. ->willReturn($folders[2]);
  306. $this->imageManager->cleanup();
  307. }
  308. }