Skip to content

AC-667: Create phpcs static check for EmailTemplateTest #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Magento2/Sniffs/Legacy/EmailTemplateSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Test for obsolete email directives in view/email/*.html
*/
class EmailTemplateSniff implements Sniff
{
private const OBSOLETE_EMAIL_DIRECTIVES = [
'/\{\{htmlescape.*?\}\}/i' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
];

private const WARNING_CODE = 'FoundObsoleteEmailDirective';

/**
* @inheritdoc
*/
public function register(): array
{
return [
T_INLINE_HTML
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$content = $phpcsFile->getTokens()[$stackPtr]['content'];
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) {
if (preg_match($directiveRegex, $content)) {
$phpcsFile->addWarning(
$errorMessage,
$stackPtr,
self::WARNING_CODE
);
}
}
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>{{var "H1"}}</h1>
<h2>{{var "H2"}}</h2>
<p>{{var "p"}} {{var "p"}}</p>

4 changes: 4 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>{{htmlescape "H1"}}</h1>
<h2>{{escapehtml "H2"}}</h2>
<p>{{escapehtml "p"}} {{htmlescape "p"}}</p>
<p class="greeting">{{trans "Translateme"}}</p>
39 changes: 39 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class EmailTemplateUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
if ($testFile === 'EmailTemplateUnitTest.1.html') {
return [];
}
if ($testFile === 'EmailTemplateUnitTest.2.html') {
return [
1 => 1,
2 => 1,
3 => 2,
];
}

return [];
}
}
7 changes: 7 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@
<type>warning</type>
</rule>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this empty line for consistency.

<rule ref="Magento2.Legacy.EmailTemplate.FoundObsoleteEmailDirective">
<include-pattern>view/email/*.html</include-pattern>
<include-pattern>view/*/email/*.html</include-pattern>
<severity>8</severity>
<type>warning</type>
Copy link
Member

@sivaschenko sivaschenko Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an error as using of obsolete directives will actually break the functionality of email templates

</rule>

<!-- Severity 7 warnings: General code issues. -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
<severity>7</severity>
Expand Down