-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-10356 Add content validator for Preview URL links (#10356)
- Loading branch information
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...stom/va_gov_backend/src/Plugin/Validation/Constraint/PreventPreviewUrlsAsPathsInLinks.php
This file contains 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,38 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_backend\Plugin\Validation\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* Checks that the text does not use any preview URLs as paths. | ||
* | ||
* In other words, we want to avoid things like this: | ||
* | ||
* `<a href="/https://www.va.gov/">VA.gov</a>` | ||
* | ||
* @Constraint( | ||
* id = "PreventPreviewUrlsAsPathsInLinks", | ||
* label = @Translation("Prevent Preview URLs as Paths in Links", context = "Validation"), | ||
* type = { "string_long", "text_long" } | ||
* ) | ||
*/ | ||
class PreventPreviewUrlsAsPathsInLinks extends Constraint { | ||
|
||
/** | ||
* The error message for plain text fields. | ||
* | ||
* @var string | ||
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator | ||
*/ | ||
public $plainTextMessage = 'Remove the preview link ":url" and replace it with a public production URL.'; | ||
|
||
/** | ||
* The error message for rich text fields. | ||
* | ||
* @var string | ||
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator | ||
*/ | ||
public $richTextMessage = 'Review the linked text ":link" (:url) and update the preview URL with a public production URL.'; | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...ov_backend/src/Plugin/Validation/Constraint/PreventPreviewUrlsAsPathsInLinksValidator.php
This file contains 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,70 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_backend\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Component\Utility\Html; | ||
use Drupal\va_gov_backend\Plugin\Validation\Constraint\Traits\TextValidatorTrait; | ||
use Drupal\va_gov_backend\Plugin\Validation\Constraint\Traits\ValidatorContextAccessTrait; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* Validates the PreventPreviewUrlsAsPathsInLinks constraint. | ||
*/ | ||
class PreventPreviewUrlsAsPathsInLinksValidator extends ConstraintValidator { | ||
|
||
use TextValidatorTrait; | ||
use ValidatorContextAccessTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validateText(string $text, Constraint $constraint, int $delta) { | ||
/* | ||
* Regular expression explanation: | ||
* | ||
* # Begin delimiter (replace '/', since we're mucking about | ||
* with URLs, which contain slashes.) | ||
* ( Open capture group. We capture the entire URL so that | ||
* we can refer to it in the error message. | ||
* https? Match either 'http' or 'https' ... | ||
* :// ... followed by a colon and two slashes ... | ||
* preview-(staging|prod) | ||
* .vfs.va.gov ... followed by a preview servers for prod|staging ... | ||
* [^\s]+ ... and any non-whitespace characters that follow. | ||
* ) Close capture group. | ||
* # End delimiter (replace '/') | ||
* | ||
* In other words, we look for a string that looks like a preview URL. | ||
*/ | ||
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks $constraint */ | ||
if (preg_match('#(https?://preview-(staging|prod).vfs.va.gov[^\s]+)#', $text, $matches)) { | ||
$this->addViolation($delta, $constraint->plainTextMessage, [ | ||
':url' => $matches[1], | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validateHtml(string $html, Constraint $constraint, int $delta) { | ||
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks $constraint */ | ||
$dom = Html::load($html); | ||
$xpath = new \DOMXPath($dom); | ||
foreach ($xpath->query('//a[starts-with(@href, "preview-staging.vfs.va.gov")] | //a[starts-with(@href, "preview-prod.vfs.va.gov")]') as $element) { | ||
$url = $element->getAttribute('href'); | ||
Check failure on line 56 in docroot/modules/custom/va_gov_backend/src/Plugin/Validation/Constraint/PreventPreviewUrlsAsPathsInLinksValidator.php GitHub Actions / PHPStan
|
||
$firstChild = $element->hasChildNodes() ? $element->childNodes[0] : NULL; | ||
$link = $element->ownerDocument->saveHTML($firstChild ?? $element); | ||
$this->getContext() | ||
->buildViolation($constraint->richTextMessage, [ | ||
':link' => $link, | ||
':url' => $url, | ||
]) | ||
->atPath((string) $delta . '.value') | ||
->addViolation(); | ||
return; | ||
} | ||
} | ||
|
||
} |
This file contains 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
131 changes: 131 additions & 0 deletions
131
tests/phpunit/Content/PreventPreviewUrlsAsPathsInLinksValidatorTest.php
This file contains 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,131 @@ | ||
<?php | ||
|
||
namespace tests\phpunit\Content; | ||
|
||
use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks; | ||
use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator; | ||
use Tests\Support\Classes\VaGovUnitTestBase; | ||
use Tests\Support\Traits\ValidatorTestTrait; | ||
|
||
/** | ||
* A test to confirm the proper functioning of this validator. | ||
* | ||
* @group functional | ||
* @group all | ||
* @group validation | ||
* | ||
* @coversDefaultClass \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventProtocolRelativeLinksValidator | ||
*/ | ||
class PreventPreviewUrlsAsPathsInLinksValidatorTest extends VaGovUnitTestBase { | ||
|
||
use ValidatorTestTrait; | ||
|
||
/** | ||
* Test ::addViolation(). | ||
* | ||
* @covers ::addViolation | ||
*/ | ||
public function testAddViolation() { | ||
$validator = new PreventPreviewUrlsAsPathsInLinksValidator(); | ||
$this->prepareValidator($validator, FALSE); | ||
$validator->addViolation(3, 'Test violation'); | ||
} | ||
|
||
/** | ||
* Test ::validate(). | ||
* | ||
* @param bool $willValidate | ||
* TRUE if the test string should validate, otherwise FALSE. | ||
* @param string $testString | ||
* Some test string to attempt to validate. | ||
* @param string $fieldType | ||
* The type of the text field, e.g. 'text_long' or 'string_long'. | ||
* @param string $format | ||
* An optional format, like 'plain_text' or 'rich_text'. | ||
* | ||
* @covers validate | ||
* @covers validateText | ||
* @covers validateHtml | ||
* @dataProvider validateDataProvider | ||
*/ | ||
public function testValidate(bool $willValidate, string $testString, string $fieldType = 'string_long', string $format = NULL) { | ||
$value = [ | ||
'value' => $testString, | ||
]; | ||
if ($fieldType !== 'string_long') { | ||
$value['format'] = $format; | ||
} | ||
$items = $this->getFieldItemList([ | ||
$this->getFieldItem($value, $fieldType), | ||
]); | ||
$validator = new PreventPreviewUrlsAsPathsInLinksValidator(); | ||
$this->prepareValidator($validator, $willValidate); | ||
$validator->validate($items, new PreventPreviewUrlsAsPathsInLinks()); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
*/ | ||
public function validateDataProvider() { | ||
return [ | ||
[ | ||
TRUE, | ||
'Normal string_long text should not fail validation.', | ||
], | ||
[ | ||
TRUE, | ||
'Normal string_long text with the occasional // scattered throughout should not // fail validation.', | ||
], | ||
[ | ||
FALSE, | ||
'However, string_long text with http://preview-staging.vfs.va.gov/path/to/content a path that consists of an Preview URL should fail validation.', | ||
], | ||
[ | ||
FALSE, | ||
'However, string_long text with http://preview-prod.vfs.va.gov/path/to/content a path that consists of an Preview URL should fail validation.', | ||
], | ||
[ | ||
TRUE, | ||
'Normal text_long text should not fail validation.', | ||
'text_long', | ||
], | ||
[ | ||
FALSE, | ||
'Something with a http://preview-staging.vfs.va.gov/path/to/content looks like it contains an Preview URL and should fail.', | ||
'text_long', | ||
'plain_text', | ||
], | ||
[ | ||
FALSE, | ||
'Something with a https://preview-prod.vfs.va.gov/path/to/content looks like it contains an Preview URL and should fail.', | ||
'text_long', | ||
'plain_text', | ||
], | ||
[ | ||
FALSE, | ||
'This contains a <a href="https://preview-staging.vfs.va.gov/path/to/content">path consisting of an Preview URL</a> inside a tag and should fail.', | ||
'text_long', | ||
'plain_text', | ||
], | ||
[ | ||
FALSE, | ||
'This contains a <a href="https://preview-prod.vfs.va.gov/path/to/content">path consisting of an Preview URL</a> inside a tag and should fail.', | ||
'text_long', | ||
'plain_text', | ||
], | ||
[ | ||
TRUE, | ||
'http://preview-staging.vfs.va.gov/path/to/content outside of <a href="https://www.example.org/">anchor tags</a> should not trigger the validator.', | ||
'text_long', | ||
'rich_text', | ||
], | ||
[ | ||
TRUE, | ||
'http://preview-prod.vfs.va.gov/path/to/content outside of <a href="https://www.example.org/">anchor tags</a> should not trigger the validator.', | ||
'text_long', | ||
'rich_text', | ||
], | ||
]; | ||
} | ||
|
||
} |