public function MultipleSelectsWidgetUiTest::testWidget in Multiple Selects 8
Test that using the widget saves the values correctly to the entity.
File
- tests/
src/ Functional/ MultipleSelectsWidgetUiTest.php, line 92
Class
- MultipleSelectsWidgetUiTest
- Tests the multiple select widget.
Namespace
Drupal\Tests\multiple_selects\FunctionalCode
public function testWidget() {
$this
->drupalGet('node/add/page');
// Check that the second field is not enabled by default.
$this
->assertSession()
->selectExists('field_tags[0][target_id]');
$this
->assertSession()
->fieldNotExists('field_tags[1][target_id]');
// Click the add another item button.
$this
->assertSession()
->buttonExists('Add another item');
$this
->click('.field--name-field-tags .field-add-more-submit');
// Second select should be visible now.
$this
->assertSession()
->selectExists('field_tags[0][target_id]');
$this
->assertSession()
->selectExists('field_tags[1][target_id]');
// Submit a node with multiple tag values.
$title = $this
->randomMachineName();
$tag_1_key = array_rand($this->tags);
$tag_1 = $this->tags[$tag_1_key];
$tag_2_key = array_rand($this->tags);
$tag_2 = $this->tags[$tag_2_key];
$this
->submitForm([
'title[0][value]' => $title,
'field_tags[0][target_id]' => $tag_1
->id(),
'field_tags[1][target_id]' => $tag_2
->id(),
], 'Save');
// Check that the node was saved.
$this
->assertSession()
->pageTextContains("Page {$title} has been created.");
// Check that the values are correctly saved.
$node = $this
->drupalGetNodeByTitle($title);
$this
->assertEquals($tag_1
->id(), $node
->get('field_tags')
->get(0)->target_id);
$this
->assertEquals($tag_2
->id(), $node
->get('field_tags')
->get(1)->target_id);
$this
->assertEmpty($node
->get('field_tags')
->get(2));
// Check that the values are correctly shown.
$this
->drupalGet('node/' . $node
->id() . '/edit');
$tag_value_1 = $this
->assertSession()
->selectExists('field_tags[0][target_id]')
->getValue();
$this
->assertEquals($tag_1
->id(), $tag_value_1);
$tag_value_2 = $this
->assertSession()
->selectExists('field_tags[1][target_id]')
->getValue();
$this
->assertEquals($tag_2
->id(), $tag_value_2);
// The 3rd field should be visible but has no value.
$tag_value_3 = $this
->assertSession()
->selectExists('field_tags[2][target_id]')
->getValue();
$this
->assertEquals('_none', $tag_value_3);
}