SvgControllerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018 Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Michael Weimann <mail@michael-weimann.eu>
  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 Tests\Core\Controller;
  25. use OC\AppFramework\Http;
  26. use OC\Core\Controller\SvgController;
  27. use OC\Template\IconsCacher;
  28. use OCP\App\AppPathNotFoundException;
  29. use OCP\App\IAppManager;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\IRequest;
  32. use Test\TestCase;
  33. /**
  34. * This class provides test cases for the svg controller
  35. */
  36. class SvgControllerTest extends TestCase {
  37. public const TEST_IMAGES_SOURCE_PATH = __DIR__ . '/../../data/svg';
  38. public const TEST_IMAGES_PATH = __DIR__ . '/../../../core/img/testImages';
  39. public const TEST_IMAGE_MIXED = 'mixed-source.svg';
  40. public const TEST_IMAGE_RECT = 'rect-black.svg';
  41. public const TEST_IMAGES = [
  42. self::TEST_IMAGE_MIXED,
  43. self::TEST_IMAGE_RECT,
  44. ];
  45. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  46. private $appManager;
  47. /**
  48. * @var SvgController
  49. */
  50. private $svgController;
  51. /**
  52. * Copy test svgs into the core img "test" directory.
  53. *
  54. * @beforeClass
  55. * @return void
  56. */
  57. public static function copyTestImagesIntoPlace() {
  58. mkdir(self::TEST_IMAGES_PATH);
  59. foreach (self::TEST_IMAGES as $testImage) {
  60. copy(
  61. self::TEST_IMAGES_SOURCE_PATH .'/' . $testImage,
  62. self::TEST_IMAGES_PATH . '/' . $testImage
  63. );
  64. }
  65. }
  66. /**
  67. * Removes the test svgs from the core img "test" directory.
  68. *
  69. * @afterClass
  70. * @return void
  71. */
  72. public static function removeTestImages() {
  73. foreach (self::TEST_IMAGES as $testImage) {
  74. unlink(self::TEST_IMAGES_PATH . '/' . $testImage);
  75. }
  76. rmdir(self::TEST_IMAGES_PATH);
  77. }
  78. /**
  79. * Setups a SVG controller instance for tests.
  80. *
  81. * @before
  82. * @return void
  83. */
  84. public function setupSvgController() {
  85. /** @var IRequest */
  86. $request = $this->getMockBuilder(IRequest::class)->getMock();
  87. /** @var ITimeFactory $timeFactory */
  88. $timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
  89. /** @var IAppManager */
  90. $this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
  91. /** @var IconsCacher $iconsCacher */
  92. $iconsCacher = $this->getMockBuilder(IconsCacher::class)->disableOriginalConstructor()->setMethods(['__construct'])->getMock();
  93. $this->svgController = new SvgController('core', $request, $timeFactory, $this->appManager, $iconsCacher);
  94. }
  95. /**
  96. * Checks that requesting an unknown image results in a 404.
  97. *
  98. * @return void
  99. */
  100. public function testGetSvgFromCoreNotFound() {
  101. $response = $this->svgController->getSvgFromCore('huhuu', '2342', '#ff0000');
  102. self::assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  103. }
  104. /**
  105. * Provides svg coloring test data.
  106. *
  107. * @return array
  108. */
  109. public function provideGetSvgFromCoreTestData(): array {
  110. return [
  111. 'mixed' => ['mixed-source', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/mixed-red.svg')],
  112. 'black rect' => ['rect-black', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/rect-red.svg')],
  113. ];
  114. }
  115. /**
  116. * Tests that retrieving a colored SVG works.
  117. *
  118. * @dataProvider provideGetSvgFromCoreTestData
  119. * @param string $name The requested svg name
  120. * @param string $color The requested color
  121. * @param string $expected The expected svg
  122. * @return void
  123. */
  124. public function testGetSvgFromCore(string $name, string $color, string $expected) {
  125. $response = $this->svgController->getSvgFromCore('testImages', $name, $color);
  126. self::assertEquals(Http::STATUS_OK, $response->getStatus());
  127. $headers = $response->getHeaders();
  128. self::assertArrayHasKey('Content-Type', $headers);
  129. self::assertEquals($headers['Content-Type'], 'image/svg+xml');
  130. self::assertEquals($expected, $response->getData());
  131. }
  132. /**
  133. * Checks that requesting an unknown image results in a 404.
  134. */
  135. public function testGetSvgFromAppNotFound(): void {
  136. $this->appManager->expects($this->once())
  137. ->method('getAppPath')
  138. ->with('invalid_app')
  139. ->willThrowException(new AppPathNotFoundException('Could not find path for invalid_app'));
  140. $response = $this->svgController->getSvgFromApp('invalid_app', 'some-icon', '#ff0000');
  141. self::assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  142. }
  143. /**
  144. * Provides svg coloring test data.
  145. *
  146. * @return array
  147. */
  148. public function provideGetSvgFromAppTestData(): array {
  149. return [
  150. 'settings admin' => ['settings', 'admin', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/settings-admin-red.svg')],
  151. 'files app' => ['files', 'app', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/files-app-red.svg')],
  152. ];
  153. }
  154. /**
  155. * Tests that retrieving a colored SVG works.
  156. *
  157. * @dataProvider provideGetSvgFromAppTestData
  158. * @param string $appName
  159. * @param string $name The requested svg name
  160. * @param string $color The requested color
  161. * @param string $expected
  162. */
  163. public function testGetSvgFromApp(string $appName, string $name, string $color, string $expected): void {
  164. $this->appManager->expects($this->once())
  165. ->method('getAppPath')
  166. ->with($appName)
  167. ->willReturn(realpath(__DIR__ . '/../../../apps/') . '/' . $appName);
  168. $response = $this->svgController->getSvgFromApp($appName, $name, $color);
  169. self::assertEquals(Http::STATUS_OK, $response->getStatus());
  170. $headers = $response->getHeaders();
  171. self::assertArrayHasKey('Content-Type', $headers);
  172. self::assertEquals($headers['Content-Type'], 'image/svg+xml');
  173. self::assertEquals($expected, $response->getData());
  174. }
  175. }