You are here

public function MultipleSelectsWidgetUiTest::testSingleWidgetRequired in Multiple Selects 8

Test the single widget when required.

File

tests/src/Functional/MultipleSelectsWidgetUiTest.php, line 231

Class

MultipleSelectsWidgetUiTest
Tests the multiple select widget.

Namespace

Drupal\Tests\multiple_selects\Functional

Code

public function testSingleWidgetRequired() {
  $field_storage_config = FieldStorageConfig::loadByName('node', 'field_tags');
  $field_storage_config
    ->setCardinality(1);
  $field_storage_config
    ->save();
  $field_config = FieldConfig::loadByName('node', 'page', 'field_tags');
  $field_config
    ->setRequired(TRUE);
  $field_config
    ->save();
  $title = $this
    ->randomMachineName();
  $this
    ->drupalGet('node/add/page');
  $this
    ->submitForm([
    'title[0][value]' => $title,
  ], 'Save');

  // Since the field is required, the form should throw an error.
  $this
    ->assertSession()
    ->pageTextContains('Tags field is required.');
  $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.");
}