Skip to content

Commit 862042a

Browse files
committed
MC-19921: Create static tests for object variables inside E-mail templates
1 parent d34c41b commit 862042a

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ public function process(File $phpcsFile, $stackPtr)
5151

5252
$this->usedVariables = [];
5353

54+
if (preg_match_all(Template::CONSTRUCTION_IF_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
55+
foreach ($constructions as $construction) {
56+
// validate {{if <var>}}
57+
$this->validateVariableUsage($phpcsFile, $construction[1]);
58+
$html = str_replace($construction[0], $construction[2] . ($construction[4] ?? ''), $html);
59+
}
60+
}
61+
62+
if (preg_match_all(Template::CONSTRUCTION_DEPEND_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
63+
foreach ($constructions as $construction) {
64+
// validate {{depend <var>}}
65+
$this->validateVariableUsage($phpcsFile, $construction[1]);
66+
$html = str_replace($construction[0], $construction[2], $html);
67+
}
68+
}
69+
70+
if (preg_match_all(Template::LOOP_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
71+
foreach ($constructions as $construction) {
72+
// validate {{for in <var>}}
73+
$this->validateVariableUsage($phpcsFile, $construction['loopData']);
74+
$html = str_replace($construction[0], $construction['loopBody'], $html);
75+
}
76+
}
77+
5478
if (preg_match_all(Template::CONSTRUCTION_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
5579
foreach ($constructions as $construction) {
5680
if (empty($construction[2])) {
@@ -63,7 +87,6 @@ public function process(File $phpcsFile, $stackPtr)
6387
$this->validateDirectiveBody($phpcsFile, $construction[2]);
6488
}
6589
}
66-
6790
}
6891

6992
$this->validateDefinedVariables($phpcsFile, $html);
@@ -141,6 +164,17 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText)
141164
return;
142165
}
143166

167+
foreach ($definedVariables as $var => $label) {
168+
if (empty($label)) {
169+
$phpcsFile->addError(
170+
'Template @vars comment block contains invalid label.' . PHP_EOL
171+
. 'Label for variable "' . $var . '" is empty.',
172+
null,
173+
'HtmlTemplates.DirectiveUsage.InvalidVariableLabel'
174+
);
175+
}
176+
}
177+
144178
$definedVariables = array_keys($definedVariables);
145179
}
146180

dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Html/HtmlDirectiveSniffTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function processDataProvider(): array
2424
[
2525
'test-html-directive.html',
2626
'test-html-directive-errors.txt'
27+
],
28+
[
29+
'test-html-directive-invalid-json.html',
30+
'test-html-directive-invalid-json-errors.txt'
2731
]
2832
];
2933
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
------------------------------------------------------------------------------------------------------------------------------------
2-
FOUND 10 ERRORS AFFECTING 1 LINE
2+
FOUND 21 ERRORS AFFECTING 1 LINE
33
------------------------------------------------------------------------------------------------------------------------------------
4+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
5+
| | Found "foo.badif().bad"
6+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
7+
| | Found "foo.badif3()"
8+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
9+
| | Found "foo.badDepend().bad"
10+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
11+
| | Found "foo.badForLoop()"
412
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
513
| | Found "foo.bad()"
614
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
@@ -15,6 +23,20 @@ FOUND 10 ERRORS AFFECTING 1 LINE
1523
| | Found "bad.bad()"
1624
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
1725
| | Found "bad.multiline()"
26+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
27+
| | Found "foo.badif2()"
28+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
29+
| | Found "foo.badif4()"
30+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
31+
| | Found "foo.badif5()"
32+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
33+
| | Found "foo.baddepend2()"
34+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
35+
| | Found "foo.badFor()"
36+
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
37+
| | Found "foo.badFor()|alsobad"
38+
1 | ERROR | Template @vars comment block contains invalid label.
39+
| | Label for variable "var bad.multiline()" is empty.
1840
1 | ERROR | Template @vars comment block is missing a variable used in the template.
1941
| | Missing variable: var foo.undeclared.baz
2042
1 | ERROR | Template @vars comment block is missing a variable used in the template.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-------------------------------------------------------------------------------------------------------------------------------------------------
2+
FOUND 1 ERROR AFFECTING 1 LINE
3+
-------------------------------------------------------------------------------------------------------------------------------------------------
4+
1 | ERROR | Template @vars comment block contains invalid JSON.
5+
-------------------------------------------------------------------------------------------------------------------------------------------------
6+
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<!--@vars {
8+
"var foo.good":"Some variable",
9+
"var foo.good|stillfine""Some variable"
10+
} @-->
11+
12+
Template content doesn't matter. The JSON is invalid.

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@
1919
"var extra.var.getOhNo()":"Uh Oh, Error time!",
2020
"var foo.bad.trans()":"Some variable",
2121
"var bad.bad()":"Some variable",
22-
"var bad.multiline()":"Some variable"
22+
"var foo.goodif":"Some Variable",
23+
"var foo.goodif":"Some Variable",
24+
"var foo.badif().bad":"Some Variable",
25+
"var foo.badif3()":"Some Variable",
26+
"var foo.gooddepend":"Some Variable",
27+
"var foo.badDepend().bad":"Some Variable",
28+
"var foo.goodFor":"Some Variable",
29+
"var foo.getGoodFor()":"Some Variable",
30+
"var foo.badForLoop()":"Some Variable",
31+
"var foo.goodif2":"Some Variable",
32+
"var foo.goodif2":"Some Variable",
33+
"var foo.goodif3":"Some Variable",
34+
"var foo.badif2()":"Some Variable",
35+
"var foo.badif4()":"Some Variable",
36+
"var foo.badif5()":"Some Variable",
37+
"var foo.gooddepend2":"Some Variable",
38+
"var foo.baddepend2()":"Some Variable",
39+
"var foo.goodFor":"Some Variable",
40+
"var foo.goodFor|stillfine":"Some Variable",
41+
"var foo.badFor()":"Some Variable",
42+
"var foo.badFor()|alsobad":"Some Variable",
43+
"var bad.multiline()":""
2344
} @-->
2445

