DyslexiaFontTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Service;
  7. use OC\Route\Router;
  8. use OC\URLGenerator;
  9. use OCA\Theming\ImageManager;
  10. use OCA\Theming\ITheme;
  11. use OCA\Theming\Themes\DyslexiaFont;
  12. use OCA\Theming\ThemingDefaults;
  13. use OCA\Theming\Util;
  14. use OCP\App\IAppManager;
  15. use OCP\Files\IAppData;
  16. use OCP\ICacheFactory;
  17. use OCP\IConfig;
  18. use OCP\IL10N;
  19. use OCP\IRequest;
  20. use OCP\IURLGenerator;
  21. use OCP\IUserSession;
  22. use OCP\ServerVersion;
  23. use PHPUnit\Framework\MockObject\MockObject;
  24. use Test\TestCase;
  25. class DyslexiaFontTest extends TestCase {
  26. /** @var ThemingDefaults|MockObject */
  27. private $themingDefaults;
  28. /** @var IUserSession|MockObject */
  29. private $userSession;
  30. /** @var IURLGenerator|MockObject */
  31. private $urlGenerator;
  32. /** @var ImageManager|MockObject */
  33. private $imageManager;
  34. /** @var IConfig|MockObject */
  35. private $config;
  36. /** @var IL10N|MockObject */
  37. private $l10n;
  38. /** @var IAppManager|MockObject */
  39. private $appManager;
  40. private DyslexiaFont $dyslexiaFont;
  41. protected function setUp(): void {
  42. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  43. $this->userSession = $this->createMock(IUserSession::class);
  44. $this->imageManager = $this->createMock(ImageManager::class);
  45. $this->config = $this->createMock(IConfig::class);
  46. $this->l10n = $this->createMock(IL10N::class);
  47. $this->appManager = $this->createMock(IAppManager::class);
  48. $util = new Util(
  49. $this->createMock(ServerVersion::class),
  50. $this->config,
  51. $this->appManager,
  52. $this->createMock(IAppData::class),
  53. $this->imageManager
  54. );
  55. $userSession = $this->createMock(IUserSession::class);
  56. $cacheFactory = $this->createMock(ICacheFactory::class);
  57. $request = $this->createMock(IRequest::class);
  58. $router = $this->createMock(Router::class);
  59. $this->urlGenerator = new URLGenerator(
  60. $this->config,
  61. $userSession,
  62. $cacheFactory,
  63. $request,
  64. $router
  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->themingDefaults
  75. ->expects($this->any())
  76. ->method('getColorBackground')
  77. ->willReturn('#0082c9');
  78. $this->themingDefaults
  79. ->expects($this->any())
  80. ->method('getDefaultColorBackground')
  81. ->willReturn('#0082c9');
  82. $this->l10n
  83. ->expects($this->any())
  84. ->method('t')
  85. ->willReturnCallback(function ($text, $parameters = []) {
  86. return vsprintf($text, $parameters);
  87. });
  88. $this->dyslexiaFont = new DyslexiaFont(
  89. $util,
  90. $this->themingDefaults,
  91. $this->userSession,
  92. $this->urlGenerator,
  93. $this->imageManager,
  94. $this->config,
  95. $this->l10n,
  96. $this->appManager,
  97. null,
  98. );
  99. parent::setUp();
  100. }
  101. public function testGetId(): void {
  102. $this->assertEquals('opendyslexic', $this->dyslexiaFont->getId());
  103. }
  104. public function testGetType(): void {
  105. $this->assertEquals(ITheme::TYPE_FONT, $this->dyslexiaFont->getType());
  106. }
  107. public function testGetTitle(): void {
  108. $this->assertNotEmpty($this->dyslexiaFont->getTitle());
  109. }
  110. public function testGetEnableLabel(): void {
  111. $this->assertNotEmpty($this->dyslexiaFont->getEnableLabel());
  112. }
  113. public function testGetDescription(): void {
  114. $this->assertNotEmpty($this->dyslexiaFont->getDescription());
  115. }
  116. public function testGetMediaQuery(): void {
  117. $this->assertEquals('', $this->dyslexiaFont->getMediaQuery());
  118. }
  119. public function testGetCSSVariables(): void {
  120. $this->assertStringStartsWith('OpenDyslexic', $this->dyslexiaFont->getCSSVariables()['--font-face']);
  121. }
  122. public function dataTestGetCustomCss() {
  123. return [
  124. ['', true],
  125. ['', false],
  126. ['/subfolder', true],
  127. ['/subfolder', false],
  128. ];
  129. }
  130. /**
  131. * @dataProvider dataTestGetCustomCss
  132. *
  133. * Ensure the fonts are always loaded from the web root
  134. * despite having url rewriting enabled or not
  135. *
  136. * @param string $webRoot
  137. * @param bool $prettyUrlsEnabled
  138. */
  139. public function testGetCustomCss($webRoot, $prettyUrlsEnabled): void {
  140. \OC::$WEBROOT = $webRoot;
  141. $this->config->expects($this->any())
  142. ->method('getSystemValue')
  143. ->with('htaccess.IgnoreFrontController', false)
  144. ->willReturn($prettyUrlsEnabled);
  145. $this->assertStringContainsString("'$webRoot/apps/theming/fonts/OpenDyslexic-Regular.otf'", $this->dyslexiaFont->getCustomCss());
  146. $this->assertStringNotContainsString('index.php', $this->dyslexiaFont->getCustomCss());
  147. }
  148. }