DefaultThemeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests\Themes;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\ImageManager;
  9. use OCA\Theming\ITheme;
  10. use OCA\Theming\Service\BackgroundService;
  11. use OCA\Theming\Themes\DefaultTheme;
  12. use OCA\Theming\ThemingDefaults;
  13. use OCA\Theming\Util;
  14. use OCP\App\IAppManager;
  15. use OCP\Files\IAppData;
  16. use OCP\IConfig;
  17. use OCP\IL10N;
  18. use OCP\IURLGenerator;
  19. use OCP\IUserSession;
  20. use OCP\ServerVersion;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. class DefaultThemeTest extends AccessibleThemeTestCase {
  23. /** @var ThemingDefaults|MockObject */
  24. private $themingDefaults;
  25. /** @var IUserSession|MockObject */
  26. private $userSession;
  27. /** @var IURLGenerator|MockObject */
  28. private $urlGenerator;
  29. /** @var ImageManager|MockObject */
  30. private $imageManager;
  31. /** @var IConfig|MockObject */
  32. private $config;
  33. /** @var IL10N|MockObject */
  34. private $l10n;
  35. /** @var IAppManager|MockObject */
  36. private $appManager;
  37. protected function setUp(): void {
  38. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  39. $this->userSession = $this->createMock(IUserSession::class);
  40. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  41. $this->imageManager = $this->createMock(ImageManager::class);
  42. $this->config = $this->createMock(IConfig::class);
  43. $this->l10n = $this->createMock(IL10N::class);
  44. $this->appManager = $this->createMock(IAppManager::class);
  45. $this->util = new Util(
  46. $this->createMock(ServerVersion::class),
  47. $this->config,
  48. $this->appManager,
  49. $this->createMock(IAppData::class),
  50. $this->imageManager
  51. );
  52. $defaultBackground = BackgroundService::SHIPPED_BACKGROUNDS[BackgroundService::DEFAULT_BACKGROUND_IMAGE];
  53. $this->themingDefaults
  54. ->expects($this->any())
  55. ->method('getColorPrimary')
  56. ->willReturn($defaultBackground['primary_color']);
  57. $this->themingDefaults
  58. ->expects($this->any())
  59. ->method('getColorBackground')
  60. ->willReturn($defaultBackground['background_color']);
  61. $this->themingDefaults
  62. ->expects($this->any())
  63. ->method('getDefaultColorPrimary')
  64. ->willReturn($defaultBackground['primary_color']);
  65. $this->themingDefaults
  66. ->expects($this->any())
  67. ->method('getDefaultColorBackground')
  68. ->willReturn($defaultBackground['background_color']);
  69. $this->themingDefaults
  70. ->expects($this->any())
  71. ->method('getBackground')
  72. ->willReturn('/apps/' . Application::APP_ID . '/img/background/' . BackgroundService::DEFAULT_BACKGROUND_IMAGE);
  73. $this->l10n
  74. ->expects($this->any())
  75. ->method('t')
  76. ->willReturnCallback(function ($text, $parameters = []) {
  77. return vsprintf($text, $parameters);
  78. });
  79. $this->urlGenerator
  80. ->expects($this->any())
  81. ->method('imagePath')
  82. ->willReturnCallback(function ($app = 'core', $filename = '') {
  83. return "/$app/img/$filename";
  84. });
  85. $this->theme = new DefaultTheme(
  86. $this->util,
  87. $this->themingDefaults,
  88. $this->userSession,
  89. $this->urlGenerator,
  90. $this->imageManager,
  91. $this->config,
  92. $this->l10n,
  93. $this->appManager,
  94. null,
  95. );
  96. parent::setUp();
  97. }
  98. public function testGetId(): void {
  99. $this->assertEquals('default', $this->theme->getId());
  100. }
  101. public function testGetType(): void {
  102. $this->assertEquals(ITheme::TYPE_THEME, $this->theme->getType());
  103. }
  104. public function testGetTitle(): void {
  105. $this->assertEquals('System default theme', $this->theme->getTitle());
  106. }
  107. public function testGetEnableLabel(): void {
  108. $this->assertEquals('Enable the system default', $this->theme->getEnableLabel());
  109. }
  110. public function testGetDescription(): void {
  111. $this->assertEquals('Using the default system appearance.', $this->theme->getDescription());
  112. }
  113. public function testGetMediaQuery(): void {
  114. $this->assertEquals('', $this->theme->getMediaQuery());
  115. }
  116. public function testGetCustomCss(): void {
  117. $this->assertEquals('', $this->theme->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(): void {
  124. // Generate variables
  125. $variables = '';
  126. foreach ($this->theme->getCSSVariables() as $variable => $value) {
  127. $variables .= " $variable: $value;" . PHP_EOL;
  128. };
  129. $css = "\n:root {" . PHP_EOL . "$variables}" . PHP_EOL;
  130. $fallbackCss = file_get_contents(__DIR__ . '/../../css/default.css');
  131. // Remove comments
  132. $fallbackCss = preg_replace('/\s*\/\*[\s\S]*?\*\//m', '', $fallbackCss);
  133. // Remove blank lines
  134. $fallbackCss = preg_replace('/\s*\n\n/', "\n", $fallbackCss);
  135. $this->assertEquals($css, $fallbackCss);
  136. }
  137. }