Skip to content

Commit cc40a25

Browse files
committed
MC-19921: Create static tests for object variables inside E-mail templates
1 parent 4c6ca15 commit cc40a25

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

dev/tests/static/framework/Magento/Sniffs/Html/HtmlDirectiveSniff.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class HtmlDirectiveSniff implements Sniff
2222
*/
2323
private $usedVariables = [];
2424

25+
/**
26+
* @var array
27+
*/
28+
private $unfilteredVariables = [];
29+
2530
/**
2631
* @inheritDoc
2732
*/
@@ -40,6 +45,7 @@ public function register()
4045
public function process(File $phpcsFile, $stackPtr)
4146
{
4247
$this->usedVariables = [];
48+
$this->unfilteredVariables = [];
4349
if ($stackPtr !== 0) {
4450
return;
4551
}
@@ -172,6 +178,9 @@ private function validateDirectiveBody(File $phpcsFile, string $body): void
172178
private function validateVariableUsage(File $phpcsFile, string $body): void
173179
{
174180
$this->usedVariables[] = 'var ' . trim($body);
181+
if (strpos($body, '|') !== false) {
182+
$this->unfilteredVariables[] = 'var ' . trim(explode('|', $body, 2)[0]);
183+
}
175184
$variableTokenizer = new Template\Tokenizer\Variable();
176185
$variableTokenizer->setString($body);
177186
$stack = $variableTokenizer->tokenize();
@@ -228,9 +237,14 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText)
228237
}
229238

230239
$definedVariables = array_keys($definedVariables);
240+
foreach ($definedVariables as $definedVariable) {
241+
if (strpos($definedVariable, '|') !== false) {
242+
$definedVariables[] = trim(explode('|', $definedVariable, 2)[0]);
243+
}
244+
}
231245
}
232246

233-
$undefinedVariables = array_diff($this->usedVariables, $definedVariables);
247+
$undefinedVariables = array_diff($this->usedVariables, $definedVariables, $this->unfilteredVariables);
234248
foreach ($undefinedVariables as $undefinedVariable) {
235249
$phpcsFile->addError(
236250
'Template @vars comment block is missing a variable used in the template.' . PHP_EOL

dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Html/_files/test-html-directive.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"var foo.badFor()":"Some Variable",
4242
"var foo.badFor()|alsobad":"Some Variable",
4343
"foobar baz":"Some other directive as a variable",
44+
"var iusefilterslater|raw":"Some Variable",
4445
"var bad.multiline()":""
4546
} @-->
4647

@@ -110,3 +111,7 @@
110111
{{for item in foo.badForLoop()}}
111112
<div>this loop has a bad variable</div>
112113
{{/for}}
114+
115+
{{depend iusefilterslater}}
116+
{{var iusefilterslater|raw}}
117+
{{/depend}}

0 commit comments

Comments
 (0)