1
0

ThemingControllerTest.php 20 KB

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