mirror of
https://github.com/Combodo/iTop.git
synced 2026-03-03 08:04:17 +01:00
N°8772 - Compiler: add relevance conditions (with test fail)
This commit is contained in:
@@ -48,11 +48,6 @@ abstract class AbstractExpressionFormBlock extends AbstractFormBlock
|
||||
$sExpression = $this->GetOption('expression');
|
||||
try {
|
||||
$oExpression = Expression::FromOQL($sExpression);
|
||||
$aParamsToResolve = $oExpression->GetParameters();
|
||||
$aResolvedParams = [];
|
||||
foreach ($aParamsToResolve as $sParamToResolve) {
|
||||
$aResolvedParams[$sParamToResolve] = strval($this->GetInputValue($sParamToResolve));
|
||||
}
|
||||
$aFieldsToResolve = $oExpression->ListRequiredFields();
|
||||
foreach ($aFieldsToResolve as $sFieldToResolve) {
|
||||
$aResolvedParams[$sFieldToResolve] = strval($this->GetInputValue($sFieldToResolve));
|
||||
|
||||
@@ -9,12 +9,16 @@ namespace Combodo\iTop\PropertyTree;
|
||||
|
||||
use Combodo\iTop\DesignElement;
|
||||
use Combodo\iTop\PropertyTree\ValueType\ValueTypeFactory;
|
||||
use Expression;
|
||||
use utils;
|
||||
|
||||
/**
|
||||
* @since 3.3.0
|
||||
*/
|
||||
class Property extends AbstractProperty
|
||||
{
|
||||
private ?string $sRelevanceCondition = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -26,6 +30,8 @@ class Property extends AbstractProperty
|
||||
if ($oValueTypeNode) {
|
||||
$this->oValueType = ValueTypeFactory::GetInstance()->CreateValueTypeFromDomNode($oValueTypeNode);
|
||||
}
|
||||
|
||||
$this->sRelevanceCondition = $oDomNode->GetChildText('relevance-condition');
|
||||
}
|
||||
|
||||
public function ToPHPFormBlock(&$aPHPFragments = []): string
|
||||
@@ -33,6 +39,32 @@ class Property extends AbstractProperty
|
||||
$sFormBlockClass = $this->oValueType->GetFormBlockClass();
|
||||
|
||||
$sInputs = '';
|
||||
$sRelevanceCondition = '';
|
||||
$sBinding = null;
|
||||
if (!is_null($this->sRelevanceCondition)) {
|
||||
$oExpression = Expression::FromOQL($this->sRelevanceCondition);
|
||||
$aFieldsToResolve = $oExpression->ListRequiredFields();
|
||||
foreach ($aFieldsToResolve as $sFieldToResolve) {
|
||||
if (preg_match('/(?<node>\w+)\.(?<output>\w+)/', $sFieldToResolve, $aMatches) === 1) {
|
||||
$sNode = $aMatches['node'];
|
||||
$sOutput = $aMatches['output'];
|
||||
$sBinding .= "\n ->AddInputDependsOn('{$sNode}.$sOutput', '$sNode', '$sOutput')";
|
||||
} else {
|
||||
// TODO Erreur field sans alias
|
||||
}
|
||||
}
|
||||
|
||||
$sRelevanceCondition = <<<PHP
|
||||
\$this->Add('{$this->sId}_relevance_condition', 'Combodo\iTop\Forms\Block\Expression\BooleanExpressionFormBlock', [
|
||||
'expression' => "{$this->sRelevanceCondition}",
|
||||
]){$sBinding};
|
||||
|
||||
|
||||
PHP;
|
||||
|
||||
$sInputs .= "\n ->InputDependsOn('visible', '{$this->sId}_relevance_condition', 'result')";
|
||||
}
|
||||
|
||||
foreach ($this->oValueType->GetInputs() as $sInput => $sValue) {
|
||||
if (preg_match("/^{{(?<node>\w+)\.(?<output>\w+)}}$/", $sValue, $aMatches) === 1) {
|
||||
$sInputs .= "\n ->InputDependsOn('$sInput', '{$aMatches['node']}', '{$aMatches['output']}')";
|
||||
@@ -42,7 +74,7 @@ class Property extends AbstractProperty
|
||||
}
|
||||
|
||||
return <<<PHP
|
||||
\$this->Add('$this->sId', '$sFormBlockClass', [
|
||||
{$sRelevanceCondition}\$this->Add('$this->sId', '$sFormBlockClass', [
|
||||
'label' => '$this->sLabel',
|
||||
]){$sInputs};
|
||||
|
||||
|
||||
@@ -5,42 +5,44 @@
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
use Combodo\iTop\Forms\Block\Base\CollectionBlock;
|
||||
use Combodo\iTop\Forms\Block\AbstractTypeFormBlock;
|
||||
use Combodo\iTop\Forms\Block\Expression\BooleanExpressionFormBlock;
|
||||
use Combodo\iTop\Forms\Compiler\FormsCompiler;
|
||||
use Combodo\iTop\ItopSdkFormDemonstrator\Form\Block\Person\PersonFormBlock;
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
|
||||
class TestFormsCompiler extends ItopDataTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider CompileFormFromXMLProvider
|
||||
*
|
||||
* @param string $sXMLContent
|
||||
* @param string $sExpectedPHP
|
||||
* @param string $sMessage
|
||||
*
|
||||
* @return void
|
||||
* @throws \Combodo\iTop\Forms\Compiler\FormsCompilerException
|
||||
* @throws \Combodo\iTop\PropertyTree\PropertyTreeException
|
||||
* @throws \DOMFormatException
|
||||
*/
|
||||
public function testCompileFormFromXML(string $sXMLContent, string $sExpectedPHP, string $sMessage = '')
|
||||
public function testCompileFormFromXML(string $sXMLContent, string $sExpectedPHP)
|
||||
{
|
||||
$sProducedPHP = FormsCompiler::GetInstance()->CompileFormFromXML($sXMLContent);
|
||||
|
||||
$this->AssertPHPCodeIsValid($sProducedPHP);
|
||||
|
||||
$sMessage = $this->dataName();
|
||||
$this->assertEquals($sExpectedPHP, $sProducedPHP, $sMessage);
|
||||
}
|
||||
|
||||
public function CompileFormFromXMLProvider()
|
||||
{
|
||||
return [
|
||||
|
||||
'Basic properties' => [
|
||||
'Basic scalar properties should generate PHP' => [
|
||||
'sXMLContent' => <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<node id="basic_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyTree" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
|
||||
<nodes>
|
||||
<node id="title_property" xsi:type="Combodo-Property">
|
||||
<label>UI:BasicTest:Prop-Title</label>
|
||||
<label>UI:BasicTest:Prop-Title2</label>
|
||||
<value-type xsi:type="Combodo-ValueTypeLabel">
|
||||
</value-type>
|
||||
</node>
|
||||
@@ -67,10 +69,9 @@ class FormFor__basic_test extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => 'Basic scalar properties should generate PHP',
|
||||
],
|
||||
|
||||
'Empty property tree' => [
|
||||
'Empty property tree should generate minimal PHP' => [
|
||||
'sXMLContent' => <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<node id="EmptyTest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyTree" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
|
||||
@@ -85,10 +86,9 @@ class FormFor__EmptyTest extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
{ }
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => 'Empty property tree should generate minimal PHP',
|
||||
],
|
||||
|
||||
'Empty property tree lower case' => [
|
||||
'Empty property tree lower case should generate lower case class name' => [
|
||||
'sXMLContent' => <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<node id="empty_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyTree" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
|
||||
@@ -103,7 +103,6 @@ class FormFor__empty_test extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
{ }
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => 'Empty property tree should generate minimal PHP',
|
||||
],
|
||||
|
||||
'Properties with all value-types' => [
|
||||
@@ -211,7 +210,6 @@ class FormFor__AllValueTypesTest extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => '',
|
||||
],
|
||||
|
||||
'Collection of trees' => [
|
||||
@@ -265,7 +263,6 @@ class FormFor__collection_of_trees_test extends Combodo\iTop\Forms\Block\Base\Fo
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => '',
|
||||
],
|
||||
|
||||
'Input static' => [
|
||||
@@ -295,7 +292,6 @@ class FormFor__input_static_test extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => '',
|
||||
],
|
||||
|
||||
'Input binding' => [
|
||||
@@ -333,8 +329,50 @@ class FormFor__input_binding_test extends Combodo\iTop\Forms\Block\Base\FormBloc
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => '',
|
||||
],
|
||||
|
||||
'Relevance condition' => [
|
||||
'sXMLContent' => <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<node id="RelevanceCondition" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyTree" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
|
||||
<nodes>
|
||||
<node id="source_property" xsi:type="Combodo-Property">
|
||||
<label>UI:Source</label>
|
||||
<value-type xsi:type="Combodo-ValueTypeString">
|
||||
</value-type>
|
||||
</node>
|
||||
<node id="dependant_property" xsi:type="Combodo-Property">
|
||||
<label>UI:Dependant</label>
|
||||
<relevance-condition>source_property.text != 'count'</relevance-condition>
|
||||
<value-type xsi:type="Combodo-ValueTypeString">
|
||||
</value-type>
|
||||
</node>
|
||||
</nodes>
|
||||
</node>
|
||||
XML,
|
||||
'sExpectedPHP' => <<<PHP
|
||||
class FormFor__RelevanceCondition extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
{
|
||||
protected function BuildForm(): void
|
||||
{
|
||||
\$this->Add('source_property', 'Combodo\iTop\Forms\Block\Base\TextFormBlock', [
|
||||
'label' => 'UI:Source',
|
||||
]);
|
||||
|
||||
\$this->Add('dependant_property_relevance_condition', 'Combodo\iTop\Forms\Block\Expression\BooleanExpressionFormBlock', [
|
||||
'expression' => "source_property.text != 'count'",
|
||||
])
|
||||
->AddInputDependsOn('source_property.text', 'source_property', 'text');
|
||||
|
||||
\$this->Add('dependant_property', 'Combodo\iTop\Forms\Block\Base\TextFormBlock', [
|
||||
'label' => 'UI:Dependant',
|
||||
])
|
||||
->InputDependsOn('visible', 'dependant_property_relevance_condition', 'result');
|
||||
}
|
||||
}
|
||||
PHP,
|
||||
],
|
||||
|
||||
'test' => [
|
||||
'sXMLContent' => <<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@@ -350,7 +388,6 @@ class FormFor__Test extends Combodo\iTop\Forms\Block\Base\FormBlock
|
||||
{ }
|
||||
}
|
||||
PHP,
|
||||
'sMessage' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user