View source
<?php
namespace Drupal\Tests\html_title\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\BrowserTestBase;
class HtmlTitleTest extends BrowserTestBase {
protected static $modules = [
'html_title_test',
'node',
'block',
'search',
'content_translation',
'path',
];
protected $defaultTheme = 'stark';
protected $state;
protected $adminUser;
protected $node1;
protected $node2;
protected $node3;
protected $node4;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
$this
->config('html_title.settings')
->set('allow_html_tags', '<br> <sub> <sup>')
->save();
$this->adminUser = $this
->createUser([], NULL, TRUE);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'test',
]);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this
->config('language.negotiation')
->set('url.source', 'path_prefix')
->set('url.prefixes', [
'en' => 'en',
'fr' => 'fr',
])
->save();
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'test');
$config
->setDefaultLangcode('en')
->setLanguageAlterable(TRUE)
->save();
$this->node1 = $this
->drupalCreateNode([
'title' => 'Test <sup>sup</sup>-tag',
'type' => 'test',
'path' => '/parent',
]);
$this->node2 = $this
->drupalCreateNode([
'title' => 'Test <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br>br-tag',
'type' => 'test',
'path' => '/parent/child',
]);
$this->node3 = $this
->drupalCreateNode([
'title' => 'Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and <p>p</p>-tag',
'type' => 'test',
]);
$translation = $this->node3
->addTranslation('fr', [
'title' => 'Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and <p>p</p>-tag FR',
]);
$translation
->save();
$this->node4 = $this
->drupalCreateNode([
'title' => 'Test <p>p</p>-tag',
'type' => 'test',
]);
$this->container
->get('cron')
->run();
$this->state = $this->container
->get('state');
}
public function testPageTitleBlock() {
$assert_session = $this
->assertSession();
$this
->drupalGet($this->node1
->toUrl());
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag</h1>');
$this
->drupalGet($this->node2
->toUrl());
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br>br-tag</h1>');
$this
->drupalGet($this->node3
->toUrl());
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag</h1>');
$this
->drupalGet('fr/node/' . $this->node3
->id());
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag FR</h1>');
$this
->drupalGet($this->node4
->toUrl());
$assert_session
->responseContains('<h1>Test p-tag</h1>');
}
public function testEditNodePageTitle() {
$assert_session = $this
->assertSession();
$this
->drupalGet(Url::fromRoute('entity.node.edit_form', [
'node' => $this->node1
->id(),
]));
$assert_session
->responseContains('<h1><em>Edit test</em> Test <sup>sup</sup>-tag</h1>');
$this
->drupalGet(Url::fromRoute('entity.node.edit_form', [
'node' => $this->node2
->id(),
]));
$assert_session
->responseContains('<h1><em>Edit test</em> Test <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br>br-tag</h1>');
$this
->drupalGet(Url::fromRoute('entity.node.edit_form', [
'node' => $this->node3
->id(),
]));
$assert_session
->responseContains('<h1><em>Edit test</em> Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag</h1>');
$this
->drupalGet('fr/node/' . $this->node3
->id() . '/edit');
$assert_session
->responseContains('<h1><em>Edit test</em> Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag FR</h1>');
$this
->drupalGet(Url::fromRoute('entity.node.edit_form', [
'node' => $this->node4
->id(),
]));
$assert_session
->responseContains('<h1><em>Edit test</em> Test p-tag</h1>');
}
public function testBreadcrumbBlock() {
$assert_session = $this
->assertSession();
$this
->drupalGet($this->node1
->toUrl());
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:last-child');
$this
->assertEquals('Test <sup>sup</sup>-tag', trim($element
->getHtml()));
$this
->drupalGet($this->node2
->toUrl());
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:nth-child(2) a');
$this
->assertEquals('Test <sup>sup</sup>-tag', trim($element
->getHtml()));
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:last-child');
$this
->assertEquals('Test <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br>br-tag', trim($element
->getHtml()));
$this
->drupalGet($this->node3
->toUrl());
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:last-child');
$this
->assertEquals('Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag', trim($element
->getHtml()));
$this
->drupalGet('fr/node/' . $this->node3
->id());
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:last-child');
$this
->assertEquals('Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag FR', trim($element
->getHtml()));
$this
->drupalGet($this->node4
->toUrl());
$element = $assert_session
->elementExists('css', 'nav[role="navigation"] ol li:last-child');
$this
->assertEquals('Test p-tag', trim($element
->getHtml()));
}
public function testSearchPage() {
$assert_session = $this
->assertSession();
$this
->drupalGet('search/node');
$this
->submitForm([
'keys' => 'test br-tag p-tag',
'language[en]' => 'en',
], 'Search');
$element = $assert_session
->elementExists('css', 'ol li:first-child h3 a');
$this
->assertEquals('Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag', trim($element
->getHtml()));
$this
->drupalGet('fr/search/node');
$this
->submitForm([
'keys' => 'test br-tag p-tag',
'language[fr]' => 'fr',
], 'Search');
$element = $assert_session
->elementExists('css', 'ol li:first-child h3 a');
$this
->assertEquals('Test <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br>br-tag and p-tag FR', trim($element
->getHtml()));
}
public function testRevisionsPage() {
$assert_session = $this
->assertSession();
$node_1_revision = clone $this->node1;
$node_1_revision
->set('body', [
'value' => $this->randomGenerator
->paragraphs(12),
]);
$node_1_revision
->setNewRevision();
$node_1_revision
->save();
$this
->drupalGet(Url::fromRoute('node.revision_revert_confirm', [
'node' => $this->node1
->id(),
'node_revision' => $this->node1
->getRevisionId(),
]));
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag</h1>');
$this
->drupalGet(Url::fromRoute('entity.node.revision', [
'node' => $this->node1
->id(),
'node_revision' => $this->node1
->getRevisionId(),
]));
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag</h1>');
$this
->drupalGet(Url::fromRoute('node.revision_delete_confirm', [
'node' => $this->node1
->id(),
'node_revision' => $this->node1
->getRevisionId(),
]));
$assert_session
->responseContains('<h1>Test <sup>sup</sup>-tag</h1>');
}
public function testNodeConfirmationMessage() {
$this
->drupalGet('node/add');
$this
->submitForm([
'title[0][value]' => 'Test <sup>sup</sup>-tag',
], 'Save');
$this
->assertSession()
->elementContains('css', 'div[data-drupal-messages]', 'Test <sup>sup</sup>-tag');
$this
->drupalGet('node/add');
$this
->submitForm([
'title[0][value]' => 'Test node',
], 'Save');
$this
->assertSession()
->elementTextContains('css', 'div[data-drupal-messages]', 'Test node');
$this
->drupalGet('node/add');
$this
->submitForm([
'title[0][value]' => 'Test <p>p</p>-tag',
], 'Save');
$this
->assertSession()
->elementTextContains('css', 'div[data-drupal-messages]', 'Test p-tag');
$this->state
->set('html_title_test.set_node_confirmation_messages', TRUE);
$this
->drupalGet('node/add');
$this
->submitForm([
'title[0][value]' => 'Test <sub>sub</sub>-tag',
], 'Save');
$this
->assertSession()
->elementContains('css', 'div[data-drupal-messages]', 'Test <sub>sub</sub>-tag');
$this
->assertSession()
->elementContains('css', 'div[data-drupal-messages]', 'Test status message');
$this
->assertSession()
->elementTextContains('css', 'div[data-drupal-messages]', 'Test status message with <p>HTML</p>');
$this
->assertSession()
->elementContains('css', 'div[data-drupal-messages]', 'Test error message');
}
}