DefaultThemeTest.php 4.7 KB

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