DefaultThemeTest.php 4.4 KB

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