NodeTitleTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\comment\Tests\CommentTestTrait;
  4. use Drupal\Component\Utility\Html;
  5. /**
  6. * Tests node title.
  7. *
  8. * @group node
  9. */
  10. class NodeTitleTest extends NodeTestBase {
  11. use CommentTestTrait;
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['comment', 'views', 'block'];
  18. /**
  19. * A user with permission to bypass access content.
  20. *
  21. * @var \Drupal\user\UserInterface
  22. */
  23. protected $adminUser;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->drupalPlaceBlock('system_breadcrumb_block');
  30. $this->adminUser = $this->drupalCreateUser(['administer nodes', 'create article content', 'create page content', 'post comments']);
  31. $this->drupalLogin($this->adminUser);
  32. $this->addDefaultCommentField('node', 'page');
  33. }
  34. /**
  35. * Creates one node and tests if the node title has the correct value.
  36. */
  37. public function testNodeTitle() {
  38. // Create "Basic page" content with title.
  39. // Add the node to the frontpage so we can test if teaser links are
  40. // clickable.
  41. $settings = [
  42. 'title' => $this->randomMachineName(8),
  43. 'promote' => 1,
  44. ];
  45. $node = $this->drupalCreateNode($settings);
  46. // Test <title> tag.
  47. $this->drupalGet('node/' . $node->id());
  48. $xpath = '//title';
  49. $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
  50. // Test breadcrumb in comment preview.
  51. $this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
  52. $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
  53. $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node');
  54. // Test node title in comment preview.
  55. $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');
  56. // Test node title is clickable on teaser list (/node).
  57. $this->drupalGet('node');
  58. $this->clickLink($node->label());
  59. // Test edge case where node title is set to 0.
  60. $settings = [
  61. 'title' => 0,
  62. ];
  63. $node = $this->drupalCreateNode($settings);
  64. // Test that 0 appears as <title>.
  65. $this->drupalGet('node/' . $node->id());
  66. $this->assertTitle(0 . ' | Drupal', 'Page title is equal to 0.', 'Node');
  67. // Test that 0 appears in the template <h1>.
  68. $xpath = '//h1';
  69. $this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');
  70. // Test edge case where node title contains special characters.
  71. $edge_case_title = 'article\'s "title".';
  72. $settings = [
  73. 'title' => $edge_case_title,
  74. ];
  75. $node = $this->drupalCreateNode($settings);
  76. // Test that the title appears as <title>. The title will be escaped on the
  77. // the page.
  78. $edge_case_title_escaped = Html::escape($edge_case_title);
  79. $this->drupalGet('node/' . $node->id());
  80. $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
  81. // Test that the title appears as <title> when reloading the node page.
  82. $this->drupalGet('node/' . $node->id());
  83. $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
  84. }
  85. }