View source
<?php
namespace Drupal\Tests\quickedit\FunctionalJavascript;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayTestBase;
use Drupal\user\Entity\Role;
class SettingsTrayIntegrationTest extends SettingsTrayTestBase {
protected static $modules = [
'node',
'block_content',
'quickedit',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$user = $this
->createUser([
'administer blocks',
'access contextual links',
'access toolbar',
'administer nodes',
'access in-place editing',
]);
$this
->drupalLogin($user);
}
public function testQuickEditLinks() {
$quick_edit_selector = '#quickedit-entity-toolbar';
$node_selector = '[data-quickedit-entity-id="node/1"]';
$body_selector = '[data-quickedit-field-id="node/1/body/en/full"]';
$web_assert = $this
->assertSession();
$this
->createContentType([
'type' => 'page',
]);
$auth_role = Role::load(Role::AUTHENTICATED_ID);
$this
->grantPermissions($auth_role, [
'edit any page content',
'access content',
]);
$node = $this
->createNode([
'title' => 'Page One',
'type' => 'page',
'body' => [
[
'value' => 'Regular NODE body for the test.',
'format' => 'plain_text',
],
],
]);
$page = $this
->getSession()
->getPage();
$block_plugin = 'system_powered_by_block';
foreach ($this
->getTestThemes() as $theme) {
$this
->enableTheme($theme);
$block = $this
->placeBlock($block_plugin);
$block_selector = $this
->getBlockSelector($block);
foreach ([
1,
2,
] as $page_load_times) {
$this
->drupalGet('node/' . $node
->id());
if ($page_load_times == 1) {
$this
->enableEditMode();
}
$page
->find('css', $body_selector)
->click();
$this
->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this
->disableEditMode();
$web_assert
->elementNotExists('css', $quick_edit_selector);
$page
->find('css', $body_selector)
->click();
$web_assert
->elementNotExists('css', $quick_edit_selector);
$this
->enableEditMode();
$this
->openBlockForm($block_selector);
$page
->find('css', $body_selector)
->click();
$this
->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this
->waitForOffCanvasToClose();
$this
->openBlockForm($block_selector);
$web_assert
->elementNotExists('css', $quick_edit_selector);
}
$this
->drupalGet('node/' . $node
->id());
$web_assert
->assertWaitOnAjaxRequest();
$this
->disableEditMode();
$this
->clickContextualLink($node_selector, "Quick edit");
$this
->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this
->clickContextualLink($block_selector, "Quick edit");
$this
->waitForOffCanvasToOpen();
$web_assert
->elementNotExists('css', $quick_edit_selector);
$this
->clickContextualLink($node_selector, "Quick edit", FALSE);
$this
->waitForOffCanvasToClose();
$this
->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this
->disableEditMode();
}
}
public function testCustomBlockLinks() {
$this
->createBlockContentType('basic', TRUE);
$block_content = $this
->createBlockContent('Custom Block', 'basic', TRUE);
$this
->placeBlock('block_content:' . $block_content
->uuid(), [
'id' => 'custom',
]);
$this
->drupalGet('user');
$page = $this
->getSession()
->getPage();
$this
->toggleContextualTriggerVisibility('#block-custom');
$page
->find('css', '#block-custom .contextual button')
->press();
$links = $page
->findAll('css', "#block-custom .contextual-links li a");
$link_labels = [];
foreach ($links as $link) {
$link_labels[$link
->getAttribute('href')] = $link
->getText();
}
$href = array_search('Quick edit', $link_labels);
$this
->assertEquals('', $href);
$href = array_search('Quick edit settings', $link_labels);
$destination = (string) $this->loggedInUser
->toUrl()
->toString();
$this
->assertStringContainsString("/admin/structure/block/manage/custom/settings-tray?destination={$destination}", $href);
}
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = $title ?: $this
->randomName();
$block_content = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
]);
if ($block_content && $save === TRUE) {
$block_content
->save();
}
return $block_content;
}
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE,
]);
$bundle
->save();
if ($create_body) {
block_content_add_body_field($bundle
->id());
}
return $bundle;
}
}