NodeTitleTest.php in Drupal 9
File
core/modules/node/tests/src/Functional/NodeTitleTest.php
View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Utility\Html;
class NodeTitleTest extends NodeTestBase {
use CommentTestTrait;
protected static $modules = [
'comment',
'views',
'block',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'administer nodes',
'create article content',
'create page content',
'post comments',
]);
$this
->drupalLogin($this->adminUser);
$this
->addDefaultCommentField('node', 'page');
}
public function testNodeTitle() {
$settings = [
'title' => $this
->randomMachineName(8),
'promote' => 1,
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$xpath = '//title';
$this
->assertEquals($this
->xpath($xpath)[0]
->getText(), $node
->label() . ' | Drupal', 'Page title is equal to node title.');
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
$this
->assertEquals($this
->xpath($xpath)[0]
->getText(), $node
->label(), 'Node breadcrumb is equal to node title.');
$this
->assertSession()
->elementTextEquals('xpath', "//article[contains(concat(' ', normalize-space(@class), ' '), ' node--type-{$node->bundle()} ')]/h2/a/span", $node
->label());
$this
->drupalGet('node');
$this
->clickLink($node
->label());
$settings = [
'title' => 0,
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->titleEquals('0 | Drupal');
$xpath = '//h1';
$this
->assertSame('0', $this
->xpath($xpath)[0]
->getText(), 'Node title is displayed as 0.');
$edge_case_title = 'article\'s "title".';
$settings = [
'title' => $edge_case_title,
];
$node = $this
->drupalCreateNode($settings);
$edge_case_title_escaped = Html::escape($edge_case_title);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains('<title>' . $edge_case_title_escaped . ' | Drupal</title>');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains('<title>' . $edge_case_title_escaped . ' | Drupal</title>');
}
}