N°2982 - fix delta xml variables not used in themes

This commit is contained in:
odain
2021-04-12 22:14:00 +02:00
parent c0f5906dce
commit 903afff687
10 changed files with 235 additions and 71 deletions

View File

@@ -119,4 +119,75 @@ class ItopTestCase extends TestCase
return $method->invokeArgs($oObject, $aArgs);
}
public function RecurseRmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir.DIRECTORY_SEPARATOR.$object))
$this->RecurseRmdir($dir.DIRECTORY_SEPARATOR.$object);
else
unlink($dir.DIRECTORY_SEPARATOR.$object);
}
}
rmdir($dir);
}
}
public function CreateTmpdir() {
$sTmpDir=tempnam(sys_get_temp_dir(),'');
if (file_exists($sTmpDir))
{
unlink($sTmpDir);
}
mkdir($sTmpDir);
if (is_dir($sTmpDir))
{
return $sTmpDir;
}
return sys_get_temp_dir();
}
public function RecurseMkdir($sDir){
if (strpos($sDir, DIRECTORY_SEPARATOR) === 0){
$sPath = DIRECTORY_SEPARATOR;
} else {
$sPath = "";
}
foreach (explode(DIRECTORY_SEPARATOR, $sDir) as $sSubDir){
if (($sSubDir === '..')) {
break;
}
if (( trim($sSubDir) === '' ) || ( $sSubDir === '.' )) {
continue;
}
$sPath .= $sSubDir . DIRECTORY_SEPARATOR;
if (!is_dir($sPath)) {
var_dump($sPath);
@mkdir($sPath);
}
}
}
public function RecurseCopy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->RecurseCopy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file);
}
else {
copy($src . DIRECTORY_SEPARATOR . $file,$dst . DIRECTORY_SEPARATOR . $file);
}
}
}
closedir($dir);
}
}

View File

@@ -27,13 +27,13 @@ class ThemeHandlerTest extends ItopTestCase
$this->oCompileCSSServiceMock = $this->createMock('CompileCSSService');
ThemeHandler::mockCompileCSSService($this->oCompileCSSServiceMock);
$this->sTmpDir = $this->tmpdir();
$this->sTmpDir = $this->CreateTmpdir();
$this->aDirsToCleanup[] = $this->sTmpDir;
$this->recurseMkdir($this->sTmpDir."/branding/themes/basque-red");
$this->sCssPath = $this->sTmpDir.'/branding/themes/basque-red/main.css';
$this->sJsonThemeParamFile = $this->sTmpDir.'/branding/themes/basque-red/theme-parameters.json';
$this->recurse_copy(APPROOT."/test/application/theme-handler/expected/css", $this->sTmpDir."/branding/css");
$this->RecurseCopy(APPROOT."/test/application/theme-handler/expected/css", $this->sTmpDir."/branding/css");
}
public function tearDown()
@@ -43,56 +43,10 @@ class ThemeHandlerTest extends ItopTestCase
foreach ($this->aDirsToCleanup as $sDir)
{
echo $sDir;
$this->rrmdir($sDir);
$this->RecurseRmdir($sDir);
}
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
$this->rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
function tmpdir() {
$sTmpfile=tempnam(sys_get_temp_dir(),'');
if (file_exists($sTmpfile))
{
unlink($sTmpfile);
}
mkdir($sTmpfile);
if (is_dir($sTmpfile))
{
return $sTmpfile;
}
return sys_get_temp_dir();
}
public function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
/**
* Test used to be notified by CI when precompiled styles are not up to date anymore in code repository.
*
@@ -151,8 +105,7 @@ class ThemeHandlerTest extends ItopTestCase
$aThemeParameters = [
'variables' => [],
'imports_utility' => [],
'stylesheets' => [],
'precompiled_stylesheet' => $sPrecompiledStylesheetUri,
'stylesheets' => []
];
/** @var \DOMNodeList $oVariables */
@@ -393,7 +346,7 @@ JSON;
$sAbsoluteImagePath = APPROOT.'test/application/theme-handler/copied/testimages/';
$this->recurseMkdir($sAbsoluteImagePath);
$this->aDirsToCleanup[] = dirname($sAbsoluteImagePath);
$this->recurse_copy(APPROOT.'test/application/theme-handler/expected/testimages/', $sAbsoluteImagePath);
$this->RecurseCopy(APPROOT.'test/application/theme-handler/expected/testimages/', $sAbsoluteImagePath);
//change approot-relative in css-variable to use absolute path
$sCssVarPath = $this->sTmpDir."/branding/css/DO_NOT_CHANGE.css-variables.scss";

