diff --git a/contextual_links_example/contextual_links_example.info.yml b/contextual_links_example/contextual_links_example.info.yml index cd2b1aa..14304ed 100644 --- a/contextual_links_example/contextual_links_example.info.yml +++ b/contextual_links_example/contextual_links_example.info.yml @@ -6,3 +6,4 @@ core: 8.x dependencies: - drupal:examples - drupal:contextual + - drupal:block diff --git a/contextual_links_example/tests/src/FunctionalJavascript/ContextualLinksExampleTest.php b/contextual_links_example/tests/src/FunctionalJavascript/ContextualLinksExampleTest.php index a678f6f..aee1794 100644 --- a/contextual_links_example/tests/src/FunctionalJavascript/ContextualLinksExampleTest.php +++ b/contextual_links_example/tests/src/FunctionalJavascript/ContextualLinksExampleTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\contextual_links_example\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\user\Entity\Role; /** * Tests the behavior of the contextual links provided by the module. @@ -19,7 +20,10 @@ class ContextualLinksExampleTest extends JavascriptTestBase { * * @var array */ - public static $modules = ['contextual_links_example']; + public static $modules = [ + 'node', + 'contextual_links_example' + ]; /** * The installation profile to use with this test. @@ -31,6 +35,19 @@ class ContextualLinksExampleTest extends JavascriptTestBase { protected $profile = 'minimal'; /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + // Place our custom block in sidebar first. + $this->placeBlock('cleblock'); + + // Create a node and promote it to the front page. + $this->drupalCreateNode(['promote' => 1]); + } + + /** * Tests contextual_links_example functionality. */ public function testContextualLinksExampleBasic() { @@ -39,41 +56,36 @@ class ContextualLinksExampleTest extends JavascriptTestBase { // Create user. $web_user = $this->drupalCreateUser([ 'access contextual links', - 'bypass node access', - 'administer blocks', ]); // Login the admin user. $this->drupalLogin($web_user); + // Grant permissions to use contextual links on blocks. + $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), [ + 'access contextual links', + 'administer blocks', + ]); + // We check for a link to the contextual example in the Tools menu. - $this->drupalGet(''); + $this->drupalGet('node'); $this->assertSession()->linkByHrefExists('examples/contextual-links'); $this->drupalGet('examples/contextual-links'); $this->assertSession()->statusCodeEquals(200); - // Create a node and promote it to the front page. Then view the front page - // and verify that the "Example action" contextual link works. - $this->drupalCreateNode(['promote' => 1]); + $this->drupalGet('node'); + // Check the presence of contextual link "Edit object" on our block. + $contextualLinks = $this->assertSession()->waitForElement('css', '.contextual-links-exampleconfigure'); + $this->assertNotEmpty($contextualLinks); + + // Check the presence of contextual link "Example Action" on our node. + $contextualLinks = $this->assertSession()->waitForElement('css', '.contextual-links-exampleexample-action'); + $this->assertNotEmpty($contextualLinks); - $this->drupalGet(''); - $assert->linkByHrefExists('/node/1/example-action?destination=node'); - $this->clickLink('Example Action'); - $assert->addressEquals('/node/1/example-action?destination=node'); - - // Visit our example overview page and click the third contextual link. - // This should take us to a page for editing the third object we defined. - $this->drupalGet(''); - $this->clickLink('Contextual Links Example'); - $this->clickLink('Edit object', 2); - $assert->addressEquals('/examples/contextual-links/3/edit?destination=examples/contextual-links'); - - // Enable our module's block, go back to the front page, and click the - // "Edit object" contextual link that we expect to be there. - $this->drupalPlaceBlock('cleblock'); - $this->drupalGet(''); - $this->clickLink('Edit object'); - $assert->addressEquals('examples/contextual-links/42/edit?destination=examples/contextual-links/3/edit'); + // Visit our example overview page and check for presence of contextual links. + $this->$this->drupalGet('examples/contextual-links'); + $contextualLinks = $this->assertSession()->waitForElement('css', '.contextual-links-exampleconfigure'); + $this->assertNotEmpty($contextualLinks); } }