* * @author Bjoern Schiessle * @author Christoph Wurst * @author Daniel Calviño Sánchez * @author Joas Schilling * @author John Molakvoæ * @author Julius Haertl * @author Julius Härtl * @author Kyle Fazzari * @author Lukas Reschke * @author Michael Weimann * @author rakekniven * @author Roeland Jago Douma * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ 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\IAppData; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\ITempManager; 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 ITempManager */ private $tempManager; /** @var IAppManager|MockObject */ private $appManager; /** @var IAppData|MockObject */ private $appData; /** @var ImageManager|MockObject */ private $imageManager; /** @var IURLGenerator|MockObject */ private $urlGenerator; /** @var ThemeService|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->appData = $this->createMock(IAppData::class); $this->appManager = $this->createMock(IAppManager::class); $this->tempManager = \OC::$server->getTempManager(); $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->tempManager, $this->appData, $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() { $urls = [ 'url' => 'web address', 'imprintUrl' => 'legal notice address', 'privacyUrl' => 'privacy policy address', ]; $urlTests = []; foreach ($urls as $urlKey => $urlName) { // Check length limit $urlTests[] = [$urlKey, 'http://example.com/' . str_repeat('a', 501), "The given {$urlName} is too long"]; // Check potential evil javascript $urlTests[] = [$urlKey, 'javascript:alert(1)', "The given {$urlName} is not a valid URL"]; // Check XSS $urlTests[] = [$urlKey, 'https://example.com/">