ContextualLinksTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\Tests\node\FunctionalJavascript;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. use Drupal\node\Entity\Node;
  5. use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
  6. /**
  7. * Create a node with revisions and test contextual links.
  8. *
  9. * @group node
  10. */
  11. class ContextualLinksTest extends WebDriverTestBase {
  12. use ContextualLinkClickTrait;
  13. /**
  14. * An array of node revisions.
  15. *
  16. * @var \Drupal\node\NodeInterface[]
  17. */
  18. protected $nodes;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected static $modules = ['node', 'contextual'];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. $this->drupalCreateContentType([
  29. 'type' => 'page',
  30. 'name' => 'Basic page',
  31. 'display_submitted' => FALSE,
  32. ]);
  33. // Create initial node.
  34. $node = $this->drupalCreateNode();
  35. $nodes = [];
  36. // Get original node.
  37. $nodes[] = clone $node;
  38. // Create two revisions.
  39. $revision_count = 2;
  40. for ($i = 0; $i < $revision_count; $i++) {
  41. // Create revision with a random title and body and update variables.
  42. $node->title = $this->randomMachineName();
  43. $node->body = [
  44. 'value' => $this->randomMachineName(32),
  45. 'format' => filter_default_format(),
  46. ];
  47. $node->setNewRevision();
  48. $node->save();
  49. // Make sure we get revision information.
  50. $node = Node::load($node->id());
  51. $nodes[] = clone $node;
  52. }
  53. $this->nodes = $nodes;
  54. $this->drupalLogin($this->createUser(
  55. [
  56. 'view page revisions',
  57. 'revert page revisions',
  58. 'delete page revisions',
  59. 'edit any page content',
  60. 'delete any page content',
  61. 'access contextual links',
  62. 'administer content types',
  63. ]
  64. ));
  65. }
  66. /**
  67. * Tests the contextual links on revisions.
  68. */
  69. public function testRevisionContextualLinks() {
  70. // Confirm that the "Edit" and "Delete" contextual links appear for the
  71. // default revision.
  72. $this->drupalGet('node/' . $this->nodes[0]->id());
  73. $page = $this->getSession()->getPage();
  74. $page->waitFor(10, function () use ($page) {
  75. return $page->find('css', "main .contextual");
  76. });
  77. $this->toggleContextualTriggerVisibility('main');
  78. $page->find('css', 'main .contextual button')->press();
  79. $links = $page->findAll('css', "main .contextual-links li a");
  80. $this->assertEquals('Edit', $links[0]->getText());
  81. $this->assertEquals('Delete', $links[1]->getText());
  82. // Confirm that "Edit" and "Delete" contextual links don't appear for
  83. // non-default revision.
  84. $this->drupalGet("node/" . $this->nodes[0]->id() . "/revisions/" . $this->nodes[1]->getRevisionId() . "/view");
  85. $this->assertSession()->pageTextContains($this->nodes[1]->getTitle());
  86. $page->waitFor(10, function () use ($page) {
  87. return $page->find('css', "main .contextual");
  88. });
  89. $this->toggleContextualTriggerVisibility('main');
  90. $contextual_button = $page->find('css', 'main .contextual button');
  91. $this->assertEmpty(0, $contextual_button);
  92. }
  93. }