function 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.

1 call to NodeDisplayConfigurableTest::assertNodeHtml()
NodeDisplayConfigurableTest::testDisplayConfigurable in core/modules/rdf/tests/src/Functional/Node/NodeDisplayConfigurableTest.php
Sets base fields to configurable display and check settings are respected.

File

core/modules/rdf/tests/src/Functional/Node/NodeDisplayConfigurableTest.php, line 123

Class

NodeDisplayConfigurableTest
Tests making node base fields' displays configurable.

Namespace

Drupal\Tests\rdf\Functional\Node

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');
    }
}

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