DefaultThemeTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <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 OCA\Theming\Tests\Service;
  24. use OC\App\AppManager;
  25. use OCA\Theming\ImageManager;
  26. use OCA\Theming\ITheme;
  27. use OCA\Theming\Themes\DefaultTheme;
  28. use OCA\Theming\ThemingDefaults;
  29. use OCA\Theming\Util;
  30. use OCP\App\IAppManager;
  31. use OCP\Files\IAppData;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use OCP\IURLGenerator;
  35. use OCP\IUserSession;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Test\TestCase;
  38. class DefaultThemeTest extends TestCase {
  39. /** @var ThemingDefaults|MockObject */
  40. private $themingDefaults;
  41. /** @var IURLGenerator|MockObject */
  42. private $urlGenerator;
  43. /** @var ImageManager|MockObject */
  44. private $imageManager;
  45. /** @var IConfig|MockObject */
  46. private $config;
  47. /** @var IL10N|MockObject */
  48. private $l10n;
  49. /** @var IAppManager|MockObject */
  50. private $appManager;
  51. private DefaultTheme $defaultTheme;
  52. protected function setUp(): void {
  53. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  54. $this->userSession = $this->createMock(IUserSession::class);
  55. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  56. $this->imageManager = $this->createMock(ImageManager::class);
  57. $this->config = $this->createMock(IConfig::class);
  58. $this->l10n = $this->createMock(IL10N::class);
  59. $this->appManager = $this->createMock(IAppManager::class);
  60. $util = new Util(
  61. $this->config,
  62. $this->appManager,
  63. $this->createMock(IAppData::class),
  64. $this->imageManager
  65. );
  66. $this->themingDefaults
  67. ->expects($this->any())
  68. ->method('getColorPrimary')
  69. ->willReturn('#0082c9');
  70. $this->themingDefaults
  71. ->expects($this->any())
  72. ->method('getDefaultColorPrimary')
  73. ->willReturn('#0082c9');
  74. $this->l10n
  75. ->expects($this->any())
  76. ->method('t')
  77. ->willReturnCallback(function ($text, $parameters = []) {
  78. return vsprintf($text, $parameters);
  79. });
  80. $this->urlGenerator
  81. ->expects($this->any())
  82. ->method('imagePath')
  83. ->willReturnCallback(function ($app = 'core', $filename = '') {
  84. return "/$app/img/$filename";
  85. });
  86. $this->defaultTheme = new DefaultTheme(
  87. $util,
  88. $this->themingDefaults,
  89. $this->userSession,
  90. $this->urlGenerator,
  91. $this->imageManager,
  92. $this->config,
  93. $this->l10n,
  94. $this->appManager,
  95. );
  96. parent::setUp();
  97. }
  98. public function testGetId() {
  99. $this->assertEquals('default', $this->defaultTheme->getId());
  100. }
  101. public function testGetType() {
  102. $this->assertEquals(ITheme::TYPE_THEME, $this->defaultTheme->getType());
  103. }
  104. public function testGetTitle() {
  105. $this->assertEquals('System default theme', $this->defaultTheme->getTitle());
  106. }
  107. public function testGetEnableLabel() {
  108. $this->assertEquals('Enable the system default', $this->defaultTheme->getEnableLabel());
  109. }
  110. public function testGetDescription() {
  111. $this->assertEquals('Using the default system appearance.', $this->defaultTheme->getDescription());
  112. }
  113. public function testGetMediaQuery() {
  114. $this->assertEquals('', $this->defaultTheme->getMediaQuery());
  115. }
  116. public function testGetCustomCss() {
  117. $this->assertEquals('', $this->defaultTheme->getCustomCss());
  118. }
  119. /**
  120. * Ensure parity between the default theme and the static generated file
  121. * @see ThemingController.php:313
  122. */
  123. public function testThemindDisabledFallbackCss() {
  124. // Generate variables
  125. $variables = '';
  126. foreach ($this->defaultTheme->getCSSVariables() as $variable => $value) {
  127. $variables .= " $variable: $value;" . PHP_EOL;
  128. };
  129. $css = ":root {" . PHP_EOL . "$variables}" . PHP_EOL;
  130. $fallbackCss = file_get_contents(__DIR__ . '/../../css/default.css');
  131. $this->assertEquals($css, $fallbackCss);
  132. }
  133. }