SvgControllerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\IAppManager;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IRequest;
  31. use Test\TestCase;
  32. /**
  33. * This class provides test cases for the svg controller
  34. */
  35. class SvgControllerTest extends TestCase {
  36. const TEST_IMAGES_SOURCE_PATH = __DIR__ . '/../../data/svg';
  37. const TEST_IMAGES_PATH = __DIR__ . '/../../../core/img/testImages';
  38. const TEST_IMAGE_MIXED = 'mixed-source.svg';
  39. const TEST_IMAGE_RECT = 'rect-black.svg';
  40. const TEST_IMAGES = [
  41. self::TEST_IMAGE_MIXED,
  42. self::TEST_IMAGE_RECT,
  43. ];
  44. /**
  45. * @var SvgController
  46. */
  47. private $svgController;
  48. /**
  49. * Copy test svgs into the core img "test" directory.
  50. *
  51. * @beforeClass
  52. * @return void
  53. */
  54. public static function copyTestImagesIntoPlace() {
  55. mkdir(self::TEST_IMAGES_PATH);
  56. foreach (self::TEST_IMAGES as $testImage) {
  57. copy(
  58. self::TEST_IMAGES_SOURCE_PATH .'/' . $testImage,
  59. self::TEST_IMAGES_PATH . '/' . $testImage
  60. );
  61. }
  62. }
  63. /**
  64. * Removes the test svgs from the core img "test" directory.
  65. *
  66. * @afterClass
  67. * @return void
  68. */
  69. public static function removeTestImages() {
  70. foreach (self::TEST_IMAGES as $testImage) {
  71. unlink(self::TEST_IMAGES_PATH . '/' . $testImage);
  72. }
  73. rmdir(self::TEST_IMAGES_PATH);
  74. }
  75. /**
  76. * Setups a SVG controller instance for tests.
  77. *
  78. * @before
  79. * @return void
  80. */
  81. public function setupSvgController() {
  82. $request = $this->getMockBuilder(IRequest::class)->getMock();
  83. $timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
  84. $appManager = $this->getMockBuilder(IAppManager::class)->getMock();
  85. $iconsCacher = $this->getMockBuilder(IconsCacher::class)->disableOriginalConstructor()->setMethods(['__construct'])->getMock();
  86. $this->svgController = new SvgController('core', $request, $timeFactory, $appManager, $iconsCacher);
  87. }
  88. /**
  89. * Checks that requesting an unknown image results in a 404.
  90. *
  91. * @test
  92. * @return void
  93. */
  94. public function testGetSvgFromCoreNotFound() {
  95. $response = $this->svgController->getSvgFromCore('huhuu', '2342', '#ff0000');
  96. self::assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
  97. }
  98. /**
  99. * Provides svg coloring test data.
  100. *
  101. * @return array
  102. */
  103. public function provideGetSvgFromCoreTestData(): array {
  104. return [
  105. 'mixed' => ['mixed-source', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/mixed-red.svg')],
  106. 'black rect' => ['rect-black', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/rect-red.svg')],
  107. ];
  108. }
  109. /**
  110. * Tests that retrieving a colored SVG works.
  111. *
  112. * @test
  113. * @dataProvider provideGetSvgFromCoreTestData
  114. * @param string $name The requested svg name
  115. * @param string $color The requested color
  116. * @param string $expected The expected svg
  117. * @return void
  118. */
  119. public function testGetSvgFromCore(string $name, string $color, string $expected) {
  120. $response = $this->svgController->getSvgFromCore('testImages', $name, $color);
  121. self::assertEquals(Http::STATUS_OK, $response->getStatus());
  122. $headers = $response->getHeaders();
  123. self::assertArrayHasKey('Content-Type', $headers);
  124. self::assertEquals($headers['Content-Type'], 'image/svg+xml');
  125. self::assertEquals($expected, $response->getData());
  126. }
  127. }