IconControllerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Controller;
  7. use OC\Files\SimpleFS\SimpleFile;
  8. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  9. use OCA\Theming\Controller\IconController;
  10. use OCA\Theming\IconBuilder;
  11. use OCA\Theming\ImageManager;
  12. use OCA\Theming\ThemingDefaults;
  13. use OCP\App\IAppManager;
  14. use OCP\AppFramework\Http;
  15. use OCP\AppFramework\Http\DataDisplayResponse;
  16. use OCP\AppFramework\Http\FileDisplayResponse;
  17. use OCP\AppFramework\Utility\ITimeFactory;
  18. use OCP\Files\NotFoundException;
  19. use OCP\IConfig;
  20. use OCP\IRequest;
  21. use Test\TestCase;
  22. class IconControllerTest extends TestCase {
  23. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  24. private $request;
  25. /** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
  26. private $themingDefaults;
  27. /** @var \OCP\AppFramework\Utility\ITimeFactory */
  28. private $timeFactory;
  29. /** @var IconController|\PHPUnit\Framework\MockObject\MockObject */
  30. private $iconController;
  31. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  32. private $config;
  33. /** @var IconBuilder|\PHPUnit\Framework\MockObject\MockObject */
  34. private $iconBuilder;
  35. /** @var FileAccessHelper|\PHPUnit\Framework\MockObject\MockObject */
  36. private $fileAccessHelper;
  37. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  38. private $appManager;
  39. /** @var ImageManager */
  40. private $imageManager;
  41. protected function setUp(): void {
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  44. $this->iconBuilder = $this->createMock(IconBuilder::class);
  45. $this->imageManager = $this->createMock(ImageManager::class);
  46. $this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
  47. $this->appManager = $this->createMock(IAppManager::class);
  48. $this->timeFactory = $this->createMock(ITimeFactory::class);
  49. $this->timeFactory->expects($this->any())
  50. ->method('getTime')
  51. ->willReturn(123);
  52. $this->overwriteService(ITimeFactory::class, $this->timeFactory);
  53. $this->iconController = new IconController(
  54. 'theming',
  55. $this->request,
  56. $this->themingDefaults,
  57. $this->iconBuilder,
  58. $this->imageManager,
  59. $this->fileAccessHelper,
  60. $this->appManager,
  61. );
  62. parent::setUp();
  63. }
  64. private function iconFileMock($filename, $data) {
  65. $icon = $this->getMockBuilder('OCP\Files\File')->getMock();
  66. $icon->expects($this->any())->method('getContent')->willReturn($data);
  67. $icon->expects($this->any())->method('getMimeType')->willReturn('image type');
  68. $icon->expects($this->any())->method('getEtag')->willReturn('my etag');
  69. $icon->expects($this->any())->method('getName')->willReturn('my name');
  70. $icon->expects($this->any())->method('getMTime')->willReturn(42);
  71. $icon->method('getName')->willReturn($filename);
  72. return new SimpleFile($icon);
  73. }
  74. public function testGetThemedIcon(): void {
  75. $file = $this->iconFileMock('icon-core-filetypes_folder.svg', 'filecontent');
  76. $this->imageManager->expects($this->once())
  77. ->method('getCachedImage')
  78. ->with('icon-core-filetypes_folder.svg')
  79. ->willReturn($file);
  80. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  81. $expected->cacheFor(86400, false, true);
  82. $this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
  83. }
  84. public function testGetFaviconDefault(): void {
  85. if (!extension_loaded('imagick')) {
  86. $this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
  87. }
  88. $checkImagick = new \Imagick();
  89. if (count($checkImagick->queryFormats('SVG')) < 1) {
  90. $this->markTestSkipped('No SVG provider present.');
  91. }
  92. $file = $this->iconFileMock('filename', 'filecontent');
  93. $this->imageManager->expects($this->once())
  94. ->method('getImage', false)
  95. ->with('favicon')
  96. ->will($this->throwException(new NotFoundException()));
  97. $this->imageManager->expects($this->any())
  98. ->method('shouldReplaceIcons')
  99. ->willReturn(true);
  100. $this->imageManager->expects($this->once())
  101. ->method('getCachedImage')
  102. ->will($this->throwException(new NotFoundException()));
  103. $this->iconBuilder->expects($this->once())
  104. ->method('getFavicon')
  105. ->with('core')
  106. ->willReturn('filecontent');
  107. $this->imageManager->expects($this->once())
  108. ->method('setCachedImage')
  109. ->willReturn($file);
  110. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  111. $expected->cacheFor(86400);
  112. $this->assertEquals($expected, $this->iconController->getFavicon());
  113. }
  114. public function testGetFaviconFail(): void {
  115. $this->imageManager->expects($this->once())
  116. ->method('getImage')
  117. ->with('favicon', false)
  118. ->will($this->throwException(new NotFoundException()));
  119. $this->imageManager->expects($this->any())
  120. ->method('shouldReplaceIcons')
  121. ->willReturn(false);
  122. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
  123. $this->fileAccessHelper->expects($this->once())
  124. ->method('file_get_contents')
  125. ->with($fallbackLogo)
  126. ->willReturn(file_get_contents($fallbackLogo));
  127. $expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  128. $expected->cacheFor(86400);
  129. $this->assertEquals($expected, $this->iconController->getFavicon());
  130. }
  131. public function testGetTouchIconDefault(): void {
  132. if (!extension_loaded('imagick')) {
  133. $this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
  134. }
  135. $checkImagick = new \Imagick();
  136. if (count($checkImagick->queryFormats('SVG')) < 1) {
  137. $this->markTestSkipped('No SVG provider present.');
  138. }
  139. $this->imageManager->expects($this->once())
  140. ->method('getImage')
  141. ->will($this->throwException(new NotFoundException()));
  142. $this->imageManager->expects($this->any())
  143. ->method('shouldReplaceIcons')
  144. ->willReturn(true);
  145. $this->iconBuilder->expects($this->once())
  146. ->method('getTouchIcon')
  147. ->with('core')
  148. ->willReturn('filecontent');
  149. $file = $this->iconFileMock('filename', 'filecontent');
  150. $this->imageManager->expects($this->once())
  151. ->method('getCachedImage')
  152. ->will($this->throwException(new NotFoundException()));
  153. $this->imageManager->expects($this->once())
  154. ->method('setCachedImage')
  155. ->willReturn($file);
  156. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  157. $expected->cacheFor(86400);
  158. $this->assertEquals($expected, $this->iconController->getTouchIcon());
  159. }
  160. public function testGetTouchIconFail(): void {
  161. $this->imageManager->expects($this->once())
  162. ->method('getImage')
  163. ->with('favicon')
  164. ->will($this->throwException(new NotFoundException()));
  165. $this->imageManager->expects($this->any())
  166. ->method('shouldReplaceIcons')
  167. ->willReturn(false);
  168. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
  169. $this->fileAccessHelper->expects($this->once())
  170. ->method('file_get_contents')
  171. ->with($fallbackLogo)
  172. ->willReturn(file_get_contents($fallbackLogo));
  173. $expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  174. $expected->cacheFor(86400);
  175. $this->assertEquals($expected, $this->iconController->getTouchIcon());
  176. }
  177. }