3 namespace Drupal\Tests\field\FunctionalJavascript;
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
12 * Tests add more behavior for a multiple value field.
16 class FormJSAddMoreTest extends WebDriverTestBase {
21 public static $modules = ['field_test', 'entity_test'];
26 protected function setUp() {
29 $account = $this->drupalCreateUser([
31 'administer entity_test content',
33 $this->drupalLogin($account);
36 'field_name' => 'field_unlimited',
37 'entity_type' => 'entity_test',
38 'bundle' => 'entity_test',
39 'label' => $this->randomMachineName() . '_label',
40 'description' => '[site:name]_description',
41 'weight' => mt_rand(0, 127),
43 'test_field_setting' => $this->randomMachineName(),
47 FieldStorageConfig::create([
48 'field_name' => 'field_unlimited',
49 'entity_type' => 'entity_test',
50 'type' => 'test_field',
51 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
53 FieldConfig::create($field)->save();
55 $entity_form_display = EntityFormDisplay::load($field['entity_type'] . '.' . $field['bundle'] . '.default');
56 $entity_form_display->setComponent($field['field_name'])->save();
60 * Tests the 'Add more' functionality.
62 public function testFieldFormJsAddMore() {
63 $this->drupalGet('entity_test/add');
65 $assert_session = $this->assertSession();
66 $page = $this->getSession()->getPage();
67 $add_more_button = $page->findButton('field_unlimited_add_more');
69 // First set a value on the first input field.
70 $field_0 = $page->findField('field_unlimited[0][value]');
71 $field_0->setValue('1');
74 $add_more_button->click();
75 $field_1 = $assert_session->waitForField('field_unlimited[1][value]');
76 $this->assertNotEmpty($field_1, 'Successfully added another item.');
78 // Validate the value of the first field has not changed.
79 $this->assertEquals('1', $field_0->getValue(), 'Value for the first item has not changed.');
81 // Validate the value of the second item is empty.
82 $this->assertEmpty($field_1->getValue(), 'Value for the second item is currently empty.');
85 $add_more_button->click();
86 $field_2 = $assert_session->waitForField('field_unlimited[2][value]');
87 $this->assertNotEmpty($field_2, 'Successfully added another item.');
89 // Set values for the 2nd and 3rd fields to validate dragging.
90 $field_1->setValue('2');
91 $field_2->setValue('3');
93 $field_weight_0 = $page->findField('field_unlimited[0][_weight]');
94 $field_weight_1 = $page->findField('field_unlimited[1][_weight]');
95 $field_weight_2 = $page->findField('field_unlimited[2][_weight]');
97 // Assert starting situation matches expectations.
98 $this->assertGreaterThan($field_weight_0->getValue(), $field_weight_1->getValue());
99 $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
101 // Drag the first row after the third row.
102 $dragged = $field_0->find('xpath', 'ancestor::tr[contains(@class, "draggable")]//a[@class="tabledrag-handle"]');
103 $target = $field_2->find('xpath', 'ancestor::tr[contains(@class, "draggable")]');
104 $dragged->dragTo($target);
106 // Assert the order of items is updated correctly after dragging.
107 $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
108 $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
110 // Validate the order of items conforms to the last drag action after a
111 // updating the form via the server.
112 $add_more_button->click();
113 $field_3 = $assert_session->waitForField('field_unlimited[3][value]');
114 $this->assertNotEmpty($field_3);
115 $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
116 $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
118 // Validate no extraneous widget is displayed.
119 $element = $page->findField('field_unlimited[4][value]');
120 $this->assertEmpty($element);