DyslexiaFontTest.php 4.8 KB

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