123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\Theming\Tests\Controller;
- use OC\L10N\L10N;
- use OCA\Theming\Controller\ThemingController;
- use OCA\Theming\ImageManager;
- use OCA\Theming\Service\ThemesService;
- use OCA\Theming\ThemingDefaults;
- use OCP\App\IAppManager;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Files\NotFoundException;
- use OCP\Files\SimpleFS\ISimpleFile;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class ThemingControllerTest extends TestCase {
- /** @var IRequest|MockObject */
- private $request;
- /** @var IConfig|MockObject */
- private $config;
- /** @var ThemingDefaults|MockObject */
- private $themingDefaults;
- /** @var IL10N|MockObject */
- private $l10n;
- /** @var ThemingController */
- private $themingController;
- /** @var IAppManager|MockObject */
- private $appManager;
- /** @var ImageManager|MockObject */
- private $imageManager;
- /** @var IURLGenerator|MockObject */
- private $urlGenerator;
- /** @var ThemesService|MockObject */
- private $themesService;
- protected function setUp(): void {
- $this->request = $this->createMock(IRequest::class);
- $this->config = $this->createMock(IConfig::class);
- $this->themingDefaults = $this->createMock(ThemingDefaults::class);
- $this->l10n = $this->createMock(L10N::class);
- $this->appManager = $this->createMock(IAppManager::class);
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->imageManager = $this->createMock(ImageManager::class);
- $this->themesService = $this->createMock(ThemesService::class);
- $timeFactory = $this->createMock(ITimeFactory::class);
- $timeFactory->expects($this->any())
- ->method('getTime')
- ->willReturn(123);
- $this->overwriteService(ITimeFactory::class, $timeFactory);
- $this->themingController = new ThemingController(
- 'theming',
- $this->request,
- $this->config,
- $this->themingDefaults,
- $this->l10n,
- $this->urlGenerator,
- $this->appManager,
- $this->imageManager,
- $this->themesService,
- );
- parent::setUp();
- }
- public function dataUpdateStylesheetSuccess() {
- return [
- ['name', str_repeat('a', 250), 'Saved'],
- ['url', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
- ['slogan', str_repeat('a', 500), 'Saved'],
- ['color', '#0082c9', 'Saved'],
- ['color', '#0082C9', 'Saved'],
- ['color', '#0082C9', 'Saved'],
- ['imprintUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
- ['privacyUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
- ];
- }
- /**
- * @dataProvider dataUpdateStylesheetSuccess
- *
- * @param string $setting
- * @param string $value
- * @param string $message
- */
- public function testUpdateStylesheetSuccess($setting, $value, $message) {
- $this->themingDefaults
- ->expects($this->once())
- ->method('set')
- ->with($setting, $value);
- $this->l10n
- ->expects($this->once())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => $message,
- ],
- 'status' => 'success',
- ]
- );
- $this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
- }
- public function dataUpdateStylesheetError() {
- return [
- ['name', str_repeat('a', 251), 'The given name is too long'],
- ['url', 'http://example.com/' . str_repeat('a', 501), 'The given web address is too long'],
- ['url', str_repeat('a', 501), 'The given web address is not a valid URL'],
- ['url', 'javascript:alert(1)', 'The given web address is not a valid URL'],
- ['slogan', str_repeat('a', 501), 'The given slogan is too long'],
- ['primary_color', '0082C9', 'The given color is invalid'],
- ['primary_color', '#0082Z9', 'The given color is invalid'],
- ['primary_color', 'Nextcloud', 'The given color is invalid'],
- ['background_color', '0082C9', 'The given color is invalid'],
- ['background_color', '#0082Z9', 'The given color is invalid'],
- ['background_color', 'Nextcloud', 'The given color is invalid'],
- ['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
- ['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
- ['imprintUrl', 'javascript:foo', 'The given legal notice address is not a valid URL'],
- ['privacyUrl', '#0082Z9', 'The given privacy policy address is not a valid URL'],
- ];
- }
- /**
- * @dataProvider dataUpdateStylesheetError
- *
- * @param string $setting
- * @param string $value
- * @param string $message
- */
- public function testUpdateStylesheetError($setting, $value, $message) {
- $this->themingDefaults
- ->expects($this->never())
- ->method('set')
- ->with($setting, $value);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => $message,
- ],
- 'status' => 'error',
- ],
- Http::STATUS_BAD_REQUEST
- );
- $this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
- }
- public function testUpdateLogoNoData() {
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('logo');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn(null);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => 'No file uploaded',
- ],
- 'status' => 'failure',
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function testUploadInvalidUploadKey() {
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('invalid');
- $this->request
- ->expects($this->never())
- ->method('getUploadedFile');
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => 'Invalid key',
- ],
- 'status' => 'failure',
- ],
- Http::STATUS_BAD_REQUEST
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- /**
- * Checks that trying to upload an SVG favicon without imagemagick
- * results in an unsupported media type response.
- *
- * @test
- * @return void
- */
- public function testUploadSVGFaviconWithoutImagemagick() {
- $this->imageManager
- ->method('shouldReplaceIcons')
- ->willReturn(false);
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('favicon');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => __DIR__ . '/../../../../tests/data/testimagelarge.svg',
- 'type' => 'image/svg',
- 'name' => 'testimagelarge.svg',
- 'error' => 0,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $this->imageManager->expects($this->once())
- ->method('updateImage')
- ->willThrowException(new \Exception('Unsupported image type'));
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => 'Unsupported image type',
- ],
- 'status' => 'failure'
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function testUpdateLogoInvalidMimeType() {
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('logo');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => __DIR__ . '/../../../../tests/data/lorem.txt',
- 'type' => 'application/pdf',
- 'name' => 'logo.pdf',
- 'error' => 0,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $this->imageManager->expects($this->once())
- ->method('updateImage')
- ->willThrowException(new \Exception('Unsupported image type'));
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => 'Unsupported image type',
- ],
- 'status' => 'failure'
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function dataUpdateImages() {
- return [
- ['image/jpeg', false],
- ['image/jpeg', true],
- ['image/gif'],
- ['image/png'],
- ['image/svg+xml'],
- ['image/svg']
- ];
- }
- /** @dataProvider dataUpdateImages */
- public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists = true) {
- $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
- $destination = \OC::$server->getTempManager()->getTemporaryFolder();
- touch($tmpLogo);
- copy(__DIR__ . '/../../../../tests/data/testimage.png', $tmpLogo);
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('logo');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => $tmpLogo,
- 'type' => $mimeType,
- 'name' => 'logo.svg',
- 'error' => 0,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $this->imageManager->expects($this->once())
- ->method('getImageUrl')
- ->with('logo')
- ->willReturn('imageUrl');
- $this->imageManager->expects($this->once())
- ->method('updateImage');
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'name' => 'logo.svg',
- 'message' => 'Saved',
- 'url' => 'imageUrl',
- ],
- 'status' => 'success'
- ]
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- /** @dataProvider dataUpdateImages */
- public function testUpdateLogoLoginScreenUpload($folderExists) {
- $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . 'logo.png';
- touch($tmpLogo);
- copy(__DIR__ . '/../../../../tests/data/desktopapp.png', $tmpLogo);
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('background');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => $tmpLogo,
- 'type' => 'image/svg+xml',
- 'name' => 'logo.svg',
- 'error' => 0,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $this->imageManager->expects($this->once())
- ->method('updateImage');
- $this->imageManager->expects($this->once())
- ->method('getImageUrl')
- ->with('background')
- ->willReturn('imageUrl');
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'name' => 'logo.svg',
- 'message' => 'Saved',
- 'url' => 'imageUrl',
- ],
- 'status' => 'success'
- ]
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
- $tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
- touch($tmpLogo);
- file_put_contents($tmpLogo, file_get_contents(__DIR__ . '/../../../../tests/data/data.zip'));
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('logo');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => $tmpLogo,
- 'type' => 'foobar',
- 'name' => 'logo.svg',
- 'error' => 0,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $this->imageManager->expects($this->once())
- ->method('updateImage')
- ->willThrowException(new \Exception('Unsupported image type'));
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => 'Unsupported image type',
- ],
- 'status' => 'failure'
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function dataPhpUploadErrors() {
- return [
- [UPLOAD_ERR_INI_SIZE, 'The uploaded file exceeds the upload_max_filesize directive in php.ini'],
- [UPLOAD_ERR_FORM_SIZE, 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'],
- [UPLOAD_ERR_PARTIAL, 'The file was only partially uploaded'],
- [UPLOAD_ERR_NO_FILE, 'No file was uploaded'],
- [UPLOAD_ERR_NO_TMP_DIR, 'Missing a temporary folder'],
- [UPLOAD_ERR_CANT_WRITE, 'Could not write file to disk'],
- [UPLOAD_ERR_EXTENSION, 'A PHP extension stopped the file upload'],
- ];
- }
- /**
- * @dataProvider dataPhpUploadErrors
- */
- public function testUpdateLogoLoginScreenUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('background');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => '',
- 'type' => 'image/svg+xml',
- 'name' => 'logo.svg',
- 'error' => $error,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => $expectedErrorMessage,
- ],
- 'status' => 'failure'
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- /**
- * @dataProvider dataPhpUploadErrors
- */
- public function testUpdateLogoUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
- $this->request
- ->expects($this->once())
- ->method('getParam')
- ->with('key')
- ->willReturn('background');
- $this->request
- ->expects($this->once())
- ->method('getUploadedFile')
- ->with('image')
- ->willReturn([
- 'tmp_name' => '',
- 'type' => 'text/svg',
- 'name' => 'logo.svg',
- 'error' => $error,
- ]);
- $this->l10n
- ->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($str) {
- return $str;
- });
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'message' => $expectedErrorMessage
- ],
- 'status' => 'failure'
- ],
- Http::STATUS_UNPROCESSABLE_ENTITY
- );
- $this->assertEquals($expected, $this->themingController->uploadImage());
- }
- public function testUndo() {
- $this->l10n
- ->expects($this->once())
- ->method('t')
- ->with('Saved')
- ->willReturn('Saved');
- $this->themingDefaults
- ->expects($this->once())
- ->method('undo')
- ->with('MySetting')
- ->willReturn('MyValue');
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'value' => 'MyValue',
- 'message' => 'Saved'
- ],
- 'status' => 'success'
- ]
- );
- $this->assertEquals($expected, $this->themingController->undo('MySetting'));
- }
- public function dataUndoDelete() {
- return [
- [ 'backgroundMime', 'background' ],
- [ 'logoMime', 'logo' ]
- ];
- }
- /** @dataProvider dataUndoDelete */
- public function testUndoDelete($value, $filename) {
- $this->l10n
- ->expects($this->once())
- ->method('t')
- ->with('Saved')
- ->willReturn('Saved');
- $this->themingDefaults
- ->expects($this->once())
- ->method('undo')
- ->with($value)
- ->willReturn($value);
- $expected = new DataResponse(
- [
- 'data' =>
- [
- 'value' => $value,
- 'message' => 'Saved',
- ],
- 'status' => 'success'
- ]
- );
- $this->assertEquals($expected, $this->themingController->undo($value));
- }
- public function testGetLogoNotExistent() {
- $this->imageManager->method('getImage')
- ->with($this->equalTo('logo'))
- ->willThrowException(new NotFoundException());
- $expected = new Http\NotFoundResponse();
- $this->assertEquals($expected, $this->themingController->getImage('logo'));
- }
- public function testGetLogo() {
- $file = $this->createMock(ISimpleFile::class);
- $file->method('getName')->willReturn('logo.svg');
- $file->method('getMTime')->willReturn(42);
- $this->imageManager->expects($this->once())
- ->method('getImage')
- ->willReturn($file);
- $this->config
- ->expects($this->any())
- ->method('getAppValue')
- ->with('theming', 'logoMime', '')
- ->willReturn('text/svg');
- @$expected = new Http\FileDisplayResponse($file);
- $expected->cacheFor(3600);
- $expected->addHeader('Content-Type', 'text/svg');
- $expected->addHeader('Content-Disposition', 'attachment; filename="logo"');
- $csp = new Http\ContentSecurityPolicy();
- $csp->allowInlineStyle();
- $expected->setContentSecurityPolicy($csp);
- @$this->assertEquals($expected, $this->themingController->getImage('logo'));
- }
- public function testGetLoginBackgroundNotExistent() {
- $this->imageManager->method('getImage')
- ->with($this->equalTo('background'))
- ->willThrowException(new NotFoundException());
- $expected = new Http\NotFoundResponse();
- $this->assertEquals($expected, $this->themingController->getImage('background'));
- }
- public function testGetLoginBackground() {
- $file = $this->createMock(ISimpleFile::class);
- $file->method('getName')->willReturn('background.png');
- $file->method('getMTime')->willReturn(42);
- $this->imageManager->expects($this->once())
- ->method('getImage')
- ->willReturn($file);
- $this->config
- ->expects($this->any())
- ->method('getAppValue')
- ->with('theming', 'backgroundMime', '')
- ->willReturn('image/png');
- @$expected = new Http\FileDisplayResponse($file);
- $expected->cacheFor(3600);
- $expected->addHeader('Content-Type', 'image/png');
- $expected->addHeader('Content-Disposition', 'attachment; filename="background"');
- $csp = new Http\ContentSecurityPolicy();
- $csp->allowInlineStyle();
- $expected->setContentSecurityPolicy($csp);
- @$this->assertEquals($expected, $this->themingController->getImage('background'));
- }
- public function testGetManifest() {
- $this->config
- ->expects($this->once())
- ->method('getAppValue')
- ->with('theming', 'cachebuster', '0')
- ->willReturn('0');
- $this->themingDefaults
- ->expects($this->any())
- ->method('getName')
- ->willReturn('Nextcloud');
- $this->urlGenerator
- ->expects($this->once())
- ->method('getBaseUrl')
- ->willReturn('localhost');
- $this->urlGenerator
- ->expects($this->exactly(2))
- ->method('linkToRoute')
- ->withConsecutive(
- ['theming.Icon.getTouchIcon', ['app' => 'core']],
- ['theming.Icon.getFavicon', ['app' => 'core']],
- )->willReturnOnConsecutiveCalls(
- 'touchicon',
- 'favicon',
- );
- $response = new Http\JSONResponse([
- 'name' => 'Nextcloud',
- 'start_url' => 'localhost',
- 'icons' =>
- [
- [
- 'src' => 'touchicon?v=0',
- 'type' => 'image/png',
- 'sizes' => '512x512'
- ],
- [
- 'src' => 'favicon?v=0',
- 'type' => 'image/svg+xml',
- 'sizes' => '16x16'
- ]
- ],
- 'display' => 'standalone',
- 'short_name' => 'Nextcloud',
- 'theme_color' => null,
- 'background_color' => null,
- 'description' => null
- ]);
- $response->cacheFor(3600);
- $this->assertEquals($response, $this->themingController->getManifest('core'));
- }
- }
|