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