Skip to content

Commit ac5f9fd

Browse files
committed
AC-667: Create phpcs static check for EmailTemplateTest
1 parent 1e3a89f commit ac5f9fd

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento2\Sniffs\Legacy;
8+
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
12+
/**
13+
* Test for obsolete email directives in view/email/*.html
14+
*/
15+
class EmailTemplateSniff implements Sniff
16+
{
17+
private const OBSOLETE_EMAIL_DIRECTIVES = [
18+
'/\{\{htmlescape.*?\}\}/i' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
19+
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
20+
];
21+
22+
private const WARNING_CODE = 'FoundObsoleteEmailDirective';
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function register(): array
28+
{
29+
return [
30+
T_INLINE_HTML
31+
];
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function process(File $phpcsFile, $stackPtr)
38+
{
39+
$content = $phpcsFile->getTokens()[$stackPtr]['content'];
40+
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) {
41+
if(preg_match($directiveRegex, $content)) {
42+
$phpcsFile->addWarning(
43+
$errorMessage,
44+
$stackPtr,
45+
self::WARNING_CODE
46+
);
47+
}
48+
}
49+
}
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>{{var "H1"}}</h1>
2+
<h2>{{var "H2"}}</h2>
3+
<p>{{var "p"}} {{var "p"}}</p>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>{{htmlescape "H1"}}</h1>
2+
<h2>{{escapehtml "H2"}}</h2>
3+
<p>{{escapehtml "p"}} {{htmlescape "p"}}</p>
4+
<p class="greeting">{{trans "Translateme"}}</p>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class EmailTemplateUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList($testFile = '')
24+
{
25+
if ($testFile === 'EmailTemplateUnitTest.1.html') {
26+
return [];
27+
}
28+
if ($testFile === 'EmailTemplateUnitTest.2.html') {
29+
return [
30+
1 => 1,
31+
2 => 1,
32+
3 => 2,
33+
];
34+
}
35+
36+
return [];
37+
}
38+
}

Magento2/ruleset.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@
242242
<type>warning</type>
243243
</rule>
244244

245+
<rule ref="Magento2.Legacy.EmailTemplate.FoundObsoleteEmailDirective">
246+
<include-pattern>view/email/*.html</include-pattern>
247+
<include-pattern>view/*/email/*.html</include-pattern>
248+
<severity>8</severity>
249+
<type>warning</type>
250+
</rule>
251+
245252
<!-- Severity 7 warnings: General code issues. -->
246253
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
247254
<severity>7</severity>

0 commit comments

Comments
 (0)