function FormTest::testFieldFormUnlimited in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testFieldFormUnlimited()
File
- core/
modules/ field/ src/ Tests/ FormTest.php, line 246 - Contains \Drupal\field\Tests\FormTest.
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\field\TestsCode
function testFieldFormUnlimited() {
$field_storage = $this->fieldStorageUnlimited;
$field_name = $field_storage['field_name'];
$this->field['field_name'] = $field_name;
entity_create('field_storage_config', $field_storage)
->save();
entity_create('field_config', $this->field)
->save();
entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
->setComponent($field_name)
->save();
// Display creation form -> 1 widget.
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
$this
->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
// Check if aria-describedby attribute is placed on multiple value widgets.
$elements = $this
->xpath('//table[@id="field-unlimited-values" and @aria-describedby="edit-field-unlimited--description"]');
$this
->assertTrue(isset($elements[0]), t('aria-describedby attribute is properly placed on multiple value widgets.'));
// Press 'add more' button -> 2 widgets.
$this
->drupalPostForm(NULL, array(), t('Add another item'));
$this
->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
$this
->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
$this
->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
// TODO : check that non-field inputs are preserved ('title'), etc.
// Yet another time so that we can play with more values -> 3 widgets.
$this
->drupalPostForm(NULL, array(), t('Add another item'));
// Prepare values and weights.
$count = 3;
$delta_range = $count - 1;
$values = $weights = $pattern = $expected_values = array();
$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["{$field_name}[{$delta}][value]"] = $value;
$edit["{$field_name}[{$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
->drupalPostForm(NULL, $edit, t('Add another item'));
for ($delta = 0; $delta <= $delta_range; $delta++) {
$this
->assertFieldByName("{$field_name}[{$delta}][value]", $values[$delta], "Widget {$delta} is displayed and has the right value");
$this
->assertFieldByName("{$field_name}[{$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("{$field_name}[{$delta}][value]", '', "New widget is displayed");
$this
->assertFieldByName("{$field_name}[{$delta}][_weight]", $delta, "New widget has the right weight");
$this
->assertNoField("{$field_name}[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
// Submit the form and create the entity.
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', array(
'@id' => $id,
)), 'Entity was created');
$entity = entity_load('entity_test', $id);
ksort($field_values);
$field_values = array_values($field_values);
$this
->assertIdentical($entity->{$field_name}
->getValue(), $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
}