Skip to content

Commit

Permalink
Fix Theme Management and write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Sep 20, 2024
1 parent 32e8ff5 commit 4e8f6d3
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 48 deletions.
2 changes: 2 additions & 0 deletions app/Core/Bootstrap/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ protected function registerBaseServiceProviders()
$this->register(new \Leantime\Core\Providers\RateLimiter($this));
$this->register(new \Leantime\Core\Providers\Db($this));
$this->register(new \Leantime\Core\Providers\Language($this));

$this->register(new \Leantime\Core\Providers\Theme($this));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions app/Core/Providers/Theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Leantime\Core\Providers;

use Illuminate\Support\ServiceProvider;

class Theme extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(\Leantime\Core\Theme::class, \Leantime\Core\Theme::class);
$this->app->alias(\Leantime\Core\Theme::class, "themne");
}


}
67 changes: 39 additions & 28 deletions app/Core/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,20 @@ class Theme
private Environment $config;

/**
* @var appSettings
* @var Setting
*/
private AppSettings $settings;
private Setting $settingsRepo;

/**
* @var language
*/
private Language $language;

/**
* @var language
*/
private AppSettings $appSettings;

/**
* @var array|false
*/
Expand Down Expand Up @@ -136,15 +141,17 @@ class Theme
* __construct - Constructor
*/
public function __construct(
environment $config,
appSettings $settings,
Environment $config,
Setting $settingsRepo,
Language $language,
array $iniData = []
AppSettings $appSettings,
) {
$this->config = $config;
$this->settings = $settings;
$this->settingsRepo = $settingsRepo;
$this->iniData = [];
$this->language = $language;
$this->appSettings = $appSettings;

}

