View source
<?php
namespace Drupal\system\Tests\System;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\simpletest\WebTestBase;
class PageTitleTest extends WebTestBase {
public static $modules = [
'node',
'test_page_test',
'form_test',
'block',
];
protected $contentUser;
protected $savedTitle;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$this
->drupalPlaceBlock('page_title_block');
$this->contentUser = $this
->drupalCreateUser(array(
'create page content',
'access content',
'administer themes',
'administer site configuration',
'link to any page',
));
$this
->drupalLogin($this->contentUser);
}
function testTitleTags() {
$title = "string with <em>HTML</em>";
$edit = array(
'title[0][value]' => '!SimpleTest! ' . $title . $this
->randomMachineName(20),
'body[0][value]' => '!SimpleTest! test body' . $this
->randomMachineName(200),
);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node, 'Node created and found in database');
$this
->assertText(Html::escape($edit['title[0][value]']), 'Check to make sure tags in the node title are converted.');
$this
->drupalGet("node/" . $node
->id());
$this
->assertText(Html::escape($edit['title[0][value]']), 'Check to make sure tags in the node title are converted.');
}
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 = array(
'site_name' => $title,
'site_slogan' => $slogan,
);
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
$this
->drupalPlaceBlock('system_branding_block', [
'region' => 'header',
]);
$this
->drupalGet('');
$this
->assertNoRaw($title, 'Check for the lack of the unfiltered version of the title.');
$this
->assertRaw($title_filtered . '</title>', 'Check for the filtered version of the title in a <title> tag.');
$this
->assertNoRaw($slogan, 'Check for the unfiltered version of the slogan.');
$this
->assertRaw($slogan_filtered, 'Check for the filtered version of the slogan.');
}
public function testRoutingTitle() {
$this
->drupalGet('test-render-title');
$this
->assertTitle('Foo | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEqual('Foo', (string) $result[0]);
$this
->drupalGet('form-test/object-builder');
$this
->assertTitle('Test dynamic title | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEqual('Test dynamic title', (string) $result[0]);
$this
->addCustomTranslations('en', array(
'' => array(
'Static title' => 'Static title translated',
),
));
$this
->writeCustomTranslations();
$this
->drupalGet('test-page-static-title');
$this
->assertTitle('Static title translated | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEqual('Static title translated', (string) $result[0]);
$this
->drupalGet('test-page-dynamic-title');
$this
->assertTitle('Dynamic title | Drupal');
$result = $this
->xpath('//h1[@class="page-title"]');
$this
->assertEqual('Dynamic title', (string) $result[0]);
$this
->drupalGet('test-page-cached-controller');
$this
->assertTitle('Cached title | Drupal');
$this
->assertRaw(Html::escape('<span>Cached title</span>') . '</h1>');
$this
->drupalGet('test-page-cached-controller');
$this
->assertTitle('Cached title | Drupal');
$this
->assertRaw(Html::escape('<span>Cached title</span>') . '</h1>');
}
}