mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-20 00:58:48 +02:00
bug2996: include images in precompiled signature
This commit is contained in:
@@ -14,6 +14,7 @@ class ThemeHandlerTest extends ItopTestCase
|
||||
private $cssPath;
|
||||
private $jsonThemeParamFile;
|
||||
private $tmpDir;
|
||||
private $aDirsToCleanup=array();
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
@@ -25,6 +26,7 @@ class ThemeHandlerTest extends ItopTestCase
|
||||
ThemeHandler::mockCompileCSSService($this->compileCSSServiceMock);
|
||||
|
||||
$this->tmpDir=$this->tmpdir();
|
||||
$aDirsToCleanup[] = $this->tmpDir;
|
||||
|
||||
if (!is_dir($this->tmpDir ."/branding"))
|
||||
{
|
||||
@@ -37,6 +39,30 @@ class ThemeHandlerTest extends ItopTestCase
|
||||
$this->recurse_copy(APPROOT."/test/application/theme-handler/expected/css", $this->tmpDir."/branding/css");
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
foreach ($this->aDirsToCleanup as $dir)
|
||||
{
|
||||
$this->rrmdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
$tmpfile=tempnam(sys_get_temp_dir(),'');
|
||||
if (file_exists($tmpfile))
|
||||
@@ -68,11 +94,106 @@ class ThemeHandlerTest extends ItopTestCase
|
||||
closedir($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to be notified by CI when precompiled styles are not up to date anymore in code repository.
|
||||
* @param $xmlDataCusto
|
||||
* @dataProvider providePrecompiledStyleSheets
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function testValidatePrecompiledStyles($xmlDataCusto)
|
||||
{
|
||||
echo "=== datamodel custo: $xmlDataCusto\n";
|
||||
$oDom = new MFDocument();
|
||||
$oDom->load($xmlDataCusto);
|
||||
/**DOMNodeList **/$oThemeNodes=$oDom->GetNodes("/itop_design/branding/themes/theme");
|
||||
$this->assertNotNull($oThemeNodes);
|
||||
|
||||
// Parsing themes from DM
|
||||
foreach($oThemeNodes as $oTheme)
|
||||
{
|
||||
$sPrecompiledStylesheet = $oTheme->GetChildText('precompiled_stylesheet', '');
|
||||
if (empty($sPrecompiledStylesheet))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$sThemeId = $oTheme->getAttribute('id');
|
||||
|
||||
echo "=== theme: $sThemeId ===\n";
|
||||
$precompiledSig= ThemeHandler::GetSignature(dirname(__FILE__)."/../../datamodels/2.x/".$sPrecompiledStylesheet);
|
||||
echo " precompiled signature: $precompiledSig\n";
|
||||
$this->assertFalse(empty($precompiledSig), "Signature in precompiled theme '".$sThemeId."' is not retrievable (cf precompiledsheet $sPrecompiledStylesheet / datamodel $xmlDataCusto)");
|
||||
|
||||
$aThemeParameters = array(
|
||||
'variables' => array(),
|
||||
'imports' => array(),
|
||||
'stylesheets' => array(),
|
||||
'precompiled_stylesheet' => '',
|
||||
);
|
||||
|
||||
$aThemeParameters['precompiled_stylesheet'] = $sPrecompiledStylesheet;
|
||||
/** @var \DOMNodeList $oVariables */
|
||||
$oVariables = $oTheme->GetNodes('variables/variable');
|
||||
foreach($oVariables as $oVariable)
|
||||
{
|
||||
$sVariableId = $oVariable->getAttribute('id');
|
||||
$aThemeParameters['variables'][$sVariableId] = $oVariable->GetText();
|
||||
}
|
||||
|
||||
/** @var \DOMNodeList $oImports */
|
||||
$oImports = $oTheme->GetNodes('imports/import');
|
||||
foreach($oImports as $oImport)
|
||||
{
|
||||
$sImportId = $oImport->getAttribute('id');
|
||||
$aThemeParameters['imports'][$sImportId] = $oImport->GetText();
|
||||
}
|
||||
|
||||
/** @var \DOMNodeList $oStylesheets */
|
||||
$oStylesheets = $oTheme->GetNodes('stylesheets/stylesheet');
|
||||
foreach($oStylesheets as $oStylesheet)
|
||||
{
|
||||
$sStylesheetId = $oStylesheet->getAttribute('id');
|
||||
$aThemeParameters['stylesheets'][$sStylesheetId] = $oStylesheet->GetText();
|
||||
}
|
||||
$sThemeFolderPath = APPROOT.'env-production/branding/themes/'.$sThemeId.'/test';
|
||||
if (!is_dir($sThemeFolderPath))
|
||||
{
|
||||
mkdir($sThemeFolderPath);
|
||||
}
|
||||
$compiled_json_sig = ThemeHandler::ComputeSignature($aThemeParameters, array(APPROOT.'datamodels'), $sThemeFolderPath);
|
||||
echo " current signature: $compiled_json_sig\n";
|
||||
rmdir($sThemeFolderPath);
|
||||
$this->assertEquals($precompiledSig, $compiled_json_sig, "Precompiled signature does not match currently compiled one on theme '".$sThemeId."' (cf precompiledsheet $sPrecompiledStylesheet / datamodel $xmlDataCusto)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function providePrecompiledStyleSheets()
|
||||
{
|
||||
$datamodelfiles=glob(dirname(__FILE__)."/../../datamodels/2.x/**/datamodel*.xml");
|
||||
$test_set = array();
|
||||
|
||||
foreach ($datamodelfiles as $datamodelfile)
|
||||
{
|
||||
if (is_file($datamodelfile) &&
|
||||
$datamodelfile=="/var/www/html/iTop/test/application/../../datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml")
|
||||
{
|
||||
$content=file_get_contents($datamodelfile);
|
||||
if (strpos($content, "precompiled_stylesheet")!==false)
|
||||
{
|
||||
$test_set[$datamodelfile]=array($datamodelfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $test_set;
|
||||
}
|
||||
|
||||
public function testGetSignature()
|
||||
{
|
||||
$sig = ThemeHandler::GetSignature(APPROOT.'test/application/theme-handler/expected/themes/basque-red/main.css');
|
||||
$expect_sig=<<<JSON
|
||||
{"variables":"37c31105548fce44fecca5cb34e455c9","stylesheets":{"css-variables":"934888ebb4991d4c76555be6b6d1d5cc","jqueryui":"78cfafc3524dac98e61fc2460918d4e5","main":"52d8a7c5530ceb3a4d777364fa4e1eea"},"imports":[]}
|
||||
{"variables":"37c31105548fce44fecca5cb34e455c9","stylesheets":{"css-variables":"934888ebb4991d4c76555be6b6d1d5cc","jqueryui":"78cfafc3524dac98e61fc2460918d4e5","main":"52d8a7c5530ceb3a4d777364fa4e1eea"},"imports":[],"images":[]}
|
||||
JSON;
|
||||
|
||||
$this->assertEquals($expect_sig,$sig);
|
||||
@@ -182,9 +303,31 @@ JSON;
|
||||
*/
|
||||
public function testCompileThemes($ThemeParametersJson, $CompileCSSFromSASSCount, $missingFile=0, $filesTouchedRecently=0, $fileMd5sumModified=0, $fileToTest=null, $expected_maincss_path=null, $bSetup=true)
|
||||
{
|
||||
$fileToTest=$this->tmpDir.'/'.$fileToTest;
|
||||
if (is_file($this->tmpDir.'/'.$fileToTest))
|
||||
{
|
||||
$fileToTest=$this->tmpDir.'/'.$fileToTest;
|
||||
}
|
||||
else
|
||||
{
|
||||
$fileToTest=APPROOT.'/'.$fileToTest;
|
||||
}
|
||||
|
||||
$cssPath = $this->tmpDir . '/branding/themes/basque-red/main.css';
|
||||
copy(APPROOT . 'test/application/theme-handler/expected/themes/basque-red/main.css', $cssPath);
|
||||
copy(APPROOT."test/application/theme-handler/expected/themes/basque-red/main_testcompilethemes.css", $cssPath);
|
||||
|
||||
$sAbsoluteImagePath = APPROOT .'test/application/theme-handler/copied/testimages/';
|
||||
@mkdir(APPROOT .'test/application/theme-handler/copied/');
|
||||
@mkdir($sAbsoluteImagePath);
|
||||
$aDirsToCleanup[] = $sAbsoluteImagePath;
|
||||
$this->recurse_copy(APPROOT .'test/application/theme-handler/expected/testimages/', $sAbsoluteImagePath);
|
||||
|
||||
//change approot-relative in css-variable to use absolute path
|
||||
$sCssVarPath = $this->tmpDir."/branding/css/css-variables.scss";
|
||||
$sCssVariableContent = file_get_contents($sCssVarPath);
|
||||
$sLine = '$approot-relative: "' . $sAbsoluteImagePath . '" !default;';
|
||||
|
||||
$sCssVariableContent=preg_replace("/\\\$approot-relative: \"(.*)\"/", $sLine, $sCssVariableContent);
|
||||
file_put_contents($sCssVarPath, $sCssVariableContent);
|
||||
|
||||
if ($missingFile==1)
|
||||
{
|
||||
@@ -223,14 +366,16 @@ JSON;
|
||||
$modifiedVariableThemeParameterJson='{"variables":{"brand-primary1":"#C53030","hover-background-color":"#F6F6F6","icons-filter":"grayscale(1)","search-form-container-bg-color":"#4A5568"},"imports":{"css-variables":"..\/css\/css-variables.scss"},"stylesheets":{"jqueryui":"..\/css\/ui-lightness\/jqueryui.scss","main":"..\/css\/light-grey.scss"}}';
|
||||
$initialThemeParamJson='{"variables":{"brand-primary":"#C53030","hover-background-color":"#F6F6F6","icons-filter":"grayscale(1)","search-form-container-bg-color":"#4A5568"},"imports":{"css-variables":"..\/css\/css-variables.scss"},"stylesheets":{"jqueryui":"..\/css\/ui-lightness\/jqueryui.scss","main":"..\/css\/light-grey.scss"}}';
|
||||
$import_file_path = '/branding/css/css-variables.scss';
|
||||
$importmodified_maincss="test/application/theme-handler/expected/themes/basque-red/main_importmodified.css";
|
||||
$varchanged_maincss="test/application/theme-handler/expected/themes/basque-red/main_varchanged.css";
|
||||
$stylesheet_maincss="test/application/theme-handler/expected/themes/basque-red/main_stylesheet.css";
|
||||
$image_maincss="test/application/theme-handler/expected/themes/basque-red/main_imagemodified.css";
|
||||
$importmodified_maincss="test/application/theme-handler/expected/themes/basque-red/main_importmodified.css";
|
||||
$stylesheet_file_path = '/branding/css/light-grey.scss';
|
||||
$image_file_path = 'test/application/theme-handler/copied/testimages/images/green-header.gif';
|
||||
return array(
|
||||
"setup context: variables list modified without any file touched" => array($modifiedVariableThemeParameterJson, 1,0,0,0,$import_file_path, $varchanged_maincss),
|
||||
"setup context: variables list modified with files touched" => array($modifiedVariableThemeParameterJson, 1,0,1,0,$import_file_path, $varchanged_maincss, false),
|
||||
"itop page/theme loading; variables list modified sans touch de fichier" => array($modifiedVariableThemeParameterJson, 0,0,0,0,$import_file_path, $varchanged_maincss, false),
|
||||
"itop page/theme loading; variables list modified without any file touched" => array($modifiedVariableThemeParameterJson, 0,0,0,0,$import_file_path, $varchanged_maincss, false),
|
||||
//imports
|
||||
"import file missing" => array($initialThemeParamJson, 0, 1, 0, 0, $import_file_path),
|
||||
"import file touched" => array($initialThemeParamJson, 0, 0, 1, 0, $import_file_path),
|
||||
@@ -238,96 +383,81 @@ JSON;
|
||||
//stylesheets
|
||||
"stylesheets file missing" => array($initialThemeParamJson, 0, 1, 0, 0, $stylesheet_file_path),
|
||||
"stylesheets file touched" => array($initialThemeParamJson, 0, 0, 1, 0, $stylesheet_file_path),
|
||||
"stylesheets file modified" => array($initialThemeParamJson, 1, 0, 0, 1, $stylesheet_file_path, $stylesheet_maincss)
|
||||
"stylesheets file modified" => array($initialThemeParamJson, 1, 0, 0, 1, $stylesheet_file_path, $stylesheet_maincss),
|
||||
//images
|
||||
"image file missing" => array($initialThemeParamJson, 0, 1, 0, 0, $image_file_path),
|
||||
"image file touched" => array($initialThemeParamJson, 0, 0, 1, 0, $image_file_path),
|
||||
"image file modified" => array($initialThemeParamJson, 1, 0, 0, 1, $image_file_path, $image_maincss),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xmlDataCusto
|
||||
* @dataProvider providePrecompiledStyleSheets
|
||||
* @throws \Exception
|
||||
* @param $sScssFile
|
||||
*
|
||||
* @dataProvider GetAllUrlFromScssProvider
|
||||
*/
|
||||
public function testValidatePrecompiledStyles($xmlDataCusto)
|
||||
public function testGetAllUrlFromScss($sScssFile)
|
||||
{
|
||||
echo "=== datamodel custo: $xmlDataCusto\n";
|
||||
$oDom = new MFDocument();
|
||||
$oDom->load($xmlDataCusto);
|
||||
/**DOMNodeList **/$oThemeNodes=$oDom->GetNodes("/itop_design/branding/themes/theme");
|
||||
$this->assertNotNull($oThemeNodes);
|
||||
|
||||
// Parsing themes from DM
|
||||
foreach($oThemeNodes as $oTheme)
|
||||
{
|
||||
$sPrecompiledStylesheet = $oTheme->GetChildText('precompiled_stylesheet', '');
|
||||
if (empty($sPrecompiledStylesheet))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$sThemeId = $oTheme->getAttribute('id');
|
||||
|
||||
echo "=== theme: $sThemeId ===\n";
|
||||
$precompiledSig= ThemeHandler::GetSignature(dirname(__FILE__)."/../../datamodels/2.x/".$sPrecompiledStylesheet);
|
||||
echo " precompiled signature: $precompiledSig\n";
|
||||
$this->assertFalse(empty($precompiledSig), "Signature in precompiled theme '".$sThemeId."' is not retrievable (cf precompiledsheet $sPrecompiledStylesheet / datamodel $xmlDataCusto)");
|
||||
|
||||
$aThemeParameters = array(
|
||||
'variables' => array(),
|
||||
'imports' => array(),
|
||||
'stylesheets' => array(),
|
||||
'precompiled_stylesheet' => '',
|
||||
);
|
||||
|
||||
$aThemeParameters['precompiled_stylesheet'] = $sPrecompiledStylesheet;
|
||||
/** @var \DOMNodeList $oVariables */
|
||||
$oVariables = $oTheme->GetNodes('variables/variable');
|
||||
foreach($oVariables as $oVariable)
|
||||
{
|
||||
$sVariableId = $oVariable->getAttribute('id');
|
||||
$aThemeParameters['variables'][$sVariableId] = $oVariable->GetText();
|
||||
}
|
||||
|
||||
/** @var \DOMNodeList $oImports */
|
||||
$oImports = $oTheme->GetNodes('imports/import');
|
||||
foreach($oImports as $oImport)
|
||||
{
|
||||
$sImportId = $oImport->getAttribute('id');
|
||||
$aThemeParameters['imports'][$sImportId] = $oImport->GetText();
|
||||
}
|
||||
|
||||
/** @var \DOMNodeList $oStylesheets */
|
||||
$oStylesheets = $oTheme->GetNodes('stylesheets/stylesheet');
|
||||
foreach($oStylesheets as $oStylesheet)
|
||||
{
|
||||
$sStylesheetId = $oStylesheet->getAttribute('id');
|
||||
$aThemeParameters['stylesheets'][$sStylesheetId] = $oStylesheet->GetText();
|
||||
}
|
||||
$compiled_json_sig = ThemeHandler::ComputeSignature($aThemeParameters, array(APPROOT.'datamodels'));
|
||||
echo " current signature: $compiled_json_sig\n";
|
||||
$this->assertEquals($precompiledSig, $compiled_json_sig, "Precompiled signature does not match currently compiled one on theme '".$sThemeId."' (cf precompiledsheet $sPrecompiledStylesheet / datamodel $xmlDataCusto)");
|
||||
}
|
||||
|
||||
$aIncludedUrls = ThemeHandler::GetAllUrlFromScss(array('attr' => "123"),APPROOT.$sScssFile);
|
||||
$this->assertEquals(array('version1'), array_values($aIncludedUrls['aMissingVariables']));
|
||||
$this->assertEquals(array("approot-relative" => "../../../../../", "version" => "aaa", "attr"=>"123"),
|
||||
$aIncludedUrls['aFoundVariables']);
|
||||
$expected_array = array(
|
||||
'css/ui-lightness/images/tutu.jpg',
|
||||
"css/ui-lightness/images/tata.jpeg",
|
||||
'abc/../../../../../css/ui-lightness/images/toutou.png?v=aaa',
|
||||
"../../../../../css/ui-lightness/images/toto.png?v=aaa",
|
||||
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7?v=aaa",
|
||||
"css/ui-lightness/images/tete.jpeg?g=123"
|
||||
);
|
||||
$aIncludedUrls['aCompleteUrls'];
|
||||
$this->assertEquals($expected_array, array_values($aIncludedUrls['aCompleteUrls']));
|
||||
$this->assertEquals(array('$approot-relative + \'css/ui-lightness/images/titi.gif?v=\' + $version1'), array_values($aIncludedUrls['aToCompleteUrls']));
|
||||
}
|
||||
|
||||
public function providePrecompiledStyleSheets()
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function GetAllUrlFromScssProvider()
|
||||
{
|
||||
$datamodelfiles=glob(dirname(__FILE__)."/../../datamodels/2.x/**/datamodel*.xml");
|
||||
$test_set = array();
|
||||
|
||||
foreach ($datamodelfiles as $datamodelfile)
|
||||
{
|
||||
if (is_file($datamodelfile) &&
|
||||
$datamodelfile=="/var/www/html/iTop/test/application/../../datamodels/2.x/itop-config-mgmt/datamodel.itop-config-mgmt.xml")
|
||||
{
|
||||
$content=file_get_contents($datamodelfile);
|
||||
if (strpos($content, "precompiled_stylesheet")!==false)
|
||||
{
|
||||
$test_set[$datamodelfile]=array($datamodelfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $test_set;
|
||||
return array('test-getimages.scss' => array('test/application/theme-handler/getimages/test-getimages.scss'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sUrlTemplate
|
||||
* @param $aFoundVariables
|
||||
* @param $sExpectedUrl
|
||||
*
|
||||
* @dataProvider ResolveUrlProvider
|
||||
*/
|
||||
public function testResolveUrl($sUrlTemplate, $aFoundVariables, $sExpectedUrl)
|
||||
{
|
||||
$this->assertEquals($sExpectedUrl, ThemeHandler::ResolveUrl($sUrlTemplate, $aFoundVariables));
|
||||
}
|
||||
|
||||
public function ResolveUrlProvider()
|
||||
{
|
||||
return array(
|
||||
'XXX + $key1 UNresolved' => array("abc/'+ \$key1", array('key'=>'123'), false),
|
||||
'$key1 + XXX UNresolved' => array("\$key1 + abs", array('key'=>'123'), false),
|
||||
'XXX + $key UNresolved' => array("abc/'+ \$unknownkey", array('key'=>'123'), false),
|
||||
'XXX + $key resolved' => array("abc/'+ \$key", array('key'=>'123'), "abc/123"),
|
||||
'XXX + $key1 resolved' => array("abc/'+ \$key1", array('key1'=>'123'), "abc/123"),
|
||||
'$key + XXX resolved' => array("\$key + \"/abc", array('key'=>'123'), "123/abc"),
|
||||
'XXX + $key + YYY resolved' => array("abc/'+ \$key + '/def", array('key'=>'123'), "abc/123/def"),
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetIncludedImages()
|
||||
{
|
||||
$aStylesheetFile=glob($this->tmpDir."/branding/css/*.scss");
|
||||
$aStylesheetFile[]=$this->tmpDir."/branding/css/ui-lightness/jqueryui.scss";
|
||||
$expectJsonFilePath = APPROOT.'test/application/theme-handler/expected/themes/basque-red/theme-parameters.json';
|
||||
$expectedThemeParamJson = file_get_contents($expectJsonFilePath);
|
||||
$aThemeParametersVariables = json_decode($expectedThemeParamJson, true);
|
||||
$aIncludedImages = ThemeHandler::GetIncludedImages($aThemeParametersVariables['variables'], $aStylesheetFile, "RELATIVEPATH");
|
||||
|
||||
$aExpectedImages = json_decode(file_get_contents(APPROOT.'test/application/theme-handler/getimages/expected-getimages.json'), true);
|
||||
$this->assertEquals($aExpectedImages, $aIncludedImages);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user