mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
ormStyle: Fix icon URL including the app. root in the MetaModel to ease usage with load balancers
This commit is contained in:
@@ -445,10 +445,10 @@ abstract class MetaModel
|
||||
|
||||
/**
|
||||
* @param string $sClass
|
||||
* @param bool $bImgTag
|
||||
* @param string $sMoreStyles
|
||||
* @param bool $bImgTag Whether to surround the icon URL with an HTML IMG tag or not
|
||||
* @param string $sMoreStyles Additional inline CSS style to add to the IMG tag. Only used if $bImgTag is set to true
|
||||
*
|
||||
* @return string
|
||||
* @return string Absolute URL the class icon
|
||||
* @throws \CoreException
|
||||
*/
|
||||
final public static function GetClassIcon($sClass, $bImgTag = true, $sMoreStyles = '')
|
||||
@@ -459,7 +459,7 @@ abstract class MetaModel
|
||||
if (array_key_exists('style', self::$m_aClassParams[$sClass])) {
|
||||
/** @var ormStyle $oStyle */
|
||||
$oStyle = self::$m_aClassParams[$sClass]['style'];
|
||||
$sIcon = $oStyle->GetIcon();
|
||||
$sIcon = $oStyle->GetIconAsAbsUrl();
|
||||
}
|
||||
if (strlen($sIcon) == 0) {
|
||||
$sParentClass = self::GetParentPersistentClass($sClass);
|
||||
@@ -494,7 +494,7 @@ abstract class MetaModel
|
||||
$oStyle = new ormStyle("ibo-class-style--$sClass", "ibo-class-style-alt--$sClass");
|
||||
}
|
||||
|
||||
if ((strlen($oStyle->GetMainColor()) > 0) && (strlen($oStyle->GetComplementaryColor()) > 0) && (strlen($oStyle->GetIcon()) > 0)) {
|
||||
if ((strlen($oStyle->GetMainColor()) > 0) && (strlen($oStyle->GetComplementaryColor()) > 0) && (strlen($oStyle->GetIconAsRelPath()) > 0)) {
|
||||
// all the parameters are set, no need to search in the parent classes
|
||||
return $oStyle;
|
||||
}
|
||||
@@ -512,10 +512,10 @@ abstract class MetaModel
|
||||
$oStyle->SetComplementaryColor($oParentStyle->GetComplementaryColor());
|
||||
$oStyle->SetAltStyleClass($oParentStyle->GetAltStyleClass());
|
||||
}
|
||||
if (strlen($oStyle->GetIcon()) == 0) {
|
||||
$oStyle->SetIcon($oParentStyle->GetIcon());
|
||||
if (strlen($oStyle->GetIconAsRelPath()) == 0) {
|
||||
$oStyle->SetIcon($oParentStyle->GetIconAsRelPath());
|
||||
}
|
||||
if ((strlen($oStyle->GetMainColor()) > 0) && (strlen($oStyle->GetComplementaryColor()) > 0) && (strlen($oStyle->GetIcon()) > 0)) {
|
||||
if ((strlen($oStyle->GetMainColor()) > 0) && (strlen($oStyle->GetComplementaryColor()) > 0) && (strlen($oStyle->GetIconAsRelPath()) > 0)) {
|
||||
// all the parameters are set, no need to search in the parent classes
|
||||
return $oStyle;
|
||||
}
|
||||
@@ -523,7 +523,7 @@ abstract class MetaModel
|
||||
$sParentClass = self::GetParentPersistentClass($sParentClass);
|
||||
}
|
||||
|
||||
if ((strlen($oStyle->GetMainColor()) == 0) && (strlen($oStyle->GetComplementaryColor()) == 0) && (strlen($oStyle->GetIcon()) == 0)) {
|
||||
if ((strlen($oStyle->GetMainColor()) == 0) && (strlen($oStyle->GetComplementaryColor()) == 0) && (strlen($oStyle->GetIconAsRelPath()) == 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ class ormStyle
|
||||
protected $sAltStyleClass;
|
||||
/** @var string */
|
||||
protected $sDecorationClasses;
|
||||
/** @var string */
|
||||
/** @var string Relative path (from current environment) to the icon */
|
||||
protected $sIcon;
|
||||
|
||||
/**
|
||||
@@ -41,7 +41,7 @@ class ormStyle
|
||||
$this->sStyleClass = $sStyleClass;
|
||||
$this->sAltStyleClass = $sAltStyleClass;
|
||||
$this->sDecorationClasses = $sDecorationClasses;
|
||||
$this->sIcon = $sIcon;
|
||||
$this->SetIcon($sIcon);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,14 +139,6 @@ class ormStyle
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetIcon(): ?string
|
||||
{
|
||||
return $this->sIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $sIcon
|
||||
*
|
||||
@@ -154,8 +146,30 @@ class ormStyle
|
||||
*/
|
||||
public function SetIcon(?string $sIcon)
|
||||
{
|
||||
$this->sIcon = $sIcon;
|
||||
$this->sIcon = (strlen($sIcon) === 0) ? null : $sIcon;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see static::$sIcon
|
||||
* @return string|null Relative path (from the current environment) of the icon
|
||||
*/
|
||||
public function GetIconAsRelPath(): ?string
|
||||
{
|
||||
return $this->sIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see static::$sIcon
|
||||
* @return string|null Absolute URL of the icon
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function GetIconAsAbsUrl(): ?string
|
||||
{
|
||||
if (is_null($this->sIcon)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return utils::GetAbsoluteUrlModulesRoot().$this->sIcon;
|
||||
}
|
||||
}
|
||||
@@ -1222,7 +1222,7 @@ EOF
|
||||
$sStyleCSSAltClass = "ibo-class-style-alt--$sClass";
|
||||
if (($sIcon = $oStyle->GetChildText('icon')) && (strlen($sIcon) > 0)) {
|
||||
$sIcon = $sModuleRelativeDir.'/'.$sIcon;
|
||||
$sIcon = ", utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
|
||||
$sIcon = ", '$sIcon'";
|
||||
}
|
||||
$aClassParams['style'] = "new ormStyle('$sStyleCSSClass', '$sStyleCSSAltClass', '$sMainColor', '$sComplementaryColor', null $sIcon)";
|
||||
}
|
||||
|
||||
137
test/core/ormStyleTest.php
Normal file
137
test/core/ormStyleTest.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/*!
|
||||
* @copyright Copyright (C) 2010-2021 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Core;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||
use ormStyle;
|
||||
use utils;
|
||||
|
||||
/**
|
||||
* Tests of the ormStyle class
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
class ormStyleTest extends ItopTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $sRegularClass
|
||||
* @param string $sAlternativeClass
|
||||
* @param string|null $sMainColor
|
||||
* @param string|null $sComplementaryColor
|
||||
* @param string|null $sDecorationClasses
|
||||
* @param string|null $sIconRelPath
|
||||
*
|
||||
* @covers ormStyle::GetStyleClass
|
||||
* @covers ormStyle::GetAltStyleClass
|
||||
* @covers ormStyle::GetMainColor
|
||||
* @covers ormStyle::GetComplementaryColor
|
||||
* @covers ormStyle::GetDecorationClasses
|
||||
* @covers ormStyle::GetIconAsRelPath
|
||||
* @covers ormStyle::SetStyleClass
|
||||
* @covers ormStyle::SetAltStyleClass
|
||||
* @covers ormStyle::SetMainColor
|
||||
* @covers ormStyle::SetComplementaryColor
|
||||
* @covers ormStyle::SetDecorationClasses
|
||||
* @covers ormStyle::SetIcon
|
||||
*
|
||||
* @dataProvider BaseSetsProvider
|
||||
*/
|
||||
public function testNonAlteringMethods(string $sRegularClass, string $sAlternativeClass, ?string $sMainColor, ?string $sComplementaryColor, ?string $sDecorationClasses, ?string $sIconRelPath)
|
||||
{
|
||||
$oStyle = new ormStyle($sRegularClass, $sAlternativeClass, $sMainColor, $sComplementaryColor, $sDecorationClasses, $sIconRelPath);
|
||||
|
||||
// Test getters straight from instantiation
|
||||
$this->assertEquals($sRegularClass, $oStyle->GetStyleClass());
|
||||
$this->assertEquals($sAlternativeClass, $oStyle->GetAltStyleClass());
|
||||
$this->assertEquals($sMainColor, $oStyle->GetMainColor());
|
||||
$this->assertEquals($sComplementaryColor, $oStyle->GetComplementaryColor());
|
||||
$this->assertEquals($sDecorationClasses, $oStyle->GetDecorationClasses());
|
||||
$this->assertEquals($sIconRelPath, $oStyle->GetIconAsRelPath());
|
||||
|
||||
// Test that setters don't change passed value
|
||||
$oStyle->SetStyleClass($sRegularClass);
|
||||
$this->assertEquals($sRegularClass, $oStyle->GetStyleClass());
|
||||
|
||||
$oStyle->SetAltStyleClass($sAlternativeClass);
|
||||
$this->assertEquals($sAlternativeClass, $oStyle->GetAltStyleClass());
|
||||
|
||||
$oStyle->SetMainColor($sMainColor);
|
||||
$this->assertEquals($sMainColor, $oStyle->GetMainColor());
|
||||
|
||||
$oStyle->SetComplementaryColor($sComplementaryColor);
|
||||
$this->assertEquals($sComplementaryColor, $oStyle->GetComplementaryColor());
|
||||
|
||||
$oStyle->SetDecorationClasses($sDecorationClasses);
|
||||
$this->assertEquals($sDecorationClasses, $oStyle->GetDecorationClasses());
|
||||
|
||||
$oStyle->SetIcon($sIconRelPath);
|
||||
$this->assertEquals($sIconRelPath, $oStyle->GetIconAsRelPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sRegularClass
|
||||
* @param string $sAlternativeClass
|
||||
* @param string|null $sMainColor
|
||||
* @param string|null $sComplementaryColor
|
||||
* @param string|null $sDecorationClasses
|
||||
* @param string|null $sIconRelPath
|
||||
*
|
||||
* @covers ormStyle::GetIconAsAbsUrl
|
||||
*
|
||||
* @dataProvider BaseSetsProvider
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function testGetIconAsAbsUrl(string $sRegularClass, string $sAlternativeClass, ?string $sMainColor, ?string $sComplementaryColor, ?string $sDecorationClasses, ?string $sIconRelPath)
|
||||
{
|
||||
$oStyle = new ormStyle($sRegularClass, $sAlternativeClass, $sMainColor, $sComplementaryColor, $sDecorationClasses, $sIconRelPath);
|
||||
|
||||
$sExpectedIconAbsUrl = (is_null($sIconRelPath) || (strlen($sIconRelPath) === 0)) ? null : utils::GetAbsoluteUrlModulesRoot().$sIconRelPath;
|
||||
$this->assertEquals($sExpectedIconAbsUrl, $oStyle->GetIconAsAbsUrl());
|
||||
}
|
||||
|
||||
public function BaseSetsProvider(): array
|
||||
{
|
||||
return [
|
||||
'Complete style with icon from /images folder' => [
|
||||
'regular-class',
|
||||
'alternative-class',
|
||||
'#ABCDEF',
|
||||
'#123456',
|
||||
'fas fa-user',
|
||||
'../../images/icons/icons8-organization.svg',
|
||||
],
|
||||
'Complete style with icon from module folder' => [
|
||||
'regular-class',
|
||||
'alternative-class',
|
||||
'#ABCDEF',
|
||||
'#123456',
|
||||
'fas fa-user',
|
||||
'images/user-request.png',
|
||||
],
|
||||
'Style with empty icon path' => [
|
||||
'regular-class',
|
||||
'alternative-class',
|
||||
'#ABCDEF',
|
||||
'#123456',
|
||||
'fas fa-user',
|
||||
'',
|
||||
],
|
||||
'Style with null icon path' => [
|
||||
'regular-class',
|
||||
'alternative-class',
|
||||
'#ABCDEF',
|
||||
'#123456',
|
||||
'fas fa-user',
|
||||
null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user