ContextualLinksTest.php

Same filename in this branch
  1. 11.x core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php
  2. 11.x core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php
Same filename in other branches
  1. 9 core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php
  2. 9 core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php
  3. 9 core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php
  4. 8.9.x core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php
  5. 8.9.x core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php
  6. 8.9.x core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php
  7. 10 core/modules/layout_builder/tests/src/FunctionalJavascript/ContextualLinksTest.php
  8. 10 core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php
  9. 10 core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php

Namespace

Drupal\Tests\node\FunctionalJavascript

File

core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\node\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;

/**
 * Create a node with revisions and test contextual links.
 *
 * @group node
 */
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() : void {
        // 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 ?: '');
    }

}

Classes

Title Deprecated Summary
ContextualLinksTest Create a node with revisions and test contextual links.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.