mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
N°6414 Validator refactoring
New AbstractValidator class, with new method Validate All existing validators are now children of AbstractRegexpValidator Handle validators JS counterparts in renderers : only regexp validators are implemented client side
This commit is contained in:
@@ -4,29 +4,32 @@
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Sources\Form;
|
||||
namespace Combodo\iTop\Test\UnitTest\Sources\Form\Field;
|
||||
|
||||
use Combodo\iTop\Form\Field\StringField;
|
||||
use Combodo\iTop\Form\Validator\CustomRegexpValidator;
|
||||
use Combodo\iTop\Form\Validator\IntegerValidator;
|
||||
use Combodo\iTop\Form\Validator\MandatoryValidator;
|
||||
use Combodo\iTop\Form\Validator\Validator;
|
||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||
|
||||
class FieldTest extends ItopTestCase
|
||||
{
|
||||
public function testIsValidationDisabled(): void
|
||||
public function testIsValidationDisabled(): void
|
||||
{
|
||||
$oField = new StringField('test');
|
||||
$oField->SetCurrentValue('toto@johny.invalid');
|
||||
$oDumbEmailValidator = new Validator('\d+@\d+\.\d{2,3}');
|
||||
$sDumbEmailValidatorErrorMessage = 'dumb email validator error message';
|
||||
$oDumbEmailValidator = new CustomRegexpValidator('\d+@\d+\.\d{2,3}', $sDumbEmailValidatorErrorMessage);
|
||||
$oField->AddValidator($oDumbEmailValidator);
|
||||
|
||||
$bIsFieldValid = $oField->Validate();
|
||||
$this->assertFalse($bIsFieldValid);
|
||||
$this->assertCount(1, $oField->GetErrorMessages());
|
||||
$this->assertSame($sDumbEmailValidatorErrorMessage, $oField->GetErrorMessages()[0]);
|
||||
|
||||
/** @noinspection PhpRedundantOptionalArgumentInspection */
|
||||
$oField->SetValidationDisabled(true);
|
||||
$bIsFieldValidWithValidationDisabled = $oField->Validate();
|
||||
$this->assertTrue($bIsFieldValidWithValidationDisabled);
|
||||
$this->assertTrue($oField->Validate());
|
||||
}
|
||||
|
||||
public function testSetMandatory(): void
|
||||
@@ -60,4 +63,21 @@ class FieldTest extends ItopTestCase
|
||||
$this->assertFalse($oField->Validate());
|
||||
$this->assertCount(1, $oField->GetErrorMessages());
|
||||
}
|
||||
|
||||
public function testValidateWithTwoValidatorsFirstWrong(): void
|
||||
{
|
||||
$oField = new StringField('test');
|
||||
$oField->SetCurrentValue('string with spaces');
|
||||
|
||||
$sFirstValidatorInvalidResultErrorMsg = 'dumb email validator error message';
|
||||
$oFirstValidatorInvalidResult = new CustomRegexpValidator('^[a-z]+$', $sFirstValidatorInvalidResultErrorMsg);
|
||||
$oField->AddValidator($oFirstValidatorInvalidResult);
|
||||
|
||||
$oSecondValidatorValidResult = new CustomRegexpValidator('^.+$', 'valid');
|
||||
$oField->AddValidator($oSecondValidatorValidResult);
|
||||
|
||||
$this->assertFalse($oField->Validate());
|
||||
$this->assertCount(1, $oField->GetErrorMessages());
|
||||
$this->assertSame($sFirstValidatorInvalidResultErrorMsg, $oField->GetErrorMessages()[0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2023 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Sources\Form\Validator;
|
||||
|
||||
use Combodo\iTop\Form\Field\StringField;
|
||||
use Combodo\iTop\Test\UnitTest\ItopTestCase;
|
||||
|
||||
class ValidatorTest extends ItopTestCase
|
||||
{
|
||||
public function testMandatoryValidator()
|
||||
{
|
||||
$oField = new StringField('test');
|
||||
$oField->SetMandatory(true);
|
||||
$oField->SetCurrentValue('there is a value !');
|
||||
|
||||
$bIsMandatoryFieldValidWithExistingValue = $oField->Validate();
|
||||
$this->assertTrue($bIsMandatoryFieldValidWithExistingValue);
|
||||
|
||||
$oField->SetCurrentValue(null);
|
||||
$bIsMandatoryFieldValidWithNoValue = $oField->Validate();
|
||||
$this->assertFalse($bIsMandatoryFieldValidWithNoValue);
|
||||
$this->assertNotEmpty($oField->GetErrorMessages());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user