You are here

protected function NodeDisplayConfigurableTest::assertNodeHtml in Drupal 10

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeDisplayConfigurableTest.php \Drupal\Tests\node\Functional\NodeDisplayConfigurableTest::assertNodeHtml()

Asserts that the node HTML is as expected.

@internal

Parameters

\Drupal\node\NodeInterface $node: The node being tested.

\Drupal\user\UserInterface $user: The logged in user.

bool $is_inline: Whether the fields are rendered inline or not.

string $metadata_region: The region of the node html content where meta data is expected.

bool $field_classes: If TRUE, check for field--name-XXX classes on created/uid fields.

bool $title_classes: If TRUE, check for field--name-XXX classes on title field.

File

core/modules/node/tests/src/Functional/NodeDisplayConfigurableTest.php, line 120

Class

NodeDisplayConfigurableTest
Tests making node base fields' displays configurable.

Namespace

Drupal\Tests\node\Functional

Code

protected function assertNodeHtml(NodeInterface $node, UserInterface $user, bool $is_inline, string $metadata_region, bool $field_classes, bool $title_classes) : void {
  $assert = $this
    ->assertSession();
  $html_element = $is_inline ? 'span' : 'div';
  $title_selector = 'h1 span' . ($title_classes ? '.field--name-title' : '');
  $assert
    ->elementTextContains('css', $title_selector, $node
    ->getTitle());

  // With field classes, the selector can be very specific.
  if ($field_classes) {
    $created_selector = 'article ' . $html_element . '.field--name-created';
    $assert
      ->elementTextContains('css', $created_selector, \Drupal::service('date.formatter')
      ->format($node
      ->getCreatedTime()));
  }
  else {

    // When field classes aren't available, use HTML elements for testing.
    $formatted_time = \Drupal::service('date.formatter')
      ->format($node
      ->getCreatedTime());
    if ($is_inline) {
      $created_selector = sprintf('//article//%s//%s[text()="%s"]', $metadata_region, $html_element, $formatted_time);
    }
    else {
      $created_selector = sprintf('//article//%s[text()="%s"]', $html_element, $formatted_time);
    }
    $assert
      ->elementExists('xpath', $created_selector);
  }
  $uid_selector = 'article ' . $html_element . ($field_classes ? '.field--name-uid' : '');
  if (!$is_inline) {
    $field_classes_selector = $field_classes ? "[contains(concat(' ', normalize-space(@class), ' '), ' field--name-uid ')]" : '';
    $assert
      ->elementExists('xpath', sprintf('//article//%s//*%s//%s[text()="Authored by"]', $html_element, $field_classes_selector, $html_element));
    $assert
      ->elementTextContains('css', $uid_selector, $user
      ->getAccountName());
    $assert
      ->elementNotExists('css', "{$uid_selector} a");
    if ($field_classes) {
      $assert
        ->elementExists('css', $created_selector);
    }
  }
  else {
    $assert
      ->elementTextContains('css', $uid_selector . ' a', $user
      ->getAccountName());
    $assert
      ->elementTextContains('css', 'article ' . $metadata_region, 'Submitted by');
  }
}