View source
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class ContextualLinksTest extends WebDriverTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'views',
'views_ui',
'layout_builder',
'layout_builder_views_test',
'layout_test',
'block',
'node',
'contextual',
];
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
$user = $this
->drupalCreateUser([
'configure any layout',
'administer node display',
'administer node fields',
'access contextual links',
'administer nodes',
'bypass node access',
'administer views',
]);
$user
->save();
$this
->drupalLogin($user);
$this
->createContentType([
'type' => 'bundle_with_section_field',
]);
$this
->createNode([
'type' => 'bundle_with_section_field',
'body' => [
[
'value' => 'The node body',
],
],
]);
}
public function testContextualLinks() {
$page = $this
->getSession()
->getPage();
$field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
$this
->drupalPostForm("{$field_ui_prefix}/display/default", [
'layout[enabled]' => TRUE,
'layout[allow_custom]' => TRUE,
], 'Save');
$this
->drupalGet('node/1/layout');
$this
->addBlock('Test Block View: Teaser block');
$this
->addBlock('Recent content');
$this
->assertCorrectContextualLinksInUi();
$page
->hasButton('Save layout');
$page
->pressButton('Save layout');
$this
->drupalGet('node/1/layout');
$this
->assertCorrectContextualLinksInUi();
$this
->drupalGet('node/1');
$this
->assertCorrectContextualLinksInNode();
}
protected function addBlock($block_name) {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$assert_session
->linkExists('Add block');
$page
->clickLink('Add block');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', "#drupal-off-canvas a:contains('{$block_name}')"));
$page
->clickLink($block_name);
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '[data-drupal-selector=\'edit-actions-submit\']'));
$page
->pressButton('Add block');
$assert_session
->assertNoElementAfterWait('css', '#drupal-off-canvas');
$assert_session
->assertWaitOnAjaxRequest();
}
protected function assertCorrectContextualLinksInUi() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.block-views-blocktest-block-view-block-2'));
$layout_builder_specific_contextual_links = $page
->findAll('css', '[data-contextual-id*=\'layout_builder_block:\']');
$this
->assertNotEmpty($layout_builder_specific_contextual_links);
$this
->assertSameSize($layout_builder_specific_contextual_links, $page
->findAll('css', '#layout-builder [data-contextual-id]'));
}
protected function assertCorrectContextualLinksInNode() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '[data-contextual-id]'));
$this
->assertEmpty($page
->findAll('css', '[data-contextual-id*=\'layout_builder_block:\']'));
$this
->assertNotEmpty($page
->findAll('css', '.layout-content [data-contextual-id]'));
}
}