diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-12-13 12:41:08 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-12-13 12:41:08 +0000 |
commit | 8b4406ef5d9d22f527fb124b335812dd9136532c (patch) | |
tree | bab37bd6a04bd027f6815b13678f3fc31b131eb3 /modules/field/tests | |
parent | d6305a6616d4e9275ae61e82334285e9fb156a62 (diff) | |
download | brdo-8b4406ef5d9d22f527fb124b335812dd9136532c.tar.gz brdo-8b4406ef5d9d22f527fb124b335812dd9136532c.tar.bz2 |
- Patch #552436 by yched: validation of the number of values in field_default_validate().
Diffstat (limited to 'modules/field/tests')
-rw-r--r-- | modules/field/tests/field.test | 83 | ||||
-rw-r--r-- | modules/field/tests/field_test.field.inc | 52 |
2 files changed, 121 insertions, 14 deletions
diff --git a/modules/field/tests/field.test b/modules/field/tests/field.test index 418017f68..1bbb36744 100644 --- a/modules/field/tests/field.test +++ b/modules/field/tests/field.test @@ -40,6 +40,32 @@ class FieldTestCase extends DrupalWebTestCase { } return $values; } + + /** + * Assert that a field has the expected values in an entity. + * + * This function only checks a single column in the field values. + * + * @param $entity + * The entity to test. + * @param $field_name + * The name of the field to test + * @param $langcode + * The language code for the values. + * @param $expected_values + * The array of expected values. + * @param $column + * (Optional) the name of the column to check. + */ + function assertFieldValues($entity, $field_name, $langcode, $expected_values, $column = 'value') { + $e = clone $entity; + field_attach_load('test_entity', array($e->ftid => $e)); + $values = isset($e->{$field_name}[$langcode]) ? $e->{$field_name}[$langcode] : array(); + $this->assertEqual(count($values), count($expected_values), t('Expected number of values were saved.')); + foreach ($expected_values as $key => $value) { + $this->assertEqual($values[$key][$column], $value, t('Value @value was saved correctly.', array('@value' => $value))); + } + } } class FieldAttachTestCase extends FieldTestCase { @@ -1036,7 +1062,6 @@ class FieldAttachOtherTestCase extends FieldAttachTestCase { $values = array(); for ($delta = 0; $delta < $this->field['cardinality']; $delta++) { $values[$delta]['value'] = -1; - $values[$delta]['_error_element'] = 'field_error_' . $delta; } // Arrange for item 1 not to generate an error $values[1]['value'] = 1; @@ -1060,6 +1085,17 @@ class FieldAttachOtherTestCase extends FieldAttachTestCase { } } $this->assertEqual(count($errors[$this->field_name][$langcode]), 0, 'No extraneous errors set'); + + // Check that cardinality is validated. + $entity->{$this->field_name}[$langcode] = $this->_generateTestFieldValues($this->field['cardinality'] + 1); + try { + field_attach_validate($entity_type, $entity); + } + catch (FieldValidationException $e) { + $errors = $e->errors; + } + $this->assertEqual($errors[$this->field_name][$langcode][0][0]['error'], 'field_cardinality', t('Cardinality validation failed.')); + } /** @@ -1538,11 +1574,6 @@ class FieldFormTestCase extends FieldTestCase { // Test with several multiple fields in a form } - // Check with a multiple widget (implement a textfield with comma separated values). - - // Check inaccessible fields are preserved on update. - // Check inaccessible fields get default value on insert (not implemented yet). - function testFieldFormJSAddMore() { $this->field = $this->field_unlimited; $this->field_name = $this->field['field_name']; @@ -1598,6 +1629,46 @@ class FieldFormTestCase extends FieldTestCase { } /** + * Tests widgets handling multiple values. + */ + function testFieldFormMultipleWidget() { + // Create a field with fixed cardinality and an instance using a multiple + // widget. + $this->field = $this->field_multiple; + $this->field_name = $this->field['field_name']; + $this->instance['field_name'] = $this->field_name; + $this->instance['widget']['type'] = 'test_field_widget_multiple'; + field_create_field($this->field); + field_create_instance($this->instance); + $langcode = LANGUAGE_NONE; + + // Display creation form. + $this->drupalGet('test-entity/add/test-bundle'); + $this->assertFieldByName("{$this->field_name}[$langcode]", '', t('Widget is displayed.')); + + // Create entity with three values. + $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3'); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + + // Check that the values were saved. + $entity_init = field_test_create_stub_entity($id); + $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3)); + + // Display the form, check that the values are correctly filled in. + $this->drupalGet('test-entity/' . $id . '/edit'); + $this->assertFieldByName("{$this->field_name}[$langcode]", '1, 2, 3', t('Widget is displayed.')); + + // Submit the form with more values than the field accepts. + $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3, 4, 5'); + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertRaw('this field cannot hold more than 4 values', t('Form validation failed.')); + // Check that the field values were not submitted. + $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3)); + } + + /** * Tests fields with no 'edit' access. */ function testFieldFormAccess() { diff --git a/modules/field/tests/field_test.field.inc b/modules/field/tests/field_test.field.inc index 89def7e10..8db03401c 100644 --- a/modules/field/tests/field_test.field.inc +++ b/modules/field/tests/field_test.field.inc @@ -157,20 +157,56 @@ function field_test_field_widget_info() { * Implements hook_field_widget(). */ function field_test_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { - $element = array( - 'value' => $element + array( - '#type' => 'textfield', - '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '', - ), - ); - return $element; + switch ($instance['widget']['type']) { + case 'test_field_widget': + $element += array( + '#type' => 'textfield', + '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '', + ); + return array('value' => $element); + + case 'test_field_widget_multiple': + $values = array(); + foreach ($items as $delta => $value) { + $values[] = $value['value']; + } + $element += array( + '#type' => 'textfield', + '#default_value' => implode(', ', $values), + '#element_validate' => array('field_test_widget_multiple_validate'), + ); + return $element; + } +} + +/** + * Form element validation handler for 'test_field_widget_multiple' widget. + */ +function field_test_widget_multiple_validate($element, &$form_state) { + $values = array_map('trim', explode(',', $element['#value'])); + $items = array(); + foreach($values as $value) { + $items[] = array('value' => $value); + } + form_set_value($element, $items, $form_state); } /** * Implements hook_field_widget_error(). */ function field_test_field_widget_error($element, $error) { - form_error($element['value'], $error['message']); + // @todo No easy way to differenciate widget types, we should receive it as a + // parameter. + if (isset($element['value'])) { + // Widget is test_field_widget. + $error_element = $element['value']; + } + else { + // Widget is test_field_widget_multiple. + $error_element = $element; + } + + form_error($error_element, $error['message']); } /** |