class FieldFormTestCase

Hierarchy

Expanded class hierarchy of FieldFormTestCase

File

drupal/modules/field/tests/field.test, line 1607
Tests for field.module.

View source
class FieldFormTestCase extends FieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Field form tests',
      'description' => 'Test Field form handling.',
      'group' => 'Field API',
    );
  }
  function setUp() {
    parent::setUp('field_test');
    $web_user = $this
      ->drupalCreateUser(array(
      'access field_test content',
      'administer field_test content',
    ));
    $this
      ->drupalLogin($web_user);
    $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',
      'bundle' => 'test_bundle',
      'label' => $this
        ->randomName() . '_label',
      'description' => $this
        ->randomName() . '_description',
      'weight' => mt_rand(0, 127),
      'settings' => array(
        'test_instance_setting' => $this
          ->randomName(),
      ),
      'widget' => array(
        'type' => 'test_field_widget',
        'label' => 'Test Field',
        'settings' => array(
          'test_widget_setting' => $this
            ->randomName(),
        ),
      ),
    );
  }
  function testFieldFormSingle() {
    $this->field = $this->field_single;
    $this->field_name = $this->field['field_name'];
    $this->instance['field_name'] = $this->field_name;
    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}][0][value]", '', 'Widget is displayed');
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][1][value]", 'No extraneous widget is displayed');

    // TODO : check that the widget is populated with default value ?
    // Submit with invalid value (field-level validation).
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => -1,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('%name does not accept the value -1.', array(
      '%name' => $this->instance['label'],
    )), 'Field validation fails with invalid input.');

    // TODO : check that the correct field is flagged for error.
    // Create an entity
    $value = mt_rand(1, 127);
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => $value,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this
      ->assertRaw(t('test_entity @id has been created.', array(
      '@id' => $id,
    )), 'Entity was created');
    $entity = field_test_entity_test_load($id);
    $this
      ->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');

    // Display edit form.
    $this
      ->drupalGet('test-entity/manage/' . $id . '/edit');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", $value, 'Widget is displayed with the correct default value');
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][1][value]", 'No extraneous widget is displayed');

    // Update the entity.
    $value = mt_rand(1, 127);
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => $value,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('test_entity @id has been updated.', array(
      '@id' => $id,
    )), 'Entity was updated');
    $entity = field_test_entity_test_load($id);
    $this
      ->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');

    // Empty the field.
    $value = '';
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => $value,
    );
    $this
      ->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
    $this
      ->assertRaw(t('test_entity @id has been updated.', array(
      '@id' => $id,
    )), 'Entity was updated');
    $entity = field_test_entity_test_load($id);
    $this
      ->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied');
  }
  function testFieldFormSingleRequired() {
    $this->field = $this->field_single;
    $this->field_name = $this->field['field_name'];
    $this->instance['field_name'] = $this->field_name;
    $this->instance['required'] = TRUE;
    field_create_field($this->field);
    field_create_instance($this->instance);
    $langcode = LANGUAGE_NONE;

    // Submit with missing required value.
    $edit = array();
    $this
      ->drupalPost('test-entity/add/test-bundle', $edit, t('Save'));
    $this
      ->assertRaw(t('!name field is required.', array(
      '!name' => $this->instance['label'],
    )), 'Required field with no value fails validation');

    // Create an entity
    $value = mt_rand(1, 127);
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => $value,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this
      ->assertRaw(t('test_entity @id has been created.', array(
      '@id' => $id,
    )), 'Entity was created');
    $entity = field_test_entity_test_load($id);
    $this
      ->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');

    // Edit with missing required value.
    $value = '';
    $edit = array(
      "{$this->field_name}[{$langcode}][0][value]" => $value,
    );
    $this
      ->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
    $this
      ->assertRaw(t('!name field is required.', array(
      '!name' => $this->instance['label'],
    )), 'Required field with no value fails validation');
  }

  //  function testFieldFormMultiple() {
  //    $this->field = $this->field_multiple;
  //    $this->field_name = $this->field['field_name'];
  //    $this->instance['field_name'] = $this->field_name;
  //    field_create_field($this->field);
  //    field_create_instance($this->instance);
  //  }
  function testFieldFormUnlimited() {
    $this->field = $this->field_unlimited;
    $this->field_name = $this->field['field_name'];
    $this->instance['field_name'] = $this->field_name;
    field_create_field($this->field);
    field_create_instance($this->instance);
    $langcode = LANGUAGE_NONE;

    // Display creation form -> 1 widget.
    $this
      ->drupalGet('test-entity/add/test-bundle');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", '', 'Widget 1 is displayed');
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][1][value]", 'No extraneous widget is displayed');

    // Press 'add more' button -> 2 widgets.
    $this
      ->drupalPost(NULL, array(), t('Add another item'));
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", '', 'Widget 1 is displayed');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][1][value]", '', 'New widget is displayed');
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][2][value]", 'No extraneous widget is displayed');

    // TODO : check that non-field inpurs are preserved ('title')...
    // Yet another time so that we can play with more values -> 3 widgets.
    $this
      ->drupalPost(NULL, array(), t('Add another item'));

    // Prepare values and weights.
    $count = 3;
    $delta_range = $count - 1;
    $values = $weights = $pattern = $expected_values = $edit = array();
    for ($delta = 0; $delta <= $delta_range; $delta++) {

      // Assign unique random values and weights.
      do {
        $value = mt_rand(1, 127);
      } while (in_array($value, $values));
      do {
        $weight = mt_rand(-$delta_range, $delta_range);
      } while (in_array($weight, $weights));
      $edit["{$this->field_name}[{$langcode}][{$delta}][value]"] = $value;
      $edit["{$this->field_name}[{$langcode}][{$delta}][_weight]"] = $weight;

      // We'll need three slightly different formats to check the values.
      $values[$delta] = $value;
      $weights[$delta] = $weight;
      $field_values[$weight]['value'] = (string) $value;
      $pattern[$weight] = "<input [^>]*value=\"{$value}\" [^>]*";
    }

    // Press 'add more' button -> 4 widgets
    $this
      ->drupalPost(NULL, $edit, t('Add another item'));
    for ($delta = 0; $delta <= $delta_range; $delta++) {
      $this
        ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][value]", $values[$delta], "Widget {$delta} is displayed and has the right value");
      $this
        ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][_weight]", $weights[$delta], "Widget {$delta} has the right weight");
    }
    ksort($pattern);
    $pattern = implode('.*', array_values($pattern));
    $this
      ->assertPattern("|{$pattern}|s", 'Widgets are displayed in the correct order');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][value]", '', "New widget is displayed");
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][_weight]", $delta, "New widget has the right weight");
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');

    // Submit the form and create the entity.
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this
      ->assertRaw(t('test_entity @id has been created.', array(
      '@id' => $id,
    )), 'Entity was created');
    $entity = field_test_entity_test_load($id);
    ksort($field_values);
    $field_values = array_values($field_values);
    $this
      ->assertIdentical($entity->{$this->field_name}[$langcode], $field_values, 'Field values were saved in the correct order');

    // Display edit form: check that the expected number of widgets is
    // displayed, with correct values change values, reorder, leave an empty
    // value in the middle.
    // Submit: check that the entity is updated with correct values
    // Re-submit: check that the field can be emptied.
    // Test with several multiple fields in a form
  }

  /**
   * Tests widget handling of multiple required radios.
   */
  function testFieldFormMultivalueWithRequiredRadio() {

    // Create a multivalue test field.
    $this->field = $this->field_unlimited;
    $this->field_name = $this->field['field_name'];
    $this->instance['field_name'] = $this->field_name;
    field_create_field($this->field);
    field_create_instance($this->instance);
    $langcode = LANGUAGE_NONE;

    // Add a required radio field.
    field_create_field(array(
      'field_name' => 'required_radio_test',
      'type' => 'list_text',
      'settings' => array(
        'allowed_values' => array(
          'yes' => 'yes',
          'no' => 'no',
        ),
      ),
    ));
    field_create_instance(array(
      'field_name' => 'required_radio_test',
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_buttons',
      ),
    ));

    // Display creation form.
    $this
      ->drupalGet('test-entity/add/test-bundle');

    // Press the 'Add more' button.
    $this
      ->drupalPost(NULL, array(), t('Add another item'));

    // Verify that no error is thrown by the radio element.
    $this
      ->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');

    // Verify that the widget is added.
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", '', 'Widget 1 is displayed');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][1][value]", '', 'New widget is displayed');
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][2][value]", 'No extraneous widget is displayed');
  }
  function testFieldFormJSAddMore() {
    $this->field = $this->field_unlimited;
    $this->field_name = $this->field['field_name'];
    $this->instance['field_name'] = $this->field_name;
    field_create_field($this->field);
    field_create_instance($this->instance);
    $langcode = LANGUAGE_NONE;

    // Display creation form -> 1 widget.
    $this
      ->drupalGet('test-entity/add/test-bundle');

    // Press 'add more' button a couple times -> 3 widgets.
    // drupalPostAJAX() will not work iteratively, so we add those through
    // non-JS submission.
    $this
      ->drupalPost(NULL, array(), t('Add another item'));
    $this
      ->drupalPost(NULL, array(), t('Add another item'));

    // Prepare values and weights.
    $count = 3;
    $delta_range = $count - 1;
    $values = $weights = $pattern = $expected_values = $edit = array();
    for ($delta = 0; $delta <= $delta_range; $delta++) {

      // Assign unique random values and weights.
      do {
        $value = mt_rand(1, 127);
      } while (in_array($value, $values));
      do {
        $weight = mt_rand(-$delta_range, $delta_range);
      } while (in_array($weight, $weights));
      $edit["{$this->field_name}[{$langcode}][{$delta}][value]"] = $value;
      $edit["{$this->field_name}[{$langcode}][{$delta}][_weight]"] = $weight;

      // We'll need three slightly different formats to check the values.
      $values[$delta] = $value;
      $weights[$delta] = $weight;
      $field_values[$weight]['value'] = (string) $value;
      $pattern[$weight] = "<input [^>]*value=\"{$value}\" [^>]*";
    }

    // Press 'add more' button through Ajax, and place the expected HTML result
    // as the tested content.
    $commands = $this
      ->drupalPostAJAX(NULL, $edit, $this->field_name . '_add_more');
    $this->content = $commands[1]['data'];
    for ($delta = 0; $delta <= $delta_range; $delta++) {
      $this
        ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][value]", $values[$delta], "Widget {$delta} is displayed and has the right value");
      $this
        ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][_weight]", $weights[$delta], "Widget {$delta} has the right weight");
    }
    ksort($pattern);
    $pattern = implode('.*', array_values($pattern));
    $this
      ->assertPattern("|{$pattern}|s", 'Widgets are displayed in the correct order');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][value]", '', "New widget is displayed");
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}][{$delta}][_weight]", $delta, "New widget has the right weight");
    $this
      ->assertNoField("{$this->field_name}[{$langcode}][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
  }

  /**
   * 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}]", '', '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/manage/(\\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/manage/' . $id . '/edit');
    $this
      ->assertFieldByName("{$this->field_name}[{$langcode}]", '1, 2, 3', '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', '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() {

    // Create a "regular" field.
    $field = $this->field_single;
    $field_name = $field['field_name'];
    $instance = $this->instance;
    $instance['field_name'] = $field_name;
    field_create_field($field);
    field_create_instance($instance);

    // Create a field with no edit access - see field_test_field_access().
    $field_no_access = array(
      'field_name' => 'field_no_edit_access',
      'type' => 'test_field',
    );
    $field_name_no_access = $field_no_access['field_name'];
    $instance_no_access = array(
      'field_name' => $field_name_no_access,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'default_value' => array(
        0 => array(
          'value' => 99,
        ),
      ),
    );
    field_create_field($field_no_access);
    field_create_instance($instance_no_access);
    $langcode = LANGUAGE_NONE;

    // Test that the form structure includes full information for each delta
    // apart from #access.
    $entity_type = 'test_entity';
    $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
    $form = array();
    $form_state = form_state_defaults();
    field_attach_form($entity_type, $entity, $form, $form_state);
    $this
      ->assertEqual($form[$field_name_no_access][$langcode][0]['value']['#entity_type'], $entity_type, 'The correct entity type is set in the field structure.');
    $this
      ->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');

    // Display creation form.
    $this
      ->drupalGet('test-entity/add/test-bundle');
    $this
      ->assertNoFieldByName("{$field_name_no_access}[{$langcode}][0][value]", '', 'Widget is not displayed if field access is denied.');

    // Create entity.
    $edit = array(
      "{$field_name}[{$langcode}][0][value]" => 1,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
    $id = $match[1];

    // Check that the default value was saved.
    $entity = field_test_entity_test_load($id);
    $this
      ->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'Default value was saved for the field with no edit access.');
    $this
      ->assertEqual($entity->{$field_name}[$langcode][0]['value'], 1, 'Entered value vas saved for the field with edit access.');

    // Create a new revision.
    $edit = array(
      "{$field_name}[{$langcode}][0][value]" => 2,
      'revision' => TRUE,
    );
    $this
      ->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));

    // Check that the new revision has the expected values.
    $entity = field_test_entity_test_load($id);
    $this
      ->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'New revision has the expected value for the field with no edit access.');
    $this
      ->assertEqual($entity->{$field_name}[$langcode][0]['value'], 2, 'New revision has the expected value for the field with edit access.');

    // Check that the revision is also saved in the revisions table.
    $entity = field_test_entity_test_load($id, $entity->ftvid);
    $this
      ->assertEqual($entity->{$field_name_no_access}[$langcode][0]['value'], 99, 'New revision has the expected value for the field with no edit access.');
    $this
      ->assertEqual($entity->{$field_name}[$langcode][0]['value'], 2, '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, 'Entity 1: field_single value appears correctly is the form.');
    $this
      ->assertFieldByName('field_unlimited[und][0][value]', 1, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
    $this
      ->assertFieldByName('entity_2[field_single][und][0][value]', 10, 'Entity 2: field_single value appears correctly is the form.');
    $this
      ->assertFieldByName('entity_2[field_unlimited][und][0][value]', 11, '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',
    )), '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, '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',
    )), '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, '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, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
    $this
      ->assertFieldByName('field_unlimited[und][1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
    $this
      ->assertFieldByName('field_unlimited[und][2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
    $this
      ->assertFieldByName('field_unlimited[und][3][value]', '', '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, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
    $this
      ->assertFieldByName('entity_2[field_unlimited][und][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
    $this
      ->assertFieldByName('entity_2[field_unlimited][und][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
    $this
      ->assertFieldByName('entity_2[field_unlimited][und][3][value]', '', '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,
    ));
  }

}

Members

Namesort ascending Modifiers Type Description Overrides
FieldTestCase::_generateTestFieldValues function Generate random values for a field_test field.
FieldTestCase::assertFieldValues function Assert that a field has the expected values in an entity.
FieldTestCase::$default_storage property
FieldFormTestCase::testNestedFieldForm function Tests Field API form integration within a subform.
FieldFormTestCase::testFieldFormUnlimited function
FieldFormTestCase::testFieldFormSingleRequired function
FieldFormTestCase::testFieldFormSingle function
FieldFormTestCase::testFieldFormMultivalueWithRequiredRadio function Tests widget handling of multiple required radios.
FieldFormTestCase::testFieldFormMultipleWidget function Tests widgets handling multiple values.
FieldFormTestCase::testFieldFormJSAddMore function
FieldFormTestCase::testFieldFormAccess function Tests fields with no 'edit' access.
FieldFormTestCase::setUp function Set the default field storage backend for fields created during tests. Overrides FieldTestCase::setUp
FieldFormTestCase::getInfo public static function
DrupalWebTestCase::__construct function Constructor for DrupalWebTestCase. Overrides DrupalTestCase::__construct 1
DrupalWebTestCase::xpath protected function Perform an xpath search on the contents of the internal browser. The search is relative to the root element (HTML tag normally) of the page.
DrupalWebTestCase::verboseEmail protected function Outputs to verbose the most recent $count emails sent.
DrupalWebTestCase::tearDown protected function Delete created files and temporary files directory, delete the tables created by setUp(), and reset the database prefix. 6
DrupalWebTestCase::resetAll protected function Reset all data structures after having enabled new modules.
DrupalWebTestCase::refreshVariables protected function Refresh the in-memory set of variables. Useful after a page request is made that changes a variable in a different thread. 1
DrupalWebTestCase::prepareEnvironment protected function Prepares the current environment for running the test.
DrupalWebTestCase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
DrupalWebTestCase::preloadRegistry protected function Preload the registry from the testing site.
DrupalWebTestCase::parse protected function Parse content returned from curlExec using DOM and SimpleXML.
DrupalWebTestCase::handleForm protected function Handle form input related to drupalPost(). Ensure that the specified fields exist and attempt to create POST data in the correct manner for the particular field type.
DrupalWebTestCase::getUrl protected function Get the current URL from the cURL handler.
DrupalWebTestCase::getSelectedItem protected function Get the selected value from a select field.
DrupalWebTestCase::getAllOptions protected function Get all option elements, including nested options, in a select.
DrupalWebTestCase::getAbsoluteUrl protected function Takes a path and returns an absolute path.
DrupalWebTestCase::drupalSetSettings protected function Sets the value of the Drupal.settings JavaScript variable for the currently loaded page.
DrupalWebTestCase::drupalSetContent protected function Sets the raw HTML content. This can be useful when a page has been fetched outside of the internal browser and assertions need to be made on the returned page.
DrupalWebTestCase::drupalPostAJAX protected function Execute an Ajax submission.
DrupalWebTestCase::drupalPost protected function Execute a POST request on a Drupal page. It will be done as usual POST request with SimpleBrowser.
DrupalWebTestCase::drupalLogout protected function
DrupalWebTestCase::drupalLogin protected function Log in a user with the internal browser.
DrupalWebTestCase::drupalHead protected function Retrieves only the headers for a Drupal path or an absolute path.
DrupalWebTestCase::drupalGetToken protected function Generate a token for the currently logged in user.
DrupalWebTestCase::drupalGetTestFiles protected function Get a list files that can be used in tests.
DrupalWebTestCase::drupalGetSettings protected function Gets the value of the Drupal.settings JavaScript variable for the currently loaded page.
DrupalWebTestCase::drupalGetNodeByTitle function Get a node from the database based on its title.
DrupalWebTestCase::drupalGetMails protected function Gets an array containing all e-mails sent during this test case.
DrupalWebTestCase::drupalGetHeaders protected function Gets the HTTP response headers of the requested page. Normally we are only interested in the headers returned by the last request. However, if a page is redirected or HTTP authentication is in use, multiple requests will be required to retrieve the…
DrupalWebTestCase::drupalGetHeader protected function Gets the value of an HTTP response header. If multiple requests were required to retrieve the page, only the headers from the last request will be checked by default. However, if TRUE is passed as the second argument, all requests will be processed…
DrupalWebTestCase::drupalGetContent protected function Gets the current raw HTML of requested page.
DrupalWebTestCase::drupalGetAJAX protected function Retrieve a Drupal path or an absolute path and JSON decode the result.
DrupalWebTestCase::drupalGet protected function Retrieves a Drupal path or an absolute path.
DrupalWebTestCase::drupalCreateUser protected function Create a user with a given set of permissions.
DrupalWebTestCase::drupalCreateRole protected function Creates a role with specified permissions.
DrupalWebTestCase::drupalCreateNode protected function Creates a node based on default settings.
DrupalWebTestCase::drupalCreateContentType protected function Creates a custom content type based on default settings.
DrupalWebTestCase::drupalCompareFiles protected function Compare two files based on size and file name.
DrupalWebTestCase::curlInitialize protected function Initializes the cURL connection.
DrupalWebTestCase::curlHeaderCallback protected function Reads headers and registers errors received from the tested site.
DrupalWebTestCase::curlExec protected function Initializes and executes a cURL request.
DrupalWebTestCase::curlClose protected function Close the cURL handler and unset the handler.
DrupalWebTestCase::cronRun protected function Runs cron in the Drupal installed by Simpletest.
DrupalWebTestCase::constructFieldXpath protected function Helper function: construct an XPath for the given set of attributes and value.
DrupalWebTestCase::clickLink protected function Follows a link by name.
DrupalWebTestCase::checkPermissions protected function Check to make sure that the array of permissions are valid.
DrupalWebTestCase::checkForMetaRefresh protected function Check for meta refresh tag and if found call drupalGet() recursively. This function looks for the http-equiv attribute to be set to "Refresh" and is case-sensitive.
DrupalWebTestCase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
DrupalWebTestCase::buildXPathQuery protected function Builds an XPath query.
DrupalWebTestCase::assertUrl protected function Pass if the internal browser's URL matches the given path.
DrupalWebTestCase::assertUniqueTextHelper protected function Helper for assertUniqueText and assertNoUniqueText.
DrupalWebTestCase::assertUniqueText protected function Pass if the text is found ONLY ONCE on the text version of the page.
DrupalWebTestCase::assertTitle protected function Pass if the page title is the given string.
DrupalWebTestCase::assertThemeOutput protected function Asserts themed output.
DrupalWebTestCase::assertTextHelper protected function Helper for assertText and assertNoText.
DrupalWebTestCase::assertText protected function Pass if the text IS found on the text version of the page. The text version is the equivalent of what a user would see when viewing through a web browser. In other words the HTML has been filtered out of the contents.
DrupalWebTestCase::assertResponse protected function Asserts the page responds with the specified response code.
DrupalWebTestCase::assertRaw protected function Pass if the raw text IS found on the loaded page, fail otherwise. Raw text refers to the raw HTML that the page generated.
DrupalWebTestCase::assertPattern protected function Will trigger a pass if the Perl regex pattern is found in the raw content.
DrupalWebTestCase::assertOptionSelected protected function Asserts that a select option in the current page is checked.
DrupalWebTestCase::assertNoUniqueText protected function Pass if the text is found MORE THAN ONCE on the text version of the page.
DrupalWebTestCase::assertNoTitle protected function Pass if the page title is not the given string.
DrupalWebTestCase::assertNoText protected function Pass if the text is NOT found on the text version of the page. The text version is the equivalent of what a user would see when viewing through a web browser. In other words the HTML has been filtered out of the contents.
DrupalWebTestCase::assertNoResponse protected function Asserts the page did not return the specified response code.
DrupalWebTestCase::assertNoRaw protected function Pass if the raw text is NOT found on the loaded page, fail otherwise. Raw text refers to the raw HTML that the page generated.
DrupalWebTestCase::assertNoPattern protected function Will trigger a pass if the perl regex pattern is not present in raw content.
DrupalWebTestCase::assertNoOptionSelected protected function Asserts that a select option in the current page is not checked.
DrupalWebTestCase::assertNoLinkByHref protected function Pass if a link containing a given href (part) is not found.
DrupalWebTestCase::assertNoLink protected function Pass if a link with the specified label is not found.
DrupalWebTestCase::assertNoFieldChecked protected function Asserts that a checkbox field in the current page is not checked.
DrupalWebTestCase::assertNoFieldByXPath protected function Asserts that a field doesn't exist or its value doesn't match, by XPath.
DrupalWebTestCase::assertNoFieldByName protected function Asserts that a field does not exist with the given name and value.
DrupalWebTestCase::assertNoFieldById protected function Asserts that a field does not exist with the given ID and value.
DrupalWebTestCase::assertNoField protected function Asserts that a field does not exist with the given name or ID.
DrupalWebTestCase::assertNoDuplicateIds protected function Asserts that each HTML ID is used for just a single element.
DrupalWebTestCase::assertMailString protected function Asserts that the most recently sent e-mail message has the string in it.
DrupalWebTestCase::assertMailPattern protected function Asserts that the most recently sent e-mail message has the pattern in it.
DrupalWebTestCase::assertMail protected function Asserts that the most recently sent e-mail message has the given value.
DrupalWebTestCase::assertLinkByHref protected function Pass if a link containing a given href (part) is found.
DrupalWebTestCase::assertLink protected function Pass if a link with the specified label is found, and optional with the specified index.
DrupalWebTestCase::assertFieldChecked protected function Asserts that a checkbox field in the current page is checked.
DrupalWebTestCase::assertFieldByXPath protected function Asserts that a field exists in the current page by the given XPath.
DrupalWebTestCase::assertFieldByName protected function Asserts that a field exists in the current page with the given name and value.
DrupalWebTestCase::assertFieldById protected function Asserts that a field exists in the current page with the given ID and value.
DrupalWebTestCase::assertField protected function Asserts that a field exists with the given name or ID.
DrupalWebTestCase::$url protected property The URL currently loaded in the internal browser.
DrupalWebTestCase::$session_name protected property The current session name, if available.
DrupalWebTestCase::$session_id protected property The current session ID, if available.
DrupalWebTestCase::$redirect_count protected property The number of redirects followed during the handling of a request.
DrupalWebTestCase::$profile protected property The profile to install as a basis for testing. 20
DrupalWebTestCase::$plainTextContent protected property The content of the page currently loaded in the internal browser (plain text version).
DrupalWebTestCase::$originalUser protected property The original user, before it was changed to a clean uid = 1 for testing purposes.
DrupalWebTestCase::$originalShutdownCallbacks protected property The original shutdown handlers array, before it was cleaned for testing purposes.
DrupalWebTestCase::$loggedInUser protected property The current user logged in using the internal browser.
DrupalWebTestCase::$httpauth_method protected property HTTP authentication method
DrupalWebTestCase::$httpauth_credentials protected property HTTP authentication credentials (<username>:<password>).
DrupalWebTestCase::$headers protected property The headers of the page currently loaded in the internal browser.
DrupalWebTestCase::$generatedTestFiles protected property Whether the files were copied to the test files directory.
DrupalWebTestCase::$elements protected property The parsed version of the page.
DrupalWebTestCase::$drupalSettings protected property The value of the Drupal.settings JavaScript variable for the page currently loaded in the internal browser.
DrupalWebTestCase::$curlHandle protected property The handle of the current cURL connection.
DrupalWebTestCase::$cookies protected property The cookies of the page currently loaded in the internal browser.
DrupalWebTestCase::$cookieFile protected property The current cookie file used by cURL.
DrupalWebTestCase::$content protected property The content of the page currently loaded in the internal browser.
DrupalWebTestCase::$additionalCurlOptions protected property Additional cURL options.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$assertions protected property Assertions thrown in that test case.