annotate core/modules/node/src/Tests/Views/NodeContextualLinksTest.php @ 16:c2387f117808

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