Chris@0: drupalCreateContentType(['type' => 'page']); Chris@0: $this->drupalCreateNode(['promote' => 1]); Chris@0: $this->drupalGet('node'); Chris@0: Chris@0: $user = $this->drupalCreateUser(['administer nodes', 'access contextual links']); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: $response = $this->renderContextualLinks(['node:node=1:'], 'node'); Chris@0: $this->assertResponse(200); Chris@0: $json = Json::decode($response); Chris@0: $this->setRawContent($json['node:node=1:']); Chris@0: Chris@0: // @todo Add these back when the functionality for making Views displays Chris@0: // appear in contextual links is working again. Chris@0: // $this->assertLinkByHref('node/1/contextual-links', 0, 'The contextual link to the view was found.'); Chris@0: // $this->assertLink('Test contextual link', 0, 'The contextual link to the view was found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get server-rendered contextual links for the given contextual link ids. Chris@0: * Chris@0: * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks(). Chris@0: * Chris@0: * @param array $ids Chris@0: * An array of contextual link ids. Chris@0: * @param string $current_path Chris@0: * The Drupal path for the page for which the contextual links are rendered. Chris@0: * Chris@0: * @return string Chris@0: * The response body. Chris@0: */ Chris@0: protected function renderContextualLinks($ids, $current_path) { Chris@0: // Build POST values. Chris@0: $post = []; Chris@0: for ($i = 0; $i < count($ids); $i++) { Chris@0: $post['ids[' . $i . ']'] = $ids[$i]; Chris@0: } Chris@0: Chris@0: // Serialize POST values. Chris@0: foreach ($post as $key => $value) { Chris@0: // Encode according to application/x-www-form-urlencoded Chris@0: // Both names and values needs to be urlencoded, according to Chris@0: // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 Chris@0: $post[$key] = urlencode($key) . '=' . urlencode($value); Chris@0: } Chris@0: $post = implode('&', $post); Chris@0: Chris@0: // Perform HTTP request. Chris@0: return $this->curlExec([ Chris@0: CURLOPT_URL => \Drupal::url('contextual.render', [], ['absolute' => TRUE, 'query' => ['destination' => $current_path]]), Chris@0: CURLOPT_POST => TRUE, Chris@0: CURLOPT_POSTFIELDS => $post, Chris@0: CURLOPT_HTTPHEADER => [ Chris@0: 'Accept: application/json', Chris@0: 'Content-Type: application/x-www-form-urlencoded', Chris@0: ], Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if the node page works if Contextual Links is disabled. Chris@0: * Chris@0: * All views have Contextual links enabled by default, even with the Chris@0: * Contextual links module disabled. This tests if no calls are done to the Chris@0: * Contextual links module by views when it is disabled. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2379811 Chris@0: */ Chris@0: public function testPageWithDisabledContextualModule() { Chris@0: \Drupal::service('module_installer')->uninstall(['contextual']); Chris@0: \Drupal::service('module_installer')->install(['views_ui']); Chris@0: Chris@0: // Ensure that contextual links don't get called for admin users. Chris@0: $admin_user = User::load(1); Chris@0: $admin_user->setPassword('new_password'); Chris@0: $admin_user->pass_raw = 'new_password'; Chris@0: $admin_user->save(); Chris@0: Chris@0: $this->drupalCreateContentType(['type' => 'page']); Chris@0: $this->drupalCreateNode(['promote' => 1]); Chris@0: Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalGet('node'); Chris@0: } Chris@0: Chris@0: }