2546
<div>{{var foo.good}}</div>
@@ -45,3 +66,46 @@
4566
<blah>foo %bar</blah>blah
4667
" bar="something" bad=$bad.multiline()
4768
}}</div>
69+
70+
{{if foo.goodif}}
71+
<div>{{var foo.goodif2}}</div>
72+
{{/if}}
73+
74+
{{if foo.goodif}}
75+
<div>{{var foo.goodif2}}</div>
76+
{{else}}
77+
<div>{{var foo.goodif3}}</div>
78+
{{/if}}
79+
80+
{{if foo.badif().bad}}
81+
<div>{{var foo.badif2()}}</div>
82+
{{/if}}
83+
84+
{{if foo.badif3()}}
85+
<div>{{var foo.badif4()}}</div>
86+
{{else}}
87+
<div>{{var foo.badif5()}}</div>
88+
{{/if}}
89+
90+
{{depend foo.gooddepend}}
91+
<div>{{var foo.gooddepend2}}</div>
92+
{{/depend}}
93+
94+
{{depend foo.badDepend().bad}}
95+
<div>{{var foo.baddepend2()}}</div>
96+
{{/depend}}
97+
98+
{{for item in foo.goodFor}}
99+
<div>{{var foo.goodFor}}</div>
100+
<div>{{var foo.goodFor|stillfine}}</div>
101+
<div>{{var foo.badFor()}}</div>
102+
<div>{{var foo.badFor()|alsobad}}</div>
103+
{{/for}}
104+
105+
{{for item in foo.getGoodFor()}}
106+
<div>loopy</div>
107+
{{/for}}
108+
109+
{{for item in foo.badForLoop()}}
110+
<div>this loop has a bad variable</div>
111+
{{/for}}

0 commit comments

Comments
 (0)