DyslexiaFontTest.php 4.8 KB

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