1
0

IconControllerTest.php 8.1 KB

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