-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation: At least 1 idp must be selected
Prior to this change, when no idp was selected in the Entity IDP selection when publishing an Entity, the form validation would handle, and and error was thrown. This change adds form validation that at least one IDP must be selected. Resolves #1331
- Loading branch information
Showing
8 changed files
with
189 additions
and
3 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
52 changes: 52 additions & 0 deletions
52
...iderDashboard/Infrastructure/DashboardBundle/Validator/Constraints/AtLeastOneSelected.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,52 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Validator\Constraints; | ||
|
||
use Attribute; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Validator\Attribute\HasNamedArguments; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
#[Attribute] | ||
class AtLeastOneSelected extends Constraint | ||
{ | ||
|
||
/** | ||
* @param string[] $fieldNames | ||
*/ | ||
#[HasNamedArguments] | ||
public function __construct( | ||
public array $fieldNames, | ||
?array $groups = null, | ||
) { | ||
foreach ($this->fieldNames as $fieldName) { | ||
if (!is_string($fieldName)) { | ||
throw new InvalidArgumentException($fieldName . ' must be a string.'); | ||
} | ||
} | ||
parent::__construct([], $groups); | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...oard/Infrastructure/DashboardBundle/Validator/Constraints/AtLeastOneSelectedValidator.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,48 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Validator\Constraints; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class AtLeastOneSelectedValidator extends ConstraintValidator | ||
{ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof AtLeastOneSelected) { | ||
throw new UnexpectedTypeException($constraint, AtLeastOneSelected::class); | ||
} | ||
|
||
foreach ($constraint->fieldNames as $fieldName) { | ||
if (!isset($value->{$fieldName}) || !is_array($value->{$fieldName})) { | ||
throw new InvalidArgumentException('$value must have array field with name: "' . $fieldName. '"'); | ||
} | ||
|
||
if (!empty(($value->{$fieldName}))) { | ||
return; | ||
} | ||
} | ||
|
||
$this->context->addViolation('validator.entity.idps.institution-idps'); | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
.../Infrastructure/DashboardBundle/Validator/Constraints/AtLeastOneSelectedValidatorTest.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,73 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Surfnet\ServiceProviderDashboard\Tests\Integration\Infrastructure\DashboardBundle\Validator\Constraints; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
use Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Validator\Constraints\AtLeastOneSelected; | ||
use Surfnet\ServiceProviderDashboard\Infrastructure\DashboardBundle\Validator\Constraints\AtLeastOneSelectedValidator; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
class AtLeastOneSelectedValidatorTest extends TestCase | ||
{ | ||
private $context; | ||
private $validator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->context = $this->createMock(ExecutionContextInterface::class); | ||
$this->validator = new AtLeastOneSelectedValidator(); | ||
$this->validator->initialize($this->context); | ||
} | ||
|
||
public function testNoFieldSelected(): void | ||
{ | ||
$constraint = new AtLeastOneSelected(fieldNames: ['field1', 'field2']); | ||
$value = (object) ['field1' => [], 'field2' => []]; | ||
|
||
$this->context->expects($this->once()) | ||
->method('addViolation'); | ||
|
||
$this->validator->validate($value, $constraint); | ||
} | ||
|
||
public function testOneFieldSelected(): void | ||
{ | ||
$constraint = new AtLeastOneSelected(fieldNames: ['field1', 'field2']); | ||
$value = (object) ['field1' => [0], 'field2' => '']; | ||
|
||
$this->context->expects($this->never()) | ||
->method('buildViolation'); | ||
|
||
$this->validator->validate($value, $constraint); | ||
} | ||
|
||
public function testAllFieldsSelected(): void | ||
{ | ||
$constraint = new AtLeastOneSelected(fieldNames: ['field1', 'field2']); | ||
$value = (object) ['field1' => [1], 'field2' => [2]]; | ||
|
||
$this->context->expects($this->never()) | ||
->method('buildViolation'); | ||
|
||
$this->validator->validate($value, $constraint); | ||
} | ||
} |
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