You are here

class ContextualLinksTest in Drupal 10

Same name in this branch
  1. 10 core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php \Drupal\Tests\node\FunctionalJavascript\ContextualLinksTest
  2. 10 core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\ContextualLinksTest
  3. 10 core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php \Drupal\Tests\contextual\FunctionalJavascript\ContextualLinksTest
Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php \Drupal\Tests\node\FunctionalJavascript\ContextualLinksTest
  2. 9 core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php \Drupal\Tests\node\FunctionalJavascript\ContextualLinksTest

Create a node with revisions and test contextual links.

@group node

Hierarchy

Expanded class hierarchy of ContextualLinksTest

File

core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php, line 14

Namespace

Drupal\Tests\node\FunctionalJavascript
View source
class ContextualLinksTest extends WebDriverTestBase {
  use ContextualLinkClickTrait;

  /**
   * An array of node revisions.
   *
   * @var \Drupal\node\NodeInterface[]
   */
  protected $nodes;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'contextual',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this
      ->drupalCreateContentType([
      'type' => 'page',
      'name' => 'Basic page',
      'display_submitted' => FALSE,
    ]);

    // Create initial node.
    $node = $this
      ->drupalCreateNode();
    $nodes = [];

    // Get original node.
    $nodes[] = clone $node;

    // Create two revisions.
    $revision_count = 2;
    for ($i = 0; $i < $revision_count; $i++) {

      // Create revision with a random title and body and update variables.
      $node->title = $this
        ->randomMachineName();
      $node->body = [
        'value' => $this
          ->randomMachineName(32),
        'format' => filter_default_format(),
      ];
      $node
        ->setNewRevision();
      $node
        ->save();

      // Make sure we get revision information.
      $node = Node::load($node
        ->id());
      $nodes[] = clone $node;
    }
    $this->nodes = $nodes;
    $this
      ->drupalLogin($this
      ->createUser([
      'view page revisions',
      'revert page revisions',
      'delete page revisions',
      'edit any page content',
      'delete any page content',
      'access contextual links',
      'administer content types',
    ]));
  }

  /**
   * Tests the contextual links on revisions.
   */
  public function testRevisionContextualLinks() {

    // Confirm that the "Edit" and "Delete" contextual links appear for the
    // default revision.
    $this
      ->drupalGet('node/' . $this->nodes[0]
      ->id());
    $page = $this
      ->getSession()
      ->getPage();
    $page
      ->waitFor(10, function () use ($page) {
      return $page
        ->find('css', "main .contextual");
    });
    $this
      ->toggleContextualTriggerVisibility('main');
    $page
      ->find('css', 'main .contextual button')
      ->press();
    $links = $page
      ->findAll('css', "main .contextual-links li a");
    $this
      ->assertEquals('Edit', $links[0]
      ->getText());
    $this
      ->assertEquals('Delete', $links[1]
      ->getText());

    // Confirm that "Edit" and "Delete" contextual links don't appear for
    // non-default revision.
    $this
      ->drupalGet("node/" . $this->nodes[0]
      ->id() . "/revisions/" . $this->nodes[1]
      ->getRevisionId() . "/view");
    $this
      ->assertSession()
      ->pageTextContains($this->nodes[1]
      ->getTitle());
    $page
      ->waitFor(10, function () use ($page) {
      return $page
        ->find('css', "main .contextual");
    });
    $this
      ->toggleContextualTriggerVisibility('main');
    $contextual_button = $page
      ->find('css', 'main .contextual button');
    $this
      ->assertEmpty(0, $contextual_button ?: '');
  }

}

Members