mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Added some tests to constraint validators
This commit is contained in:
parent
e72b120c12
commit
2ebb4fef4c
12 changed files with 719 additions and 14 deletions
|
@ -63,7 +63,7 @@ class NoneOfItsChildrenValidator extends ConstraintValidator
|
|||
|
||||
// Check if the targeted parent is the object itself:
|
||||
$entity_id = $entity->getID();
|
||||
if (null !== $entity_id && $entity_id === $value->getID()) {
|
||||
if ($entity === $value || (null !== $entity_id && $entity_id === $value->getID())) {
|
||||
//Set the entity to a valid state
|
||||
$entity->setParent(null);
|
||||
$this->context->buildViolation($constraint->self_message)->addViolation();
|
||||
|
|
|
@ -53,7 +53,7 @@ class SelectableValidator extends ConstraintValidator
|
|||
|
||||
//Check type of value. Validating only works for StructuralDBElements
|
||||
if (!$value instanceof AbstractStructuralDBElement) {
|
||||
throw new UnexpectedValueException($value, 'StructuralDBElement');
|
||||
throw new UnexpectedValueException($value, AbstractStructuralDBElement::class);
|
||||
}
|
||||
|
||||
//Check if the value is not selectable -> show error message then.
|
||||
|
|
|
@ -69,9 +69,12 @@ class UniqueObjectCollectionValidator extends ConstraintValidator
|
|||
|
||||
$violation = $this->context->buildViolation($constraint->message);
|
||||
|
||||
$violation->atPath('[' . $key . ']' . '.' . $constraint->fields[0]);
|
||||
//Use the first supplied field as the target field, or the first defined field name of the element if none is supplied
|
||||
$target_field = $constraint->fields[0] ?? array_keys($element)[0];
|
||||
|
||||
$violation->setParameter('{{ value }}', $this->formatValue($value))
|
||||
$violation->atPath('[' . $key . ']' . '.' . $target_field);
|
||||
|
||||
$violation->setParameter('{{ object }}', $this->formatValue($object, ConstraintValidator::OBJECT_TO_STRING))
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->addViolation();
|
||||
|
||||
|
|
|
@ -22,8 +22,20 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
class ValidGoogleAuthCode extends Constraint
|
||||
{
|
||||
/**
|
||||
* @param TwoFactorInterface|null $user The user to use for the validation process, if null, the current user is used
|
||||
*/
|
||||
public function __construct(
|
||||
array $options = null,
|
||||
string $message = null,
|
||||
array $groups = null,
|
||||
public ?TwoFactorInterface $user = null)
|
||||
{
|
||||
parent::__construct($options, $message, $groups);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,10 @@ declare(strict_types=1);
|
|||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
@ -36,7 +38,7 @@ use function strlen;
|
|||
|
||||
class ValidGoogleAuthCodeValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(protected GoogleAuthenticatorInterface $googleAuthenticator)
|
||||
public function __construct(private GoogleAuthenticatorInterface $googleAuthenticator, private Security $security)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -56,23 +58,24 @@ class ValidGoogleAuthCodeValidator extends ConstraintValidator
|
|||
|
||||
if (!ctype_digit($value)) {
|
||||
$this->context->addViolation('validator.google_code.only_digits_allowed');
|
||||
return;
|
||||
}
|
||||
|
||||
//Number must have 6 digits
|
||||
if (6 !== strlen($value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_digit_count');
|
||||
return;
|
||||
}
|
||||
|
||||
//Try to retrieve the user we want to check
|
||||
if ($this->context->getObject() instanceof FormInterface &&
|
||||
$this->context->getObject()->getParent() instanceof FormInterface
|
||||
&& $this->context->getObject()->getParent()->getData() instanceof User) {
|
||||
$user = $this->context->getObject()->getParent()->getData();
|
||||
//Use the current user to check the code
|
||||
$user = $constraint->user ?? $this->security->getUser();
|
||||
if (!$user instanceof TwoFactorInterface) {
|
||||
throw new UnexpectedValueException($user, TwoFactorInterface::class);
|
||||
}
|
||||
|
||||
//Check if the given code is valid
|
||||
if (!$this->googleAuthenticator->checkCode($user, $value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_code');
|
||||
}
|
||||
//Check if the given code is valid
|
||||
if (!$this->googleAuthenticator->checkCode($user, $value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_code');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
139
tests/Validator/Constraints/NoneOfItsChildrenValidatorTest.php
Normal file
139
tests/Validator/Constraints/NoneOfItsChildrenValidatorTest.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
use App\Validator\Constraints\NoneOfItsChildren;
|
||||
use App\Validator\Constraints\NoneOfItsChildrenValidator;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class NoneOfItsChildrenValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
|
||||
protected AttachmentType $root_node;
|
||||
protected AttachmentType $child1;
|
||||
protected AttachmentType $child2;
|
||||
protected AttachmentType $child3;
|
||||
protected AttachmentType $child1_1;
|
||||
protected AttachmentType $child1_2;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
|
||||
parent::setUp();
|
||||
|
||||
//Build a simple hierachy
|
||||
$this->root_node = new AttachmentType();
|
||||
$this->root_node->setName('root')->setParent(null);
|
||||
$this->child1 = new AttachmentType();
|
||||
$this->child1->setParent($this->root_node)->setName('child1');
|
||||
$this->child2 = new AttachmentType();
|
||||
$this->child2->setName('child2')->setParent($this->root_node);
|
||||
$this->child3 = new AttachmentType();
|
||||
$this->child3->setName('child3')->setParent($this->root_node);
|
||||
$this->child1_1 = new AttachmentType();
|
||||
$this->child1_1->setName('child1_1')->setParent($this->child1);
|
||||
$this->child1_2 = new AttachmentType();
|
||||
$this->child1_2->setName('child1_2')->setParent($this->child1);
|
||||
}
|
||||
|
||||
private function getDummyObjects(): array
|
||||
{
|
||||
$obj1 = new AttachmentType();
|
||||
}
|
||||
|
||||
protected function createValidator(): NoneOfItsChildrenValidator
|
||||
{
|
||||
return new NoneOfItsChildrenValidator();
|
||||
}
|
||||
|
||||
public function testNullIsValid(): void
|
||||
{
|
||||
$this->setObject($this->child1);
|
||||
$this->validator->validate(null, new NoneOfItsChildren());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testWithUnrelatedObject(): void
|
||||
{
|
||||
$this->setObject($this->child1);
|
||||
$this->validator->validate(new AttachmentType(), new NoneOfItsChildren());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testWithParentObject(): void
|
||||
{
|
||||
$this->setObject($this->child1);
|
||||
$this->validator->validate($this->root_node, new NoneOfItsChildren());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testWithIntermediateChild(): void
|
||||
{
|
||||
$this->setObject($this->child1);
|
||||
$this->validator->validate($this->child1_1, new NoneOfItsChildren());
|
||||
$this->buildViolation('validator.noneofitschild.children')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testWithIndirectChild(): void
|
||||
{
|
||||
$this->setObject($this->root_node);
|
||||
$this->validator->validate($this->child1_1, new NoneOfItsChildren());
|
||||
$this->buildViolation('validator.noneofitschild.children')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testWithSelfInstance(): void
|
||||
{
|
||||
$this->setObject($this->root_node);
|
||||
$this->validator->validate($this->root_node, new NoneOfItsChildren());
|
||||
$this->buildViolation('validator.noneofitschild.self')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testWithSelfByID(): void
|
||||
{
|
||||
$obj1 = new class extends AbstractStructuralDBElement {
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = 1;
|
||||
parent::__construct();
|
||||
}
|
||||
};
|
||||
|
||||
$obj2 = new class extends AbstractStructuralDBElement {
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = 1;
|
||||
parent::__construct();
|
||||
}
|
||||
};
|
||||
|
||||
$this->setObject($obj1);
|
||||
$this->validator->validate($obj2, new NoneOfItsChildren());
|
||||
$this->buildViolation('validator.noneofitschild.self')
|
||||
->assertRaised();
|
||||
|
||||
}
|
||||
}
|
71
tests/Validator/Constraints/SelectableValidatorTest.php
Normal file
71
tests/Validator/Constraints/SelectableValidatorTest.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Validator\Constraints\Selectable;
|
||||
use App\Validator\Constraints\SelectableValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class SelectableValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator(): SelectableValidator
|
||||
{
|
||||
return new SelectableValidator();
|
||||
}
|
||||
|
||||
public function testNullIsValid(): void
|
||||
{
|
||||
$this->validator->validate(null, new Selectable());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testExpectAbstractStructuralElement(): void
|
||||
{
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->validator->validate('test', new Selectable());
|
||||
}
|
||||
|
||||
public function testWithSelectableObj(): void
|
||||
{
|
||||
$selectable_obj = new AttachmentType();
|
||||
$selectable_obj->setNotSelectable(false);
|
||||
|
||||
$this->validator->validate($selectable_obj, new Selectable());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testWithNotSelectableObj(): void
|
||||
{
|
||||
$selectable_obj = new AttachmentType();
|
||||
$selectable_obj->setNotSelectable(true);
|
||||
$selectable_obj->setName('Test');
|
||||
|
||||
$this->validator->validate($selectable_obj, new Selectable());
|
||||
$this->buildViolation('validator.isSelectable')
|
||||
->setParameter('{{ name }}', 'Test')
|
||||
->setParameter('{{ full_path }}', 'Test')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Tests\Validator\DummyUniqueValidatableObject;
|
||||
use App\Validator\Constraints\UniqueObjectCollection;
|
||||
use App\Validator\Constraints\UniqueObjectCollectionValidator;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class UniqueObjectCollectionValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator(): UniqueObjectCollectionValidator
|
||||
{
|
||||
return new UniqueObjectCollectionValidator();
|
||||
}
|
||||
|
||||
public function testEmptyCollection(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([]), new UniqueObjectCollection());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testUnqiueCollectionDefaultSettings(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 2, 'b' => 1])
|
||||
]),
|
||||
new UniqueObjectCollection());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testUnqiueCollectionSpecifiedFields(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 2, 'b' => 1])
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['a']));
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testExpectsIterableElement(): void
|
||||
{
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->validator->validate('string', new UniqueObjectCollection());
|
||||
}
|
||||
|
||||
public function testExpectsUniqueValidatableObject(): void
|
||||
{
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->validator->validate(new ArrayCollection([new \stdClass()]), new UniqueObjectCollection());
|
||||
}
|
||||
|
||||
public function testNonUniqueCollectionDefaultSettings(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1])
|
||||
]),
|
||||
new UniqueObjectCollection());
|
||||
|
||||
$this
|
||||
->buildViolation('This collection should contain only unique elements.')
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->setParameter('{{ object }}', 'objectString')
|
||||
->atPath('property.path[1].a')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testNonUniqueCollectionSpecifyFields(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1])
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['b']));
|
||||
|
||||
$this
|
||||
->buildViolation('This collection should contain only unique elements.')
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->setParameter('{{ object }}', 'objectString')
|
||||
->atPath('property.path[1].b')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testNonUniqueCollectionFirstFieldIsTarget(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => 1])
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['b', 'a']));
|
||||
|
||||
$this
|
||||
->buildViolation('This collection should contain only unique elements.')
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->setParameter('{{ object }}', 'objectString')
|
||||
->atPath('property.path[1].b')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testNonUniqueCollectionAllowNull(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => null]),
|
||||
new DummyUniqueValidatableObject(['a' => 2, 'b' => 2]),
|
||||
new DummyUniqueValidatableObject(['a' => 3, 'b' => null])
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['b'], allowNull: true));
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testNonUniqueCollectionDoNotAllowNull(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1, 'b' => null]),
|
||||
new DummyUniqueValidatableObject(['a' => 2, 'b' => 2]),
|
||||
new DummyUniqueValidatableObject(['a' => 3, 'b' => null])
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['b'], allowNull: false));
|
||||
|
||||
$this
|
||||
->buildViolation('This collection should contain only unique elements.')
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->setParameter('{{ object }}', 'objectString')
|
||||
->atPath('property.path[2].b')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
73
tests/Validator/Constraints/UrlOrBuiltinValidatorTest.php
Normal file
73
tests/Validator/Constraints/UrlOrBuiltinValidatorTest.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Validator\Constraints\UrlOrBuiltin;
|
||||
use App\Validator\Constraints\UrlOrBuiltinValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class UrlOrBuiltinValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
|
||||
protected function createValidator(): UrlOrBuiltinValidator
|
||||
{
|
||||
return new UrlOrBuiltinValidator();
|
||||
}
|
||||
|
||||
public function testNullIsValid(): void
|
||||
{
|
||||
$this->validator->validate(null, new UrlOrBuiltin());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testEmptyStringIsValid(): void
|
||||
{
|
||||
$this->validator->validate('', new UrlOrBuiltin());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidUrlIsValid(): void
|
||||
{
|
||||
$this->validator->validate('https://example.com', new UrlOrBuiltin());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidBuiltinIsValid(): void
|
||||
{
|
||||
$this->validator->validate('%FOOTPRINTS%/test/footprint.png', new UrlOrBuiltin());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testInvalidUrlIsInvalid(): void
|
||||
{
|
||||
$constraint = new UrlOrBuiltin([
|
||||
'message' => 'myMessage',
|
||||
]);
|
||||
|
||||
$this->validator->validate('invalid-url', $constraint);
|
||||
|
||||
$this->buildViolation('myMessage')
|
||||
->setParameter('{{ value }}', '"invalid-url"')
|
||||
->setCode(UrlOrBuiltin::INVALID_URL_ERROR)
|
||||
->assertRaised();
|
||||
}
|
||||
}
|
143
tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php
Normal file
143
tests/Validator/Constraints/ValidGoogleAuthCodeValidatorTest.php
Normal file
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use App\Validator\Constraints\ValidGoogleAuthCode;
|
||||
use App\Validator\Constraints\ValidGoogleAuthCodeValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class ValidGoogleAuthCodeValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
|
||||
protected function createValidator()
|
||||
{
|
||||
$googleAuth = new class implements GoogleAuthenticatorInterface
|
||||
{
|
||||
|
||||
public function checkCode(TwoFactorInterface $user, string $code): bool
|
||||
{
|
||||
if ($code === '123456') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getQRContent(TwoFactorInterface $user): string
|
||||
{
|
||||
return 'not_needed';
|
||||
}
|
||||
|
||||
public function generateSecret(): string
|
||||
{
|
||||
return 'not_needed';
|
||||
}
|
||||
};
|
||||
|
||||
$security = new class extends Security {
|
||||
public function __construct()
|
||||
{
|
||||
//Leave empty
|
||||
}
|
||||
public function getUser(): ?\Symfony\Component\Security\Core\User\UserInterface
|
||||
{
|
||||
return new class implements TwoFactorInterface, UserInterface {
|
||||
|
||||
public function isGoogleAuthenticatorEnabled(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getGoogleAuthenticatorUsername(): string
|
||||
{
|
||||
return "test";
|
||||
}
|
||||
|
||||
public function getGoogleAuthenticatorSecret(): ?string
|
||||
{
|
||||
return "not_needed";
|
||||
}
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function eraseCredentials()
|
||||
{
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return 'test';
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return new ValidGoogleAuthCodeValidator($googleAuth, $security);
|
||||
}
|
||||
|
||||
public function testAllowNull(): void
|
||||
{
|
||||
$this->validator->validate(null, new ValidGoogleAuthCode());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testAllowEmpty(): void
|
||||
{
|
||||
$this->validator->validate('', new ValidGoogleAuthCode());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidCode(): void
|
||||
{
|
||||
$this->validator->validate('123456', new ValidGoogleAuthCode());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testInvalidCode(): void
|
||||
{
|
||||
$this->validator->validate('111111', new ValidGoogleAuthCode());
|
||||
$this->buildViolation('validator.google_code.wrong_code')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testCheckNumerical(): void
|
||||
{
|
||||
$this->validator->validate('123456a', new ValidGoogleAuthCode());
|
||||
$this->buildViolation('validator.google_code.only_digits_allowed')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testCheckLength(): void
|
||||
{
|
||||
$this->validator->validate('12345', new ValidGoogleAuthCode());
|
||||
$this->buildViolation('validator.google_code.wrong_digit_count')
|
||||
->assertRaised();
|
||||
}
|
||||
}
|
63
tests/Validator/Constraints/ValidThemeValidatorTest.php
Normal file
63
tests/Validator/Constraints/ValidThemeValidatorTest.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Validator\Constraints\ValidTheme;
|
||||
use App\Validator\Constraints\ValidThemeValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class ValidThemeValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
|
||||
protected function createValidator(): ValidThemeValidator
|
||||
{
|
||||
return new ValidThemeValidator(['bootstrap', 'theme1', 'theme2']);
|
||||
}
|
||||
|
||||
public function testAllowNull(): void
|
||||
{
|
||||
$this->validator->validate(null, new ValidTheme());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testAllowEmpty(): void
|
||||
{
|
||||
$this->validator->validate('', new ValidTheme());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidTheme(): void
|
||||
{
|
||||
$this->validator->validate('bootstrap', new ValidTheme());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testInvalidTheme(): void
|
||||
{
|
||||
$this->validator->validate('invalid', new ValidTheme());
|
||||
$this->buildViolation('validator.selected_theme_is_invalid')
|
||||
->setParameter('{{ value }}', 'invalid')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
|
||||
}
|
41
tests/Validator/DummyUniqueValidatableObject.php
Normal file
41
tests/Validator/DummyUniqueValidatableObject.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Validator;
|
||||
|
||||
use App\Validator\UniqueValidatableInterface;
|
||||
|
||||
class DummyUniqueValidatableObject implements UniqueValidatableInterface, \Stringable
|
||||
{
|
||||
|
||||
public function __construct(public readonly array $array = [])
|
||||
{
|
||||
}
|
||||
|
||||
public function getComparableFields(): array
|
||||
{
|
||||
return $this->array;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'objectString';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue