generated from SU-SWS/stanford_module_example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added relative internal link validation option for link fields
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
src/Plugin/Validation/Constraint/RelativeLinkFieldItemConstraint.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,20 @@ | ||
<?php | ||
|
||
namespace Drupal\stanford_fields\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Symfony\Component\Validator\Constraint as SymfonyConstraint; | ||
use Drupal\Core\Validation\Attribute\Constraint; | ||
|
||
/** | ||
* Checks that the submitted value is a unique integer. | ||
*/ | ||
#[Constraint( | ||
id: 'relative_internal_link', | ||
label: new TranslatableMarkup('Relative Internal Link', [], ['context' => 'Validation']) | ||
)] | ||
class RelativeLinkFieldItemConstraint extends SymfonyConstraint { | ||
|
||
public $absoluteLink = 'Please use relative links that start with "/" for paths on this site.'; | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/Plugin/Validation/Constraint/RelativeLinkFieldItemConstraintValidator.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,51 @@ | ||
<?php | ||
|
||
namespace Drupal\stanford_fields\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* Validates the Menu link item constraint. | ||
*/ | ||
class RelativeLinkFieldItemConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { | ||
|
||
/** | ||
* Current request. | ||
* | ||
* @var \Drupal\path_alias\AliasManagerInterface | ||
*/ | ||
protected $request; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static($container->get('request_stack')); | ||
} | ||
|
||
/** | ||
* Validation constructor. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack | ||
* Current request stack. | ||
*/ | ||
public function __construct(RequestStack $request_stack) { | ||
$this->request = $request_stack->getCurrentRequest(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) { | ||
/** @var \Drupal\Core\Field\FieldItemListInterface $value */ | ||
$link_uri = $value->get(0)?->get('uri')?->getString(); | ||
if ($link_uri && str_contains($link_uri, $this->request->getSchemeAndHttpHost())) { | ||
$this->context->addViolation($constraint->absoluteLink); | ||
} | ||
} | ||
|
||
} |
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
90 changes: 90 additions & 0 deletions
90
tests/src/Unit/Plugin/Validation/Constraint/LinkFieldItemConstraintValidatorTest.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,90 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\stanford_fields\Unit\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\Container; | ||
use Drupal\Core\Validation\ExecutionContext; | ||
use Drupal\Core\Validation\TranslatorInterface; | ||
use Drupal\stanford_fields\Plugin\Validation\Constraint\RelativeLinkFieldItemConstraint; | ||
use Drupal\stanford_fields\Plugin\Validation\Constraint\RelativeLinkFieldItemConstraintValidator; | ||
use Drupal\Tests\UnitTestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Drupal\Core\Field\FieldItemListInterface; | ||
use Drupal\Core\Field\FieldItemInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* @coversDefaultClass \Drupal\stanford_fields\Plugin\Validation\Constraint\RelativeLinkFieldItemConstraintValidator | ||
*/ | ||
class LinkFieldItemConstraintValidatorTest extends UnitTestCase { | ||
|
||
/** | ||
* Data provider for validator. | ||
* | ||
* @return array[] | ||
*/ | ||
public function dataProvider() { | ||
return [ | ||
['http://localhost', 'http://localhost/foo/bar', TRUE], | ||
['http://localhost', '/foo/bar', FALSE], | ||
['http://localhost', 'http://hostlocal/foo/bar', FALSE], | ||
]; | ||
} | ||
|
||
/** | ||
* Tests the validate method. | ||
* | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testValidation($currentDomain, $linkUrl, $shouldHaveViolations) { | ||
// Create mocks for the services and dependencies. | ||
$request_stack = $this->createMock(RequestStack::class); | ||
|
||
$field_item_list = $this->createMock(FieldItemListInterface::class); | ||
$field_item = $this->createMock(FieldItemInterface::class); | ||
$request = $this->createMock(Request::class); | ||
|
||
// Configure the request mock to return a specific scheme and host. | ||
$request->method('getSchemeAndHttpHost')->willReturn($currentDomain); | ||
$request_stack->method('getCurrentRequest')->willReturn($request); | ||
|
||
// Configure the field item list mock to return a field item with a specific URI. | ||
$field_item->method('get')->willReturnSelf(); | ||
$field_item->method('getString')->willReturn($linkUrl); | ||
$field_item_list->method('get')->with(0)->willReturn($field_item); | ||
|
||
$container = new Container(); | ||
$container->set('request_stack', $request_stack); | ||
|
||
$validator = $this->createMock(ValidatorInterface::class); | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
$translator->method('trans')->willReturnCallback(fn($message) => $message); | ||
$context = new ExecutionContext($validator, NULL, $translator); | ||
|
||
// Instantiate the validator and set the context. | ||
$validator = TestRelativeLinkValidator::create($container); | ||
$validator->initialize($context); | ||
|
||
$constraint = new RelativeLinkFieldItemConstraint(); | ||
$context->setConstraint($constraint); | ||
|
||
// Call the validate method. | ||
$validator->validate($field_item_list, $constraint); | ||
if ($shouldHaveViolations) { | ||
$this->assertTrue($validator->hasViolation()); | ||
} | ||
else { | ||
$this->assertFalse($validator->hasViolation()); | ||
} | ||
} | ||
|
||
} | ||
|
||
class TestRelativeLinkValidator extends RelativeLinkFieldItemConstraintValidator { | ||
|
||
public function hasViolation() { | ||
return count($this->context->getViolations()) > 0; | ||
} | ||
|
||
} |