1
0

ThemingControllerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Haertl <jus@bitgrid.net>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Kyle Fazzari <kyrofa@ubuntu.com>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Michael Weimann <mail@michael-weimann.eu>
  15. * @author rakekniven <mark.ziegler@rakekniven.de>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. *
  18. * @license GNU AGPL version 3 or any later version
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License as
  22. * published by the Free Software Foundation, either version 3 of the
  23. * License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. */
  34. namespace OCA\Theming\Tests\Controller;
  35. use OC\L10N\L10N;
  36. use OCA\Theming\Controller\ThemingController;
  37. use OCA\Theming\ImageManager;
  38. use OCA\Theming\Service\ThemesService;
  39. use OCA\Theming\ThemingDefaults;
  40. use OCP\App\IAppManager;
  41. use OCP\AppFramework\Http;
  42. use OCP\AppFramework\Http\DataResponse;
  43. use OCP\AppFramework\Utility\ITimeFactory;
  44. use OCP\Files\NotFoundException;
  45. use OCP\Files\SimpleFS\ISimpleFile;
  46. use OCP\IConfig;
  47. use OCP\IL10N;
  48. use OCP\IRequest;
  49. use OCP\IURLGenerator;
  50. use PHPUnit\Framework\MockObject\MockObject;
  51. use Test\TestCase;
  52. class ThemingControllerTest extends TestCase {
  53. /** @var IRequest|MockObject */
  54. private $request;
  55. /** @var IConfig|MockObject */
  56. private $config;
  57. /** @var ThemingDefaults|MockObject */
  58. private $themingDefaults;
  59. /** @var IL10N|MockObject */
  60. private $l10n;
  61. /** @var ThemingController */
  62. private $themingController;
  63. /** @var IAppManager|MockObject */
  64. private $appManager;
  65. /** @var ImageManager|MockObject */
  66. private $imageManager;
  67. /** @var IURLGenerator|MockObject */
  68. private $urlGenerator;
  69. /** @var ThemesService|MockObject */
  70. private $themesService;
  71. protected function setUp(): void {
  72. $this->request = $this->createMock(IRequest::class);
  73. $this->config = $this->createMock(IConfig::class);
  74. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  75. $this->l10n = $this->createMock(L10N::class);
  76. $this->appManager = $this->createMock(IAppManager::class);
  77. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  78. $this->imageManager = $this->createMock(ImageManager::class);
  79. $this->themesService = $this->createMock(ThemesService::class);
  80. $timeFactory = $this->createMock(ITimeFactory::class);
  81. $timeFactory->expects($this->any())
  82. ->method('getTime')
  83. ->willReturn(123);
  84. $this->overwriteService(ITimeFactory::class, $timeFactory);
  85. $this->themingController = new ThemingController(
  86. 'theming',
  87. $this->request,
  88. $this->config,
  89. $this->themingDefaults,
  90. $this->l10n,
  91. $this->urlGenerator,
  92. $this->appManager,
  93. $this->imageManager,
  94. $this->themesService,
  95. );
  96. parent::setUp();
  97. }
  98. public function dataUpdateStylesheetSuccess() {
  99. return [
  100. ['name', str_repeat('a', 250), 'Saved'],
  101. ['url', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
  102. ['slogan', str_repeat('a', 500), 'Saved'],
  103. ['color', '#0082c9', 'Saved'],
  104. ['color', '#0082C9', 'Saved'],
  105. ['color', '#0082C9', 'Saved'],
  106. ['imprintUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
  107. ['privacyUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
  108. ];
  109. }
  110. /**
  111. * @dataProvider dataUpdateStylesheetSuccess
  112. *
  113. * @param string $setting
  114. * @param string $value
  115. * @param string $message
  116. */
  117. public function testUpdateStylesheetSuccess($setting, $value, $message) {
  118. $this->themingDefaults
  119. ->expects($this->once())
  120. ->method('set')
  121. ->with($setting, $value);
  122. $this->l10n
  123. ->expects($this->once())
  124. ->method('t')
  125. ->willReturnCallback(function ($str) {
  126. return $str;
  127. });
  128. $expected = new DataResponse(
  129. [
  130. 'data' =>
  131. [
  132. 'message' => $message,
  133. ],
  134. 'status' => 'success',
  135. ]
  136. );
  137. $this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
  138. }
  139. public function dataUpdateStylesheetError() {
  140. return [
  141. ['name', str_repeat('a', 251), 'The given name is too long'],
  142. ['url', 'http://example.com/' . str_repeat('a', 501), 'The given web address is too long'],
  143. ['url', str_repeat('a', 501), 'The given web address is not a valid URL'],
  144. ['url', 'javascript:alert(1)', 'The given web address is not a valid URL'],
  145. ['slogan', str_repeat('a', 501), 'The given slogan is too long'],
  146. ['primary_color', '0082C9', 'The given color is invalid'],
  147. ['primary_color', '#0082Z9', 'The given color is invalid'],
  148. ['primary_color', 'Nextcloud', 'The given color is invalid'],
  149. ['background_color', '0082C9', 'The given color is invalid'],
  150. ['background_color', '#0082Z9', 'The given color is invalid'],
  151. ['background_color', 'Nextcloud', 'The given color is invalid'],
  152. ['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
  153. ['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
  154. ['imprintUrl', 'javascript:foo', 'The given legal notice address is not a valid URL'],
  155. ['privacyUrl', '#0082Z9', 'The given privacy policy address is not a valid URL'],
  156. ];
  157. }
  158. /**
  159. * @dataProvider dataUpdateStylesheetError
  160. *
  161. * @param string $setting
  162. * @param string $value
  163. * @param string $message
  164. */
  165. public function testUpdateStylesheetError($setting, $value, $message) {
  166. $this->themingDefaults
  167. ->expects($this->never())
  168. ->method('set')
  169. ->with($setting, $value);
  170. $this->l10n
  171. ->expects($this->any())
  172. ->method('t')
  173. ->willReturnCallback(function ($str) {
  174. return $str;
  175. });
  176. $expected = new DataResponse(
  177. [
  178. 'data' =>
  179. [
  180. 'message' => $message,
  181. ],
  182. 'status' => 'error',
  183. ],
  184. Http::STATUS_BAD_REQUEST
  185. );
  186. $this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
  187. }
  188. public function testUpdateLogoNoData() {
  189. $this->request
  190. ->expects($this->once())
  191. ->method('getParam')
  192. ->with('key')
  193. ->willReturn('logo');
  194. $this->request
  195. ->expects($this->once())
  196. ->method('getUploadedFile')
  197. ->with('image')
  198. ->willReturn(null);
  199. $this->l10n
  200. ->expects($this->any())
  201. ->method('t')
  202. ->willReturnCallback(function ($str) {
  203. return $str;
  204. });
  205. $expected = new DataResponse(
  206. [
  207. 'data' =>
  208. [
  209. 'message' => 'No file uploaded',
  210. ],
  211. 'status' => 'failure',
  212. ],
  213. Http::STATUS_UNPROCESSABLE_ENTITY
  214. );
  215. $this->assertEquals($expected, $this->themingController->uploadImage());
  216. }
  217. public function testUploadInvalidUploadKey() {
  218. $this->request
  219. ->expects($this->once())
  220. ->method('getParam')
  221. ->with('key')
  222. ->willReturn('invalid');
  223. $this->request
  224. ->expects($this->never())
  225. ->method('getUploadedFile');
  226. $this->l10n
  227. ->expects($this->any())
  228. ->method('t')
  229. ->willReturnCallback(function ($str) {
  230. return $str;
  231. });
  232. $expected = new DataResponse(
  233. [
  234. 'data' =>
  235. [
  236. 'message' => 'Invalid key',
  237. ],
  238. 'status' => 'failure',
  239. ],
  240. Http::STATUS_BAD_REQUEST
  241. );
  242. $this->assertEquals($expected, $this->themingController->uploadImage());
  243. }
  244. /**
  245. * Checks that trying to upload an SVG favicon without imagemagick
  246. * results in an unsupported media type response.
  247. *
  248. * @test
  249. * @return void
  250. */
  251. public function testUploadSVGFaviconWithoutImagemagick() {
  252. $this->imageManager
  253. ->method('shouldReplaceIcons')
  254. ->willReturn(false);
  255. $this->request
  256. ->expects($this->once())
  257. ->method('getParam')
  258. ->with('key')
  259. ->willReturn('favicon');
  260. $this->request
  261. ->expects($this->once())
  262. ->method('getUploadedFile')
  263. ->with('image')
  264. ->willReturn([
  265. 'tmp_name' => __DIR__ . '/../../../../tests/data/testimagelarge.svg',
  266. 'type' => 'image/svg',
  267. 'name' => 'testimagelarge.svg',
  268. 'error' => 0,
  269. ]);
  270. $this->l10n
  271. ->expects($this->any())
  272. ->method('t')
  273. ->willReturnCallback(function ($str) {
  274. return $str;
  275. });
  276. $this->imageManager->expects($this->once())
  277. ->method('updateImage')
  278. ->willThrowException(new \Exception('Unsupported image type'));
  279. $expected = new DataResponse(
  280. [
  281. 'data' =>
  282. [
  283. 'message' => 'Unsupported image type',
  284. ],
  285. 'status' => 'failure'
  286. ],
  287. Http::STATUS_UNPROCESSABLE_ENTITY
  288. );
  289. $this->assertEquals($expected, $this->themingController->uploadImage());
  290. }
  291. public function testUpdateLogoInvalidMimeType() {
  292. $this->request
  293. ->expects($this->once())
  294. ->method('getParam')
  295. ->with('key')
  296. ->willReturn('logo');
  297. $this->request
  298. ->expects($this->once())
  299. ->method('getUploadedFile')
  300. ->with('image')
  301. ->willReturn([
  302. 'tmp_name' => __DIR__ . '/../../../../tests/data/lorem.txt',
  303. 'type' => 'application/pdf',
  304. 'name' => 'logo.pdf',
  305. 'error' => 0,
  306. ]);
  307. $this->l10n
  308. ->expects($this->any())
  309. ->method('t')
  310. ->willReturnCallback(function ($str) {
  311. return $str;
  312. });
  313. $this->imageManager->expects($this->once())
  314. ->method('updateImage')
  315. ->willThrowException(new \Exception('Unsupported image type'));
  316. $expected = new DataResponse(
  317. [
  318. 'data' =>
  319. [
  320. 'message' => 'Unsupported image type',
  321. ],
  322. 'status' => 'failure'
  323. ],
  324. Http::STATUS_UNPROCESSABLE_ENTITY
  325. );
  326. $this->assertEquals($expected, $this->themingController->uploadImage());
  327. }
  328. public function dataUpdateImages() {
  329. return [
  330. ['image/jpeg', false],
  331. ['image/jpeg', true],
  332. ['image/gif'],
  333. ['image/png'],
  334. ['image/svg+xml'],
  335. ['image/svg']
  336. ];
  337. }
  338. /** @dataProvider dataUpdateImages */
  339. public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists = true) {
  340. $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
  341. $destination = \OC::$server->getTempManager()->getTemporaryFolder();
  342. touch($tmpLogo);
  343. copy(__DIR__ . '/../../../../tests/data/testimage.png', $tmpLogo);
  344. $this->request
  345. ->expects($this->once())
  346. ->method('getParam')
  347. ->with('key')
  348. ->willReturn('logo');
  349. $this->request
  350. ->expects($this->once())
  351. ->method('getUploadedFile')
  352. ->with('image')
  353. ->willReturn([
  354. 'tmp_name' => $tmpLogo,
  355. 'type' => $mimeType,
  356. 'name' => 'logo.svg',
  357. 'error' => 0,
  358. ]);
  359. $this->l10n
  360. ->expects($this->any())
  361. ->method('t')
  362. ->willReturnCallback(function ($str) {
  363. return $str;
  364. });
  365. $this->imageManager->expects($this->once())
  366. ->method('getImageUrl')
  367. ->with('logo')
  368. ->willReturn('imageUrl');
  369. $this->imageManager->expects($this->once())
  370. ->method('updateImage');
  371. $expected = new DataResponse(
  372. [
  373. 'data' =>
  374. [
  375. 'name' => 'logo.svg',
  376. 'message' => 'Saved',
  377. 'url' => 'imageUrl',
  378. ],
  379. 'status' => 'success'
  380. ]
  381. );
  382. $this->assertEquals($expected, $this->themingController->uploadImage());
  383. }
  384. /** @dataProvider dataUpdateImages */
  385. public function testUpdateLogoLoginScreenUpload($folderExists) {
  386. $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . 'logo.png';
  387. touch($tmpLogo);
  388. copy(__DIR__ . '/../../../../tests/data/desktopapp.png', $tmpLogo);
  389. $this->request
  390. ->expects($this->once())
  391. ->method('getParam')
  392. ->with('key')
  393. ->willReturn('background');
  394. $this->request
  395. ->expects($this->once())
  396. ->method('getUploadedFile')
  397. ->with('image')
  398. ->willReturn([
  399. 'tmp_name' => $tmpLogo,
  400. 'type' => 'image/svg+xml',
  401. 'name' => 'logo.svg',
  402. 'error' => 0,
  403. ]);
  404. $this->l10n
  405. ->expects($this->any())
  406. ->method('t')
  407. ->willReturnCallback(function ($str) {
  408. return $str;
  409. });
  410. $this->imageManager->expects($this->once())
  411. ->method('updateImage');
  412. $this->imageManager->expects($this->once())
  413. ->method('getImageUrl')
  414. ->with('background')
  415. ->willReturn('imageUrl');
  416. $expected = new DataResponse(
  417. [
  418. 'data' =>
  419. [
  420. 'name' => 'logo.svg',
  421. 'message' => 'Saved',
  422. 'url' => 'imageUrl',
  423. ],
  424. 'status' => 'success'
  425. ]
  426. );
  427. $this->assertEquals($expected, $this->themingController->uploadImage());
  428. }
  429. public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
  430. $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
  431. touch($tmpLogo);
  432. file_put_contents($tmpLogo, file_get_contents(__DIR__ . '/../../../../tests/data/data.zip'));
  433. $this->request
  434. ->expects($this->once())
  435. ->method('getParam')
  436. ->with('key')
  437. ->willReturn('logo');
  438. $this->request
  439. ->expects($this->once())
  440. ->method('getUploadedFile')
  441. ->with('image')
  442. ->willReturn([
  443. 'tmp_name' => $tmpLogo,
  444. 'type' => 'foobar',
  445. 'name' => 'logo.svg',
  446. 'error' => 0,
  447. ]);
  448. $this->l10n
  449. ->expects($this->any())
  450. ->method('t')
  451. ->willReturnCallback(function ($str) {
  452. return $str;
  453. });
  454. $this->imageManager->expects($this->once())
  455. ->method('updateImage')
  456. ->willThrowException(new \Exception('Unsupported image type'));
  457. $expected = new DataResponse(
  458. [
  459. 'data' =>
  460. [
  461. 'message' => 'Unsupported image type',
  462. ],
  463. 'status' => 'failure'
  464. ],
  465. Http::STATUS_UNPROCESSABLE_ENTITY
  466. );
  467. $this->assertEquals($expected, $this->themingController->uploadImage());
  468. }
  469. public function dataPhpUploadErrors() {
  470. return [
  471. [UPLOAD_ERR_INI_SIZE, 'The uploaded file exceeds the upload_max_filesize directive in php.ini'],
  472. [UPLOAD_ERR_FORM_SIZE, 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'],
  473. [UPLOAD_ERR_PARTIAL, 'The file was only partially uploaded'],
  474. [UPLOAD_ERR_NO_FILE, 'No file was uploaded'],
  475. [UPLOAD_ERR_NO_TMP_DIR, 'Missing a temporary folder'],
  476. [UPLOAD_ERR_CANT_WRITE, 'Could not write file to disk'],
  477. [UPLOAD_ERR_EXTENSION, 'A PHP extension stopped the file upload'],
  478. ];
  479. }
  480. /**
  481. * @dataProvider dataPhpUploadErrors
  482. */
  483. public function testUpdateLogoLoginScreenUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
  484. $this->request
  485. ->expects($this->once())
  486. ->method('getParam')
  487. ->with('key')
  488. ->willReturn('background');
  489. $this->request
  490. ->expects($this->once())
  491. ->method('getUploadedFile')
  492. ->with('image')
  493. ->willReturn([
  494. 'tmp_name' => '',
  495. 'type' => 'image/svg+xml',
  496. 'name' => 'logo.svg',
  497. 'error' => $error,
  498. ]);
  499. $this->l10n
  500. ->expects($this->any())
  501. ->method('t')
  502. ->willReturnCallback(function ($str) {
  503. return $str;
  504. });
  505. $expected = new DataResponse(
  506. [
  507. 'data' =>
  508. [
  509. 'message' => $expectedErrorMessage,
  510. ],
  511. 'status' => 'failure'
  512. ],
  513. Http::STATUS_UNPROCESSABLE_ENTITY
  514. );
  515. $this->assertEquals($expected, $this->themingController->uploadImage());
  516. }
  517. /**
  518. * @dataProvider dataPhpUploadErrors
  519. */
  520. public function testUpdateLogoUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
  521. $this->request
  522. ->expects($this->once())
  523. ->method('getParam')
  524. ->with('key')
  525. ->willReturn('background');
  526. $this->request
  527. ->expects($this->once())
  528. ->method('getUploadedFile')
  529. ->with('image')
  530. ->willReturn([
  531. 'tmp_name' => '',
  532. 'type' => 'text/svg',
  533. 'name' => 'logo.svg',
  534. 'error' => $error,
  535. ]);
  536. $this->l10n
  537. ->expects($this->any())
  538. ->method('t')
  539. ->willReturnCallback(function ($str) {
  540. return $str;
  541. });
  542. $expected = new DataResponse(
  543. [
  544. 'data' =>
  545. [
  546. 'message' => $expectedErrorMessage
  547. ],
  548. 'status' => 'failure'
  549. ],
  550. Http::STATUS_UNPROCESSABLE_ENTITY
  551. );
  552. $this->assertEquals($expected, $this->themingController->uploadImage());
  553. }
  554. public function testUndo() {
  555. $this->l10n
  556. ->expects($this->once())
  557. ->method('t')
  558. ->with('Saved')
  559. ->willReturn('Saved');
  560. $this->themingDefaults
  561. ->expects($this->once())
  562. ->method('undo')
  563. ->with('MySetting')
  564. ->willReturn('MyValue');
  565. $expected = new DataResponse(
  566. [
  567. 'data' =>
  568. [
  569. 'value' => 'MyValue',
  570. 'message' => 'Saved'
  571. ],
  572. 'status' => 'success'
  573. ]
  574. );
  575. $this->assertEquals($expected, $this->themingController->undo('MySetting'));
  576. }
  577. public function dataUndoDelete() {
  578. return [
  579. [ 'backgroundMime', 'background' ],
  580. [ 'logoMime', 'logo' ]
  581. ];
  582. }
  583. /** @dataProvider dataUndoDelete */
  584. public function testUndoDelete($value, $filename) {
  585. $this->l10n
  586. ->expects($this->once())
  587. ->method('t')
  588. ->with('Saved')
  589. ->willReturn('Saved');
  590. $this->themingDefaults
  591. ->expects($this->once())
  592. ->method('undo')
  593. ->with($value)
  594. ->willReturn($value);
  595. $expected = new DataResponse(
  596. [
  597. 'data' =>
  598. [
  599. 'value' => $value,
  600. 'message' => 'Saved',
  601. ],
  602. 'status' => 'success'
  603. ]
  604. );
  605. $this->assertEquals($expected, $this->themingController->undo($value));
  606. }
  607. public function testGetLogoNotExistent() {
  608. $this->imageManager->method('getImage')
  609. ->with($this->equalTo('logo'))
  610. ->willThrowException(new NotFoundException());
  611. $expected = new Http\NotFoundResponse();
  612. $this->assertEquals($expected, $this->themingController->getImage('logo'));
  613. }
  614. public function testGetLogo() {
  615. $file = $this->createMock(ISimpleFile::class);
  616. $file->method('getName')->willReturn('logo.svg');
  617. $file->method('getMTime')->willReturn(42);
  618. $this->imageManager->expects($this->once())
  619. ->method('getImage')
  620. ->willReturn($file);
  621. $this->config
  622. ->expects($this->any())
  623. ->method('getAppValue')
  624. ->with('theming', 'logoMime', '')
  625. ->willReturn('text/svg');
  626. @$expected = new Http\FileDisplayResponse($file);
  627. $expected->cacheFor(3600);
  628. $expected->addHeader('Content-Type', 'text/svg');
  629. $expected->addHeader('Content-Disposition', 'attachment; filename="logo"');
  630. $csp = new Http\ContentSecurityPolicy();
  631. $csp->allowInlineStyle();
  632. $expected->setContentSecurityPolicy($csp);
  633. @$this->assertEquals($expected, $this->themingController->getImage('logo'));
  634. }
  635. public function testGetLoginBackgroundNotExistent() {
  636. $this->imageManager->method('getImage')
  637. ->with($this->equalTo('background'))
  638. ->willThrowException(new NotFoundException());
  639. $expected = new Http\NotFoundResponse();
  640. $this->assertEquals($expected, $this->themingController->getImage('background'));
  641. }
  642. public function testGetLoginBackground() {
  643. $file = $this->createMock(ISimpleFile::class);
  644. $file->method('getName')->willReturn('background.png');
  645. $file->method('getMTime')->willReturn(42);
  646. $this->imageManager->expects($this->once())
  647. ->method('getImage')
  648. ->willReturn($file);
  649. $this->config
  650. ->expects($this->any())
  651. ->method('getAppValue')
  652. ->with('theming', 'backgroundMime', '')
  653. ->willReturn('image/png');
  654. @$expected = new Http\FileDisplayResponse($file);
  655. $expected->cacheFor(3600);
  656. $expected->addHeader('Content-Type', 'image/png');
  657. $expected->addHeader('Content-Disposition', 'attachment; filename="background"');
  658. $csp = new Http\ContentSecurityPolicy();
  659. $csp->allowInlineStyle();
  660. $expected->setContentSecurityPolicy($csp);
  661. @$this->assertEquals($expected, $this->themingController->getImage('background'));
  662. }
  663. public function testGetManifest() {
  664. $this->config
  665. ->expects($this->once())
  666. ->method('getAppValue')
  667. ->with('theming', 'cachebuster', '0')
  668. ->willReturn('0');
  669. $this->themingDefaults
  670. ->expects($this->any())
  671. ->method('getName')
  672. ->willReturn('Nextcloud');
  673. $this->urlGenerator
  674. ->expects($this->once())
  675. ->method('getBaseUrl')
  676. ->willReturn('localhost');
  677. $this->urlGenerator
  678. ->expects($this->exactly(2))
  679. ->method('linkToRoute')
  680. ->withConsecutive(
  681. ['theming.Icon.getTouchIcon', ['app' => 'core']],
  682. ['theming.Icon.getFavicon', ['app' => 'core']],
  683. )->willReturnOnConsecutiveCalls(
  684. 'touchicon',
  685. 'favicon',
  686. );
  687. $response = new Http\JSONResponse([
  688. 'name' => 'Nextcloud',
  689. 'start_url' => 'localhost',
  690. 'icons' =>
  691. [
  692. [
  693. 'src' => 'touchicon?v=0',
  694. 'type' => 'image/png',
  695. 'sizes' => '512x512'
  696. ],
  697. [
  698. 'src' => 'favicon?v=0',
  699. 'type' => 'image/svg+xml',
  700. 'sizes' => '16x16'
  701. ]
  702. ],
  703. 'display' => 'standalone',
  704. 'short_name' => 'Nextcloud',
  705. 'theme_color' => null,
  706. 'background_color' => null,
  707. 'description' => null
  708. ]);
  709. $response->cacheFor(3600);
  710. $this->assertEquals($response, $this->themingController->getManifest('core'));
  711. }
  712. }