You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThemingDefaultsTest.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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\IAppData;
  13. use OCP\Files\NotFoundException;
  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 Test\TestCase;
  23. class ThemingDefaultsTest extends TestCase {
  24. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  25. private $config;
  26. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  27. private $l10n;
  28. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  29. private $userSession;
  30. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  31. private $urlGenerator;
  32. /** @var \OC_Defaults|\PHPUnit\Framework\MockObject\MockObject */
  33. private $defaults;
  34. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  35. private $appData;
  36. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  37. private $cacheFactory;
  38. /** @var ThemingDefaults */
  39. private $template;
  40. /** @var Util|\PHPUnit\Framework\MockObject\MockObject */
  41. private $util;
  42. /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
  43. private $cache;
  44. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  45. private $appManager;
  46. /** @var ImageManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $imageManager;
  48. /** @var INavigationManager|\PHPUnit\Framework\MockObject\MockObject */
  49. private $navigationManager;
  50. /** @var BackgroundService|\PHPUnit\Framework\MockObject\MockObject */
  51. private $backgroundService;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->config = $this->createMock(IConfig::class);
  55. $this->l10n = $this->createMock(IL10N::class);
  56. $this->userSession = $this->createMock(IUserSession::class);
  57. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  58. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  59. $this->cache = $this->createMock(ICache::class);
  60. $this->util = $this->createMock(Util::class);
  61. $this->imageManager = $this->createMock(ImageManager::class);
  62. $this->appManager = $this->createMock(IAppManager::class);
  63. $this->navigationManager = $this->createMock(INavigationManager::class);
  64. $this->backgroundService = $this->createMock(BackgroundService::class);
  65. $this->defaults = new \OC_Defaults();
  66. $this->urlGenerator
  67. ->expects($this->any())
  68. ->method('getBaseUrl')
  69. ->willReturn('');
  70. $this->template = new ThemingDefaults(
  71. $this->config,
  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/><a href="https://example.com/imprint" class="legal" target="_blank" rel="noreferrer noopener">Legal notice</a>', $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/><a href="https://example.com/privacy" class="legal" target="_blank" rel="noreferrer noopener">Privacy policy</a>', $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/><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>', $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->config
  348. ->expects($this->exactly(2))
  349. ->method('getAppValue')
  350. ->willReturnMap([
  351. ['theming', 'disable-user-theming', 'no', 'no'],
  352. ['theming', 'primary_color', '', $this->defaults->getColorPrimary()],
  353. ]);
  354. $this->assertEquals($this->defaults->getColorPrimary(), $this->template->getColorPrimary());
  355. }
  356. public function testGetColorPrimaryWithCustom() {
  357. $this->config
  358. ->expects($this->exactly(2))
  359. ->method('getAppValue')
  360. ->willReturnMap([
  361. ['theming', 'disable-user-theming', 'no', 'no'],
  362. ['theming', 'primary_color', '', '#fff'],
  363. ]);
  364. $this->assertEquals('#fff', $this->template->getColorPrimary());
  365. }
  366. public function dataGetColorPrimary() {
  367. return [
  368. 'with fallback default' => [
  369. 'disableTheming' => 'no',
  370. 'primaryColor' => '',
  371. 'userPrimaryColor' => '',
  372. 'expected' => BackgroundService::DEFAULT_COLOR,
  373. ],
  374. 'with custom admin primary' => [
  375. 'disableTheming' => 'no',
  376. 'primaryColor' => '#aaa',
  377. 'userPrimaryColor' => '',
  378. 'expected' => '#aaa',
  379. ],
  380. 'with custom invalid admin primary' => [
  381. 'disableTheming' => 'no',
  382. 'primaryColor' => 'invalid',
  383. 'userPrimaryColor' => '',
  384. 'expected' => BackgroundService::DEFAULT_COLOR,
  385. ],
  386. 'with custom invalid user primary' => [
  387. 'disableTheming' => 'no',
  388. 'primaryColor' => '',
  389. 'userPrimaryColor' => 'invalid-name',
  390. 'expected' => BackgroundService::DEFAULT_COLOR,
  391. ],
  392. 'with custom user primary' => [
  393. 'disableTheming' => 'no',
  394. 'primaryColor' => '',
  395. 'userPrimaryColor' => '#bbb',
  396. 'expected' => '#bbb',
  397. ],
  398. 'with disabled user theming primary' => [
  399. 'disableTheming' => 'yes',
  400. 'primaryColor' => '#aaa',
  401. 'userPrimaryColor' => '#bbb',
  402. 'expected' => '#aaa',
  403. ],
  404. ];
  405. }
  406. /**
  407. * @dataProvider dataGetColorPrimary
  408. */
  409. public function testGetColorPrimary(string $disableTheming, string $primaryColor, string $userPrimaryColor, string $expected) {
  410. $user = $this->createMock(IUser::class);
  411. $this->userSession->expects($this->any())
  412. ->method('getUser')
  413. ->willReturn($user);
  414. $user->expects($this->any())
  415. ->method('getUID')
  416. ->willReturn('user');
  417. $this->config
  418. ->expects($this->any())
  419. ->method('getAppValue')
  420. ->willReturnMap([
  421. ['theming', 'disable-user-theming', 'no', $disableTheming],
  422. ['theming', 'primary_color', '', $primaryColor],
  423. ]);
  424. $this->config
  425. ->expects($this->any())
  426. ->method('getUserValue')
  427. ->with('user', 'theming', 'primary_color', '')
  428. ->willReturn($userPrimaryColor);
  429. $this->assertEquals($expected, $this->template->getColorPrimary());
  430. }
  431. public function testSet() {
  432. $this->config
  433. ->expects($this->exactly(2))
  434. ->method('setAppValue')
  435. ->withConsecutive(
  436. ['theming', 'MySetting', 'MyValue'],
  437. ['theming', 'cachebuster', 16],
  438. );
  439. $this->config
  440. ->expects($this->once())
  441. ->method('getAppValue')
  442. ->with('theming', 'cachebuster', '0')
  443. ->willReturn('15');
  444. $this->cacheFactory
  445. ->expects($this->exactly(2))
  446. ->method('createDistributed')
  447. ->withConsecutive(
  448. ['theming-'],
  449. ['imagePath'],
  450. )
  451. ->willReturn($this->cache);
  452. $this->cache
  453. ->expects($this->any())
  454. ->method('clear')
  455. ->with('');
  456. $this->template->set('MySetting', 'MyValue');
  457. }
  458. public function testUndoName() {
  459. $this->config
  460. ->expects($this->once())
  461. ->method('deleteAppValue')
  462. ->with('theming', 'name');
  463. $this->config
  464. ->expects($this->exactly(2))
  465. ->method('getAppValue')
  466. ->withConsecutive(
  467. ['theming', 'cachebuster', '0'],
  468. ['theming', 'name', 'Nextcloud'],
  469. )->willReturnOnConsecutiveCalls(
  470. '15',
  471. 'Nextcloud',
  472. );
  473. $this->config
  474. ->expects($this->once())
  475. ->method('setAppValue')
  476. ->with('theming', 'cachebuster', 16);
  477. $this->assertSame('Nextcloud', $this->template->undo('name'));
  478. }
  479. public function testUndoBaseUrl() {
  480. $this->config
  481. ->expects($this->once())
  482. ->method('deleteAppValue')
  483. ->with('theming', 'url');
  484. $this->config
  485. ->expects($this->exactly(2))
  486. ->method('getAppValue')
  487. ->withConsecutive(
  488. ['theming', 'cachebuster', '0'],
  489. ['theming', 'url', $this->defaults->getBaseUrl()],
  490. )->willReturnOnConsecutiveCalls(
  491. '15',
  492. $this->defaults->getBaseUrl(),
  493. );
  494. $this->config
  495. ->expects($this->once())
  496. ->method('setAppValue')
  497. ->with('theming', 'cachebuster', 16);
  498. $this->assertSame($this->defaults->getBaseUrl(), $this->template->undo('url'));
  499. }
  500. public function testUndoSlogan() {
  501. $this->config
  502. ->expects($this->once())
  503. ->method('deleteAppValue')
  504. ->with('theming', 'slogan');
  505. $this->config
  506. ->expects($this->exactly(2))
  507. ->method('getAppValue')
  508. ->withConsecutive(
  509. ['theming', 'cachebuster', '0'],
  510. ['theming', 'slogan', $this->defaults->getSlogan()],
  511. )->willReturnOnConsecutiveCalls(
  512. '15',
  513. $this->defaults->getSlogan(),
  514. );
  515. $this->config
  516. ->expects($this->once())
  517. ->method('setAppValue')
  518. ->with('theming', 'cachebuster', 16);
  519. $this->assertSame($this->defaults->getSlogan(), $this->template->undo('slogan'));
  520. }
  521. public function testUndoPrimaryColor() {
  522. $this->config
  523. ->expects($this->once())
  524. ->method('deleteAppValue')
  525. ->with('theming', 'primary_color');
  526. $this->config
  527. ->expects($this->once())
  528. ->method('getAppValue')
  529. ->with('theming', 'cachebuster', '0')
  530. ->willReturn('15');
  531. $this->config
  532. ->expects($this->once())
  533. ->method('setAppValue')
  534. ->with('theming', 'cachebuster', 16);
  535. $this->assertSame($this->defaults->getColorPrimary(), $this->template->undo('primary_color'));
  536. }
  537. public function testUndoDefaultAction() {
  538. $this->config
  539. ->expects($this->once())
  540. ->method('deleteAppValue')
  541. ->with('theming', 'defaultitem');
  542. $this->config
  543. ->expects($this->once())
  544. ->method('getAppValue')
  545. ->with('theming', 'cachebuster', '0')
  546. ->willReturn('15');
  547. $this->config
  548. ->expects($this->once())
  549. ->method('setAppValue')
  550. ->with('theming', 'cachebuster', 16);
  551. $this->assertSame('', $this->template->undo('defaultitem'));
  552. }
  553. public function testGetBackground() {
  554. $this->imageManager
  555. ->expects($this->once())
  556. ->method('getImageUrl')
  557. ->with('background')
  558. ->willReturn('custom-background?v=0');
  559. $this->assertEquals('custom-background?v=0', $this->template->getBackground());
  560. }
  561. private function getLogoHelper($withName, $useSvg) {
  562. $this->imageManager->expects($this->any())
  563. ->method('getImage')
  564. ->with('logo')
  565. ->willThrowException(new NotFoundException());
  566. $this->config
  567. ->expects($this->exactly(2))
  568. ->method('getAppValue')
  569. ->withConsecutive(
  570. ['theming', 'logoMime'],
  571. ['theming', 'cachebuster', '0'],
  572. )->willReturnOnConsecutiveCalls(
  573. '',
  574. '0'
  575. );
  576. $this->urlGenerator->expects($this->once())
  577. ->method('imagePath')
  578. ->with('core', $withName)
  579. ->willReturn('core-logo');
  580. $this->assertEquals('core-logo?v=0', $this->template->getLogo($useSvg));
  581. }
  582. public function testGetLogoDefaultWithSvg() {
  583. $this->getLogoHelper('logo/logo.svg', true);
  584. }
  585. public function testGetLogoDefaultWithoutSvg() {
  586. $this->getLogoHelper('logo/logo.png', false);
  587. }
  588. public function testGetLogoCustom() {
  589. $this->config
  590. ->expects($this->exactly(2))
  591. ->method('getAppValue')
  592. ->withConsecutive(
  593. ['theming', 'logoMime', false],
  594. ['theming', 'cachebuster', '0'],
  595. )->willReturnOnConsecutiveCalls(
  596. 'image/svg+xml',
  597. '0',
  598. );
  599. $this->urlGenerator->expects($this->once())
  600. ->method('linkToRoute')
  601. ->with('theming.Theming.getImage')
  602. ->willReturn('custom-logo?v=0');
  603. $this->assertEquals('custom-logo' . '?v=0', $this->template->getLogo());
  604. }
  605. public function testGetScssVariablesCached() {
  606. $this->config->expects($this->any())->method('getAppValue')->with('theming', 'cachebuster', '0')->willReturn('1');
  607. $this->cacheFactory->expects($this->once())
  608. ->method('createDistributed')
  609. ->with('theming-1-')
  610. ->willReturn($this->cache);
  611. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(['foo' => 'bar']);
  612. $this->assertEquals(['foo' => 'bar'], $this->template->getScssVariables());
  613. }
  614. public function testGetScssVariables() {
  615. $this->config
  616. ->expects($this->any())
  617. ->method('getAppValue')
  618. ->willReturnMap([
  619. ['theming', 'cachebuster', '0', '0'],
  620. ['theming', 'logoMime', '', 'jpeg'],
  621. ['theming', 'backgroundMime', '', 'jpeg'],
  622. ['theming', 'logoheaderMime', '', 'jpeg'],
  623. ['theming', 'faviconMime', '', 'jpeg'],
  624. ['theming', 'primary_color', '', $this->defaults->getColorPrimary()],
  625. ['theming', 'primary_color', $this->defaults->getColorPrimary(), $this->defaults->getColorPrimary()],
  626. ]);
  627. $this->util->expects($this->any())->method('invertTextColor')->with($this->defaults->getColorPrimary())->willReturn(false);
  628. $this->util->expects($this->any())->method('elementColor')->with($this->defaults->getColorPrimary())->willReturn('#aaaaaa');
  629. $this->cacheFactory->expects($this->once())
  630. ->method('createDistributed')
  631. ->with('theming-0-')
  632. ->willReturn($this->cache);
  633. $this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(null);
  634. $this->imageManager->expects($this->exactly(4))
  635. ->method('getImageUrl')
  636. ->willReturnMap([
  637. ['logo', 'custom-logo?v=0'],
  638. ['logoheader', 'custom-logoheader?v=0'],
  639. ['favicon', 'custom-favicon?v=0'],
  640. ['background', 'custom-background?v=0'],
  641. ]);
  642. $expected = [
  643. 'theming-cachebuster' => '\'0\'',
  644. 'theming-logo-mime' => '\'jpeg\'',
  645. 'theming-background-mime' => '\'jpeg\'',
  646. 'image-logo' => "url('custom-logo?v=0')",
  647. 'image-login-background' => "url('custom-background?v=0')",
  648. 'color-primary' => $this->defaults->getColorPrimary(),
  649. 'color-primary-text' => '#ffffff',
  650. 'image-login-plain' => 'false',
  651. 'color-primary-element' => '#aaaaaa',
  652. 'theming-logoheader-mime' => '\'jpeg\'',
  653. 'theming-favicon-mime' => '\'jpeg\'',
  654. 'image-logoheader' => "url('custom-logoheader?v=0')",
  655. 'image-favicon' => "url('custom-favicon?v=0')",
  656. 'has-legal-links' => 'false'
  657. ];
  658. $this->assertEquals($expected, $this->template->getScssVariables());
  659. }
  660. public function testGetDefaultAndroidURL() {
  661. $this->config
  662. ->expects($this->once())
  663. ->method('getAppValue')
  664. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  665. ->willReturn('https://play.google.com/store/apps/details?id=com.nextcloud.client');
  666. $this->assertEquals('https://play.google.com/store/apps/details?id=com.nextcloud.client', $this->template->getAndroidClientUrl());
  667. }
  668. public function testGetCustomAndroidURL() {
  669. $this->config
  670. ->expects($this->once())
  671. ->method('getAppValue')
  672. ->with('theming', 'AndroidClientUrl', 'https://play.google.com/store/apps/details?id=com.nextcloud.client')
  673. ->willReturn('https://play.google.com/store/apps/details?id=com.mycloud.client');
  674. $this->assertEquals('https://play.google.com/store/apps/details?id=com.mycloud.client', $this->template->getAndroidClientUrl());
  675. }
  676. public function testGetDefaultiOSURL() {
  677. $this->config
  678. ->expects($this->once())
  679. ->method('getAppValue')
  680. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  681. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8');
  682. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8', $this->template->getiOSClientUrl());
  683. }
  684. public function testGetCustomiOSURL() {
  685. $this->config
  686. ->expects($this->once())
  687. ->method('getAppValue')
  688. ->with('theming', 'iOSClientUrl', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8')
  689. ->willReturn('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8');
  690. $this->assertEquals('https://geo.itunes.apple.com/us/app/nextcloud/id1234567890?mt=8', $this->template->getiOSClientUrl());
  691. }
  692. public function testGetDefaultiTunesAppId() {
  693. $this->config
  694. ->expects($this->once())
  695. ->method('getAppValue')
  696. ->with('theming', 'iTunesAppId', '1125420102')
  697. ->willReturn('1125420102');
  698. $this->assertEquals('1125420102', $this->template->getiTunesAppId());
  699. }
  700. public function testGetCustomiTunesAppId() {
  701. $this->config
  702. ->expects($this->once())
  703. ->method('getAppValue')
  704. ->with('theming', 'iTunesAppId', '1125420102')
  705. ->willReturn('1234567890');
  706. $this->assertEquals('1234567890', $this->template->getiTunesAppId());
  707. }
  708. public function dataReplaceImagePath() {
  709. return [
  710. ['core', 'test.png', false],
  711. ['core', 'manifest.json'],
  712. ['core', 'favicon.ico'],
  713. ['core', 'favicon-touch.png']
  714. ];
  715. }
  716. /** @dataProvider dataReplaceImagePath */
  717. public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=1234abcd') {
  718. $this->cache->expects($this->any())
  719. ->method('get')
  720. ->with('shouldReplaceIcons')
  721. ->willReturn(true);
  722. $this->config
  723. ->expects($this->any())
  724. ->method('getAppValue')
  725. ->with('theming', 'cachebuster', '0')
  726. ->willReturn('0');
  727. $this->urlGenerator
  728. ->expects($this->any())
  729. ->method('linkToRoute')
  730. ->willReturn('themingRoute');
  731. if ($result) {
  732. $this->util
  733. ->expects($this->once())
  734. ->method('getCacheBuster')
  735. ->willReturn('1234abcd');
  736. }
  737. $this->assertEquals($result, $this->template->replaceImagePath($app, $image));
  738. }
  739. }