/**
Expand All @@ -164,10 +171,9 @@ public function getAvailableColorSchemes(): array
"secondaryColor" => $this->iniData["general"]["secondaryColor"] ?? $this->colorSchemes["leantime2_0"]["secondaryColor"],
);

$settingsRepo = app()->make(Setting::class);
$primaryColor = $settingsRepo->getSetting("companysettings.primarycolor") ? $settingsRepo->getSetting("companysettings.primarycolor") : null;
$secondaryColor = $settingsRepo->getSetting("companysettings.secondarycolor") ? $settingsRepo->getSetting("companysettings.secondarycolor") : null;
;
$primaryColor = $this->settingsRepo->getSetting("companysettings.primarycolor") ? $this->settingsRepo->getSetting("companysettings.primarycolor") : null;
$secondaryColor = $this->settingsRepo->getSetting("companysettings.secondarycolor") ? $this->settingsRepo->getSetting("companysettings.secondarycolor") : null;

$parsedColorSchemes["companyColors"] = array(
"name" => "label.companyColors",
"primaryColor" => $primaryColor ?? $this->config->primarycolor ?? $parsedColorSchemes["themeDefault"]["primaryColor"],
Expand Down Expand Up @@ -204,8 +210,7 @@ public function getActive(): string
//This is an active logged in session.
if (Auth::isLoggedIn()) {
//User is logged in, we don't have a theme yet, check settings
$settingsRepo = app()->make(Setting::class);
$theme = $settingsRepo->getSetting("usersettings." . session("userdata.id") . ".theme");
$theme = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".theme");
if ($theme !== false) {
$this->setActive($theme);
return $theme;
Expand Down Expand Up @@ -246,8 +251,7 @@ public function getColorMode()

if (Auth::isLoggedIn()) {
//User is logged in, we don't have a theme yet, check settings
$settingsRepo = app()->make(Setting::class);
$colorMode = $settingsRepo->getSetting("usersettings." . session("userdata.id") . ".colorMode");
$colorMode = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".colorMode");
if ($colorMode !== false) {
$this->setColorMode($colorMode);
return $colorMode;
Expand Down Expand Up @@ -284,8 +288,8 @@ public function getColorScheme()

if (Auth::isLoggedIn()) {
//User is logged in, we don't have a theme yet, check settings
$settingsRepo = app()->make(Setting::class);
$colorScheme = $settingsRepo->getSetting("usersettings." . session("userdata.id") . ".colorScheme");

$colorScheme = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".colorScheme");
if ($colorScheme !== false) {
$this->setColorScheme($colorScheme);
return $colorScheme;
Expand All @@ -297,9 +301,16 @@ public function getColorScheme()
return $_COOKIE['colorScheme'];
}

//Return default
$this->setColorScheme('themeDefault');
return 'themeDefault';
if(!empty($this->config->primarycolor) && !empty($this->config->secondarycolor)) {
//Return default
$this->setColorScheme('companyColors');
return 'companyColors';
}else{
//Return default
$this->setColorScheme('themeDefault');
return 'themeDefault';
}

}

/**
Expand All @@ -318,9 +329,9 @@ public function getFont()
}

if (Auth::isLoggedIn()) {

//User is logged in, we don't have a theme yet, check settings
$settingsRepo = app()->make(Setting::class);
$themeFont = $settingsRepo->getSetting("usersettings." . session("userdata.id") . ".themeFont");
$themeFont = $this->settingsRepo->getSetting("usersettings." . session("userdata.id") . ".themeFont");
if ($themeFont !== false) {
$this->setFont($themeFont);
return $themeFont;
Expand Down Expand Up @@ -624,11 +635,11 @@ private function getAssetPath(string $fileName, string $assetType): string|bool
}

if (file_exists($this->getDir() . '/' . $assetType . '/' . $fileName . '.min.' . $assetType)) {
return $this->getUrl() . '/' . $assetType . '/' . $fileName . '.min.' . $assetType . '?v=' . $this->settings->appVersion;
return $this->getUrl() . '/' . $assetType . '/' . $fileName . '.min.' . $assetType . '?v=' . $this->appSettings->appVersion;
}

if (file_exists($this->getDir() . '/' . $assetType . '/' . $fileName . '.' . $assetType)) {
return $this->getUrl() . '/' . $assetType . '/' . $fileName . '.' . $assetType . '?v=' . $this->settings->appVersion;
return $this->getUrl() . '/' . $assetType . '/' . $fileName . '.' . $assetType . '?v=' . $this->appSettings->appVersion;
}

return false;
Expand Down Expand Up @@ -704,8 +715,8 @@ public function getLogoUrl(): string|false

$logoPath = false;
if (session()->exists("companysettings.logoPath") === false || session("companysettings.logoPath") == '') {
$settingsRepo = app()->make(Setting::class);
$logoPath = $settingsRepo->getSetting("companysettings.logoPath");

$logoPath = $this->settingsRepo->getSetting("companysettings.logoPath");

if (
$logoPath !== false &&
Expand Down Expand Up @@ -776,8 +787,8 @@ public function setCompanyColors()
{

if (! session()->exists("usersettings.colors.primaryColor")) {
$settingsRepo = app()->make(Setting::class);
$primaryColor = $settingsRepo->getSetting("companysettings.primarycolor");

$primaryColor = $this->settingsRepo->getSetting("companysettings.primarycolor");

if ($primaryColor !== false) {
session(["usersettings.colors.primaryColor" => $primaryColor]);
Expand All @@ -787,7 +798,7 @@ public function setCompanyColors()
session(["usersettings.colors.secondaryColor" => $this->config->secondaryColor]);
}

$secondaryColor = $settingsRepo->getSetting("companysettings.secondaryColor");
$secondaryColor = $this->settingsRepo->getSetting("companysettings.secondaryColor");
if ($secondaryColor !== false) {
session(["usersettings.colors.secondaryColor" => $secondaryColor]);
}
Expand Down
5 changes: 1 addition & 4 deletions app/Domain/Menu/Repositories/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,12 @@ public function getMenuStructure(string $menuType = ''): array
$language = $this->language;
$filter = "menuStructures.$menuType";

$menuCollection = [];
$menuCollection[$menuType] = self::dispatch_filter(
$this->menuStructures[$menuType] = self::dispatch_filter(
$filter,
$this->menuStructures[$menuType],
['menuType' => $menuType]
);



$menuStructure = $this->menuStructures[$menuType];

if (session()->exists("usersettings.submenuToggle") === false || is_array(session("usersettings.submenuToggle")) === false) {
Expand Down
4 changes: 2 additions & 2 deletions app/Views/Templates/sections/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
<style id="colorSchemeSetter">
@foreach ($accents as $accent)
@if($accent !== false)
:root {
--accent {{ $loop->iteration }}: {{{ $accent }}};
:root {
--accent{{ $loop->iteration }}: {{{ $accent }}};
}
@endif
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion public/assets/css/components/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ body .croppie-container {

.selectable .selectableContent {
border: 1px solid var(--main-border-color);
padding:5px 10px 2px 10px;
padding:10px;
border-radius: var(--element-radius);
background: var(--tab-background);
color: var(--tab-color);
Expand Down
103 changes: 103 additions & 0 deletions tests/Unit/app/Core/ThemeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Unit\app\Domain\Menu\Repositories;

use Codeception\Test\Unit;
use Leantime\Config\Config;
use Leantime\Core\Configuration\AppSettings;
use Leantime\Core\Configuration\Environment;
use Leantime\Core\Events\EventDispatcher;
use Leantime\Core\Language;
use Leantime\Core\Theme;
use Leantime\Domain\Menu\Repositories\Menu;
use Leantime\Domain\Setting\Repositories\Setting;
use Leantime\Domain\Tickets\Services\Tickets;

class ThemeTest extends Unit
{

use \Codeception\Test\Feature\Stub;

/**
* The test object
*
* @var Theme
*/
protected $theme;

