function OptionsWidgetsTest::testSelectListMultiple

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

Tests the 'options_select' widget (multiple select).

File

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

Class

OptionsWidgetsTest
Tests the Options widgets.

Namespace

Drupal\Tests\options\Functional

Code

public function testSelectListMultiple() : void {
  // Create an instance of the 'multiple values' field.
  $field = FieldConfig::create([
    'field_storage' => $this->card2,
    'bundle' => 'entity_test',
  ]);
  $field->save();
  \Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($this->card2
    ->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: with no field data, nothing is selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_2', '_none')
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  $this->assertSession()
    ->responseContains('Some dangerous & unescaped markup');
  // Submit form: select first and third options.
  $edit = [
    'card_2[]' => [
      0 => 0,
      2 => 2,
    ],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', [
    0,
    2,
  ]);
  // Display form: check that the right options are selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this->assertTrue($this->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  // Submit form: select only first option.
  $edit = [
    'card_2[]' => [
      0 => 0,
    ],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);
  // Display form: check that the right options are selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  // Submit form: select the three options while the field accepts only 2.
  $edit = [
    'card_2[]' => [
      0 => 0,
      1 => 1,
      2 => 2,
    ],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('this field cannot hold more than 2 values');
  // Submit form: uncheck all options.
  $edit = [
    'card_2[]' => [],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', []);
  // Test the 'None' option.
  // Check that the 'none' option has no effect if actual options are selected
  // as well.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
      0 => 0,
    ],
  ];
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);
  // Check that selecting the 'none' option empties the field.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
    ],
  ];
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', []);
  // A required select list does not have an empty key.
  $field->setRequired(TRUE);
  $field->save();
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertEmpty($this->assertSession()
    ->selectExists('edit-card-2')
    ->find('xpath', 'option[@value=""]'));
  // We do not have to test that a required select list with one option is
  // auto-selected because the browser does it for us.
  // Test optgroups.
  // Use a callback function defining optgroups.
  $this->card2
    ->setSetting('allowed_values', []);
  $this->card2
    ->setSetting('allowed_values_function', '\\Drupal\\options_test\\OptionsAllowedValues::simpleValues');
  $this->card2
    ->save();
  $field->setRequired(FALSE);
  $field->save();
  // Display form: with no field data, nothing is selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 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_2[]' => [
      0 => 0,
    ],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', [
    0,
  ]);
  // Display form: check that the right options are selected.
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertTrue($this->assertSession()
    ->optionExists('card_2', 0)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 1)
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('card_2', 2)
    ->isSelected());
  // Submit form: Unselect the option.
  $edit = [
    'card_2[]' => [
      '_none' => '_none',
    ],
  ];
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->submitForm($edit, 'Save');
  $this->assertFieldValues($entity_init, 'card_2', []);
  // Test required entity reference field when selected value is deleted.
  // Create vocabulary and terms.
  $vocab = $this->createVocabulary();
  // Create some test terms.
  $term_to_delete = $this->createTerm($vocab);
  $term2 = $this->createTerm($vocab, [
    'weight' => 100,
  ]);
  $term3 = $this->createTerm($vocab, [
    'weight' => 1000,
  ]);
  // Create a required unlimited-cardinality entity reference field.
  $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, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $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 selected term. Two options remain but neither should be
  // selected.
  $term_to_delete->delete();
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertTrue($this->assertSession()
    ->fieldExists('Test term reference')
    ->hasAttribute('multiple'));
  $this->assertSession()
    ->optionNotExists('Test term reference', '_none');
  $this->assertFalse($this->assertSession()
    ->optionExists('Test term reference', $term2->id())
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('Test term reference', $term3->id())
    ->isSelected());
  // Delete term3 as well. Since there's only one option left the select will
  // not be a multiple select and _none should appear to force the user to
  // choose.
  $term3->delete();
  $this->drupalGet('entity_test/manage/' . $entity->id() . '/edit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertFalse($this->assertSession()
    ->fieldExists('Test term reference')
    ->hasAttribute('multiple'));
  $this->assertTrue($this->assertSession()
    ->optionExists('Test term reference', '_none')
    ->isSelected());
  $this->assertFalse($this->assertSession()
    ->optionExists('Test term reference', $term2->id())
    ->isSelected());
}

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