NodeContextualLinksTest.php 3.6 KB

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