2020-02-13 20:21:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2020-05-27 23:52:28 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
2020-02-13 20:21:22 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
|
|
|
|
use SilverStripe\Forms\TextField;
|
2020-05-27 23:35:07 +02:00
|
|
|
use SilverStripe\Forms\CompositeValidator;
|
2020-02-13 20:21:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2020-05-27 23:35:07 +02:00
|
|
|
class CompositeValidatorTest extends SapphireTest
|
2020-02-13 20:21:22 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Common method for setting up form, since that will always be a dependency for the validator.
|
|
|
|
*
|
|
|
|
* @param array $fieldNames
|
|
|
|
* @return Form
|
|
|
|
*/
|
2020-06-07 23:35:00 +02:00
|
|
|
protected function getForm(array $fieldNames = []): Form
|
2020-02-13 20:21:22 +01:00
|
|
|
{
|
|
|
|
// Setup field list now. We're only worried about names right now
|
|
|
|
$fieldList = new FieldList();
|
|
|
|
|
|
|
|
foreach ($fieldNames as $name) {
|
|
|
|
$fieldList->add(new TextField($name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Form(Controller::curr(), "testForm", $fieldList, new FieldList([/* no actions */]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddValidator(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$this->assertCount(2, $compositeValidator->getValidators());
|
2020-02-13 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 23:52:28 +02:00
|
|
|
/**
|
|
|
|
* @throws ReflectionException
|
|
|
|
*/
|
2020-02-13 20:21:22 +01:00
|
|
|
public function testSetForm(): void
|
|
|
|
{
|
|
|
|
$form = $this->getForm();
|
|
|
|
|
2020-05-27 23:52:28 +02:00
|
|
|
$reflectionClass = new ReflectionClass(CompositeValidator::class);
|
|
|
|
$property = $reflectionClass->getProperty('form');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
$compositeValidator = new CompositeValidator();
|
2020-02-13 20:21:22 +01:00
|
|
|
$validator = new TestValidator();
|
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->addValidator($validator);
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->setForm($form);
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:52:28 +02:00
|
|
|
$this->assertNotNull($property->getValue($compositeValidator));
|
2020-05-27 23:35:07 +02:00
|
|
|
$this->assertCount(1, $compositeValidator->getValidators());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
foreach ($compositeValidator->getValidators() as $validator) {
|
2020-02-13 20:21:22 +01:00
|
|
|
/** @var TestValidator $validator */
|
2020-05-27 23:52:28 +02:00
|
|
|
$this->assertNotNull($property->getValue($validator));
|
2020-02-13 20:21:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValidatorsByType(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
|
|
|
$compositeValidator->addValidator(new TestValidator());
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
|
|
|
$compositeValidator->addValidator(new TestValidator());
|
|
|
|
|
|
|
|
$this->assertCount(4, $compositeValidator->getValidators());
|
|
|
|
$this->assertCount(2, $compositeValidator->getValidatorsByType(RequiredFields::class));
|
2020-02-13 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveValidatorsByType(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
|
|
|
$compositeValidator->addValidator(new TestValidator());
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
|
|
|
$compositeValidator->addValidator(new TestValidator());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$this->assertCount(4, $compositeValidator->getValidators());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->removeValidatorsByType(RequiredFields::class);
|
|
|
|
$this->assertCount(2, $compositeValidator->getValidators());
|
2020-02-13 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanBeCached(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new RequiredFields());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$this->assertTrue($compositeValidator->canBeCached());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Foor']));
|
2020-02-13 20:21:22 +01:00
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$this->assertFalse($compositeValidator->canBeCached());
|
2020-02-13 20:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldIsRequired(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
2020-02-13 20:21:22 +01:00
|
|
|
|
|
|
|
$fieldNames = [
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
];
|
|
|
|
|
|
|
|
$requiredFieldsFirst = new RequiredFields(
|
|
|
|
[
|
|
|
|
$fieldNames[0],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$requiredFieldsSecond = new RequiredFields(
|
|
|
|
[
|
|
|
|
$fieldNames[1],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->addValidator($requiredFieldsFirst);
|
|
|
|
$compositeValidator->addValidator($requiredFieldsSecond);
|
2020-02-13 20:21:22 +01:00
|
|
|
|
|
|
|
foreach ($fieldNames as $field) {
|
|
|
|
$this->assertTrue(
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->fieldIsRequired($field),
|
2020-02-13 20:21:22 +01:00
|
|
|
sprintf('Failed to find "%s" field in required list', $field)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidate(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
2020-02-13 20:21:22 +01:00
|
|
|
// Add two separate validators, each with one required field
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Foo']));
|
|
|
|
$compositeValidator->addValidator(new RequiredFields(['Bar']));
|
2020-02-13 20:21:22 +01:00
|
|
|
|
|
|
|
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now)
|
|
|
|
// We'll add three empty fields, but only two of them should be required
|
|
|
|
$data = [
|
|
|
|
'Foo' => '',
|
|
|
|
'Bar' => '',
|
|
|
|
'FooBar' => '',
|
|
|
|
];
|
|
|
|
// We only care right now about the fields we've got setup in this array
|
2022-04-14 03:12:59 +02:00
|
|
|
$form = $this->getForm(array_keys($data ?? []));
|
2020-02-13 20:21:22 +01:00
|
|
|
$form->disableSecurityToken();
|
|
|
|
// Setup validator now that we've got our form
|
2020-05-27 23:35:07 +02:00
|
|
|
$form->setValidator($compositeValidator);
|
2020-02-13 20:21:22 +01:00
|
|
|
// Put data into the form so the validator can pull it back out again
|
|
|
|
$form->loadDataFrom($data);
|
|
|
|
|
|
|
|
$result = $form->validationResult();
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertCount(2, $result->getMessages());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveValidation(): void
|
|
|
|
{
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator = new CompositeValidator();
|
|
|
|
$compositeValidator->addValidator(new TestValidator());
|
2020-02-13 20:21:22 +01:00
|
|
|
|
|
|
|
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now)
|
|
|
|
$data = [
|
|
|
|
'Foo' => '',
|
|
|
|
];
|
|
|
|
// We only care right now about the fields we've got setup in this array
|
2022-04-14 03:12:59 +02:00
|
|
|
$form = $this->getForm(array_keys($data ?? []));
|
2020-02-13 20:21:22 +01:00
|
|
|
$form->disableSecurityToken();
|
|
|
|
// Setup validator now that we've got our form
|
2020-05-27 23:35:07 +02:00
|
|
|
$form->setValidator($compositeValidator);
|
2020-02-13 20:21:22 +01:00
|
|
|
// Put data into the form so the validator can pull it back out again
|
|
|
|
$form->loadDataFrom($data);
|
|
|
|
|
|
|
|
$result = $form->validationResult();
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertCount(1, $result->getMessages());
|
|
|
|
|
2020-05-28 02:23:35 +02:00
|
|
|
// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should
|
|
|
|
// reset errors)
|
2020-05-27 23:35:07 +02:00
|
|
|
$compositeValidator->removeValidation();
|
2020-02-13 20:21:22 +01:00
|
|
|
$result = $form->validationResult();
|
|
|
|
$this->assertTrue($result->isValid());
|
|
|
|
$this->assertEmpty($result->getMessages());
|
|
|
|
}
|
|
|
|
}
|