summaryrefslogtreecommitdiff
path: root/modules/field/tests
diff options
context:
space:
mode:
Diffstat (limited to 'modules/field/tests')
-rw-r--r--modules/field/tests/field.test115
-rw-r--r--modules/field/tests/field_test.entity.inc64
-rw-r--r--modules/field/tests/field_test.module8
3 files changed, 184 insertions, 3 deletions
diff --git a/modules/field/tests/field.test b/modules/field/tests/field.test
index e5200a9e0..30b1d8699 100644
--- a/modules/field/tests/field.test
+++ b/modules/field/tests/field.test
@@ -1212,9 +1212,9 @@ class FieldFormTestCase extends FieldTestCase {
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($web_user);
- $this->field_single = array('field_name' => drupal_strtolower($this->randomName()), 'type' => 'test_field');
- $this->field_multiple = array('field_name' => drupal_strtolower($this->randomName()), 'type' => 'test_field', 'cardinality' => 4);
- $this->field_unlimited = array('field_name' => drupal_strtolower($this->randomName()), 'type' => 'test_field', 'cardinality' => FIELD_CARDINALITY_UNLIMITED);
+ $this->field_single = array('field_name' => 'field_single', 'type' => 'test_field');
+ $this->field_multiple = array('field_name' => 'field_multiple', 'type' => 'test_field', 'cardinality' => 4);
+ $this->field_unlimited = array('field_name' => 'field_unlimited', 'type' => 'test_field', 'cardinality' => FIELD_CARDINALITY_UNLIMITED);
$this->instance = array(
'entity_type' => 'test_entity',
@@ -1551,6 +1551,115 @@ class FieldFormTestCase extends FieldTestCase {
$this->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, t('New revision has the expected value for the field with no edit access.'));
$this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 2, t('New revision has the expected value for the field with edit access.'));
}
+
+ /**
+ * Tests Field API form integration within a subform.
+ */
+ function testNestedFieldForm() {
+ // Add two instances on the 'test_bundle'
+ field_create_field($this->field_single);
+ field_create_field($this->field_unlimited);
+ $this->instance['field_name'] = 'field_single';
+ $this->instance['label'] = 'Single field';
+ field_create_instance($this->instance);
+ $this->instance['field_name'] = 'field_unlimited';
+ $this->instance['label'] = 'Unlimited field';
+ field_create_instance($this->instance);
+
+ // Create two entities.
+ $entity_1 = field_test_create_stub_entity(1, 1);
+ $entity_1->is_new = TRUE;
+ $entity_1->field_single[LANGUAGE_NONE][] = array('value' => 0);
+ $entity_1->field_unlimited[LANGUAGE_NONE][] = array('value' => 1);
+ field_test_entity_save($entity_1);
+
+ $entity_2 = field_test_create_stub_entity(2, 2);
+ $entity_2->is_new = TRUE;
+ $entity_2->field_single[LANGUAGE_NONE][] = array('value' => 10);
+ $entity_2->field_unlimited[LANGUAGE_NONE][] = array('value' => 11);
+ field_test_entity_save($entity_2);
+
+ // Display the 'combined form'.
+ $this->drupalGet('test-entity/nested/1/2');
+ $this->assertFieldByName('field_single[und][0][value]', 0, t('Entity 1: field_single value appears correctly is the form.'));
+ $this->assertFieldByName('field_unlimited[und][0][value]', 1, t('Entity 1: field_unlimited value 0 appears correctly is the form.'));
+ $this->assertFieldByName('entity_2[field_single][und][0][value]', 10, t('Entity 2: field_single value appears correctly is the form.'));
+ $this->assertFieldByName('entity_2[field_unlimited][und][0][value]', 11, t('Entity 2: field_unlimited value 0 appears correctly is the form.'));
+
+ // Submit the form and check that the entities are updated accordingly.
+ $edit = array(
+ 'field_single[und][0][value]' => 1,
+ 'field_unlimited[und][0][value]' => 2,
+ 'field_unlimited[und][1][value]' => 3,
+ 'entity_2[field_single][und][0][value]' => 11,
+ 'entity_2[field_unlimited][und][0][value]' => 12,
+ 'entity_2[field_unlimited][und][1][value]' => 13,
+ );
+ $this->drupalPost(NULL, $edit, t('Save'));
+ field_cache_clear();
+ $entity_1 = field_test_create_stub_entity(1);
+ $entity_2 = field_test_create_stub_entity(2);
+ $this->assertFieldValues($entity_1, 'field_single', LANGUAGE_NONE, array(1));
+ $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(2, 3));
+ $this->assertFieldValues($entity_2, 'field_single', LANGUAGE_NONE, array(11));
+ $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(12, 13));
+
+ // Submit invalid values and check that errors are reported on the
+ // correct widgets.
+ $edit = array(
+ 'field_unlimited[und][1][value]' => -1,
+ );
+ $this->drupalPost('test-entity/nested/1/2', $edit, t('Save'));
+ $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), t('Entity 1: the field validation error was reported.'));
+ $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-field-unlimited-und-1-value'));
+ $this->assertTrue($error_field, t('Entity 1: the error was flagged on the correct element.'));
+ $edit = array(
+ 'entity_2[field_unlimited][und][1][value]' => -1,
+ );
+ $this->drupalPost('test-entity/nested/1/2', $edit, t('Save'));
+ $this->assertRaw(t('%label does not accept the value -1', array('%label' => 'Unlimited field')), t('Entity 2: the field validation error was reported.'));
+ $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', array(':id' => 'edit-entity-2-field-unlimited-und-1-value'));
+ $this->assertTrue($error_field, t('Entity 2: the error was flagged on the correct element.'));
+
+ // Test that reordering works on both entities.
+ $edit = array(
+ 'field_unlimited[und][0][_weight]' => 0,
+ 'field_unlimited[und][1][_weight]' => -1,
+ 'entity_2[field_unlimited][und][0][_weight]' => 0,
+ 'entity_2[field_unlimited][und][1][_weight]' => -1,
+ );
+ $this->drupalPost('test-entity/nested/1/2', $edit, t('Save'));
+ field_cache_clear();
+ $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(3, 2));
+ $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(13, 12));
+
+ // Test the 'add more' buttons. Only AJAX submission is tested, because
+ // the two 'add more' buttons present in the form have the same #value,
+ // which confuses drupalPost().
+ // 'Add more' button in the first entity:
+ $this->drupalGet('test-entity/nested/1/2');
+ $this->drupalPostAJAX(NULL, array(), 'field_unlimited_add_more');
+ $this->assertFieldByName('field_unlimited[und][0][value]', 3, t('Entity 1: field_unlimited value 0 appears correctly is the form.'));
+ $this->assertFieldByName('field_unlimited[und][1][value]', 2, t('Entity 1: field_unlimited value 1 appears correctly is the form.'));
+ $this->assertFieldByName('field_unlimited[und][2][value]', '', t('Entity 1: field_unlimited value 2 appears correctly is the form.'));
+ $this->assertFieldByName('field_unlimited[und][3][value]', '', t('Entity 1: an empty widget was added for field_unlimited value 3.'));
+ // 'Add more' button in the first entity (changing field values):
+ $edit = array(
+ 'entity_2[field_unlimited][und][0][value]' => 13,
+ 'entity_2[field_unlimited][und][1][value]' => 14,
+ 'entity_2[field_unlimited][und][2][value]' => 15,
+ );
+ $this->drupalPostAJAX(NULL, $edit, 'entity_2_field_unlimited_add_more');
+ $this->assertFieldByName('entity_2[field_unlimited][und][0][value]', 13, t('Entity 2: field_unlimited value 0 appears correctly is the form.'));
+ $this->assertFieldByName('entity_2[field_unlimited][und][1][value]', 14, t('Entity 2: field_unlimited value 1 appears correctly is the form.'));
+ $this->assertFieldByName('entity_2[field_unlimited][und][2][value]', 15, t('Entity 2: field_unlimited value 2 appears correctly is the form.'));
+ $this->assertFieldByName('entity_2[field_unlimited][und][3][value]', '', t('Entity 2: an empty widget was added for field_unlimited value 3.'));
+ // Save the form and check values are saved correclty.
+ $this->drupalPost(NULL, array(), t('Save'));
+ field_cache_clear();
+ $this->assertFieldValues($entity_1, 'field_unlimited', LANGUAGE_NONE, array(3, 2));
+ $this->assertFieldValues($entity_2, 'field_unlimited', LANGUAGE_NONE, array(13, 14, 15));
+ }
}
class FieldDisplayAPITestCase extends FieldTestCase {
diff --git a/modules/field/tests/field_test.entity.inc b/modules/field/tests/field_test.entity.inc
index 4c0269d3e..4273dd95f 100644
--- a/modules/field/tests/field_test.entity.inc
+++ b/modules/field/tests/field_test.entity.inc
@@ -413,6 +413,70 @@ function field_test_entity_form_submit_build_test_entity($form, &$form_state) {
}
/**
+ * Form combining two separate entities.
+ */
+function field_test_entity_nested_form($form, &$form_state, $entity_1, $entity_2) {
+ // First entity.
+ foreach (array('ftid', 'ftvid', 'fttype') as $key) {
+ $form[$key] = array(
+ '#type' => 'value',
+ '#value' => $entity_1->$key,
+ );
+ }
+ field_attach_form('test_entity', $entity_1, $form, $form_state);
+
+ // Second entity.
+ $form['entity_2'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Second entity'),
+ '#tree' => TRUE,
+ '#parents' => array('entity_2'),
+ '#weight' => 50,
+ );
+ foreach (array('ftid', 'ftvid', 'fttype') as $key) {
+ $form['entity_2'][$key] = array(
+ '#type' => 'value',
+ '#value' => $entity_2->$key,
+ );
+ }
+ field_attach_form('test_entity', $entity_2, $form['entity_2'], $form_state);
+
+ $form['save'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save'),
+ '#weight' => 100,
+ );
+
+ return $form;
+}
+
+/**
+ * Validate handler for field_test_entity_nested_form().
+ */
+function field_test_entity_nested_form_validate($form, &$form_state) {
+ $entity_1 = (object) $form_state['values'];
+ field_attach_form_validate('test_entity', $entity_1, $form, $form_state);
+
+ $entity_2 = (object) $form_state['values']['entity_2'];
+ field_attach_form_validate('test_entity', $entity_2, $form['entity_2'], $form_state);
+}
+
+/**
+ * Submit handler for field_test_entity_nested_form().
+ */
+function field_test_entity_nested_form_submit($form, &$form_state) {
+ $entity_1 = (object) $form_state['values'];
+ field_attach_submit('test_entity', $entity_1, $form, $form_state);
+ field_test_entity_save($entity_1);
+
+ $entity_2 = (object) $form_state['values']['entity_2'];
+ field_attach_submit('test_entity', $entity_2, $form['entity_2'], $form_state);
+ field_test_entity_save($entity_2);
+
+ drupal_set_message(t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->ftid, '@id_2' => $entity_2->ftid)));
+}
+
+/**
* Controller class for the test_entity_bundle entity type.
*
* This extends the DrupalDefaultEntityController class, adding required
diff --git a/modules/field/tests/field_test.module b/modules/field/tests/field_test.module
index 425432303..693e825a9 100644
--- a/modules/field/tests/field_test.module
+++ b/modules/field/tests/field_test.module
@@ -60,6 +60,14 @@ function field_test_menu() {
'type' => MENU_NORMAL_ITEM,
);
+ $items['test-entity/nested/%field_test_entity_test/%field_test_entity_test'] = array(
+ 'title' => 'Nested entity form',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('field_test_entity_nested_form', 2, 3),
+ 'access arguments' => array('administer field_test content'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
return $items;
}