3 namespace Drupal\Tests\node\Functional;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Component\Utility\Html;
13 class NodeTitleTest extends NodeTestBase {
22 public static $modules = ['comment', 'views', 'block'];
25 * A user with permission to bypass access content.
27 * @var \Drupal\user\UserInterface
34 protected function setUp() {
36 $this->drupalPlaceBlock('system_breadcrumb_block');
38 $this->adminUser = $this->drupalCreateUser(['administer nodes', 'create article content', 'create page content', 'post comments']);
39 $this->drupalLogin($this->adminUser);
40 $this->addDefaultCommentField('node', 'page');
44 * Creates one node and tests if the node title has the correct value.
46 public function testNodeTitle() {
47 // Create "Basic page" content with title.
48 // Add the node to the frontpage so we can test if teaser links are
51 'title' => $this->randomMachineName(8),
54 $node = $this->drupalCreateNode($settings);
57 $this->drupalGet('node/' . $node->id());
59 $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
61 // Test breadcrumb in comment preview.
62 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
63 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
64 $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node');
66 // Test node title in comment preview.
67 $this->assertEqual($this->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]/h2/a/span', [':node-class' => ' node--type-' . $node->bundle() . ' '])[0]->getText(), $node->label(), 'Node preview title is equal to node title.', 'Node');
69 // Test node title is clickable on teaser list (/node).
70 $this->drupalGet('node');
71 $this->clickLink($node->label());
73 // Test edge case where node title is set to 0.
77 $node = $this->drupalCreateNode($settings);
78 // Test that 0 appears as <title>.
79 $this->drupalGet('node/' . $node->id());
80 $this->assertTitle(0 . ' | Drupal', 'Page title is equal to 0.', 'Node');
81 // Test that 0 appears in the template <h1>.
83 $this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');
85 // Test edge case where node title contains special characters.
86 $edge_case_title = 'article\'s "title".';
88 'title' => $edge_case_title,
90 $node = $this->drupalCreateNode($settings);
91 // Test that the title appears as <title>. The title will be escaped on the
93 $edge_case_title_escaped = Html::escape($edge_case_title);
94 $this->drupalGet('node/' . $node->id());
95 $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
97 // Test that the title appears as <title> when reloading the node page.
98 $this->drupalGet('node/' . $node->id());
99 $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');