2016-12-13 22:45:42 +01:00
< ? php
2017-08-15 23:31:47 +02:00
namespace SilverStripe\UserForms\Tests\FormField ;
2017-08-09 01:55:09 +02:00
use SilverStripe\Dev\SapphireTest ;
2018-05-08 07:02:01 +02:00
use SilverStripe\UserForms\Form\UserFormsRequiredFields ;
2017-08-11 02:20:12 +02:00
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField ;
2020-07-21 07:30:55 +02:00
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField ;
2017-08-09 01:55:09 +02:00
2016-12-13 22:45:42 +01:00
class UserFormsCheckboxSetFieldTest extends SapphireTest
{
2020-07-21 07:30:55 +02:00
protected static $fixture_file = '../UserFormsTest.yml' ;
2017-08-11 02:37:03 +02:00
public function testValidate ()
{
2017-08-11 02:20:12 +02:00
$field = new UserFormsCheckboxSetField ( 'Field' , 'My field' , [ 'One' => 'One' , 'Two' => 'Two' ]);
2018-05-08 07:02:01 +02:00
$validator = new UserFormsRequiredFields ();
2016-12-13 22:45:42 +01:00
// String values
$field -> setValue ( 'One' );
$this -> assertTrue ( $field -> validate ( $validator ));
$field -> setValue ( 'One,Two' );
$this -> assertTrue ( $field -> validate ( $validator ));
$field -> setValue ( 'Three,Four' );
$this -> assertFalse ( $field -> validate ( $validator ));
// Array values
$field -> setValue ( array ( 'One' ));
$this -> assertTrue ( $field -> validate ( $validator ));
$field -> setValue ( array ( 'One' , 'Two' ));
$this -> assertTrue ( $field -> validate ( $validator ));
// Invalid
$field -> setValue ( 'Three' );
$this -> assertFalse ( $field -> validate ( $validator ));
$field -> setValue ( array ( 'Three' , 'Four' ));
$this -> assertFalse ( $field -> validate ( $validator ));
}
2020-07-21 07:30:55 +02:00
public function testCustomErrorMessageValidationAttributesHTML ()
{
/** @var EditableCheckboxGroupField $editableCheckboxGroupField */
$editableCheckboxGroupField = $this -> objFromFixture ( EditableCheckboxGroupField :: class , 'checkbox-group' );
$editableCheckboxGroupField -> Required = true ;
$editableCheckboxGroupField -> CustomErrorMessage = 'My custom error message with \'single\' and "double" quotes' ;
$userFormsCheckboxSetField = $editableCheckboxGroupField -> getFormField ();
$html = $userFormsCheckboxSetField -> renderWith ( UserFormsCheckboxSetField :: class ) -> getValue ();
$attributesHTML = 'data-rule-required="true" data-msg-required="My custom error message with &#039;single&#039; and &quot;double&quot; quotes"' ;
2022-04-13 03:52:56 +02:00
$this -> assertTrue ( strpos ( $html ? ? '' , $attributesHTML ? ? '' ) > 0 );
2020-07-21 07:30:55 +02:00
}
2017-08-11 02:20:12 +02:00
}