protected $settingsRepoMock;

protected $languageMock;

protected $configMock;

protected $appSettingsMock;

protected function _before()
{

//Mock classes
$this->settingsRepoMock = $this->make(Setting::class, [

]);
$this->languageMock = $this->make(Language::class, [

]);

$this->configMock = $this->make(Environment::class, [
"primarycolor"=> "#123",
"secondarycolor"=> "#123",

]);

$this->appSettingsMock = $this->make(AppSettings::class, [
"appVersion" => "123",

]);

//Load class to be tested
$this->theme = new Theme(
settingsRepo: $this->settingsRepoMock,
language: $this->languageMock,
config:$this->configMock,
appSettings: $this->appSettingsMock
);

}

protected function _after()
{
$this->theme = null;
}

//Write tests below

/**
* Test GetMenuTypes method
*/
public function testGetDefaultColorSchemeWithColorEnvSet() {

$colorScheme = $this->theme->getColorScheme();
$this->assertEquals("companyColors", $colorScheme);

}

/**
* Test GetMenuTypes method
*/
public function testGetDefaultColorSchemeWithoutEnv() {

$configMock = $this->make(Environment::class, []);

$theme = new Theme(
settingsRepo: $this->settingsRepoMock,
language: $this->languageMock,
config: $configMock,
appSettings: $this->appSettingsMock
);
$colorScheme = $theme->getColorScheme();
$this->assertEquals("themeDefault", $colorScheme);

}

}
29 changes: 16 additions & 13 deletions tests/Unit/app/Domain/Menu/Repositories/MenuRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ protected function _before()
ticketsService:$ticketService
);

//Creating a test event
//Unsetting a menu item (replacing would work the same way
\Leantime\Core\Events\EventDispatcher::add_filter_listener("leantime.domain.menu.repositories.menu.getMenuStructure.menuStructures.company", function($menu){

if(isset($menu[15]) && isset($menu[15]['submenu'])) {
unset($menu[15]['submenu'][20]);
}

return $menu;

}, 10);
}

protected function _after()
Expand Down Expand Up @@ -116,8 +105,19 @@ public function testGetInvalidMenuStructure()
public function testGetFilteredMenuStructure()
{

\Leantime\Core\Events\EventDispatcher::add_filter_listener("leantime.domain.menu.repositories.menu.getMenuStructure.menuStructures.company", function($menu){

if(isset($menu[15]) && isset($menu[15]['submenu'])) {
unset($menu[15]['submenu'][20]);
}

return $menu;

}, 10);

$fullMenuStructure = $this->menu->getMenuStructure('company');
$this->assertIsArray($fullMenuStructure[15]['submenu']);

$this->assertFalse(isset($fullMenuStructure[15]['submenu'][20]), "menu item was not removed");

}
Expand All @@ -127,7 +127,10 @@ public function testInjectNewProjectMenuType()

\Leantime\Core\Events\EventDispatcher::add_filter_listener("leantime.domain.menu.repositories.menu.getMenuStructure.menuStructures", function($menuStructure){

$testStructure = ["item" => "myNewMenu"];
$testStructure = [
10 => ["item" => "myNewMenu", "type"=>"item"]
];

$menuStructure["testType"] = $testStructure;

return $menuStructure;
Expand All @@ -136,7 +139,7 @@ public function testInjectNewProjectMenuType()

$fullMenuStructure = $this->menu->getMenuStructure('testType');
$this->assertIsArray($fullMenuStructure);
$this->assertEquals("muNewMenu", $fullMenuStructure["item"]);
$this->assertEquals("myNewMenu", $fullMenuStructure[10]["item"]);

}
}

0 comments on commit 4e8f6d3

Please sign in to comment.