1
0

DyslexiaFontTest.php 4.3 KB

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