Skip to content

Commit

Permalink
Phpunit
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <[email protected]>
  • Loading branch information
skjnldsv committed Apr 21, 2022
1 parent 0c366f6 commit 38e99e3
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 127 deletions.
48 changes: 31 additions & 17 deletions apps/theming/lib/Service/ThemesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function __construct(IUserSession $userSession,
IConfig $config,
DefaultTheme $defaultTheme,
DarkTheme $darkTheme,
DarkHighContrastTheme $darkHighContrastTheme,
HighContrastTheme $highContrastTheme,
DarkHighContrastTheme $darkHighContrastTheme,
DyslexiaFont $dyslexiaFont) {
$this->userSession = $userSession;
$this->config = $config;
Expand Down Expand Up @@ -73,44 +73,54 @@ public function getThemes(): array {
* Enable a theme for the logged-in user
*
* @param ITheme $theme the theme to enable
* @return string[] the enabled themes
*/
public function enableTheme(ITheme $theme): void {
public function enableTheme(ITheme $theme): array {
$themesIds = $this->getEnabledThemes();

// If already enabled, ignore
if (in_array($theme->getId(), $themesIds)) {
return $themesIds;
}

/** @var ITheme[] */
$themes = array_map(function($themeId) {
return $this->getThemes()[$themeId];
}, $themesIds);

// Filtering all themes with the same type
$filteredThemes = array_filter($themes, function($t) use ($theme) {
$filteredThemes = array_filter($themes, function(ITheme $t) use ($theme) {
return $theme->getType() === $t->getType();
});

// Disable all the other themes of the same type
// as there can only be one enabled at the same time
foreach ($filteredThemes as $t) {
$this->disableTheme($t);
}
// Retrieve IDs only
$filteredThemesIds = array_map(function(ITheme $t) {
return $t->getId();
}, $filteredThemes);

$this->setEnabledThemes([...$this->getEnabledThemes(), $theme->getId()]);
$enabledThemes = [...array_diff($themesIds, $filteredThemesIds), $theme->getId()];
$this->setEnabledThemes($enabledThemes);

return $enabledThemes;
}

/**
* Disable a theme for the logged-in user
*
* @param ITheme $theme the theme to disable
* @return string[] the enabled themes
*/
public function disableTheme(ITheme $theme): void {
// Using keys as it's faster
$themes = $this->getEnabledThemes();
public function disableTheme(ITheme $theme): array {
$themesIds = $this->getEnabledThemes();

// If enabled, removing it
if (in_array($theme->getId(), $themes)) {
$this->setEnabledThemes(array_filter($themes, function($themeId) use ($theme) {
return $themeId !== $theme->getId();
}));
if (in_array($theme->getId(), $themesIds)) {
$enabledThemes = array_diff($themesIds, [$theme->getId()]);
$this->setEnabledThemes($enabledThemes);
return $enabledThemes;
}

return $themesIds;
}

/**
Expand All @@ -136,6 +146,10 @@ public function isEnabled(ITheme $theme): bool {
*/
public function getEnabledThemes(): array {
$user = $this->userSession->getUser();
if ($user === null) {
return [];
}

try {
return json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]'));
} catch (\Exception $e) {
Expand All @@ -151,6 +165,6 @@ public function getEnabledThemes(): array {
*/
private function setEnabledThemes(array $themes): void {
$user = $this->userSession->getUser();
$this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($themes)));
$this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique(array_values($themes))));
}
}
2 changes: 1 addition & 1 deletion apps/theming/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function toHSL(string $red, string $green, string $blue): array {
public function calculateLuminance(string $color): float {
[$red, $green, $blue] = $this->hexToRGB($color);
$hsl = $this->toHSL($red, $green, $blue);
return $hsl[2] / 100;
return $hsl[2];
}

/**
Expand Down
104 changes: 8 additions & 96 deletions apps/theming/tests/Controller/ThemingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
namespace OCA\Theming\Tests\Controller;

use OC\L10N\L10N;
use OC\Template\SCSSCacher;
use OCA\Theming\Controller\ThemingController;
use OCA\Theming\Service\ThemesService;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -72,10 +72,10 @@ class ThemingControllerTest extends TestCase {
private $appData;
/** @var ImageManager|MockObject */
private $imageManager;
/** @var SCSSCacher */
private $scssCacher;
/** @var IURLGenerator */
/** @var IURLGenerator|MockObject */
private $urlGenerator;
/** @var ThemeService|MockObject */
private $themesService;

protected function setUp(): void {
$this->request = $this->createMock(IRequest::class);
Expand All @@ -85,9 +85,9 @@ protected function setUp(): void {
$this->appData = $this->createMock(IAppData::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->tempManager = \OC::$server->getTempManager();
$this->scssCacher = $this->createMock(SCSSCacher::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())
Expand All @@ -104,10 +104,10 @@ protected function setUp(): void {
$this->l10n,
$this->tempManager,
$this->appData,
$this->scssCacher,
$this->urlGenerator,
$this->appManager,
$this->imageManager
$this->imageManager,
$this->themesService,
);

parent::setUp();
Expand Down Expand Up @@ -144,23 +144,12 @@ public function testUpdateStylesheetSuccess($setting, $value, $message) {
->willReturnCallback(function ($str) {
return $str;
});
$this->scssCacher
->expects($this->once())
->method('getCachedSCSS')
->with('core', '/core/css/css-variables.scss')
->willReturn('/core/css/someHash-css-variables.scss');
$this->urlGenerator
->expects($this->once())
->method('linkTo')
->with('', '/core/css/someHash-css-variables.scss')
->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');

$expected = new DataResponse(
[
'data' =>
[
'message' => $message,
'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
],
'status' => 'success',
]
Expand Down Expand Up @@ -382,9 +371,6 @@ public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists = true)
return $str;
});

$this->urlGenerator->expects($this->once())
->method('linkTo')
->willReturn('serverCss');
$this->imageManager->expects($this->once())
->method('getImageUrl')
->with('logo')
Expand All @@ -400,7 +386,6 @@ public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists = true)
'name' => 'logo.svg',
'message' => 'Saved',
'url' => 'imageUrl',
'serverCssUrl' => 'serverCss'
],
'status' => 'success'
]
Expand Down Expand Up @@ -440,9 +425,6 @@ public function testUpdateLogoLoginScreenUpload($folderExists) {
$this->imageManager->expects($this->once())
->method('updateImage');

$this->urlGenerator->expects($this->once())
->method('linkTo')
->willReturn('serverCss');
$this->imageManager->expects($this->once())
->method('getImageUrl')
->with('background')
Expand All @@ -454,7 +436,6 @@ public function testUpdateLogoLoginScreenUpload($folderExists) {
'name' => 'logo.svg',
'message' => 'Saved',
'url' => 'imageUrl',
'serverCssUrl' => 'serverCss'
],
'status' => 'success'
]
Expand Down Expand Up @@ -607,24 +588,13 @@ public function testUndo() {
->method('undo')
->with('MySetting')
->willReturn('MyValue');
$this->scssCacher
->expects($this->once())
->method('getCachedSCSS')
->with('core', '/core/css/css-variables.scss')
->willReturn('/core/css/someHash-css-variables.scss');
$this->urlGenerator
->expects($this->once())
->method('linkTo')
->with('', '/core/css/someHash-css-variables.scss')
->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');

$expected = new DataResponse(
[
'data' =>
[
'value' => 'MyValue',
'message' => 'Saved',
'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
'message' => 'Saved'
],
'status' => 'success'
]
Expand All @@ -651,24 +621,13 @@ public function testUndoDelete($value, $filename) {
->method('undo')
->with($value)
->willReturn($value);
$this->scssCacher
->expects($this->once())
->method('getCachedSCSS')
->with('core', '/core/css/css-variables.scss')
->willReturn('/core/css/someHash-css-variables.scss');
$this->urlGenerator
->expects($this->once())
->method('linkTo')
->with('', '/core/css/someHash-css-variables.scss')
->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');

$expected = new DataResponse(
[
'data' =>
[
'value' => $value,
'message' => 'Saved',
'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
],
'status' => 'success'
]
Expand Down Expand Up @@ -743,53 +702,6 @@ public function testGetLoginBackground() {
@$this->assertEquals($expected, $this->themingController->getImage('background'));
}


public function testGetStylesheet() {
$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn(\OC::$SERVERROOT . '/theming');
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->any())->method('getName')->willReturn('theming.css');
$file->expects($this->any())->method('getMTime')->willReturn(42);
$file->expects($this->any())->method('getContent')->willReturn('compiled');
$this->scssCacher->expects($this->once())->method('process')->willReturn(true);
$this->scssCacher->expects($this->once())->method('getCachedCSS')->willReturn($file);

$response = new Http\FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$response->cacheFor(86400);

$actual = $this->themingController->getStylesheet();
$this->assertEquals($response, $actual);
}

public function testGetStylesheetFails() {
$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn(\OC::$SERVERROOT . '/theming');
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->any())->method('getName')->willReturn('theming.css');
$file->expects($this->any())->method('getMTime')->willReturn(42);
$file->expects($this->any())->method('getContent')->willReturn('compiled');
$this->scssCacher->expects($this->once())->method('process')->willReturn(true);
$this->scssCacher->expects($this->once())->method('getCachedCSS')->willThrowException(new NotFoundException());
$response = new Http\NotFoundResponse();

$actual = $this->themingController->getStylesheet();
$this->assertEquals($response, $actual);
}

public function testGetStylesheetOutsideServerroot() {
$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn('/outside/serverroot/theming');
$file = $this->createMock(ISimpleFile::class);
$file->expects($this->any())->method('getName')->willReturn('theming.css');
$file->expects($this->any())->method('getMTime')->willReturn(42);
$file->expects($this->any())->method('getContent')->willReturn('compiled');
$this->scssCacher->expects($this->once())->method('process')->with('/outside/serverroot/theming', 'css/theming.scss', 'theming')->willReturn(true);
$this->scssCacher->expects($this->once())->method('getCachedCSS')->willReturn($file);

$response = new Http\FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$response->cacheFor(86400);

$actual = $this->themingController->getStylesheet();
$this->assertEquals($response, $actual);
}

public function testGetManifest() {
$this->config
->expects($this->once())
Expand Down
Loading

0 comments on commit 38e99e3

Please sign in to comment.