function OptionsWidgetsTest::testSelectListSingle

Same name and namespace in other branches
  1. 11.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
  2. 10 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
  3. 9 core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()
  4. 8.9.x core/modules/options/tests/src/Functional/OptionsWidgetsTest.php \Drupal\Tests\options\Functional\OptionsWidgetsTest::testSelectListSingle()

Tests the 'options_select' widget (single select).

File

core/modules/options/tests/src/Functional/OptionsWidgetsTest.php, line 283

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListSingle() : void {
  // Create an instance of the 'single value' field.
  $field = FieldConfig::create([
    'field_storage' => $this->card1,
    'bundle' => 'entity_test',
    'required' => TRUE,
  ]);
  $field->save();
  \Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($this->card1
    ->getName(), [
    'type' => 'options_select',
  ])
    ->save();
  // Create an entity.
  $entity = EntityTest::create([
    'user_id' => 1,
    'name' => $this->randomMachineName(),
  ]);
  $entity->save();
  $entity_init = clone $entity;
  // Display form.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  // A required field without any value has a "none" option.
  $option = $this->assertSession()
    ->optionExists('edit-card-1', '_none');
  $this->assertSame('- Select a value -', $option->getText());
  // With no field data, nothing is selected.
  $this->assertTrue($this->assertSession()
    ->optionExists('card_1', '_none')
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 2)
    ->isSelected());
  $this->assertSession()
    ->responseContains('Some dangerous & unescaped markup');
  // Submit form: select invalid 'none' option.
  $edit = [
    'card_1' => '_none',
  ];
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains("{$field->getName()} field is required.");
  // Submit form: select first option.
  $edit = [
    'card_1' => 0,
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_1', [
    0,
  ]);
  // Display form: check that the right options are selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  // A required field with a value has no 'none' option.
  $this->assertSession()
    ->optionNotExists('edit-card-1', '_none');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_1', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 2)
    ->isSelected());
  // Make the field non required.
  $field->setRequired(FALSE);
  $field->save();
  // Display form.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  // A non-required field has a 'none' option.
  $option = $this->assertSession()
    ->optionExists('edit-card-1', '_none');
  $this->assertSame('- None -', $option->getText());
  // Submit form: Unselect the option.
  $edit = [
    'card_1' => '_none',
  ];
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_1', []);
  // Test optgroups.
  $this->card1
    ->setSetting('allowed_values', []);
  $this->card1
    ->setSetting('allowed_values_function', '\\Drupal\\options_test\\OptionsAllowedValues::simpleValues');
  $this->card1
    ->save();
  // Display form: with no field data, nothing is selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 2)
    ->isSelected());
  $this->assertSession()
    ->responseContains('Some dangerous & unescaped markup');
  $this->assertSession()
    ->responseContains('More <script>dangerous</script> markup');
  $this->assertSession()
    ->responseContains('Group 1');
  // Submit form: select first option.
  $edit = [
    'card_1' => 0,
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_1', [
    0,
  ]);
  // Display form: check that the right options are selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_1', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_1', 2)
    ->isSelected());
  // Submit form: Unselect the option.
  $edit = [
    'card_1' => '_none',
  ];
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_1', []);
  // Test required entity reference field when default value is deleted.
  // Create vocabulary and terms.
  $vocab = $this->createVocabulary();
  // Create some test terms.
  $term_to_delete = $this->createTerm($vocab);
  $another_test_term = $this->createTerm($vocab, [
    'weight' => 1000,
  ]);
  // Create the field and set as required.
  $field_name = mb_strtolower($this->randomMachineName());
  $handler_settings = [
    'target_bundles' => [
      $vocab->id() => $vocab->id(),
    ],
    'auto_create' => FALSE,
  ];
  $this->createEntityReferenceField('entity_test', 'entity_test', $field_name, 'Test term reference', 'taxonomy_term', 'default', $handler_settings);
  $field = FieldConfig::load('entity_test.entity_test.' . $field_name);
  $field->setRequired(TRUE);
  $field->save();
  // Set field widget as select list.
  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'options_select',
  ])
    ->save();
  // Create a page assigning it the $term_to_delete.
  $entity = EntityTest::create([
    'user_id' => 1,
    'name' => $this->randomMachineName(),
    $field_name => $term_to_delete->id(),
  ]);
  $entity->save();
  // Delete the $term_to_delete.
  $term_to_delete->delete();
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->optionExists($field_name, $another_test_term->id());
  $empty_option = $this->assertSession()
    ->optionExists($field_name, '_none');
  $this->assertTrue($empty_option->isSelected());
  $this->assertSame('- Select a value -', $empty_option->getText());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.