Skip to content

Commit 16ed324

Browse files
committed
MC-19921: Create static tests for object variables inside E-mail templates
1 parent f419ec3 commit 16ed324

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

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

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,23 @@ public function process(File $phpcsFile, $stackPtr)
4949
return;
5050
}
5151

52-
$this->usedVariables = [];
52+
$html = $this->processIfDirectives($html, $phpcsFile);
53+
$html = $this->processDependDirectives($html, $phpcsFile);
54+
$html = $this->processForDirectives($html, $phpcsFile);
55+
$html = $this->processVarDirectivesAndParams($html, $phpcsFile);
5356

57+
$this->validateDefinedVariables($phpcsFile, $html);
58+
}
59+
60+
/**
61+
* Process the {{if}} directives in the file
62+
*
63+
* @param string $html
64+
* @param File $phpcsFile
65+
* @return string The processed template
66+
*/
67+
private function processIfDirectives(string $html, File $phpcsFile): string
68+
{
5469
if (preg_match_all(Template::CONSTRUCTION_IF_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
5570
foreach ($constructions as $construction) {
5671
// validate {{if <var>}}
@@ -59,6 +74,18 @@ public function process(File $phpcsFile, $stackPtr)
5974
}
6075
}
6176

77+
return $html;
78+
}
79+
80+
/**
81+
* Process the {{depend}} directives in the file
82+
*
83+
* @param string $html
84+
* @param File $phpcsFile
85+
* @return string The processed template
86+
*/
87+
private function processDependDirectives(string $html, File $phpcsFile): string
88+
{
6289
if (preg_match_all(Template::CONSTRUCTION_DEPEND_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
6390
foreach ($constructions as $construction) {
6491
// validate {{depend <var>}}
@@ -67,6 +94,18 @@ public function process(File $phpcsFile, $stackPtr)
6794
}
6895
}
6996

97+
return $html;
98+
}
99+
100+
/**
101+
* Process the {{for}} directives in the file
102+
*
103+
* @param string $html
104+
* @param File $phpcsFile
105+
* @return string The processed template
106+
*/
107+
private function processForDirectives(string $html, File $phpcsFile): string
108+
{
70109
if (preg_match_all(Template::LOOP_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
71110
foreach ($constructions as $construction) {
72111
// validate {{for in <var>}}
@@ -75,6 +114,18 @@ public function process(File $phpcsFile, $stackPtr)
75114
}
76115
}
77116

117+
return $html;
118+
}
119+
120+
/**
121+
* Process the all var directives and var directive params in the file
122+
*
123+
* @param string $html
124+
* @param File $phpcsFile
125+
* @return string The processed template
126+
*/
127+
private function processVarDirectivesAndParams(string $html, File $phpcsFile): string
128+
{
78129
if (preg_match_all(Template::CONSTRUCTION_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
79130
foreach ($constructions as $construction) {
80131
if (empty($construction[2])) {
@@ -89,7 +140,7 @@ public function process(File $phpcsFile, $stackPtr)
89140
}
90141
}
91142

92-
$this->validateDefinedVariables($phpcsFile, $html);
143+
return $html;
93144
}
94145

95146
/**
@@ -187,18 +238,5 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText)
187238
'HtmlTemplates.DirectiveUsage.UndefinedVariable'
188239
);
189240
}
190-
191-
$extraDefinedVariables = array_diff($definedVariables, $this->usedVariables);
192-
foreach ($extraDefinedVariables as $extraDefinedVariable) {
193-
if (substr($extraDefinedVariable, 0, 4) !== 'var ') {
194-
continue;
195-
}
196-
$phpcsFile->addError(
197-
'Template @vars comment block contains a variable not used in the template.' . PHP_EOL
198-
. 'Extra variable: ' . $extraDefinedVariable,
199-
null,
200-
'HtmlTemplates.DirectiveUsage.ExtraVariable'
201-
);
202-
}
203241
}
204242
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
------------------------------------------------------------------------------------------------------------------------------------
2-
FOUND 21 ERRORS AFFECTING 1 LINE
2+
FOUND 20 ERRORS AFFECTING 1 LINE
33
------------------------------------------------------------------------------------------------------------------------------------
44
1 | ERROR | Template directives may not invoke methods. Only scalar array access is allowed.
55
| | Found "foo.badif().bad"
@@ -41,8 +41,6 @@ FOUND 21 ERRORS AFFECTING 1 LINE
4141
| | Missing variable: var foo.undeclared.baz
4242
1 | ERROR | Template @vars comment block is missing a variable used in the template.
4343
| | Missing variable: var undeclared.var.error
44-
1 | ERROR | Template @vars comment block contains a variable not used in the template.
45-
| | Extra variable: var extra.var.getOhNo()
4644
------------------------------------------------------------------------------------------------------------------------------------
4745

4846

0 commit comments

Comments
 (0)