public function FormJSAddMoreTest::testFieldFormJsAddMore in Drupal 9
Same name and namespace in other branches
- 8 core/modules/field/tests/src/FunctionalJavascript/FormJSAddMoreTest.php \Drupal\Tests\field\FunctionalJavascript\FormJSAddMoreTest::testFieldFormJsAddMore()
- 10 core/modules/field/tests/src/FunctionalJavascript/FormJSAddMoreTest.php \Drupal\Tests\field\FunctionalJavascript\FormJSAddMoreTest::testFieldFormJsAddMore()
Tests the 'Add more' functionality.
File
- core/
modules/ field/ tests/ src/ FunctionalJavascript/ FormJSAddMoreTest.php, line 67
Class
- FormJSAddMoreTest
- Tests add more behavior for a multiple value field.
Namespace
Drupal\Tests\field\FunctionalJavascriptCode
public function testFieldFormJsAddMore() {
$this
->drupalGet('entity_test/add');
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$add_more_button = $page
->findButton('field_unlimited_add_more');
// First set a value on the first input field.
$field_0 = $page
->findField('field_unlimited[0][value]');
$field_0
->setValue('1');
// Add another item.
$add_more_button
->click();
$field_1 = $assert_session
->waitForField('field_unlimited[1][value]');
$this
->assertNotEmpty($field_1, 'Successfully added another item.');
// Validate the value of the first field has not changed.
$this
->assertEquals('1', $field_0
->getValue(), 'Value for the first item has not changed.');
// Validate the value of the second item is empty.
$this
->assertEmpty($field_1
->getValue(), 'Value for the second item is currently empty.');
// Add another item.
$add_more_button
->click();
$field_2 = $assert_session
->waitForField('field_unlimited[2][value]');
$this
->assertNotEmpty($field_2, 'Successfully added another item.');
// Set values for the 2nd and 3rd fields to validate dragging.
$field_1
->setValue('2');
$field_2
->setValue('3');
$field_weight_0 = $page
->findField('field_unlimited[0][_weight]');
$field_weight_1 = $page
->findField('field_unlimited[1][_weight]');
$field_weight_2 = $page
->findField('field_unlimited[2][_weight]');
// Assert starting situation matches expectations.
$this
->assertGreaterThan($field_weight_0
->getValue(), $field_weight_1
->getValue());
$this
->assertGreaterThan($field_weight_1
->getValue(), $field_weight_2
->getValue());
// Drag the first row after the third row.
$dragged = $field_0
->find('xpath', 'ancestor::tr[contains(@class, "draggable")]//a[@class="tabledrag-handle"]');
$target = $field_2
->find('xpath', 'ancestor::tr[contains(@class, "draggable")]');
$dragged
->dragTo($target);
// Assert the order of items is updated correctly after dragging.
$this
->assertGreaterThan($field_weight_2
->getValue(), $field_weight_0
->getValue());
$this
->assertGreaterThan($field_weight_1
->getValue(), $field_weight_2
->getValue());
// Validate the order of items conforms to the last drag action after a
// updating the form via the server.
$add_more_button
->click();
$field_3 = $assert_session
->waitForField('field_unlimited[3][value]');
$this
->assertNotEmpty($field_3);
$this
->assertGreaterThan($field_weight_2
->getValue(), $field_weight_0
->getValue());
$this
->assertGreaterThan($field_weight_1
->getValue(), $field_weight_2
->getValue());
// Validate no extraneous widget is displayed.
$element = $page
->findField('field_unlimited[4][value]');
$this
->assertEmpty($element);
}