function FilterFormTest::assertOptions

Asserts that a select element has the correct options.

Parameters

string $id: The HTML ID of the select element.

array $expected_options: An array of option values.

string $selected: The value of the selected option.

Return value

bool TRUE if the assertion passed; FALSE otherwise.

3 calls to FilterFormTest::assertOptions()
FilterFormTest::assertRequiredSelectAndOptions in core/modules/filter/tests/src/Functional/FilterFormTest.php
Asserts that there is a select element with the given ID that is required.
FilterFormTest::doFilterFormTestAsAdmin in core/modules/filter/tests/src/Functional/FilterFormTest.php
Tests the behavior of the 'text_format' element as an administrator.
FilterFormTest::doFilterFormTestAsNonAdmin in core/modules/filter/tests/src/Functional/FilterFormTest.php
Tests the behavior of the 'text_format' element as a normal user.

File

core/modules/filter/tests/src/Functional/FilterFormTest.php, line 200

Class

FilterFormTest
Tests form elements with associated text formats.

Namespace

Drupal\Tests\filter\Functional

Code

protected function assertOptions($id, array $expected_options, $selected) {
  $select = $this->xpath('//select[@id=:id]', [
    ':id' => $id,
  ]);
  $this->assertNotEmpty($select, new FormattableMarkup('Field @id exists.', [
    '@id' => $id,
  ]));
  $select = reset($select);
  $found_options = $select->findAll('css', 'option');
  foreach ($found_options as $found_key => $found_option) {
    $expected_key = array_search($found_option->getValue(), $expected_options);
    if ($expected_key !== FALSE) {
      unset($found_options[$found_key]);
      unset($expected_options[$expected_key]);
    }
  }
  // Make sure that all expected options were found and that there are no
  // unexpected options.
  foreach ($expected_options as $expected_option) {
    $this->fail(new FormattableMarkup('Option @option for field @id exists.', [
      '@option' => $expected_option,
      '@id' => $id,
    ]));
  }
  foreach ($found_options as $found_option) {
    $this->fail(new FormattableMarkup('Option @option for field @id does not exist.', [
      '@option' => $found_option->getValue(),
      '@id' => $id,
    ]));
  }
  $this->assertOptionSelected($id, $selected);
}

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