ThemingDefaultsTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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) {
  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) {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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) {
  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) {
  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() {
  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() {
  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) {
  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() {
  441. $this->config
  442. ->expects($this->exactly(2))
  443. ->method('setAppValue')
  444. ->withConsecutive(
  445. ['theming', 'MySetting', 'MyValue'],
  446. ['theming', 'cachebuster', 16],
  447. );
  448. $this->config
  449. ->expects($this->once())
  450. ->method('getAppValue')
  451. ->with('theming', 'cachebuster', '0')
  452. ->willReturn('15');
  453. $this->cacheFactory
  454. ->expects($this->exactly(2))
  455. ->method('createDistributed')
  456. ->withConsecutive(
  457. ['theming-'],
  458. ['imagePath'],
  459. )
  460. ->willReturn($this->cache);
  461. $this->cache
  462. ->expects($this->any())
  463. ->method('clear')
  464. ->with('');
  465. $this->template->set('MySetting', 'MyValue');
  466. }
  467. public function testUndoName() {
  468. $this->config
  469. ->expects($this->once())
  470. ->method('deleteAppValue')
  471. ->with('theming', 'name');
  472. $this->config
  473. ->expects($this->exactly(2))
  474. ->method('getAppValue')
  475. ->withConsecutive(
  476. ['theming', 'cachebuster', '0'],
  477. ['theming', 'name', 'Nextcloud'],
  478. )->willReturnOnConsecutiveCalls(
  479. '15',
  480. 'Nextcloud',
  481. );
  482. $this->config
  483. ->expects($this->once())
  484. ->method('setAppValue')
  485. ->with('theming', 'cachebuster', 16);
  486. $this->assertSame('Nextcloud', $this->template->undo('name'));
  487. }
  488. public function testUndoBaseUrl() {
  489. $this->config
  490. ->expects($this->once())
  491. ->method('deleteAppValue')
  492. ->with('theming', 'url');
  493. $this->config
  494. ->expects($this->exactly(2))
  495. ->method('getAppValue')
  496. ->withConsecutive(
  497. ['theming', 'cachebuster', '0'],
  498. ['theming', 'url', $this->defaults->getBaseUrl()],
  499. )->willReturnOnConsecutiveCalls(
  500. '15',
  501. $this->defaults->getBaseUrl(),
  502. );
  503. $this->config
  504. ->expects($this->once())
  505. ->method('setAppValue')
  506. ->with('theming', 'cachebuster', 16);
  507. $this->assertSame($this->defaults->getBaseUrl(), $this->template->undo('url'));
  508. }
  509. public function testUndoSlogan() {
  510. $this->config
  511. ->expects($this->once())
  512. ->method('deleteAppValue')
  513. ->with('theming', 'slogan');
  514. $this->config
  515. ->expects($this->exactly(2))
  516. ->method('getAppValue')
  517. ->withConsecutive(
  518. ['theming', 'cachebuster', '0'],
  519. ['theming', 'slogan', $this->defaults->getSlogan()],
  520. )->willReturnOnConsecutiveCalls(
  521. '15',
  522. $this->defaults->getSlogan(),
  523. );
  524. $this->config
  525. ->expects($this->once())
  526. ->method('setAppValue')
  527. ->with('theming', 'cachebuster', 16);
  528. $this->assertSame($this->defaults->getSlogan(), $this->template->undo('slogan'));
  529. }
  530. public function testUndoPrimaryColor() {
  531. $this->config
  532. ->expects($this->once())
  533. ->method('deleteAppValue')
  534. ->with('theming', 'primary_color');
  535. $this->config
  536. ->expects($this->once())
  537. ->method('getAppValue')
  538. ->with('theming', 'cachebuster', '0')
  539. ->willReturn('15');
  540. $this->config
  541. ->expects($this->once())
  542. ->method('setAppValue')
  543. ->with('theming', 'cachebuster', 16);
  544. $this->assertSame($this->defaults->getColorPrimary(), $this->template->undo('primary_color'));
  545. }
  546. public function testUndoDefaultAction() {
  547. $this->config
  548. ->expects($this->once())
  549. ->method('deleteAppValue')
  550. ->with('theming', 'defaultitem');
  551. $this->config
  552. ->expects($this->once())
  553. ->method('getAppValue')
  554. ->with('theming', 'cachebuster', '0')
  555. ->willReturn('15');
  556. $this->config
  557. ->expects($this->once())
  558. ->method('setAppValue')
  559. ->with('theming', 'cachebuster', 16);
  560. $this->assertSame('', $this->template->undo('defaultitem'));
  561. }
  562. public function testGetBackground() {
  563. $this->imageManager
  564. ->expects($this->once())
  565. ->method('getImageUrl')
  566. ->with('background')
  567. ->willReturn('custom-background?v=0');
  568. $this->assertEquals('custom-background?v=0', $this->template->getBackground());
  569. }
  570. private function getLogoHelper($withName, $useSvg) {
  571. $this->imageManager->expects($this->any())
  572. ->method('getImage')
  573. ->with('logo')
  574. ->willThrowException(new NotFoundException());
  575. $this->config
  576. ->expects($this->exactly(2))
  577. ->method('getAppValue')
  578. ->withConsecutive(
  579. ['theming', 'logoMime'],
  580. ['theming', 'cachebuster', '0'],
  581. )->willReturnOnConsecutiveCalls(
  582. '',
  583. '0'
  584. );
  585. $this->urlGenerator->expects($this->once())
  586. ->method('imagePath')
  587. ->with('core', $withName)
  588. ->willReturn('core-logo');
  589. $this->assertEquals('core-logo?v=0', $this->template->getLogo($useSvg));
  590. }
  591. public function testGetLogoDefaultWithSvg() {
  592. $this->getLogoHelper('logo/logo.svg', true);
  593. }
  594. public function testGetLogoDefaultWithoutSvg() {
  595. $this->getLogoHelper('logo/logo.png', false);
  596. }
  597. public function testGetLogoCustom() {
  598. $this->config
  599. ->expects($this->exactly(2))
  600. ->method('getAppValue')
  601. ->withConsecutive(
  602. ['theming', 'logoMime', false],
  603. ['theming', 'cachebuster', '0'],
  604. )->willReturnOnConsecutiveCalls(
  605. 'image/svg+xml',
  606. '0',
  607. );
  608. $this->urlGenerator->expects($this->once())
  609. ->method('linkToRoute')
  610. ->with('theming.Theming.getImage')
  611. ->willReturn('custom-logo?v=0');
  612. $this->assertEquals('custom-logo' . '?v=0', $this->template->getLogo());
  613. }
  614. public function testGetScssVariablesCached() {
  615. $this->config->expects($this->any())->method('getAppValue')->with('theming', 'cachebuster', '0')->willReturn('1');
  616. $this->cacheFactory->expects($this->once())
  617. ->method('createDistributed')
  618. ->with('theming-1-')
  619. ->willReturn($this->cache);
  620. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(['foo' => 'bar']);
  621. $this->assertEquals(['foo' => 'bar'], $this->template->getScssVariables());
  622. }
  623. public function testGetScssVariables() {
  624. $this->config
  625. ->expects($this->any())
  626. ->method('getAppValue')
  627. ->willReturnMap([
  628. ['theming', 'cachebuster', '0', '0'],
  629. ['theming', 'logoMime', '', 'jpeg'],
  630. ['theming', 'backgroundMime', '', 'jpeg'],
  631. ['theming', 'logoheaderMime', '', 'jpeg'],
  632. ['theming', 'faviconMime', '', 'jpeg'],
  633. ]);
  634. $this->appConfig
  635. ->expects(self::atLeastOnce())
  636. ->method('getValueString')
  637. ->willReturnMap([
  638. ['theming', 'primary_color', '', false, $this->defaults->getColorPrimary()],
  639. ['theming', 'primary_color', $this->defaults->getColorPrimary(), false, $this->defaults->getColorPrimary()],
  640. ]);
  641. $this->util->expects($this->any())->method('invertTextColor')->with($this->defaults->getColorPrimary())->willReturn(false);
  642. $this->util->expects($this->any())->method('elementColor')->with($this->defaults->getColorPrimary())->willReturn('#aaaaaa');
  643. $this->cacheFactory->expects($this->once())
  644. ->method('createDistributed')
  645. ->with('theming-0-')
  646. ->willReturn($this->cache);
  647. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(null);
  648. $this->imageManager->expects($this->exactly(4))
  649. ->method('getImageUrl')
  650. ->willReturnMap([
  651. ['logo', 'custom-logo?v=0'],
  652. ['logoheader', 'custom-logoheader?v=0'],
  653. ['favicon', 'custom-favicon?v=0'],
  654. ['background', 'custom-background?v=0'],
  655. ]);
  656. $expected = [
  657. 'theming-cachebuster' => '\'0\'',
  658. 'theming-logo-mime' => '\'jpeg\'',
  659. 'theming-background-mime' => '\'jpeg\'',
  660. 'image-logo' => "url('custom-logo?v=0')",
  661. 'image-login-background' => "url('custom-background?v=0')",
  662. 'color-primary' => $this->defaults->getColorPrimary(),
  663. 'color-primary-text' => '#ffffff',
  664. 'image-login-plain' => 'false',
  665. 'color-primary-element' => '#aaaaaa',
  666. 'theming-logoheader-mime' => '\'jpeg\'',
  667. 'theming-favicon-mime' => '\'jpeg\'',
  668. 'image-logoheader' => "url('custom-logoheader?v=0')",
  669. 'image-favicon' => "url('custom-favicon?v=0')",
  670. 'has-legal-links' => 'false'
  671. ];
  672. $this->assertEquals($expected, $this->template->getScssVariables());
  673. }
  674. public function testGetDefaultAndroidURL() {
  675. $this->config
  676. ->expects($this->once())
  677. ->method('getAppValue')
  678. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  679. ->willReturn('https://play.google.com/store/apps/details?id=com.nextcloud.client');
  680. $this->assertEquals('https://play.google.com/store/apps/details?id=com.nextcloud.client', $this->template->getAndroidClientUrl());
  681. }
  682. public function testGetCustomAndroidURL() {
  683. $this->config
  684. ->expects($this->once())
  685. ->method('getAppValue')
  686. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  687. ->willReturn('https://play.google.com/store/apps/details?id=com.mycloud.client');
  688. $this->assertEquals('https://play.google.com/store/apps/details?id=com.mycloud.client', $this->template->getAndroidClientUrl());
  689. }
  690. public function testGetDefaultiOSURL() {
  691. $this->config
  692. ->expects($this->once())
  693. ->method('getAppValue')
  694. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  695. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8');
  696. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8', $this->template->getiOSClientUrl());
  697. }
  698. public function testGetCustomiOSURL() {
  699. $this->config
  700. ->expects($this->once())
  701. ->method('getAppValue')
  702. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  703. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8');
  704. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8', $this->template->getiOSClientUrl());
  705. }
  706. public function testGetDefaultiTunesAppId() {
  707. $this->config
  708. ->expects($this->once())
  709. ->method('getAppValue')
  710. ->with('theming', 'iTunesAppId', '1125420102')
  711. ->willReturn('1125420102');
  712. $this->assertEquals('1125420102', $this->template->getiTunesAppId());
  713. }
  714. public function testGetCustomiTunesAppId() {
  715. $this->config
  716. ->expects($this->once())
  717. ->method('getAppValue')
  718. ->with('theming', 'iTunesAppId', '1125420102')
  719. ->willReturn('1234567890');
  720. $this->assertEquals('1234567890', $this->template->getiTunesAppId());
  721. }
  722. public function dataReplaceImagePath() {
  723. return [
  724. ['core', 'test.png', false],
  725. ['core', 'manifest.json'],
  726. ['core', 'favicon.ico'],
  727. ['core', 'favicon-touch.png']
  728. ];
  729. }
  730. /** @dataProvider dataReplaceImagePath */
  731. public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=1234abcd') {
  732. $this->cache->expects($this->any())
  733. ->method('get')
  734. ->with('shouldReplaceIcons')
  735. ->willReturn(true);
  736. $this->config
  737. ->expects($this->any())
  738. ->method('getAppValue')
  739. ->with('theming', 'cachebuster', '0')
  740. ->willReturn('0');
  741. $this->urlGenerator
  742. ->expects($this->any())
  743. ->method('linkToRoute')
  744. ->willReturn('themingRoute');
  745. if ($result) {
  746. $this->util
  747. ->expects($this->once())
  748. ->method('getCacheBuster')
  749. ->willReturn('1234abcd');
  750. }
  751. $this->assertEquals($result, $this->template->replaceImagePath($app, $image));
  752. }
  753. }