1 field.test FieldFormTestCase::testFieldFormSingle()

File

core/modules/field/tests/field.test, line 1480
Tests for field.module.

Class

FieldFormTestCase

Code

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->backdropGet('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->backdropPost(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->backdropPost(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->backdropGet('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->backdropPost(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->backdropPost('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');

}