silverstripe-userforms/tests/Form/UserFormsRequiredFieldsTest.php

148 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Tests\Form;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Form\UserFormsRequiredFields;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Form\UserForm;
class UserFormsRequiredFieldsTest extends SapphireTest
{
protected static $fixture_file = '../UserFormsTest.yml';
private function getValidatorFromPage($page)
{
$controller = ModelAsController::controller_for($page);
$form = new UserForm($controller);
return $form->getValidator();
}
public function testUsesUserFormsRequiredFieldsValidator()
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-custom-rules-form');
$this->assertEquals(3, $page->Fields()->count());
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);
$this->assertInstanceOf(UserFormsRequiredFields::class, $validator, 'Uses UserFormsRequiredFields validator');
}
2020-01-13 00:15:34 +01:00
public function dataProviderValidationOfConditionalRequiredFields()
{
2020-01-13 00:15:34 +01:00
return [
'Passes when non-conditional required field has a value' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'N',
'conditional-required-text' => ''
2020-01-13 00:15:34 +01:00
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => ''
2020-01-13 00:15:34 +01:00
],
false
],
'Passes when conditional required field has a value' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => 'some more text'
2020-01-13 00:15:34 +01:00
],
true
]
];
}
2020-01-13 00:15:34 +01:00
/**
* @param $data
* @param $expected
* @dataProvider dataProviderValidationOfConditionalRequiredFields
*/
public function testValidationOfConditionalRequiredFields($data, $expected)
{
2020-01-13 00:15:34 +01:00
$page = $this->objFromFixture(UserDefinedForm::class, 'required-custom-rules-form');
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);
$this->assertFalse(
$validator->php([]),
'Fails when non-conditional required field is empty'
);
2020-01-13 00:15:34 +01:00
$this->assertEquals($expected, $validator->php($data));
}
public function dataProviderValidationOfNestedConditionalRequiredFields()
{
return [
'Fails when non-conditional required field is empty' => [[], false],
'Passes when non-conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
2020-01-13 00:15:34 +01:00
'radio-option-3' => 'N',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
2020-01-13 00:15:34 +01:00
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'required-text-field-3' => 'some text',
2020-01-13 00:15:34 +01:00
'radio-option-3' => 'Y',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
2020-01-13 00:15:34 +01:00
],
false
],
'Passes when non-conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'this text',
'conditional-required-text-3' => ''
2020-01-13 00:15:34 +01:00
],
true
],
'Fails when nested conditional required is displayed but not completed' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => ''
2020-01-13 00:15:34 +01:00
],
false
],
'Passes when nested conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => 'more text'
2020-01-13 00:15:34 +01:00
],
true
]
];
}
/**
* @param string $data
* @param array $expected
* @dataProvider dataProviderValidationOfNestedConditionalRequiredFields
*/
public function testValidationOfNestedConditionalRequiredFields($data, $expected)
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-nested-custom-rules-form');
$this->assertEquals(4, $page->Fields()->count());
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);
$this->assertEquals($expected, $validator->php($data));
}
}