Expanded class hierarchy of FormTest
class FormTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array(
'node',
'field_test',
'options',
);
public static function getInfo() {
return array(
'name' => 'Field form tests',
'description' => 'Test Field form handling.',
'group' => 'Field API',
);
}
function setUp() {
parent::setUp();
$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' => '[site:name]_description',
'weight' => mt_rand(0, 127),
'settings' => array(
'test_instance_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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form.
$this
->drupalGet('test-entity/add/test_bundle');
// Create token value expected for description.
$token_description = check_plain(config('system.site')
->get('name')) . '_description';
$this
->assertText($token_description, 'Token replacement for description is displayed');
$this
->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", '', 'Widget is displayed');
$this
->assertNoField("{$this->field_name}[{$langcode}][1][value]", 'No extraneous widget is displayed');
// Check that hook_field_widget_form_alter() does not believe this is the
// default value form.
$this
->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().');
// 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');
$this->container
->get('plugin.manager.entity')
->getStorageController('test_entity')
->resetCache(array(
$id,
));
$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');
$this->container
->get('plugin.manager.entity')
->getStorageController('test_entity')
->resetCache(array(
$id,
));
$entity = field_test_entity_test_load($id);
$this
->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied');
}
/**
* Tests field widget default values on entity forms.
*/
function testFieldFormDefaultValue() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$default = rand(1, 127);
$this->instance['default_value'] = array(
array(
'value' => $default,
),
);
field_create_field($this->field);
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form.
$this
->drupalGet('test-entity/add/test_bundle');
// Test that the default value is displayed correctly.
$this
->assertFieldByXpath("//input[@name='{$this->field_name}[{$langcode}][0][value]' and @value='{$default}']");
// Try to submit an empty value.
$edit = array(
"{$this->field_name}[{$langcode}][0][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
->assertTrue(empty($entity->{$this->field_name}), 'Field is now empty.');
}
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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// 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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// 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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// 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',
),
),
));
$instance = array(
'field_name' => 'required_radio_test',
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'required' => TRUE,
);
field_create_instance($instance);
entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($instance['field_name'], array(
'type' => 'options_buttons',
))
->save();
// 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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// 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;
field_create_field($this->field);
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name, array(
'type' => 'test_field_widget_multiple',
))
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// 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_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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($field_name)
->save();
// 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);
entity_get_form_display($instance_no_access['entity_type'], $instance_no_access['bundle'], 'default')
->setComponent($field_name_no_access)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Test that the form structure includes full information for each delta
// apart from #access.
$entity_type = 'test_entity';
$entity = field_test_create_entity(0, 0, $this->instance['bundle']);
$form = array();
$form_state = form_state_defaults();
$form_state['form_display'] = entity_get_form_display($entity_type, $this->instance['bundle'], 'default');
field_attach_form($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.
$this->container
->get('plugin.manager.entity')
->getStorageController('test_entity')
->resetCache(array(
$id,
));
$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);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'])
->save();
$this->instance['field_name'] = 'field_unlimited';
$this->instance['label'] = 'Unlimited field';
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'])
->save();
// Create two entities.
$entity_1 = field_test_create_entity(1, 1);
$entity_1->is_new = TRUE;
$entity_1->field_single[Language::LANGCODE_NOT_SPECIFIED][] = array(
'value' => 0,
);
$entity_1->field_unlimited[Language::LANGCODE_NOT_SPECIFIED][] = array(
'value' => 1,
);
field_test_entity_save($entity_1);
$entity_2 = field_test_create_entity(2, 2);
$entity_2->is_new = TRUE;
$entity_2->field_single[Language::LANGCODE_NOT_SPECIFIED][] = array(
'value' => 10,
);
$entity_2->field_unlimited[Language::LANGCODE_NOT_SPECIFIED][] = 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_entity(1);
$entity_2 = field_test_create_entity(2);
$this
->assertFieldValues($entity_1, 'field_single', Language::LANGCODE_NOT_SPECIFIED, array(
1,
));
$this
->assertFieldValues($entity_1, 'field_unlimited', Language::LANGCODE_NOT_SPECIFIED, array(
2,
3,
));
$this
->assertFieldValues($entity_2, 'field_single', Language::LANGCODE_NOT_SPECIFIED, array(
11,
));
$this
->assertFieldValues($entity_2, 'field_unlimited', Language::LANGCODE_NOT_SPECIFIED, 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::LANGCODE_NOT_SPECIFIED, array(
3,
2,
));
$this
->assertFieldValues($entity_2, 'field_unlimited', Language::LANGCODE_NOT_SPECIFIED, 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::LANGCODE_NOT_SPECIFIED, array(
3,
2,
));
$this
->assertFieldValues($entity_2, 'field_unlimited', Language::LANGCODE_NOT_SPECIFIED, array(
13,
14,
15,
));
}
/**
* Tests the Hidden widget.
*/
function testFieldFormHiddenWidget() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$this->instance['default_value'] = array(
0 => array(
'value' => 99,
),
);
field_create_field($this->field);
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'hidden',
))
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display the entity creation form.
$this
->drupalGet('test-entity/add/test_bundle');
// Create an entity and test that the default value is assigned correctly to
// the field that uses the hidden widget.
$this
->assertNoField("{$this->field_name}[{$langcode}][0][value]", 'The hidden widget is not displayed');
$this
->drupalPost(NULL, array(), 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'], 99, 'Default value was saved');
// Update the instance to remove the default value and switch to the
// default widget.
$this->instance['default_value'] = NULL;
field_update_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'test_field_widget',
))
->save();
// Display edit form.
$this
->drupalGet('test-entity/manage/' . $id . '/edit');
$this
->assertFieldByName("{$this->field_name}[{$langcode}][0][value]", 99, 'Widget is displayed with the correct default value');
// 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_get_controller('test_entity')
->resetCache(array(
$id,
));
$entity = field_test_entity_test_load($id);
$this
->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
// Update the form display and switch to the Hidden widget again.
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->instance['field_name'], array(
'type' => 'hidden',
))
->save();
// Create a new revision.
$edit = array(
'revision' => TRUE,
);
$this
->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save'));
// Check that the expected value has been carried over to the new revision.
entity_get_controller('test_entity')
->resetCache(array(
$id,
));
$entity = field_test_entity_test_load($id);
$this
->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget');
}
/**
* {inheritdoc}
*/
function assertFieldValues(EntityInterface $entity, $field_name, $langcode, $expected_values, $column = 'value') {
// Override the base implementation with one that works with the old
// entity API.
// @todo: Remove this when replacing the remaining of test_entity with
// entity_test.
$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), 'Expected number of values were saved.');
foreach ($expected_values as $key => $value) {
$this
->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array(
'@value' => $value,
)));
}
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormTest:: |
function |
Sets up a Drupal site for running functional and integration tests. Overrides WebTestBase:: |
||
FormTest:: |
function | |||
FormTest:: |
function | Tests field widget default values on entity forms. | ||
FormTest:: |
function | |||
FormTest:: |
function | |||
FormTest:: |
function | Tests widget handling of multiple required radios. | ||
FormTest:: |
function | |||
FormTest:: |
function | Tests widgets handling multiple values. | ||
FormTest:: |
function | Tests fields with no 'edit' access. | ||
FormTest:: |
function | Tests Field API form integration within a subform. | ||
FormTest:: |
function | Tests the Hidden widget. | ||
FormTest:: |
function |
{inheritdoc} Overrides FieldTestBase:: |
||
FieldTestBase:: |
function | Generate random values for a field_test field. | ||
WebTestBase:: |
function |
Constructor for Drupal\simpletest\WebTestBase. Overrides TestBase:: |
||
WebTestBase:: |
function | Get a node from the database based on its title. | ||
WebTestBase:: |
protected | function | Creates a node based on default settings. | |
WebTestBase:: |
protected | function | Creates a custom content type based on default settings. | |
WebTestBase:: |
protected | function | Creates a block instance based on default settings. | |
WebTestBase:: |
protected | function | Get a list files that can be used in tests. | |
WebTestBase:: |
protected | function | Compare two files based on size and file name. | |
WebTestBase:: |
protected | function | Create a user with a given set of permissions. | |
WebTestBase:: |
protected | function | Internal helper function; Create a role with specified permissions. | |
WebTestBase:: |
protected | function | Check to make sure that the array of permissions are valid. | |
WebTestBase:: |
protected | function | Log in a user with the internal browser. | 1 |
WebTestBase:: |
protected | function | Returns whether a given user account is logged in. | |
WebTestBase:: |
protected | function | Generate a token for the currently logged in user. | |
WebTestBase:: |
protected | function | ||
WebTestBase:: |
protected | function | Returns the parameters that will be used when Simpletest installs Drupal. | 1 |
WebTestBase:: |
protected | function | Writes a test-specific settings.php file for the child site. | |
WebTestBase:: |
protected | function |
Overrides Drupal\simpletest\TestBase::rebuildContainer(). Overrides TestBase:: |
|
WebTestBase:: |
protected | function | Reset all data structures after having enabled new modules. | |
WebTestBase:: |
protected | function | Refresh the in-memory set of variables. Useful after a page request is made that changes a variable in a different thread. | 2 |
WebTestBase:: |
protected | function |
Delete created files and temporary files directory, delete the tables created by setUp(),
and reset the database prefix. Overrides TestBase:: |
5 |
WebTestBase:: |
protected | function | Initializes the cURL connection. | |
WebTestBase:: |
protected | function | Initializes and executes a cURL request. | |
WebTestBase:: |
protected | function | Reads headers and registers errors received from the tested site. | |
WebTestBase:: |
protected | function | Close the cURL handler and unset the handler. | |
WebTestBase:: |
protected | function | Parse content returned from curlExec using DOM and SimpleXML. | |
WebTestBase:: |
protected | function | Retrieves a Drupal path or an absolute path. | 1 |
WebTestBase:: |
protected | function | Retrieves a Drupal path or an absolute path and JSON decode the result. | |
WebTestBase:: |
protected | function | Requests a Drupal path in drupal_ajax format, and JSON decodes the response. | |
WebTestBase:: |
protected | function | Execute a POST request on a Drupal page. It will be done as usual POST request with SimpleBrowser. | |
WebTestBase:: |
protected | function | Execute an Ajax submission. | |
WebTestBase:: |
protected | function | Runs cron in the Drupal installed by Simpletest. | |
WebTestBase:: |
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. | |
WebTestBase:: |
protected | function | Retrieves only the headers for a Drupal path or an absolute path. | |
WebTestBase:: |
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. | |
WebTestBase:: |
protected | function | Builds an XPath query. | |
WebTestBase:: |
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. | |
WebTestBase:: |
protected | function | Get all option elements, including nested options, in a select. | |
WebTestBase:: |
protected | function | Pass if a link with the specified label is found, and optional with the specified index. | |
WebTestBase:: |
protected | function | Pass if a link with the specified label is not found. | |
WebTestBase:: |
protected | function | Pass if a link containing a given href (part) is found. | |
WebTestBase:: |
protected | function | Pass if a link containing a given href (part) is not found. | |
WebTestBase:: |
protected | function | Follows a link by name. | |
WebTestBase:: |
protected | function | Takes a path and returns an absolute path. | |
WebTestBase:: |
protected | function | Get the current URL from the cURL handler. | |
WebTestBase:: |
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… | |
WebTestBase:: |
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… | |
WebTestBase:: |
protected | function | Gets the current raw HTML of requested page. | |
WebTestBase:: |
protected | function | Gets the value of the Drupal.settings JavaScript variable for the currently loaded page. | |
WebTestBase:: |
protected | function | Gets an array containing all e-mails sent during this test case. | |
WebTestBase:: |
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. | |
WebTestBase:: |
protected | function | Sets the value of the Drupal.settings JavaScript variable for the currently loaded page. | |
WebTestBase:: |
protected | function | Pass if the internal browser's URL matches the given path. | |
WebTestBase:: |
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. | |
WebTestBase:: |
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. | |
WebTestBase:: |
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. | |
WebTestBase:: |
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. | |
WebTestBase:: |
protected | function | Helper for assertText and assertNoText. | |
WebTestBase:: |
protected | function | Pass if the text is found ONLY ONCE on the text version of the page. | |
WebTestBase:: |
protected | function | Pass if the text is found MORE THAN ONCE on the text version of the page. | |
WebTestBase:: |
protected | function | Helper for assertUniqueText and assertNoUniqueText. | |
WebTestBase:: |
protected | function | Will trigger a pass if the Perl regex pattern is found in the raw content. | |
WebTestBase:: |
protected | function | Will trigger a pass if the perl regex pattern is not present in raw content. | |
WebTestBase:: |
protected | function | Pass if the page title is the given string. | |
WebTestBase:: |
protected | function | Pass if the page title is not the given string. | |
WebTestBase:: |
protected | function | Asserts themed output. | |
WebTestBase:: |
protected | function | Asserts that a field exists in the current page by the given XPath. | |
WebTestBase:: |
protected | function | Get the selected value from a select field. | |
WebTestBase:: |
protected | function | Asserts that a field does not exist in the current page by the given XPath. | |
WebTestBase:: |
protected | function | Asserts that a field exists in the current page with the given name and value. | |
WebTestBase:: |
protected | function | Asserts that a field does not exist with the given name and value. | |
WebTestBase:: |
protected | function | Asserts that a field exists in the current page with the given id and value. | |
WebTestBase:: |
protected | function | Asserts that a field does not exist with the given id and value. | |
WebTestBase:: |
protected | function | Asserts that a checkbox field in the current page is checked. | |
WebTestBase:: |
protected | function | Asserts that a checkbox field in the current page is not checked. | |
WebTestBase:: |
protected | function | Asserts that a select option in the current page exists. | |
WebTestBase:: |
protected | function | Asserts that a select option in the current page does not exist. | |
WebTestBase:: |
protected | function | Asserts that a select option in the current page is checked. | |
WebTestBase:: |
protected | function | Asserts that a select option in the current page is not checked. | |
WebTestBase:: |
protected | function | Asserts that a field exists with the given name or id. | |
WebTestBase:: |
protected | function | Asserts that a field does not exist with the given name or id. | |
WebTestBase:: |
protected | function | Asserts that each HTML ID is used for just a single element. | |
WebTestBase:: |
protected | function | Helper function: construct an XPath for the given set of attributes and value. | |
WebTestBase:: |
protected | function | Asserts the page responds with the specified response code. | |
WebTestBase:: |
protected | function | Asserts the page did not return the specified response code. | |
WebTestBase:: |
protected | function | Asserts that the most recently sent e-mail message has the given value. | |
WebTestBase:: |
protected | function | Asserts that the most recently sent e-mail message has the string in it. | |
WebTestBase:: |
protected | function | Asserts that the most recently sent e-mail message has the pattern in it. | |
WebTestBase:: |
protected | function | Outputs to verbose the most recent $count emails sent. | |
WebTestBase:: |
protected | function | Creates a mock request and sets it on the generator. | |
TestBase:: |
protected | function | Checks the matching requirements for Test. | 4 |
TestBase:: |
protected | function | Internal helper: stores the assert. | |
TestBase:: |
protected | function | Cycles through backtrace until the first non-assertion method is found. | |
TestBase:: |
protected | function | Check to see if a value is not false (not an empty string, 0, NULL, or FALSE). | |
TestBase:: |
protected | function | Check to see if a value is false (an empty string, 0, NULL, or FALSE). | |
TestBase:: |
protected | function | Check to see if a value is NULL. | |
TestBase:: |
protected | function | Check to see if a value is not NULL. | |
TestBase:: |
protected | function | Check to see if two values are equal. | |
TestBase:: |
protected | function | Check to see if two values are not equal. | |
TestBase:: |
protected | function | Check to see if two values are identical. | |
TestBase:: |
protected | function | Check to see if two values are not identical. | |
TestBase:: |
protected | function | Checks to see if two objects are identical. | |
TestBase:: |
protected | function | Fire an assertion that is always positive. | |
TestBase:: |
protected | function | Fire an assertion that is always negative. | |
TestBase:: |
protected | function | Fire an error assertion. | 1 |
TestBase:: |
protected | function | Logs verbose message in a text file. | |
TestBase:: |
protected | function | Generates a database prefix for running tests. | |
TestBase:: |
protected | function | Changes the database connection to the prefixed one. | |
TestBase:: |
protected | function | Prepares the current environment for running the test. | |
TestBase:: |
protected | function | Create and set new configuration directories. | 1 |
TestBase:: |
protected | function | Handle exceptions. | |
TestBase:: |
protected | function | Changes in memory settings. | |
WebTestBase:: |
protected | property | The profile to install as a basis for testing. | 25 |
WebTestBase:: |
protected | property | The URL currently loaded in the internal browser. | |
WebTestBase:: |
protected | property | The handle of the current cURL connection. | |
WebTestBase:: |
protected | property | The headers of the page currently loaded in the internal browser. | |
WebTestBase:: |
protected | property | Indicates that headers should be dumped if verbose output is enabled. | 3 |
WebTestBase:: |
protected | property | The content of the page currently loaded in the internal browser. | |
WebTestBase:: |
protected | property | The content of the page currently loaded in the internal browser (plain text version). | |
WebTestBase:: |
protected | property | The value of the Drupal.settings JavaScript variable for the page currently loaded in the internal browser. | |
WebTestBase:: |
protected | property | The parsed version of the page. | 1 |
WebTestBase:: |
protected | property | The current user logged in using the internal browser. | |
WebTestBase:: |
protected | property | The current cookie file used by cURL. | |
WebTestBase:: |
protected | property | Additional cURL options. | |
WebTestBase:: |
protected | property | The original user, before it was changed to a clean uid = 1 for testing purposes. | |
WebTestBase:: |
protected | property | The original shutdown handlers array, before it was cleaned for testing purposes. | |
WebTestBase:: |
protected | property | HTTP authentication method | |
WebTestBase:: |
protected | property | HTTP authentication credentials (<username>:<password>). | |
WebTestBase:: |
protected | property | The current session name, if available. | |
WebTestBase:: |
protected | property | The current session ID, if available. | |
WebTestBase:: |
protected | property | Whether the files were copied to the test files directory. | |
WebTestBase:: |
protected | property | The maximum number of redirects to follow when handling responses. | |
WebTestBase:: |
protected | property | The number of redirects followed during the handling of a request. | |
WebTestBase:: |
protected | property | The kernel used in this test. | |
TestBase:: |
protected | property | The test run ID. | |
TestBase:: |
protected | property | The database prefix of this test run. | |
TestBase:: |
protected | property | The original file directory, before it was changed for testing purposes. | |
TestBase:: |
protected | property | Time limit for the test. | |
TestBase:: |
protected | property | Assertions thrown in that test case. | |
TestBase:: |
protected | property | This class is skipped when looking for the source of an assertion. | |
TestBase:: |
protected | property | Flag to indicate whether the test has been set up. | |
TestBase:: |
protected | property | ||
TestBase:: |
protected | property | ||
TestBase:: |
protected | property | TRUE if verbose debugging is enabled. | |
TestBase:: |
protected | property | Incrementing identifier for verbose output filenames. | |
TestBase:: |
protected | property | Safe class name for use in verbose output filenames. | |
TestBase:: |
protected | property | Directory where verbose output files are put. | |
TestBase:: |
protected | property | The original database prefix when running inside Simpletest. | |
TestBase:: |
protected | property | URL to the verbose output file directory. | |
TestBase:: |
protected | property | The settings array. | |
TestBase:: |
protected | property | The public file directory for the test environment. | |
TestBase:: |
protected | property | The dependency injection container used in the test. | 1 |
TestBase:: |
protected | property | The config importer that can used in a test. | 1 |
TestBase:: |
public | function | Run all tests in this class. | |
TestBase:: |
public | function | Handle errors during test runs. | |
TestBase:: |
public | function | Returns a ConfigImporter object to import test importing of configuration. | 1 |
TestBase:: |
public | function | Copies configuration objects from source storage to target storage. | |
TestBase:: |
public | property | Current results of this test case. | |
TestBase:: |
public | property | Whether to die in case any test assertion fails. | |
FormTest:: |
public static | function | ||
TestBase:: |
public static | function | Store an assertion from outside the testing context. | |
TestBase:: |
public static | function | Delete an assertion record by message ID. | |
TestBase:: |
public static | function | Returns the database connection to the site running Simpletest. | |
TestBase:: |
public static | function | Generates a random string of ASCII characters of codes 32 to 126. | |
TestBase:: |
public static | function | Generates a random string containing letters and numbers. | |
TestBase:: |
public static | function | Generates a random PHP object. | |
TestBase:: |
public static | function | Converts a list of possible parameters into a stack of permutations. | |
TestBase:: |
public static | function | Ensures test files are deletable within file_unmanaged_delete_recursive(). | |
FormTest:: |
public static | property | Modules to enable. |