function FormTest::testFieldFormJSAddMore in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testFieldFormJSAddMore()
File
- core/
modules/ field/ src/ Tests/ FormTest.php, line 404 - Contains \Drupal\field\Tests\FormTest.
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\field\TestsCode
function testFieldFormJSAddMore() {
$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');
// Press 'add more' button a couple times -> 3 widgets.
// drupalPostAjaxForm() will not work iteratively, so we add those through
// non-JS submission.
$this
->drupalPostForm(NULL, array(), t('Add another item'));
$this
->drupalPostForm(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["{$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 through Ajax, and place the expected HTML result
// as the tested content.
$commands = $this
->drupalPostAjaxForm(NULL, $edit, $field_name . '_add_more');
$this
->setRawContent($commands[2]['data']);
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');
}