dc23d0ce55f297cda9b74b77908c0e3034687d1b
[yaffs-website] / web / core / modules / node / src / Tests / Views / NodeContextualLinksTest.php
1 <?php
2
3 namespace Drupal\node\Tests\Views;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests views contextual links on nodes.
10  *
11  * @group node
12  */
13 class NodeContextualLinksTest extends NodeTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['contextual'];
21
22   /**
23    * Views used by this test.
24    *
25    * @var array
26    */
27   public static $testViews = ['test_contextual_links'];
28
29   /**
30    * Tests contextual links.
31    */
32   public function testNodeContextualLinks() {
33     $this->drupalCreateContentType(['type' => 'page']);
34     $this->drupalCreateNode(['promote' => 1]);
35     $this->drupalGet('node');
36
37     $user = $this->drupalCreateUser(['administer nodes', 'access contextual links']);
38     $this->drupalLogin($user);
39
40     $response = $this->renderContextualLinks(['node:node=1:'], 'node');
41     $this->assertResponse(200);
42     $json = Json::decode($response);
43     $this->setRawContent($json['node:node=1:']);
44
45     // @todo Add these back when the functionality for making Views displays
46     //   appear in contextual links is working again.
47     // $this->assertLinkByHref('node/1/contextual-links', 0, 'The contextual link to the view was found.');
48     // $this->assertLink('Test contextual link', 0, 'The contextual link to the view was found.');
49   }
50
51   /**
52    * Get server-rendered contextual links for the given contextual link ids.
53    *
54    * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
55    *
56    * @param array $ids
57    *   An array of contextual link ids.
58    * @param string $current_path
59    *   The Drupal path for the page for which the contextual links are rendered.
60    *
61    * @return string
62    *   The response body.
63    */
64   protected function renderContextualLinks($ids, $current_path) {
65     // Build POST values.
66     $post = [];
67     for ($i = 0; $i < count($ids); $i++) {
68       $post['ids[' . $i . ']'] = $ids[$i];
69     }
70
71     // Serialize POST values.
72     foreach ($post as $key => $value) {
73       // Encode according to application/x-www-form-urlencoded
74       // Both names and values needs to be urlencoded, according to
75       // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
76       $post[$key] = urlencode($key) . '=' . urlencode($value);
77     }
78     $post = implode('&', $post);
79
80     // Perform HTTP request.
81     return $this->curlExec([
82       CURLOPT_URL => \Drupal::url('contextual.render', [], ['absolute' => TRUE, 'query' => ['destination' => $current_path]]),
83       CURLOPT_POST => TRUE,
84       CURLOPT_POSTFIELDS => $post,
85       CURLOPT_HTTPHEADER => [
86         'Accept: application/json',
87         'Content-Type: application/x-www-form-urlencoded',
88       ],
89     ]);
90   }
91
92   /**
93    * Tests if the node page works if Contextual Links is disabled.
94    *
95    * All views have Contextual links enabled by default, even with the
96    * Contextual links module disabled. This tests if no calls are done to the
97    * Contextual links module by views when it is disabled.
98    *
99    * @see https://www.drupal.org/node/2379811
100    */
101   public function testPageWithDisabledContextualModule() {
102     \Drupal::service('module_installer')->uninstall(['contextual']);
103     \Drupal::service('module_installer')->install(['views_ui']);
104
105     // Ensure that contextual links don't get called for admin users.
106     $admin_user = User::load(1);
107     $admin_user->setPassword('new_password');
108     $admin_user->pass_raw = 'new_password';
109     $admin_user->save();
110
111     $this->drupalCreateContentType(['type' => 'page']);
112     $this->drupalCreateNode(['promote' => 1]);
113
114     $this->drupalLogin($admin_user);
115     $this->drupalGet('node');
116   }
117
118 }