public function MultipleSelectsWidgetUiTest::testSingleWidget in Multiple Selects 8
Test widget with cardinality 1.
File
- tests/
src/ Functional/ MultipleSelectsWidgetUiTest.php, line 195
Class
- MultipleSelectsWidgetUiTest
- Tests the multiple select widget.
Namespace
Drupal\Tests\multiple_selects\FunctionalCode
public function testSingleWidget() {
$field_storage_config = FieldStorageConfig::loadByName('node', 'field_tags');
$field_storage_config
->setCardinality(1);
$field_storage_config
->save();
$this
->drupalGet('node/add/page');
$this
->assertSession()
->selectExists('field_tags[0][target_id]');
$this
->assertSession()
->fieldNotExists('field_tags[1][target_id]');
$this
->assertSession()
->buttonNotExists('Add another item');
$title = $this
->randomMachineName();
$tag_1_key = array_rand($this->tags);
$tag_1 = $this->tags[$tag_1_key];
$this
->submitForm([
'title[0][value]' => $title,
'field_tags[0][target_id]' => $tag_1
->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
->assertEmpty($node
->get('field_tags')
->get(1));
// 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);
$this
->assertSession()
->fieldNotExists('field_tags[1][target_id]');
}