NodeViewTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Component\Utility\Html;
  4. /**
  5. * Tests the node/{node} page.
  6. *
  7. * @group node
  8. * @see \Drupal\node\Controller\NodeController
  9. */
  10. class NodeViewTest extends NodeTestBase {
  11. /**
  12. * Tests the html head links.
  13. */
  14. public function testHtmlHeadLinks() {
  15. $node = $this->drupalCreateNode();
  16. $this->drupalGet($node->urlInfo());
  17. $result = $this->xpath('//link[@rel = "canonical"]');
  18. $this->assertEqual($result[0]->getAttribute('href'), $node->url());
  19. // Link relations are checked for access for anonymous users.
  20. $result = $this->xpath('//link[@rel = "version-history"]');
  21. $this->assertFalse($result, 'Version history not present for anonymous users without access.');
  22. $result = $this->xpath('//link[@rel = "edit-form"]');
  23. $this->assertFalse($result, 'Edit form not present for anonymous users without access.');
  24. $this->drupalLogin($this->createUser(['access content']));
  25. $this->drupalGet($node->urlInfo());
  26. $result = $this->xpath('//link[@rel = "canonical"]');
  27. $this->assertEqual($result[0]->getAttribute('href'), $node->url());
  28. // Link relations are present regardless of access for authenticated users.
  29. $result = $this->xpath('//link[@rel = "version-history"]');
  30. $this->assertEqual($result[0]->getAttribute('href'), $node->url('version-history'));
  31. $result = $this->xpath('//link[@rel = "edit-form"]');
  32. $this->assertEqual($result[0]->getAttribute('href'), $node->url('edit-form'));
  33. // Give anonymous users access to edit the node. Do this through the UI to
  34. // ensure caches are handled properly.
  35. $this->drupalLogin($this->rootUser);
  36. $edit = [
  37. 'anonymous[edit own ' . $node->bundle() . ' content]' => TRUE,
  38. ];
  39. $this->drupalPostForm('admin/people/permissions', $edit, 'Save permissions');
  40. $this->drupalLogout();
  41. // Anonymous user's should now see the edit-form link but not the
  42. // version-history link.
  43. $this->drupalGet($node->urlInfo());
  44. $result = $this->xpath('//link[@rel = "canonical"]');
  45. $this->assertEqual($result[0]->getAttribute('href'), $node->url());
  46. $result = $this->xpath('//link[@rel = "version-history"]');
  47. $this->assertFalse($result, 'Version history not present for anonymous users without access.');
  48. $result = $this->xpath('//link[@rel = "edit-form"]');
  49. $this->assertEqual($result[0]->getAttribute('href'), $node->url('edit-form'));
  50. }
  51. /**
  52. * Tests the Link header.
  53. */
  54. public function testLinkHeader() {
  55. $node = $this->drupalCreateNode();
  56. $expected = [
  57. '<' . Html::escape($node->url('canonical')) . '>; rel="canonical"',
  58. '<' . Html::escape($node->url('canonical'), ['alias' => TRUE]) . '>; rel="shortlink"',
  59. '<' . Html::escape($node->url('revision')) . '>; rel="revision"',
  60. ];
  61. $this->drupalGet($node->urlInfo());
  62. $links = $this->drupalGetHeaders()['Link'];
  63. $this->assertEqual($links, $expected);
  64. }
  65. /**
  66. * Tests that we store and retrieve multi-byte UTF-8 characters correctly.
  67. */
  68. public function testMultiByteUtf8() {
  69. $title = '🐝';
  70. $this->assertTrue(mb_strlen($title, 'utf-8') < strlen($title), 'Title has multi-byte characters.');
  71. $node = $this->drupalCreateNode(['title' => $title]);
  72. $this->drupalGet($node->urlInfo());
  73. $result = $this->xpath('//span[contains(@class, "field--name-title")]');
  74. $this->assertEqual($result[0]->getText(), $title, 'The passed title was returned.');
  75. }
  76. }