You are here

protected function AssertContentTrait::assertFieldsByValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertFieldsByValue()

Asserts that a field exists in the current page with a given Xpath result.

Parameters

\SimpleXmlElement[] $fields: Xml elements.

string $value: (optional) Value of the field to assert. You may pass in NULL (default) to skip checking the actual value, while still checking that the field exists.

string $message: (optional) A message to display with the assertion. Do not translate messages: use \Drupal\Component\Render\FormattableMarkup to embed variables in the message text, not t(). If left blank, a default message will be displayed.

string $group: (optional) The group this message is in, which is displayed in a column in test output. Use 'Debug' to indicate this is debugging output. Do not translate this string. Defaults to 'Other'; most tests do not override this default.

Return value

bool TRUE on pass.

1 call to AssertContentTrait::assertFieldsByValue()
AssertContentTrait::assertFieldByXPath in core/tests/Drupal/KernelTests/AssertContentTrait.php
Asserts that a field exists in the current page by the given XPath.

File

core/tests/Drupal/KernelTests/AssertContentTrait.php, line 910

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertFieldsByValue($fields, $value = NULL, $message = '', $group = 'Other') {

  // If value specified then check array for match.
  $found = TRUE;
  if (isset($value)) {
    $found = FALSE;
    if ($fields) {
      foreach ($fields as $field) {
        if (isset($field['value']) && $field['value'] == $value) {

          // Input element with correct value.
          $found = TRUE;
        }
        elseif (isset($field->option) || isset($field->optgroup)) {

          // Select element found.
          $selected = $this
            ->getSelectedItem($field);
          if ($selected === FALSE) {

            // No item selected so use first item.
            $items = $this
              ->getAllOptions($field);
            if (!empty($items) && $items[0]['value'] == $value) {
              $found = TRUE;
            }
          }
          elseif ($selected == $value) {
            $found = TRUE;
          }
        }
        elseif ((string) $field == $value) {

          // Text area with correct text.
          $found = TRUE;
        }
      }
    }
  }
  $this
    ->assertNotEmpty($fields);
  $this
    ->assertTrue($found, $message);
  return TRUE;
}