Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeTitleTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Component\Utility\Html;
7
8 /**
9  * Tests node title.
10  *
11  * @group node
12  */
13 class NodeTitleTest extends NodeTestBase {
14
15   use CommentTestTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['comment', 'views', 'block'];
23
24   /**
25    * A user with permission to bypass access content.
26    *
27    * @var \Drupal\user\UserInterface
28    */
29   protected $adminUser;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36     $this->drupalPlaceBlock('system_breadcrumb_block');
37
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');
41   }
42
43   /**
44    * Creates one node and tests if the node title has the correct value.
45    */
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
49     // clickable.
50     $settings = [
51       'title' => $this->randomMachineName(8),
52       'promote' => 1,
53     ];
54     $node = $this->drupalCreateNode($settings);
55
56     // Test <title> tag.
57     $this->drupalGet('node/' . $node->id());
58     $xpath = '//title';
59     $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
60
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');
65
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');
68
69     // Test node title is clickable on teaser list (/node).
70     $this->drupalGet('node');
71     $this->clickLink($node->label());
72
73     // Test edge case where node title is set to 0.
74     $settings = [
75       'title' => 0,
76     ];
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>.
82     $xpath = '//h1';
83     $this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');
84
85     // Test edge case where node title contains special characters.
86     $edge_case_title = 'article\'s "title".';
87     $settings = [
88       'title' => $edge_case_title,
89     ];
90     $node = $this->drupalCreateNode($settings);
91     // Test that the title appears as <title>. The title will be escaped on the
92     // the page.
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');
96
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');
100
101   }
102
103 }