function AssertContentTrait::assertTitle

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertTitle()
  2. 8.9.x core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertTitle()
  3. 10 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertTitle()

Pass if the page title is the given string.

Parameters

string $title: The string the title should be.

string $message: (optional) A message to display with the assertion. Do not translate messages with t(). Use double quotes and embed variables directly in message text, or use sprintf() if necessary. Avoid the use of \Drupal\Component\Render\FormattableMarkup unless you cast the object to a string. If left blank, a default message will be displayed.

2 calls to AssertContentTrait::assertTitle()
RouteProviderTest::testAdminHtmlRoutes in core/tests/Drupal/KernelTests/Core/Entity/RouteProviderTest.php
@covers \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider::getEditFormRoute @covers \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider::getDeleteFormRoute
RouteProviderTest::testHtmlRoutes in core/tests/Drupal/KernelTests/Core/Entity/RouteProviderTest.php
@covers \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider::getRoutes

File

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

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertTitle($title, $message = '') {
    // Don't use xpath as it messes with HTML escaping.
    preg_match('@<title>(.*)</title>@', $this->getRawContent(), $matches);
    if (isset($matches[1])) {
        $actual = $matches[1];
        if (!$message) {
            $message = new FormattableMarkup('Page title @actual is equal to @expected.', [
                '@actual' => var_export($actual, TRUE),
                '@expected' => var_export($title, TRUE),
            ]);
        }
        $this->assertEquals($title, $actual, (string) $message);
    }
    else {
        $this->fail('No title element found on the page.');
    }
}

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