View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Site\Settings;
use Drupal\Tests\BrowserTestBase;
class PageTitleTest extends BrowserTestBase {
protected static $modules = [
'node',
'test_page_test',
'form_test',
'block',
];
protected $defaultTheme = 'classy';
protected $contentUser;
protected $savedTitle;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalPlaceBlock('page_title_block');
$this->contentUser = $this
->drupalCreateUser([
'create page content',
'access content',
'administer themes',
'administer site configuration',
'link to any page',
]);
$this
->drupalLogin($this->contentUser);
}
public function testTitleTags() {
$title = "string with <em>HTML</em>";
$edit = [
'title[0][value]' => '!SimpleTest! ' . $title . $this
->randomMachineName(20),
'body[0][value]' => '!SimpleTest! test body' . $this
->randomMachineName(200),
];
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node, 'Node created and found in database');
$this
->assertSession()
->responseContains(Html::escape($edit['title[0][value]']));
$this
->drupalGet("node/" . $node
->id());
$this
->assertSession()
->responseContains(Html::escape($edit['title[0][value]']));
}
public function testTitleXSS() {
$title = '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ';
$title_filtered = Html::escape($title);
$slogan = '<script type="text/javascript">alert("Slogan XSS!");</script>';
$slogan_filtered = Xss::filterAdmin($slogan);
$edit = [
'site_name' => $title,
'site_slogan' => $slogan,
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalPlaceBlock('system_branding_block', [
'region' => 'header',
]);
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains($title);
$this
->assertSession()
->responseContains($title_filtered . '</title>');
$this
->assertSession()
->responseNotContains($slogan);
$this
->assertSession()
->responseContains($slogan_filtered);
}
public function testRoutingTitle() {
$this
->drupalGet('test-render-title');
$this
->assertSession()
->titleEquals('Foo | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('Foo', $result[0]
->getText());
$this
->drupalGet('form-test/object-builder');
$this
->assertSession()
->titleEquals('Test dynamic title | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('Test dynamic title', $result[0]
->getText());
$settings_key = 'locale_custom_strings_en';
$settings = Settings::getAll();
$settings[$settings_key] = [
'' => [
'Static title' => 'Static title translated',
],
];
new Settings($settings);
$this
->writeSettings([
'settings' => [
$settings_key => (object) [
'value' => $settings[$settings_key],
'required' => TRUE,
],
],
]);
$this
->drupalGet('test-page-static-title');
$this
->assertSession()
->titleEquals('Static title translated | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('Static title translated', $result[0]
->getText());
$this
->drupalGet('test-page-dynamic-title');
$this
->assertSession()
->titleEquals('Dynamic title | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEquals('Dynamic title', $result[0]
->getText());
$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>');
}
}