1
0

ThemingDefaultsTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests;
  7. use OCA\Theming\ImageManager;
  8. use OCA\Theming\Service\BackgroundService;
  9. use OCA\Theming\ThemingDefaults;
  10. use OCA\Theming\Util;
  11. use OCP\App\IAppManager;
  12. use OCP\Files\NotFoundException;
  13. use OCP\IAppConfig;
  14. use OCP\ICache;
  15. use OCP\ICacheFactory;
  16. use OCP\IConfig;
  17. use OCP\IL10N;
  18. use OCP\INavigationManager;
  19. use OCP\IURLGenerator;
  20. use OCP\IUser;
  21. use OCP\IUserSession;
  22. use PHPUnit\Framework\MockObject\MockObject;
  23. use Test\TestCase;
  24. class ThemingDefaultsTest extends TestCase {
  25. private IAppConfig&MockObject $appConfig;
  26. private IConfig&MockObject $config;
  27. private \OC_Defaults $defaults;
  28. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  29. private $l10n;
  30. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  31. private $userSession;
  32. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  33. private $urlGenerator;
  34. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  35. private $cacheFactory;
  36. /** @var ThemingDefaults */
  37. private $template;
  38. /** @var Util|\PHPUnit\Framework\MockObject\MockObject */
  39. private $util;
  40. /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
  41. private $cache;
  42. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $appManager;
  44. /** @var ImageManager|\PHPUnit\Framework\MockObject\MockObject */
  45. private $imageManager;
  46. /** @var INavigationManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $navigationManager;
  48. /** @var BackgroundService|\PHPUnit\Framework\MockObject\MockObject */
  49. private $backgroundService;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->appConfig = $this->createMock(IAppConfig::class);
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->l10n = $this->createMock(IL10N::class);
  55. $this->userSession = $this->createMock(IUserSession::class);
  56. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  57. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  58. $this->cache = $this->createMock(ICache::class);
  59. $this->util = $this->createMock(Util::class);
  60. $this->imageManager = $this->createMock(ImageManager::class);
  61. $this->appManager = $this->createMock(IAppManager::class);
  62. $this->navigationManager = $this->createMock(INavigationManager::class);
  63. $this->backgroundService = $this->createMock(BackgroundService::class);
  64. $this->defaults = new \OC_Defaults();
  65. $this->urlGenerator
  66. ->expects($this->any())
  67. ->method('getBaseUrl')
  68. ->willReturn('');
  69. $this->template = new ThemingDefaults(
  70. $this->config,
  71. $this->appConfig,
  72. $this->l10n,
  73. $this->userSession,
  74. $this->urlGenerator,
  75. $this->cacheFactory,
  76. $this->util,
  77. $this->imageManager,
  78. $this->appManager,
  79. $this->navigationManager,
  80. $this->backgroundService,
  81. );
  82. }
  83. public function testGetNameWithDefault(): void {
  84. $this->config
  85. ->expects($this->once())
  86. ->method('getAppValue')
  87. ->with('theming', 'name', 'Nextcloud')
  88. ->willReturn('Nextcloud');
  89. $this->assertEquals('Nextcloud', $this->template->getName());
  90. }
  91. public function testGetNameWithCustom(): void {
  92. $this->config
  93. ->expects($this->once())
  94. ->method('getAppValue')
  95. ->with('theming', 'name', 'Nextcloud')
  96. ->willReturn('MyCustomCloud');
  97. $this->assertEquals('MyCustomCloud', $this->template->getName());
  98. }
  99. public function testGetHTMLNameWithDefault(): void {
  100. $this->config
  101. ->expects($this->once())
  102. ->method('getAppValue')
  103. ->with('theming', 'name', 'Nextcloud')
  104. ->willReturn('Nextcloud');
  105. $this->assertEquals('Nextcloud', $this->template->getHTMLName());
  106. }
  107. public function testGetHTMLNameWithCustom(): void {
  108. $this->config
  109. ->expects($this->once())
  110. ->method('getAppValue')
  111. ->with('theming', 'name', 'Nextcloud')
  112. ->willReturn('MyCustomCloud');
  113. $this->assertEquals('MyCustomCloud', $this->template->getHTMLName());
  114. }
  115. public function testGetTitleWithDefault(): void {
  116. $this->config
  117. ->expects($this->once())
  118. ->method('getAppValue')
  119. ->with('theming', 'name', 'Nextcloud')
  120. ->willReturn('Nextcloud');
  121. $this->assertEquals('Nextcloud', $this->template->getTitle());
  122. }
  123. public function testGetTitleWithCustom(): void {
  124. $this->config
  125. ->expects($this->once())
  126. ->method('getAppValue')
  127. ->with('theming', 'name', 'Nextcloud')
  128. ->willReturn('MyCustomCloud');
  129. $this->assertEquals('MyCustomCloud', $this->template->getTitle());
  130. }
  131. public function testGetEntityWithDefault(): void {
  132. $this->config
  133. ->expects($this->once())
  134. ->method('getAppValue')
  135. ->with('theming', 'name', 'Nextcloud')
  136. ->willReturn('Nextcloud');
  137. $this->assertEquals('Nextcloud', $this->template->getEntity());
  138. }
  139. public function testGetEntityWithCustom(): void {
  140. $this->config
  141. ->expects($this->once())
  142. ->method('getAppValue')
  143. ->with('theming', 'name', 'Nextcloud')
  144. ->willReturn('MyCustomCloud');
  145. $this->assertEquals('MyCustomCloud', $this->template->getEntity());
  146. }
  147. public function testGetBaseUrlWithDefault(): void {
  148. $this->config
  149. ->expects($this->once())
  150. ->method('getAppValue')
  151. ->with('theming', 'url', $this->defaults->getBaseUrl())
  152. ->willReturn($this->defaults->getBaseUrl());
  153. $this->assertEquals($this->defaults->getBaseUrl(), $this->template->getBaseUrl());
  154. }
  155. public function testGetBaseUrlWithCustom(): void {
  156. $this->config
  157. ->expects($this->once())
  158. ->method('getAppValue')
  159. ->with('theming', 'url', $this->defaults->getBaseUrl())
  160. ->willReturn('https://example.com/');
  161. $this->assertEquals('https://example.com/', $this->template->getBaseUrl());
  162. }
  163. public function legalUrlProvider() {
  164. return [
  165. [ '' ],
  166. [ 'https://example.com/legal.html']
  167. ];
  168. }
  169. /**
  170. * @param $imprintUrl
  171. * @dataProvider legalUrlProvider
  172. */
  173. public function testGetImprintURL($imprintUrl): void {
  174. $this->config
  175. ->expects($this->once())
  176. ->method('getAppValue')
  177. ->with('theming', 'imprintUrl', '')
  178. ->willReturn($imprintUrl);
  179. $this->assertEquals($imprintUrl, $this->template->getImprintUrl());
  180. }
  181. /**
  182. * @param $privacyUrl
  183. * @dataProvider legalUrlProvider
  184. */
  185. public function testGetPrivacyURL($privacyUrl): void {
  186. $this->config
  187. ->expects($this->once())
  188. ->method('getAppValue')
  189. ->with('theming', 'privacyUrl', '')
  190. ->willReturn($privacyUrl);
  191. $this->assertEquals($privacyUrl, $this->template->getPrivacyUrl());
  192. }
  193. public function testGetSloganWithDefault(): void {
  194. $this->config
  195. ->expects($this->once())
  196. ->method('getAppValue')
  197. ->with('theming', 'slogan', $this->defaults->getSlogan())
  198. ->willReturn($this->defaults->getSlogan());
  199. $this->assertEquals($this->defaults->getSlogan(), $this->template->getSlogan());
  200. }
  201. public function testGetSloganWithCustom(): void {
  202. $this->config
  203. ->expects($this->once())
  204. ->method('getAppValue')
  205. ->with('theming', 'slogan', $this->defaults->getSlogan())
  206. ->willReturn('My custom Slogan');
  207. $this->assertEquals('My custom Slogan', $this->template->getSlogan());
  208. }
  209. public function testGetShortFooter(): void {
  210. $this->config
  211. ->expects($this->exactly(5))
  212. ->method('getAppValue')
  213. ->willReturnMap([
  214. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  215. ['theming', 'name', 'Nextcloud', 'Name'],
  216. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  217. ['theming', 'imprintUrl', '', ''],
  218. ['theming', 'privacyUrl', '', ''],
  219. ]);
  220. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan', $this->template->getShortFooter());
  221. }
  222. public function testGetShortFooterEmptyUrl(): void {
  223. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  224. $this->config
  225. ->expects($this->exactly(5))
  226. ->method('getAppValue')
  227. ->willReturnMap([
  228. ['theming', 'url', $this->defaults->getBaseUrl(), ''],
  229. ['theming', 'name', 'Nextcloud', 'Name'],
  230. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  231. ['theming', 'imprintUrl', '', ''],
  232. ['theming', 'privacyUrl', '', ''],
  233. ]);
  234. $this->assertEquals('<span class="entity-name">Name</span> – Slogan', $this->template->getShortFooter());
  235. }
  236. public function testGetShortFooterEmptySlogan(): void {
  237. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  238. $this->config
  239. ->expects($this->exactly(5))
  240. ->method('getAppValue')
  241. ->willReturnMap([
  242. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  243. ['theming', 'name', 'Nextcloud', 'Name'],
  244. ['theming', 'slogan', $this->defaults->getSlogan(), ''],
  245. ['theming', 'imprintUrl', '', ''],
  246. ['theming', 'privacyUrl', '', ''],
  247. ]);
  248. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a>', $this->template->getShortFooter());
  249. }
  250. public function testGetShortFooterImprint(): void {
  251. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  252. $this->config
  253. ->expects($this->exactly(5))
  254. ->method('getAppValue')
  255. ->willReturnMap([
  256. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  257. ['theming', 'name', 'Nextcloud', 'Name'],
  258. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  259. ['theming', 'imprintUrl', '', 'https://example.com/imprint'],
  260. ['theming', 'privacyUrl', '', ''],
  261. ]);
  262. $this->l10n
  263. ->expects($this->any())
  264. ->method('t')
  265. ->willReturnArgument(0);
  266. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan<br/><span class="footer__legal-links"><a href="https://example.com/imprint" class="legal" target="_blank" rel="noreferrer noopener">Legal notice</a></span>', $this->template->getShortFooter());
  267. }
  268. public function testGetShortFooterPrivacy(): void {
  269. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  270. $this->config
  271. ->expects($this->exactly(5))
  272. ->method('getAppValue')
  273. ->willReturnMap([
  274. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  275. ['theming', 'name', 'Nextcloud', 'Name'],
  276. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  277. ['theming', 'imprintUrl', '', ''],
  278. ['theming', 'privacyUrl', '', 'https://example.com/privacy'],
  279. ]);
  280. $this->l10n
  281. ->expects($this->any())
  282. ->method('t')
  283. ->willReturnArgument(0);
  284. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan<br/><span class="footer__legal-links"><a href="https://example.com/privacy" class="legal" target="_blank" rel="noreferrer noopener">Privacy policy</a></span>', $this->template->getShortFooter());
  285. }
  286. public function testGetShortFooterAllLegalLinks(): void {
  287. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  288. $this->config
  289. ->expects($this->exactly(5))
  290. ->method('getAppValue')
  291. ->willReturnMap([
  292. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  293. ['theming', 'name', 'Nextcloud', 'Name'],
  294. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  295. ['theming', 'imprintUrl', '', 'https://example.com/imprint'],
  296. ['theming', 'privacyUrl', '', 'https://example.com/privacy'],
  297. ]);
  298. $this->l10n
  299. ->expects($this->any())
  300. ->method('t')
  301. ->willReturnArgument(0);
  302. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan<br/><span class="footer__legal-links"><a href="https://example.com/imprint" class="legal" target="_blank" rel="noreferrer noopener">Legal notice</a> · <a href="https://example.com/privacy" class="legal" target="_blank" rel="noreferrer noopener">Privacy policy</a></span>', $this->template->getShortFooter());
  303. }
  304. public function invalidLegalUrlProvider() {
  305. return [
  306. ['example.com/legal'], # missing scheme
  307. ['https:///legal'], # missing host
  308. ];
  309. }
  310. /**
  311. * @param $invalidImprintUrl
  312. * @dataProvider invalidLegalUrlProvider
  313. */
  314. public function testGetShortFooterInvalidImprint($invalidImprintUrl): void {
  315. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  316. $this->config
  317. ->expects($this->exactly(5))
  318. ->method('getAppValue')
  319. ->willReturnMap([
  320. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  321. ['theming', 'name', 'Nextcloud', 'Name'],
  322. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  323. ['theming', 'imprintUrl', '', $invalidImprintUrl],
  324. ['theming', 'privacyUrl', '', ''],
  325. ]);
  326. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan', $this->template->getShortFooter());
  327. }
  328. /**
  329. * @param $invalidPrivacyUrl
  330. * @dataProvider invalidLegalUrlProvider
  331. */
  332. public function testGetShortFooterInvalidPrivacy($invalidPrivacyUrl): void {
  333. $this->navigationManager->expects($this->once())->method('getAll')->with(INavigationManager::TYPE_GUEST)->willReturn([]);
  334. $this->config
  335. ->expects($this->exactly(5))
  336. ->method('getAppValue')
  337. ->willReturnMap([
  338. ['theming', 'url', $this->defaults->getBaseUrl(), 'url'],
  339. ['theming', 'name', 'Nextcloud', 'Name'],
  340. ['theming', 'slogan', $this->defaults->getSlogan(), 'Slogan'],
  341. ['theming', 'imprintUrl', '', ''],
  342. ['theming', 'privacyUrl', '', $invalidPrivacyUrl],
  343. ]);
  344. $this->assertEquals('<a href="url" target="_blank" rel="noreferrer noopener" class="entity-name">Name</a> – Slogan', $this->template->getShortFooter());
  345. }
  346. public function testGetColorPrimaryWithDefault(): void {
  347. $this->appConfig
  348. ->expects(self::once())
  349. ->method('getValueBool')
  350. ->with('theming', 'disable-user-theming')
  351. ->willReturn(false);
  352. $this->appConfig
  353. ->expects(self::once())
  354. ->method('getValueString')
  355. ->with('theming', 'primary_color', '')
  356. ->willReturn($this->defaults->getColorPrimary());
  357. $this->assertEquals($this->defaults->getColorPrimary(), $this->template->getColorPrimary());
  358. }
  359. public function testGetColorPrimaryWithCustom(): void {
  360. $this->appConfig
  361. ->expects(self::once())
  362. ->method('getValueBool')
  363. ->with('theming', 'disable-user-theming')
  364. ->willReturn(false);
  365. $this->appConfig
  366. ->expects(self::once())
  367. ->method('getValueString')
  368. ->with('theming', 'primary_color', '')
  369. ->willReturn('#fff');
  370. $this->assertEquals('#fff', $this->template->getColorPrimary());
  371. }
  372. public function dataGetColorPrimary() {
  373. return [
  374. 'with fallback default' => [
  375. 'disableTheming' => false,
  376. 'primaryColor' => '',
  377. 'userPrimaryColor' => '',
  378. 'expected' => BackgroundService::DEFAULT_COLOR,
  379. ],
  380. 'with custom admin primary' => [
  381. 'disableTheming' => false,
  382. 'primaryColor' => '#aaa',
  383. 'userPrimaryColor' => '',
  384. 'expected' => '#aaa',
  385. ],
  386. 'with custom invalid admin primary' => [
  387. 'disableTheming' => false,
  388. 'primaryColor' => 'invalid',
  389. 'userPrimaryColor' => '',
  390. 'expected' => BackgroundService::DEFAULT_COLOR,
  391. ],
  392. 'with custom invalid user primary' => [
  393. 'disableTheming' => false,
  394. 'primaryColor' => '',
  395. 'userPrimaryColor' => 'invalid-name',
  396. 'expected' => BackgroundService::DEFAULT_COLOR,
  397. ],
  398. 'with custom user primary' => [
  399. 'disableTheming' => false,
  400. 'primaryColor' => '',
  401. 'userPrimaryColor' => '#bbb',
  402. 'expected' => '#bbb',
  403. ],
  404. 'with disabled user theming primary' => [
  405. 'disableTheming' => true,
  406. 'primaryColor' => '#aaa',
  407. 'userPrimaryColor' => '#bbb',
  408. 'expected' => '#aaa',
  409. ],
  410. ];
  411. }
  412. /**
  413. * @dataProvider dataGetColorPrimary
  414. */
  415. public function testGetColorPrimary(bool $disableTheming, string $primaryColor, string $userPrimaryColor, string $expected): void {
  416. $user = $this->createMock(IUser::class);
  417. $this->userSession->expects($this->any())
  418. ->method('getUser')
  419. ->willReturn($user);
  420. $user->expects($this->any())
  421. ->method('getUID')
  422. ->willReturn('user');
  423. $this->appConfig
  424. ->expects(self::any())
  425. ->method('getValueBool')
  426. ->with('theming', 'disable-user-theming')
  427. ->willReturn($disableTheming);
  428. $this->appConfig
  429. ->expects(self::any())
  430. ->method('getValueString')
  431. ->with('theming', 'primary_color', '')
  432. ->willReturn($primaryColor);
  433. $this->config
  434. ->expects($this->any())
  435. ->method('getUserValue')
  436. ->with('user', 'theming', 'primary_color', '')
  437. ->willReturn($userPrimaryColor);
  438. $this->assertEquals($expected, $this->template->getColorPrimary());
  439. }
  440. public function testSet(): void {
  441. $expectedCalls = [
  442. ['theming', 'MySetting', 'MyValue'],
  443. ['theming', 'cachebuster', 16],
  444. ];
  445. $i = 0;
  446. $this->config
  447. ->expects($this->exactly(2))
  448. ->method('setAppValue')
  449. ->willReturnCallback(function () use ($expectedCalls, &$i): void {
  450. $this->assertEquals($expectedCalls[$i], func_get_args());
  451. $i++;
  452. });
  453. $this->config
  454. ->expects($this->once())
  455. ->method('getAppValue')
  456. ->with('theming', 'cachebuster', '0')
  457. ->willReturn('15');
  458. $this->cacheFactory
  459. ->expects($this->exactly(2))
  460. ->method('createDistributed')
  461. ->willReturnMap([
  462. ['theming-', $this->cache],
  463. ['imagePath', $this->cache],
  464. ]);
  465. $this->cache
  466. ->expects($this->any())
  467. ->method('clear')
  468. ->with('');
  469. $this->template->set('MySetting', 'MyValue');
  470. }
  471. public function testUndoName(): void {
  472. $this->config
  473. ->expects($this->once())
  474. ->method('deleteAppValue')
  475. ->with('theming', 'name');
  476. $this->config
  477. ->expects($this->exactly(2))
  478. ->method('getAppValue')
  479. ->willReturnMap([
  480. ['theming', 'cachebuster', '0', '15'],
  481. ['theming', 'name', 'Nextcloud', 'Nextcloud'],
  482. ]);
  483. $this->config
  484. ->expects($this->once())
  485. ->method('setAppValue')
  486. ->with('theming', 'cachebuster', 16);
  487. $this->assertSame('Nextcloud', $this->template->undo('name'));
  488. }
  489. public function testUndoBaseUrl(): void {
  490. $this->config
  491. ->expects($this->once())
  492. ->method('deleteAppValue')
  493. ->with('theming', 'url');
  494. $this->config
  495. ->expects($this->exactly(2))
  496. ->method('getAppValue')
  497. ->willReturnMap([
  498. ['theming', 'cachebuster', '0', '15'],
  499. ['theming', 'url', $this->defaults->getBaseUrl(), $this->defaults->getBaseUrl()],
  500. ]);
  501. $this->config
  502. ->expects($this->once())
  503. ->method('setAppValue')
  504. ->with('theming', 'cachebuster', 16);
  505. $this->assertSame($this->defaults->getBaseUrl(), $this->template->undo('url'));
  506. }
  507. public function testUndoSlogan(): void {
  508. $this->config
  509. ->expects($this->once())
  510. ->method('deleteAppValue')
  511. ->with('theming', 'slogan');
  512. $this->config
  513. ->expects($this->exactly(2))
  514. ->method('getAppValue')
  515. ->willReturnMap([
  516. ['theming', 'cachebuster', '0', '15'],
  517. ['theming', 'slogan', $this->defaults->getSlogan(), $this->defaults->getSlogan()],
  518. ]);
  519. $this->config
  520. ->expects($this->once())
  521. ->method('setAppValue')
  522. ->with('theming', 'cachebuster', 16);
  523. $this->assertSame($this->defaults->getSlogan(), $this->template->undo('slogan'));
  524. }
  525. public function testUndoPrimaryColor(): void {
  526. $this->config
  527. ->expects($this->once())
  528. ->method('deleteAppValue')
  529. ->with('theming', 'primary_color');
  530. $this->config
  531. ->expects($this->once())
  532. ->method('getAppValue')
  533. ->with('theming', 'cachebuster', '0')
  534. ->willReturn('15');
  535. $this->config
  536. ->expects($this->once())
  537. ->method('setAppValue')
  538. ->with('theming', 'cachebuster', 16);
  539. $this->assertSame($this->defaults->getColorPrimary(), $this->template->undo('primary_color'));
  540. }
  541. public function testUndoDefaultAction(): void {
  542. $this->config
  543. ->expects($this->once())
  544. ->method('deleteAppValue')
  545. ->with('theming', 'defaultitem');
  546. $this->config
  547. ->expects($this->once())
  548. ->method('getAppValue')
  549. ->with('theming', 'cachebuster', '0')
  550. ->willReturn('15');
  551. $this->config
  552. ->expects($this->once())
  553. ->method('setAppValue')
  554. ->with('theming', 'cachebuster', 16);
  555. $this->assertSame('', $this->template->undo('defaultitem'));
  556. }
  557. public function testGetBackground(): void {
  558. $this->imageManager
  559. ->expects($this->once())
  560. ->method('getImageUrl')
  561. ->with('background')
  562. ->willReturn('custom-background?v=0');
  563. $this->assertEquals('custom-background?v=0', $this->template->getBackground());
  564. }
  565. private function getLogoHelper($withName, $useSvg) {
  566. $this->imageManager->expects($this->any())
  567. ->method('getImage')
  568. ->with('logo')
  569. ->willThrowException(new NotFoundException());
  570. $this->config
  571. ->expects($this->exactly(2))
  572. ->method('getAppValue')
  573. ->willReturnMap([
  574. ['theming', 'logoMime', '', ''],
  575. ['theming', 'cachebuster', '0', '0'],
  576. ]);
  577. $this->urlGenerator->expects($this->once())
  578. ->method('imagePath')
  579. ->with('core', $withName)
  580. ->willReturn('core-logo');
  581. $this->assertEquals('core-logo?v=0', $this->template->getLogo($useSvg));
  582. }
  583. public function testGetLogoDefaultWithSvg(): void {
  584. $this->getLogoHelper('logo/logo.svg', true);
  585. }
  586. public function testGetLogoDefaultWithoutSvg(): void {
  587. $this->getLogoHelper('logo/logo.png', false);
  588. }
  589. public function testGetLogoCustom(): void {
  590. $this->config
  591. ->expects($this->exactly(2))
  592. ->method('getAppValue')
  593. ->willReturnMap([
  594. ['theming', 'logoMime', '', 'image/svg+xml'],
  595. ['theming', 'cachebuster', '0', '0'],
  596. ]);
  597. $this->urlGenerator->expects($this->once())
  598. ->method('linkToRoute')
  599. ->with('theming.Theming.getImage')
  600. ->willReturn('custom-logo?v=0');
  601. $this->assertEquals('custom-logo' . '?v=0', $this->template->getLogo());
  602. }
  603. public function testGetScssVariablesCached(): void {
  604. $this->config->expects($this->any())->method('getAppValue')->with('theming', 'cachebuster', '0')->willReturn('1');
  605. $this->cacheFactory->expects($this->once())
  606. ->method('createDistributed')
  607. ->with('theming-1-')
  608. ->willReturn($this->cache);
  609. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(['foo' => 'bar']);
  610. $this->assertEquals(['foo' => 'bar'], $this->template->getScssVariables());
  611. }
  612. public function testGetScssVariables(): void {
  613. $this->config
  614. ->expects($this->any())
  615. ->method('getAppValue')
  616. ->willReturnMap([
  617. ['theming', 'cachebuster', '0', '0'],
  618. ['theming', 'logoMime', '', 'jpeg'],
  619. ['theming', 'backgroundMime', '', 'jpeg'],
  620. ['theming', 'logoheaderMime', '', 'jpeg'],
  621. ['theming', 'faviconMime', '', 'jpeg'],
  622. ]);
  623. $this->appConfig
  624. ->expects(self::atLeastOnce())
  625. ->method('getValueString')
  626. ->willReturnMap([
  627. ['theming', 'primary_color', '', false, $this->defaults->getColorPrimary()],
  628. ['theming', 'primary_color', $this->defaults->getColorPrimary(), false, $this->defaults->getColorPrimary()],
  629. ]);
  630. $this->util->expects($this->any())->method('invertTextColor')->with($this->defaults->getColorPrimary())->willReturn(false);
  631. $this->util->expects($this->any())->method('elementColor')->with($this->defaults->getColorPrimary())->willReturn('#aaaaaa');
  632. $this->cacheFactory->expects($this->once())
  633. ->method('createDistributed')
  634. ->with('theming-0-')
  635. ->willReturn($this->cache);
  636. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(null);
  637. $this->imageManager->expects($this->exactly(4))
  638. ->method('getImageUrl')
  639. ->willReturnMap([
  640. ['logo', 'custom-logo?v=0'],
  641. ['logoheader', 'custom-logoheader?v=0'],
  642. ['favicon', 'custom-favicon?v=0'],
  643. ['background', 'custom-background?v=0'],
  644. ]);
  645. $expected = [
  646. 'theming-cachebuster' => '\'0\'',
  647. 'theming-logo-mime' => '\'jpeg\'',
  648. 'theming-background-mime' => '\'jpeg\'',
  649. 'image-logo' => "url('custom-logo?v=0')",
  650. 'image-login-background' => "url('custom-background?v=0')",
  651. 'color-primary' => $this->defaults->getColorPrimary(),
  652. 'color-primary-text' => '#ffffff',
  653. 'image-login-plain' => 'false',
  654. 'color-primary-element' => '#aaaaaa',
  655. 'theming-logoheader-mime' => '\'jpeg\'',
  656. 'theming-favicon-mime' => '\'jpeg\'',
  657. 'image-logoheader' => "url('custom-logoheader?v=0')",
  658. 'image-favicon' => "url('custom-favicon?v=0')",
  659. 'has-legal-links' => 'false'
  660. ];
  661. $this->assertEquals($expected, $this->template->getScssVariables());
  662. }
  663. public function testGetDefaultAndroidURL(): void {
  664. $this->config
  665. ->expects($this->once())
  666. ->method('getAppValue')
  667. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  668. ->willReturn('https://play.google.com/store/apps/details?id=com.nextcloud.client');
  669. $this->assertEquals('https://play.google.com/store/apps/details?id=com.nextcloud.client', $this->template->getAndroidClientUrl());
  670. }
  671. public function testGetCustomAndroidURL(): void {
  672. $this->config
  673. ->expects($this->once())
  674. ->method('getAppValue')
  675. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  676. ->willReturn('https://play.google.com/store/apps/details?id=com.mycloud.client');
  677. $this->assertEquals('https://play.google.com/store/apps/details?id=com.mycloud.client', $this->template->getAndroidClientUrl());
  678. }
  679. public function testGetDefaultiOSURL(): void {
  680. $this->config
  681. ->expects($this->once())
  682. ->method('getAppValue')
  683. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  684. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8');
  685. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8', $this->template->getiOSClientUrl());
  686. }
  687. public function testGetCustomiOSURL(): void {
  688. $this->config
  689. ->expects($this->once())
  690. ->method('getAppValue')
  691. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  692. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8');
  693. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8', $this->template->getiOSClientUrl());
  694. }
  695. public function testGetDefaultiTunesAppId(): void {
  696. $this->config
  697. ->expects($this->once())
  698. ->method('getAppValue')
  699. ->with('theming', 'iTunesAppId', '1125420102')
  700. ->willReturn('1125420102');
  701. $this->assertEquals('1125420102', $this->template->getiTunesAppId());
  702. }
  703. public function testGetCustomiTunesAppId(): void {
  704. $this->config
  705. ->expects($this->once())
  706. ->method('getAppValue')
  707. ->with('theming', 'iTunesAppId', '1125420102')
  708. ->willReturn('1234567890');
  709. $this->assertEquals('1234567890', $this->template->getiTunesAppId());
  710. }
  711. public function dataReplaceImagePath() {
  712. return [
  713. ['core', 'test.png', false],
  714. ['core', 'manifest.json'],
  715. ['core', 'favicon.ico'],
  716. ['core', 'favicon-touch.png']
  717. ];
  718. }
  719. /** @dataProvider dataReplaceImagePath */
  720. public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=1234abcd'): void {
  721. $this->cache->expects($this->any())
  722. ->method('get')
  723. ->with('shouldReplaceIcons')
  724. ->willReturn(true);
  725. $this->config
  726. ->expects($this->any())
  727. ->method('getAppValue')
  728. ->with('theming', 'cachebuster', '0')
  729. ->willReturn('0');
  730. $this->urlGenerator
  731. ->expects($this->any())
  732. ->method('linkToRoute')
  733. ->willReturn('themingRoute');
  734. if ($result) {
  735. $this->util
  736. ->expects($this->once())
  737. ->method('getCacheBuster')
  738. ->willReturn('1234abcd');
  739. }
  740. $this->assertEquals($result, $this->template->replaceImagePath($app, $image));
  741. }
  742. }