3 namespace Drupal\node\Tests\Views;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\user\Entity\User;
9 * Tests views contextual links on nodes.
13 class NodeContextualLinksTest extends NodeTestBase {
20 public static $modules = ['contextual'];
23 * Views used by this test.
27 public static $testViews = ['test_contextual_links'];
30 * Tests contextual links.
32 public function testNodeContextualLinks() {
33 $this->drupalCreateContentType(['type' => 'page']);
34 $this->drupalCreateNode(['promote' => 1]);
35 $this->drupalGet('node');
37 $user = $this->drupalCreateUser(['administer nodes', 'access contextual links']);
38 $this->drupalLogin($user);
40 $response = $this->renderContextualLinks(['node:node=1:'], 'node');
41 $this->assertResponse(200);
42 $json = Json::decode($response);
43 $this->setRawContent($json['node:node=1:']);
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.');
52 * Get server-rendered contextual links for the given contextual link ids.
54 * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
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.
64 protected function renderContextualLinks($ids, $current_path) {
67 for ($i = 0; $i < count($ids); $i++) {
68 $post['ids[' . $i . ']'] = $ids[$i];
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);
78 $post = implode('&', $post);
80 // Perform HTTP request.
81 return $this->curlExec([
82 CURLOPT_URL => \Drupal::url('contextual.render', [], ['absolute' => TRUE, 'query' => ['destination' => $current_path]]),
84 CURLOPT_POSTFIELDS => $post,
85 CURLOPT_HTTPHEADER => [
86 'Accept: application/json',
87 'Content-Type: application/x-www-form-urlencoded',
93 * Tests if the node page works if Contextual Links is disabled.
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.
99 * @see https://www.drupal.org/node/2379811
101 public function testPageWithDisabledContextualModule() {
102 \Drupal::service('module_installer')->uninstall(['contextual']);
103 \Drupal::service('module_installer')->install(['views_ui']);
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';
111 $this->drupalCreateContentType(['type' => 'page']);
112 $this->drupalCreateNode(['promote' => 1]);
114 $this->drupalLogin($admin_user);
115 $this->drupalGet('node');