IconsCacherTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  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 Test\Template;
  25. use OC\Files\AppData\AppData;
  26. use OC\Files\AppData\Factory;
  27. use OC\Template\IconsCacher;
  28. use OCA\Theming\ThemingDefaults;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Files\IAppData;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\SimpleFS\ISimpleFile;
  33. use OCP\Files\SimpleFS\ISimpleFolder;
  34. use OCP\ICache;
  35. use OCP\ICacheFactory;
  36. use OCP\IConfig;
  37. use OCP\ILogger;
  38. use OCP\IURLGenerator;
  39. use OC_App;
  40. class IconsCacherTest extends \Test\TestCase {
  41. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $logger;
  43. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $appData;
  45. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $urlGenerator;
  47. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  48. private $timeFactory;
  49. protected function setUp() {
  50. $this->logger = $this->createMock(ILogger::class);
  51. $this->appData = $this->createMock(AppData::class);
  52. $this->timeFactory = $this->createMock(ITimeFactory::class);
  53. /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
  54. $factory = $this->createMock(Factory::class);
  55. $factory->method('get')->with('css')->willReturn($this->appData);
  56. $this->folder = $this->createMock(ISimpleFolder::class);
  57. $this->appData->method('getFolder')->willReturn($this->folder);
  58. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  59. $this->iconsCacher = new IconsCacher(
  60. $this->logger,
  61. $factory,
  62. $this->urlGenerator,
  63. $this->timeFactory
  64. );
  65. }
  66. public function testGetIconsFromEmptyCss() {
  67. $css = "
  68. icon.test {
  69. color: #aaa;
  70. }
  71. ";
  72. $icons = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
  73. $this->assertTrue(empty($icons));
  74. }
  75. public function testGetIconsFromValidCss() {
  76. $css = "
  77. icon.test {
  78. --icon-test: url('/svg/core/actions/add/000?v=1');
  79. background-image: var(--icon-test);
  80. }
  81. ";
  82. $actual = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
  83. $expected = array(
  84. 'icon-test' => '/svg/core/actions/add/000?v=1'
  85. );
  86. $this->assertEquals($expected, $actual);
  87. }
  88. public function testSetIconsFromEmptyCss() {
  89. $expected = "
  90. icon.test {
  91. color: #aaa;
  92. }
  93. ";
  94. $actual = $this->iconsCacher->setIconsCss($expected);
  95. $this->assertEquals($expected, $actual);
  96. }
  97. public function testSetIconsFromValidCss() {
  98. $css = "
  99. icon.test {
  100. --icon-test: url('/index.php/svg/core/actions/add?color=000&v=1');
  101. background-image: var(--icon-test);
  102. }
  103. ";
  104. $expected = "
  105. icon.test {
  106. background-image: var(--icon-test);
  107. }
  108. ";
  109. $iconsFile = $this->createMock(ISimpleFile::class);
  110. $this->folder->expects($this->exactly(2))
  111. ->method('getFile')
  112. ->willReturn($iconsFile);
  113. $actual = $this->iconsCacher->setIconsCss($css);
  114. $this->assertEquals($expected, $actual);
  115. }
  116. public function testSetIconsFromValidCssMultipleTimes() {
  117. $css = "
  118. icon.test {
  119. --icon-test: url('/index.php/svg/core/actions/add?color=000&v=1');
  120. background-image: var(--icon-test);
  121. }
  122. ";
  123. $expected = "
  124. icon.test {
  125. background-image: var(--icon-test);
  126. }
  127. ";
  128. $iconsFile = $this->createMock(ISimpleFile::class);
  129. $this->folder->expects($this->exactly(6))
  130. ->method('getFile')
  131. ->willReturn($iconsFile);
  132. $actual = $this->iconsCacher->setIconsCss($css);
  133. $actual = $this->iconsCacher->setIconsCss($actual);
  134. $actual = $this->iconsCacher->setIconsCss($actual);
  135. $this->assertEquals($expected, $actual);
  136. }
  137. }