NodeTitleTest.php 3.55 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\node\Functional;
4

5
use Drupal\comment\Tests\CommentTestTrait;
6
use Drupal\Component\Utility\Html;
7

8
/**
9 10 11
 * Tests node title.
 *
 * @group node
12 13
 */
class NodeTitleTest extends NodeTestBase {
14

15 16
  use CommentTestTrait;

17 18 19 20 21
  /**
   * Modules to enable.
   *
   * @var array
   */
22
  protected static $modules = ['comment', 'views', 'block'];
23

24 25 26 27 28
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

29 30 31 32 33 34
  /**
   * A user with permission to bypass access content.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;
35

36 37 38
  /**
   * {@inheritdoc}
   */
39
  protected function setUp(): void {
40
    parent::setUp();
41
    $this->drupalPlaceBlock('system_breadcrumb_block');
42
    $this->drupalPlaceBlock('page_title_block');
43

44
    $this->adminUser = $this->drupalCreateUser(['administer nodes', 'create article content', 'create page content', 'post comments']);
45
    $this->drupalLogin($this->adminUser);
46
    $this->addDefaultCommentField('node', 'page');
47 48 49
  }

  /**
50
   * Creates one node and tests if the node title has the correct value.
51
   */
52
  public function testNodeTitle() {
53
    // Create "Basic page" content with title.
54 55
    // Add the node to the frontpage so we can test if teaser links are
    // clickable.
56
    $settings = [
57
      'title' => $this->randomMachineName(8),
58
      'promote' => 1,
59
    ];
60 61 62
    $node = $this->drupalCreateNode($settings);

    // Test <title> tag.
63
    $this->drupalGet('node/' . $node->id());
64
    $xpath = '//title';
65
    $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
66 67

    // Test breadcrumb in comment preview.
68
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
69
    $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
70
    $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node');
71 72

    // Test node title in comment preview.
73
    $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');
74 75 76

    // Test node title is clickable on teaser list (/node).
    $this->drupalGet('node');
77
    $this->clickLink($node->label());
78 79

    // Test edge case where node title is set to 0.
80
    $settings = [
81
      'title' => 0,
82
    ];
83 84 85
    $node = $this->drupalCreateNode($settings);
    // Test that 0 appears as <title>.
    $this->drupalGet('node/' . $node->id());
86
    $this->assertTitle('0 | Drupal');
87 88
    // Test that 0 appears in the template <h1>.
    $xpath = '//h1';
89
    $this->assertSame('0', $this->xpath($xpath)[0]->getText(), 'Node title is displayed as 0.');
90 91 92

    // Test edge case where node title contains special characters.
    $edge_case_title = 'article\'s "title".';
93
    $settings = [
94
      'title' => $edge_case_title,
95
    ];
96 97 98 99 100
    $node = $this->drupalCreateNode($settings);
    // Test that the title appears as <title>. The title will be escaped on the
    // the page.
    $edge_case_title_escaped = Html::escape($edge_case_title);
    $this->drupalGet('node/' . $node->id());
101
    $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
102 103 104

    // Test that the title appears as <title> when reloading the node page.
    $this->drupalGet('node/' . $node->id());
105
    $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
106

107
  }
108

109
}