-
Notifications
You must be signed in to change notification settings - Fork 160
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac5f9fd
AC-667: Create phpcs static check for EmailTemplateTest
jcuerdo 0cb8f67
AC-667: Create phpcs static check for EmailTemplateTest
jcuerdo 4a78a94
AC-667: Create phpcs static check for EmailTemplateTest
jcuerdo e4698fb
AC-667: Create phpcs static check for EmailTemplateTest
jcuerdo e4f5540
Update Magento2/ruleset.xml
jcuerdo 2751031
AC-667: Create phpcs static check for EmailTemplateTest
jcuerdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,6 +242,13 @@ | |
<type>warning</type> | ||
</rule> | ||
|
||
<rule ref="Magento2.Legacy.EmailTemplate.FoundObsoleteEmailDirective"> | ||
<include-pattern>view/email/*.html</include-pattern> | ||
<include-pattern>view/*/email/*.html</include-pattern> | ||
<severity>8</severity> | ||
jcuerdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<type>warning</type> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.