You are here

protected function NameWidgetTest::assertComponentSettings in Name Field 8

Asserts that the components exists and appear in the right order.

Parameters

string $key: The name component key, for example 'given'.

array $settings: The field settings, as form post array.

1 call to NameWidgetTest::assertComponentSettings()
NameWidgetTest::testFieldEntry in tests/src/Functional/NameWidgetTest.php
The most basic test.

File

tests/src/Functional/NameWidgetTest.php, line 232

Class

NameWidgetTest
Various tests on creating a name widget on a node.

Namespace

Drupal\Tests\name\Functional

Code

protected function assertComponentSettings($key, array $settings) {
  $xpath = '//div[contains(@class,:value)]';
  $elements = $this
    ->xpath($this
    ->buildXPathQuery($xpath, [
    ':value' => "name-{$key}-wrapper",
  ]));
  $this
    ->assertNotEmpty($elements, "Component {$key} field found.");
  $object = reset($elements);
  $type = $settings["settings[field_type][{$key}]"] == 'select' ? 'select' : 'input';
  $show_required = $settings['settings[show_component_required_marker]'];
  $is_required = $settings["settings[minimum_components][{$key}]"];
  $content = str_replace([
    "\n",
    "\r",
  ], " ", $object
    ->getHtml());
  switch ($settings["settings[title_display][{$key}]"]) {
    case 'title':
      $result = (bool) preg_match('/<label .*<' . $type . ' /i', $content);
      $this
        ->assertTrue($result, "Testing label is before field of type {$type} for {$key} component.");
      if ($result) {
        $required_marker_preg = '@<label .*?class=".*?js-form-required.*form-required.*?".*>@';
        if ($show_required && $is_required) {
          $this
            ->assertTrue((bool) preg_match($required_marker_preg, $content), "Required class is added for {$key} component in label");
        }
        else {
          $this
            ->assertFalse((bool) preg_match($required_marker_preg, $content), "Required class is not added for {$key} component in label");
        }
      }
      break;
    case 'description':
      $result = (bool) preg_match('/<' . $type . ' .*<label /i', $content);
      $this
        ->assertTrue($result, "Testing label is after field of type {$type} for {$key} component.");
      if ($result) {
        $required_marker_preg = '@<label .*?class=".*?js-form-required.*form-required.*?">@';
        if ($show_required && $is_required) {
          $this
            ->assertTrue((bool) preg_match($required_marker_preg, $content), "Required class is added for {$key} component in label");
        }
        else {
          $this
            ->assertFalse((bool) preg_match($required_marker_preg, $content), "Required class is not added for {$key} component in label");
        }
      }
      break;
    case 'placeholder':
      $result = (bool) preg_match('@<' . $type . ' [^>]*?placeholder=".*?' . $settings["settings[labels][{$key}]"] . '.*?"@', $content);
      $this
        ->assertTrue($result, "Testing label is a placeholder on the field of type {$type} for {$key} component.");
      if ($result) {
        $required_marker_preg = '@<' . $type . ' [^>]*?placeholder=".*?Required.*?"@';
        if ($show_required && $is_required) {
          $this
            ->assertTrue((bool) preg_match($required_marker_preg, $content), "Required text is added for {$key} component in placeholder attribute");
        }
        else {
          $this
            ->assertFalse((bool) preg_match($required_marker_preg, $content), "Required text is added for {$key} component in placeholder attribute");
        }
      }
      break;
    case 'attribute':
      $result = (bool) preg_match('@<' . $type . ' [^>]*?title=".*?' . $settings["settings[labels][{$key}]"] . '.*?"@', $content);
      $this
        ->assertTrue($result, "Testing label is a title attribute on the field of type {$type} for {$key} component.");
      if ($result) {
        $required_marker_preg = '@<' . $type . ' [^>]*?title=".*?Required.*?"@';
        if ($show_required && $is_required) {
          $this
            ->assertTrue((bool) preg_match($required_marker_preg, $content), "Required text is added for {$key} component in {$type} title attribute");
        }
        else {
          $this
            ->assertFalse((bool) preg_match($required_marker_preg, $content), "Required text is added for {$key} component in {$type} title attribute");
        }
      }
      break;
    case 'none':
      $result = (bool) preg_match('@<label [^>]*?class=".*?visually-hidden.*?"@', $content);
      $this
        ->assertTrue($result, "Testing label is present but hidden on the field of type {$type} for {$key} component.");
      break;
  }
  if (isset($settings["settings[max_length][{$key}]"]) && $type != 'select') {
    $result = (bool) preg_match('@<' . $type . ' [^>]*?maxlength="' . $settings["settings[max_length][{$key}]"] . '"@', $content);
    $this
      ->assertTrue($result, "Testing max_length is set on field of type {$type} for {$key} component.");
  }
  if (isset($settings["settings[size][{$key}]"]) && $type != 'select') {
    $result = (bool) preg_match('@<' . $type . ' [^>]*?size="' . $settings["settings[size][{$key}]"] . '"@', $content);
    $this
      ->assertTrue($result, "Testing size is set on field of type {$type} for {$key} component.");
  }
}