View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\webform\Entity\Webform;
class WebformElementMultipleTest extends WebformElementBrowserTestBase {
protected static $testWebforms = [
'test_element_multiple',
];
public function testMultiple() {
$webform = Webform::load('test_element_multiple');
$this
->drupalPostForm('/webform/test_element_multiple', [], 'Submit');
$this
->assertRaw("webform_multiple_default:\n - One\n - Two\n - Three\nwebform_multiple_no_sorting:\n - One\n - Two\n - Three\nwebform_multiple_no_operations:\n - One\n - Two\n - Three\nwebform_multiple_no_add_more:\n - One\n - Two\n - Three\nwebform_multiple_no_add_more_input:\n - One\n - Two\n - Three\nwebform_multiple_custom_label:\n - One\n - Two\n - Three\nwebform_multiple_required:\n - One\n - Two\n - Three\nwebform_multiple_email_five:\n - example@example.com\n - test@test.com\nwebform_multiple_datelist: { }\nwebform_multiple_name_composite:\n - title: ''\n first: John\n middle: ''\n last: Smith\n suffix: ''\n degree: ''\n - title: ''\n first: Jane\n middle: ''\n last: Doe\n suffix: ''\n degree: ''\nwebform_multiple_elements_name_item:\n - first_name: John\n last_name: Smith\n - first_name: Jane\n last_name: Doe\nwebform_multiple_elements_name_table:\n - first_name: John\n last_name: Smith\n - first_name: Jane\n last_name: Doe\nwebform_multiple_options:\n - value: one\n text: One\n - value: two\n text: Two\nwebform_multiple_key:\n one:\n text: One\n score: '1'\n two:\n text: Two\n score: '2'\nwebform_multiple_elements_hidden_table:\n - first_name: John\n id: john\n last_name: Smith\n - first_name: Jane\n id: jane\n last_name: Doe\nwebform_multiple_elements_flattened:\n - value: one\n text: One\n description: 'This is the number 1.'\n - value: two\n text: Two\n description: 'This is the number 2.'\nwebform_multiple_no_items: { }\nwebform_multiple_custom_attributes: { }");
$this
->drupalGet('/webform/test_element_multiple');
$this
->assertRaw('<tr class="draggable" data-drupal-selector="edit-webform-multiple-default-items-0">');
$this
->assertRaw('<td><div class="js-form-item form-item js-form-type-textfield form-item-webform-multiple-default-items-0--item- js-form-item-webform-multiple-default-items-0--item- form-no-label">');
$this
->assertRaw('<label for="edit-webform-multiple-default-items-0-item-" class="visually-hidden">Item value</label>');
$this
->assertRaw('<input data-drupal-selector="edit-webform-multiple-default-items-0-item-" type="text" id="edit-webform-multiple-default-items-0-item-" name="webform_multiple_default[items][0][_item_]" value="One" size="60" maxlength="128" placeholder="Enter value…" class="form-text" />');
$this
->assertRaw('<td class="webform-multiple-table--weight"><div class="webform-multiple-table--weight js-form-item form-item js-form-type-number form-item-webform-multiple-default-items-0-weight js-form-item-webform-multiple-default-items-0-weight form-no-label">');
$this
->assertRaw('<label for="edit-webform-multiple-default-items-0-weight" class="visually-hidden">Item weight</label>');
$this
->assertRaw('<input class="webform-multiple-sort-weight form-number" data-drupal-selector="edit-webform-multiple-default-items-0-weight" type="number" id="edit-webform-multiple-default-items-0-weight" name="webform_multiple_default[items][0][weight]" value="0" step="1" size="10" />');
$this
->assertRaw('<td class="webform-multiple-table--operations webform-multiple-table--operations-two"><input data-drupal-selector="edit-webform-multiple-default-items-0-operations-add" formnovalidate="formnovalidate" type="image" id="edit-webform-multiple-default-items-0-operations-add" name="webform_multiple_default_table_add_0"');
$this
->assertRaw('<input data-drupal-selector="edit-webform-multiple-default-items-0-operations-remove" formnovalidate="formnovalidate" type="image" id="edit-webform-multiple-default-items-0-operations-remove" name="webform_multiple_default_table_remove_0"');
$this
->assertNoRaw('<tr class="draggable" data-drupal-selector="edit-webform-multiple-no-sorting-items-0">');
$this
->assertRaw('<tr data-drupal-selector="edit-webform-multiple-no-sorting-items-0">');
$this
->assertFieldByName('webform_multiple_no_operations[add][more_items]', '1');
$this
->assertNoFieldByName('webform_multiple_no_add_more_table_add', 'Add');
$this
->assertNoFieldByName('webform_multiple_no_add_more[add][more_items]', '1');
$this
->assertFieldByName('webform_multiple_no_add_more_input_table_add', 'Add');
$this
->assertNoFieldByName('webform_multiple_no_add_more_input[add][more_items]', '1');
$this
->assertRaw('<input data-drupal-selector="edit-webform-multiple-custom-label-add-submit" formnovalidate="formnovalidate" type="submit" id="edit-webform-multiple-custom-label-add-submit" name="webform_multiple_custom_label_table_add" value="{add_more_button_label}" class="button js-form-submit form-submit" />');
$this
->assertRaw('<span class="field-suffix">{add_more_input_label}</span>');
$this
->assertNoRaw('data-drupal-selector="edit-webform-multiple-no-operations-items-0-operations-remove"');
$this
->assertRaw('No items entered. Please add items below.');
$this
->assertFieldByName('webform_multiple_required[items][2][_item_]');
$this
->assertNoFieldByName('webform_multiple_required[items][3][_item_]');
$this
->assertRaw('<div class="custom-ajax" id="webform_multiple_custom_attributes_table">');
$this
->assertRaw('<div class="custom-table-wrapper webform-multiple-table">');
$this
->assertRaw('<table class="custom-table responsive-enabled" data-drupal-selector="edit-webform-multiple-custom-attributes-items" id="edit-webform-multiple-custom-attributes-items" data-striping="1">');
$this
->assertRaw('<th class="custom-label webform_multiple_custom_attributes-table--textfield webform-multiple-table--textfield">textfield</th>');
$this
->assertRaw('<label class="custom-label visually-hidden"');
$this
->assertRaw('<div class="custom-wrapper js-form-item form-item');
$this
->assertRaw('<input class="custom-element form-text"');
$edit = [
'webform_multiple_key[items][1][value]' => 'one',
];
$this
->drupalPostForm('/webform/test_element_multiple', $edit, 'Submit');
$this
->assertRaw('The <em class="placeholder">Option value</em> \'one\' is already in use. It must be unique.');
$this
->assertFieldByName('webform_multiple_default[items][0][_item_]', 'One');
$this
->assertFieldByName('webform_multiple_default[items][1][_item_]', 'Two');
$this
->assertFieldByName('webform_multiple_default[items][2][_item_]', 'Three');
$this
->assertFieldByName('webform_multiple_default[items][3][_item_]', '');
$this
->assertNoFieldByName('webform_multiple_default[items][4][_item_]', '');
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_add_0');
$this
->assertFieldByName('webform_multiple_default[items][0][_item_]', 'One');
$this
->assertFieldByName('webform_multiple_default[items][1][_item_]', '');
$this
->assertNoFieldByName('webform_multiple_default[items][1][_item_]', 'Two');
$this
->assertFieldByName('webform_multiple_default[items][2][_item_]', 'Two');
$this
->assertFieldByName('webform_multiple_default[items][3][_item_]', 'Three');
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_remove_1');
$this
->assertFieldByName('webform_multiple_default[items][0][_item_]', 'One');
$this
->assertFieldByName('webform_multiple_default[items][1][_item_]', 'Two');
$this
->assertFieldByName('webform_multiple_default[items][2][_item_]', 'Three');
$edit = [
'webform_multiple_default[items][3][_item_]' => 'Four',
];
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_add');
$this
->assertFieldByName('webform_multiple_default[items][3][_item_]', 'Four');
$this
->assertFieldByName('webform_multiple_default[items][4][_item_]', '');
$edit = [
'webform_multiple_default[add][more_items]' => 10,
];
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_add');
$this
->assertFieldByName('webform_multiple_default[items][14][_item_]', '');
$this
->assertNoFieldByName('webform_multiple_default[items][15][_item_]', '');
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_remove_0');
$this
->assertNoFieldByName('webform_multiple_default[items][14][_item_]', '');
$this
->assertNoFieldByName('webform_multiple_default[items][0][_item_]', 'One');
$this
->assertFieldByName('webform_multiple_default[items][0][_item_]', 'Two');
$this
->assertFieldByName('webform_multiple_default[items][1][_item_]', 'Three');
$this
->assertFieldByName('webform_multiple_default[items][2][_item_]', 'Four');
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_no_items_table_add');
$this
->assertNoRaw('No items entered. Please add items below.');
$this
->assertFieldByName('webform_multiple_no_items[items][0][_item_]');
$webform
->setElementProperties('webform_multiple_no_items', [
'#type' => 'webform_multiple',
'#title' => 'webform_multiple_no_items',
'#required' => TRUE,
]);
$webform
->save();
$this
->drupalGet('/webform/test_element_multiple');
$this
->assertNoRaw('No items entered. Please add items below.');
$this
->drupalPostForm(NULL, $edit, 'webform_multiple_default_table_remove_0');
$this
->assertNoRaw('No items entered. Please add items below.');
}
}