function PageTitleTest::testRoutingTitle

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/PageTitleTest.php \Drupal\Tests\system\Functional\System\PageTitleTest::testRoutingTitle()
  2. 8.9.x core/modules/system/tests/src/Functional/System/PageTitleTest.php \Drupal\Tests\system\Functional\System\PageTitleTest::testRoutingTitle()
  3. 11.x core/modules/system/tests/src/Functional/System/PageTitleTest.php \Drupal\Tests\system\Functional\System\PageTitleTest::testRoutingTitle()

Tests the page title of render arrays.

See also

\Drupal\test_page_test\Controller\Test

File

core/modules/system/tests/src/Functional/System/PageTitleTest.php, line 118

Class

PageTitleTest
Tests HTML output escaping of page title, site name, and slogan.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testRoutingTitle() : void {
    // Test the '#title' render array attribute.
    $this->drupalGet('test-render-title');
    $this->assertSession()
        ->titleEquals('Foo | Drupal');
    $this->assertSession()
        ->elementTextEquals('xpath', '//h1[@class="page-title"]', 'Foo');
    // Test forms
    $this->drupalGet('form-test/object-builder');
    $this->assertSession()
        ->titleEquals('Test dynamic title | Drupal');
    $this->assertSession()
        ->elementTextEquals('xpath', '//h1[@class="page-title"]', 'Test dynamic title');
    // Set some custom translated strings.
    $settings_key = 'locale_custom_strings_en';
    // Update in-memory settings directly.
    $settings = Settings::getAll();
    $settings[$settings_key] = [
        '' => [
            'Static title' => 'Static title translated',
        ],
    ];
    new Settings($settings);
    // Rewrites the settings.php.
    $this->writeSettings([
        'settings' => [
            $settings_key => (object) [
                'value' => $settings[$settings_key],
                'required' => TRUE,
            ],
        ],
    ]);
    // Ensure that the title got translated.
    $this->drupalGet('test-page-static-title');
    $this->assertSession()
        ->titleEquals('Static title translated | Drupal');
    $this->assertSession()
        ->elementTextEquals('xpath', '//h1[@class="page-title"]', 'Static title translated');
    // Test the dynamic '_title_callback' route option.
    $this->drupalGet('test-page-dynamic-title');
    $this->assertSession()
        ->titleEquals('Dynamic title | Drupal');
    $this->assertSession()
        ->elementTextEquals('xpath', '//h1[@class="page-title"]', 'Dynamic title');
    // Ensure that titles are cacheable and are escaped normally if the
    // controller does not escape them.
    $this->drupalGet('test-page-cached-controller');
    $this->assertSession()
        ->titleEquals('Cached title | Drupal');
    $this->assertSession()
        ->responseContains(Html::escape('<span>Cached title</span>') . '</h1>');
    $this->drupalGet('test-page-cached-controller');
    $this->assertSession()
        ->titleEquals('Cached title | Drupal');
    $this->assertSession()
        ->responseContains(Html::escape('<span>Cached title</span>') . '</h1>');
}

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