/** * Test different scenarii for a failed upload : an error occured, no files where provided */ public function testUploadMissingRequiredFile() { /** @skipUpgrade */ $form = new Form(new Controller(), 'Form', new FieldList($fileField = new FileField('cv', 'Upload your CV')), new FieldList(), new RequiredFields('cv')); // All fields are filled but for some reason an error occured when uploading the file => fails $fileFieldValue = array('name' => 'aCV.txt', 'type' => 'application/octet-stream', 'tmp_name' => '/private/var/tmp/phpzTQbqP', 'error' => 1, 'size' => 3471); $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'An error occured when uploading a file, but the validator returned true'); // We pass an empty set of parameters for the uploaded file => fails $fileFieldValue = array(); $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'An empty array was passed as parameter for an uploaded file, but the validator returned true'); // We pass an null value for the uploaded file => fails $fileFieldValue = null; $fileField->setValue($fileFieldValue); $this->assertFalse($form->validate(), 'A null value was passed as parameter for an uploaded file, but the validator returned true'); }
public function testGetStateWithFieldValidationErrors() { $fields = new FieldList(new TextField('Title')); $actions = new FieldList(); $validator = new RequiredFields('Title'); $form = new Form(new Controller(), 'TestForm', $fields, $actions, $validator); $form->loadDataFrom(['Title' => null]); $this->assertFalse($form->validate()); $formSchema = new FormSchema(); $expected = ['id' => 'Form_TestForm', 'fields' => [['id' => 'Form_TestForm_Title', 'value' => null, 'message' => ['value' => ['html' => '"Title" is required'], 'type' => 'required'], 'data' => [], 'name' => 'Title'], ['id' => 'Form_TestForm_SecurityID', 'value' => $form->getSecurityToken()->getValue(), 'message' => null, 'data' => [], 'name' => 'SecurityID']], 'valid' => false, 'messages' => []]; $state = $formSchema->getState($form); $this->assertInternalType('array', $state); $this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); }
public function testValidation() { $field = OptionsetField::create('Test', 'Testing', array("One" => "One", "Two" => "Two", "Five" => "Five")); $validator = new RequiredFields('Test'); /** @skipUpgrade */ $form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator); $field->setValue("One"); $this->assertTrue($field->validate($validator)); //non-existent value should make the field invalid $field->setValue("Three"); $this->assertFalse($field->validate($validator)); //empty string should pass field-level validation... $field->setValue(''); $this->assertTrue($field->validate($validator)); // ... but should not pass "RequiredFields" validation $this->assertFalse($form->validate()); //disabled items shouldn't validate $field->setDisabledItems(array('Five')); $field->setValue('Five'); $this->assertFalse($field->validate($validator)); }
public function testLookupFieldDisabledSaving() { $object = new DataObjectTest_Team(); $form = new Form(new Controller(), 'Form', new FieldList(new LookupField('Players', 'Players')), new FieldList()); $form->loadDataFrom(array('Players' => array(14, 18, 22))); $form->saveInto($object); $playersIds = $object->Players()->getIDList(); $this->assertTrue($form->validate()); $this->assertEquals($playersIds, array(), 'saveInto() should not save into the DataObject for the LookupField'); }
public function testGetSchemaStateWithFormValidation() { $field = new FormField('MyField', 'My Field'); $validator = new RequiredFields('MyField'); $form = new Form(new Controller(), 'TestForm', new FieldList($field), new FieldList(), $validator); $form->validate(); $form->setupFormErrors(); $schema = $field->getSchemaState(); $this->assertEquals(['html' => '"My Field" is required'], $schema['message']['value']); }