IconsCacherTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  4. *
  5. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Template;
  24. use OC\Files\AppData\AppData;
  25. use OC\Files\AppData\Factory;
  26. use OC\Template\IconsCacher;
  27. use OCA\Theming\ThemingDefaults;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\Files\SimpleFS\ISimpleFolder;
  32. use OCP\ICache;
  33. use OCP\ICacheFactory;
  34. use OCP\IConfig;
  35. use OCP\ILogger;
  36. use OCP\IURLGenerator;
  37. use OC_App;
  38. class IconsCacherTest extends \Test\TestCase {
  39. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $logger;
  41. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $appData;
  43. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $urlGenerator;
  45. protected function setUp() {
  46. $this->logger = $this->createMock(ILogger::class);
  47. $this->appData = $this->createMock(AppData::class);
  48. /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
  49. $factory = $this->createMock(Factory::class);
  50. $factory->method('get')->with('css')->willReturn($this->appData);
  51. $this->folder = $this->createMock(ISimpleFolder::class);
  52. $this->appData->method('getFolder')->willReturn($this->folder);
  53. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  54. $this->iconsCacher = new IconsCacher(
  55. $this->logger,
  56. $factory,
  57. $this->urlGenerator
  58. );
  59. }
  60. public function testGetIconsFromEmptyCss() {
  61. $css = "
  62. icon.test {
  63. color: #aaa;
  64. }
  65. ";
  66. $icons = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
  67. $this->assertTrue(empty($icons));
  68. }
  69. public function testGetIconsFromValidCss() {
  70. $css = "
  71. icon.test {
  72. --icon-test: url('/svg/core/actions/add/000');
  73. background-image: var(--icon-test);
  74. }
  75. ";
  76. $actual = self::invokePrivate($this->iconsCacher, 'getIconsFromCss', [$css]);
  77. $expected = array(
  78. 'icon-test' => '/svg/core/actions/add/000'
  79. );
  80. $this->assertEquals($expected, $actual);
  81. }
  82. public function testSetIconsFromEmptyCss() {
  83. $expected = "
  84. icon.test {
  85. color: #aaa;
  86. }
  87. ";
  88. $actual = $this->iconsCacher->setIconsCss($expected);
  89. $this->assertEquals($expected, $actual);
  90. }
  91. public function testSetIconsFromValidCss() {
  92. $css = "
  93. icon.test {
  94. --icon-test: url('/svg/core/actions/add/000');
  95. background-image: var(--icon-test);
  96. }
  97. ";
  98. $expected = "
  99. icon.test {
  100. background-image: var(--icon-test);
  101. }
  102. ";
  103. $iconsFile = $this->createMock(ISimpleFile::class);
  104. $this->folder->expects($this->once())
  105. ->method('getFile')
  106. ->willReturn($iconsFile);
  107. $actual = $this->iconsCacher->setIconsCss($css);
  108. $this->assertEquals($expected, $actual);
  109. }
  110. }