Skip to content

Commit

Permalink
Rename PreventPreviewUrlsAsPathsInLinks => PreventPreviewUrlLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Nov 9, 2023
1 parent 1262fe4 commit 7f64776
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
use Symfony\Component\Validator\Constraint;

/**
* Checks that the text does not use any preview URLs as paths.
* Checks that the text does not use any preview URL links.
*
* In other words, we want to avoid things like this:
*
* `<a href="/https://www.va.gov/">VA.gov</a>`
* `<a href="https://www.va.gov/">VA.gov</a>`
*
* @Constraint(
* id = "PreventPreviewUrlsAsPathsInLinks",
* label = @Translation("Prevent Preview URLs as Paths in Links", context = "Validation"),
* id = "PreventPreviewUrlLinks",
* label = @Translation("Prevent Preview URL Links", context = "Validation"),
* type = { "string_long", "text_long" }
* )
*/
class PreventPreviewUrlsAsPathsInLinks extends Constraint {
class PreventPreviewUrlLinks extends Constraint {

/**
* The error message for plain text fields.
*
* @var string
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinksValidator
*/
public $plainTextMessage = 'Replace the preview link ":url" with a public URL.';
public $plainTextMessage = 'Replace the preview URL ":url" with a public URL.';

/**
* The error message for rich text fields.
*
* @var string
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator
* @see \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinksValidator
*/
public $richTextMessage = 'Review the linked text ":link" (:url) and replace the preview URL with a public URL.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Symfony\Component\Validator\ConstraintValidator;

/**
* Validates the PreventPreviewUrlsAsPathsInLinks constraint.
* Validates the PreventPreviewUrlLinks constraint.
*/
class PreventPreviewUrlsAsPathsInLinksValidator extends ConstraintValidator {
class PreventPreviewUrlLinksValidator extends ConstraintValidator {

use TextValidatorTrait;
use ValidatorContextAccessTrait;
Expand All @@ -37,7 +37,7 @@ public function validateText(string $text, Constraint $constraint, int $delta) {
*
* In other words, we look for a string that looks like a preview URL.
*/
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks $constraint */
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinks $constraint */
if (preg_match('#(https?://preview-(staging|prod).vfs.va.gov[^\s]+)#', $text, $matches)) {
$this->addViolation($delta, $constraint->plainTextMessage, [
':url' => $matches[1],
Expand All @@ -49,7 +49,7 @@ public function validateText(string $text, Constraint $constraint, int $delta) {
* {@inheritdoc}
*/
public function validateHtml(string $html, Constraint $constraint, int $delta) {
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks $constraint */
/** @var \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinks $constraint */
$dom = Html::load($html);
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//a[contains(@href, "preview-staging.vfs.va.gov") or contains(@href, "preview-prod.vfs.va.gov")]') as $element) {
Expand Down
4 changes: 2 additions & 2 deletions docroot/modules/custom/va_gov_backend/va_gov_backend.module
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ function va_gov_backend_entity_bundle_field_info_alter(&$fields, EntityTypeInter
$field->addConstraint('PreventAbsoluteUrlsAsPathsInLinks');
$field->addConstraint('PreventVaGovDomainsAsPathsInLinks');
$field->addConstraint('PreventDomainsAsPathsInLinks');
$field->addConstraint('PreventPreviewUrlsAsPathsInLinks');
$field->addConstraint('PreventPreviewUrlLinks');
}
elseif ($field->getType() === 'text_long') {
$field->addConstraint('PreventAbsoluteCmsLinks');
Expand All @@ -1333,7 +1333,7 @@ function va_gov_backend_entity_bundle_field_info_alter(&$fields, EntityTypeInter
$field->addConstraint('PreventAbsoluteUrlsAsPathsInLinks');
$field->addConstraint('PreventVaGovDomainsAsPathsInLinks');
$field->addConstraint('PreventDomainsAsPathsInLinks');
$field->addConstraint('PreventPreviewUrlsAsPathsInLinks');
$field->addConstraint('PreventPreviewUrlLinks');
}
}
// Add paragraph checks on clp panels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace tests\phpunit\Content;

use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinks;
use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlsAsPathsInLinksValidator;
use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinks;
use Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventPreviewUrlLinksValidator;
use Tests\Support\Classes\VaGovUnitTestBase;
use Tests\Support\Traits\ValidatorTestTrait;

Expand All @@ -16,7 +16,7 @@
*
* @coversDefaultClass \Drupal\va_gov_backend\Plugin\Validation\Constraint\PreventProtocolRelativeLinksValidator
*/
class PreventPreviewUrlsAsPathsInLinksValidatorTest extends VaGovUnitTestBase {
class PreventPreviewUrlLinksValidatorTest extends VaGovUnitTestBase {

use ValidatorTestTrait;

Expand All @@ -26,7 +26,7 @@ class PreventPreviewUrlsAsPathsInLinksValidatorTest extends VaGovUnitTestBase {
* @covers ::addViolation
*/
public function testAddViolation() {
$validator = new PreventPreviewUrlsAsPathsInLinksValidator();
$validator = new PreventPreviewUrlLinksValidator();
$this->prepareValidator($validator, FALSE);
$validator->addViolation(3, 'Test violation');
}
Expand Down Expand Up @@ -58,9 +58,9 @@ public function testValidate(bool $willValidate, string $testString, string $fie
$items = $this->getFieldItemList([
$this->getFieldItem($value, $fieldType),
]);
$validator = new PreventPreviewUrlsAsPathsInLinksValidator();
$validator = new PreventPreviewUrlLinksValidator();
$this->prepareValidator($validator, $willValidate);
$validator->validate($items, new PreventPreviewUrlsAsPathsInLinks());
$validator->validate($items, new PreventPreviewUrlLinks());
}

/**
Expand All @@ -78,11 +78,11 @@ public function validateDataProvider() {
],
[
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.',
'However, string_long text with http://preview-staging.vfs.va.gov/path/to/content 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.',
'However, string_long text with http://preview-prod.vfs.va.gov/path/to/content that consists of an Preview URL should fail validation.',
],
[
TRUE,
Expand Down

0 comments on commit 7f64776

Please sign in to comment.