View File

@@ -18,12 +18,21 @@ class MFCompilerTest extends ItopTestCase {
/** @var \MFCompiler */
private $oMFCompiler;
public function setUp()
{
private $sTmpDir;
public function setUp() {
parent::setUp();
require_once(APPROOT.'setup/compiler.class.inc.php');
require_once(APPROOT.'setup/modelfactory.class.inc.php');
require_once(__DIR__.'/SubMFCompiler.php');
$this->oMFCompiler = new MFCompiler($this->createMock(\ModelFactory::class), '');
$this->sTmpDir = $this->CreateTmpdir();
$this->oMFCompiler = new SubMFCompiler($this->createMock(\ModelFactory::class), '');
}
public function tearDown() {
parent::tearDown();
$this->RecurseRmdir($this->sTmpDir);
}
public static function Init(){
@@ -133,5 +142,46 @@ class MFCompilerTest extends ItopTestCase {
];
}
public function testCompileThemes(){
$sXmlDataCustoFilePath = realpath(__DIR__ . '/ressources/datamodels/datamodel-branding.xml');
$oDom = new MFDocument();
$oDom->load($sXmlDataCustoFilePath);
/** @var \MFElement $oBrandingNode */
$oBrandingNode = $oDom->GetNodes('branding')->item(0);
$this->RecurseMkdir($this->sTmpDir.'/branding/themes/fullmoon/');
file_put_contents($this->sTmpDir.'/branding/themes/fullmoon/main.css', "");
$aImportsPaths = array(
APPROOT.'css/',
APPROOT.'css/backoffice/main.scss',
$this->sTmpDir.'//',
);
$aThemeParameters = [
'variables' => [
'ibo-page-banner--background-color' => '$ibo-color-red-600',
'ibo-page-banner--text-color' => '$ibo-color-red-100',
'ibo-page-banner--text-content' => '"THIS IS A TEST INSTANCE"',
],
'imports_variable' => [ 'style2' => 'style2.scss'],
'imports_utility' => [ 'style1' => 'style1.scss', 'style3' => 'style3.scss'],
'stylesheets' => [
"fullmoon" => '../css/backoffice/main.scss',
"environment-banner" => '../css/backoffice/themes/page-banner.scss'
],
];
$oThemeHandlerService = $this->createMock(\ThemeHandlerService::class);
$oThemeHandlerService->expects($this->exactly(1))
->method("CompileTheme")
->with("fullmoon", true, $this->oMFCompiler->GetCompilationTimeStamp(), $aThemeParameters, $aImportsPaths, $this->sTmpDir . '/');
//CompileTheme($sThemeId, $bSetup = false, $sSetupCompilationTimestamp="", $aThemeParameters = null, $aImportsPaths = null, $sWorkingPath = null)
MFCompiler::setThemeHandlerService($oThemeHandlerService);
$this->oMFCompiler->CompileThemes($oBrandingNode, $this->sTmpDir);
}
}

View File

@@ -0,0 +1,14 @@
<?php
/**
* Class SubMFCompiler: used to call a protected method for testing purpose
*/
class SubMFCompiler extends MFCompiler {
public function CompileThemes($oBrandingNode, $sTempTargetDir) {
return parent::CompileThemes($oBrandingNode, $sTempTargetDir);
}
public function GetCompilationTimeStamp(){
return $this->sCompilationTimeStamp;
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
<branding>
<themes>
<theme id="fullmoon" _delta="define">
<variables>
<variable id="ibo-page-banner--background-color">$ibo-color-red-600</variable>
<variable id="ibo-page-banner--text-color">$ibo-color-red-100</variable>
<variable id="ibo-page-banner--text-content">"THIS IS A TEST INSTANCE"</variable>
</variables>
<imports>
<import id="style1">style1.scss</import>
<import id="style2" xsi:type="variables">style2.scss</import>
<import id="style3" xsi:type="utility">style3.scss</import>
</imports>
<stylesheets>
<stylesheet id="fullmoon">../css/backoffice/main.scss</stylesheet>
<stylesheet id="environment-banner">../css/backoffice/themes/page-banner.scss</stylesheet>
</stylesheets>
<precompiled_stylesheet>itop-structure/precompiled-themes/test-red/main.css</precompiled_stylesheet>
</theme>
</themes>
</branding>
</itop_design>