-
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.
D8CORE-7715: Add constraint plugin and field validation plugin to sup…
…port sunet validation on fields (#24) * D8CORE-7715: Add constraint plugin and field validation plugin to support sunet validation on fields * Added tests * added service in config form * revert drush refactor
- Loading branch information
Showing
11 changed files
with
254 additions
and
10 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
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
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
71 changes: 71 additions & 0 deletions
71
src/Plugin/FieldValidationRule/ValidSunetIDFieldValidationRule.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,71 @@ | ||
<?php | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\FieldValidationRule; | ||
|
||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\field_validation\ConstraintFieldValidationRuleBase; | ||
|
||
/** | ||
* Provides functionality for ValidSunetIDValidation. | ||
* | ||
* @FieldValidationRule( | ||
* id = "valid_sunetid_constraint_rule", | ||
* label = @Translation("Valid SunetID"), | ||
* description = @Translation("Validate string is valid sunet.") | ||
* ) | ||
*/ | ||
class ValidSunetIDFieldValidationRule extends ConstraintFieldValidationRuleBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConstraintName(): string { | ||
return "ValidSunetID"; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isPropertyConstraint(): bool { | ||
return TRUE; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return ['message' => 'This is not a valid SunetID.'] + parent::defaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfiguration() { | ||
return $this->configuration + parent::getConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | ||
$form = parent::buildConfigurationForm($form, $form_state); | ||
|
||
$form['message'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('Message'), | ||
'#default_value' => $this->configuration['message'], | ||
'#maxlength' => 255, | ||
]; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | ||
parent::submitConfigurationForm($form, $form_state); | ||
$this->configuration['message'] = $form_state->getValue('message'); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/Plugin/Validation/Constraint/ValidSunetIDConstraint.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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Symfony\Component\Validator\Constraint as SymfonyConstraint; | ||
use Drupal\Core\Validation\Attribute\Constraint; | ||
|
||
/** | ||
* Provides a Valid SunetID constraint. | ||
*/ | ||
#[Constraint( | ||
id: 'ValidSunetID', | ||
label: new TranslatableMarkup('Valid SunetID', [], ['context' => 'Validation']), | ||
)] | ||
final class ValidSunetIDConstraint extends SymfonyConstraint { | ||
|
||
public string $message = '@sunetid is not a valid SunetID.'; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/Plugin/Validation/Constraint/ValidSunetIDConstraintValidator.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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\stanford_samlauth\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\stanford_samlauth\Service\WorkgroupApiInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
/** | ||
* Validates the Valid SunetID constraint. | ||
*/ | ||
final class ValidSunetIDConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { | ||
|
||
/** | ||
* Constructs the object. | ||
*/ | ||
public function __construct(private readonly WorkgroupApiInterface $workgroupApi) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container): self { | ||
return new self($container->get('stanford_samlauth.workgroup_api')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate(mixed $item, Constraint $constraint): void { | ||
if ($this->workgroupApi->connectionSuccessful() && !$this->workgroupApi->isSunetValid($item)) { | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
|
||
} |
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
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
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
51 changes: 51 additions & 0 deletions
51
tests/src/Unit/Plugin/FieldValidationRule/ValidSunetIDFieldValidationRuleTest.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\Tests\stanford_samlauth\Unit\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drupal\Core\Form\FormState; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
use Drupal\Core\Logger\LoggerChannelInterface; | ||
use Drupal\Core\Utility\Token; | ||
use Drupal\stanford_samlauth\Plugin\FieldValidationRule\ValidSunetIDFieldValidationRule; | ||
use Drupal\Tests\UnitTestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Drupal\stanford_samlauth\Plugin\FieldValidationRule\ValidSunetIDFieldValidationRule | ||
*/ | ||
class ValidSunetIDFieldValidationRuleTest extends UnitTestCase { | ||
|
||
protected $validationRule; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$logger_channel = $this->createMock(LoggerChannelInterface::class); | ||
|
||
$logger = $this->createMock(LoggerChannelFactoryInterface::class); | ||
$logger->method('get')->willReturn($logger_channel); | ||
|
||
$container = new ContainerBuilder(); | ||
$container->set('logger.factory', $logger); | ||
$container->set('token', $this->createMock(Token::class)); | ||
$container->set('string_translation', $this->getStringTranslationStub()); | ||
\Drupal::setContainer($container); | ||
|
||
$this->validationRule = ValidSunetIDFieldValidationRule::create($container, [], 'foo', []); | ||
} | ||
|
||
public function testFieldValidation() { | ||
$this->assertEquals('ValidSunetID', $this->validationRule->getConstraintName()); | ||
$this->assertTrue($this->validationRule->isPropertyConstraint()); | ||
|
||
$form = []; | ||
$form_state = new FormState(); | ||
$this->assertArrayHasKey('message', $this->validationRule->buildConfigurationForm($form, $form_state)); | ||
|
||
$form_state->setValue('message', 'foobarbaz'); | ||
$this->validationRule->submitConfigurationForm($form, $form_state); | ||
|
||
$this->assertEquals('foobarbaz', $this->validationRule->getConfiguration()['message']); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
tests/src/Unit/Plugin/Validation/Constraint/ValidSunetIDConstraintValidatorTest.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,39 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\stanford_samlauth\Unit\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraint; | ||
use Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraintValidator; | ||
use Drupal\stanford_samlauth\Service\WorkgroupApiInterface; | ||
use Drupal\Tests\UnitTestCase; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
/** | ||
* @coversDefaultClass \Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraintValidator | ||
*/ | ||
class ValidSunetIDConstraintValidatorTest extends UnitTestCase { | ||
|
||
protected $plugin; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$workgroup_api = $this->createMock(WorkgroupApiInterface::class); | ||
$workgroup_api->method('connectionSuccessful')->willReturn(TRUE); | ||
$workgroup_api->method('isSunetValid')->willReturn(FALSE); | ||
|
||
$container = new ContainerBuilder(); | ||
$container->set('stanford_samlauth.workgroup_api', $workgroup_api); | ||
$this->plugin = ValidSunetIDConstraintValidator::create($container); | ||
|
||
$context = $this->createMock(ExecutionContextInterface::class); | ||
$this->plugin->initialize($context); | ||
} | ||
|
||
public function testValidation() { | ||
$constraint = new ValidSunetIDConstraint(); | ||
$this->assertNull($this->plugin->validate('foobar', $constraint)); | ||
} | ||
|
||
} |