View source
<?php
namespace Drupal\Tests\quickedit\FunctionalJavascript;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class LayoutBuilderQuickEditTest extends QuickEditJavascriptTestBase {
use EntityReferenceTestTrait;
use ContextualLinkClickTrait;
protected static $modules = [
'node',
'layout_builder',
'field_ui',
];
protected $defaultTheme = 'classy';
protected $article;
protected $contentAuthorUser;
protected $usingLayoutBuilder = FALSE;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->article = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'My Test Node',
'body' => [
'value' => 'Hello Layout Builder!',
'format' => 'plain_text',
],
]);
$this->contentAuthorUser = $this
->drupalCreateUser([
'access contextual links',
'access in-place editing',
'access content',
'edit any article content',
]);
$this
->drupalLogin($this->contentAuthorUser);
}
public function testQuickEditIgnoresDuplicateFields() {
$this
->enableLayouts('admin/structure/types/manage/article/display/default');
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->loginLayoutAdmin();
$this
->drupalGet('admin/structure/types/manage/article/display/default/layout');
$page
->clickLink('Add block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->waitForElementVisible('named', [
'link',
'Body',
]);
$page
->clickLink('Body');
$assert_session
->waitForElementVisible('named', [
'button',
'Add block',
]);
$page
->pressButton('Add block');
$assert_session
->waitForElementVisible('named', [
'button',
'Save layout',
]);
$page
->pressButton('Save layout');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.messages--status'));
$assert_session
->pageTextContains('The layout has been saved.');
$this
->drupalLogin($this->contentAuthorUser);
$this->usingLayoutBuilder = TRUE;
$this
->assertQuickEditInit([
'title',
]);
$this
->drupalLogin($this
->drupalCreateUser([
'access contextual links',
'access in-place editing',
'access content',
'edit any article content',
'administer nodes',
]));
$this
->assertQuickEditInit([
'title',
'uid',
'created',
]);
}
public function testEnableDisableLayoutBuilder($use_revisions, $admin_permission = FALSE) {
if (!$use_revisions) {
$content_type = NodeType::load('article');
$content_type
->setNewRevision(FALSE);
$content_type
->save();
}
$fields = [
'title',
'body',
];
if ($admin_permission) {
$fields = array_merge($fields, [
'uid',
'created',
]);
$this
->drupalLogin($this
->drupalCreateUser([
'access contextual links',
'access in-place editing',
'access content',
'edit any article content',
'administer nodes',
]));
}
$this
->assertQuickEditInit($fields);
$this
->enableLayouts('admin/structure/types/manage/article/display/default');
$this->usingLayoutBuilder = TRUE;
$this
->assertQuickEditInit($fields);
$this
->createLayoutOverride();
$this
->assertQuickEditInit($fields);
if (!$use_revisions) {
$this
->revertLayoutToDefaults();
$this
->assertQuickEditInit($fields);
$this->usingLayoutBuilder = FALSE;
$this
->disableLayoutBuilder('admin/structure/types/manage/article/display/default');
$this
->assertQuickEditInit($fields);
}
}
public function providerEnableDisableLayoutBuilder() {
return [
'use revisions, not admin' => [
TRUE,
],
'do not use revisions, not admin' => [
FALSE,
],
'use revisions, admin' => [
TRUE,
TRUE,
],
'do not use revisions, admin' => [
FALSE,
TRUE,
],
];
}
protected function enableLayouts($path) {
$user = $this->loggedInUser;
$this
->loginLayoutAdmin();
$page = $this
->getSession()
->getPage();
$this
->drupalGet($path);
$page
->checkField('layout[enabled]');
$page
->checkField('layout[allow_custom]');
$page
->pressButton('Save');
$this
->drupalLogin($user);
}
protected function assertEntityInstanceFieldStates($entity_type_id, $entity_id, $entity_instance_id, array $expected_field_states) : void {
parent::assertEntityInstanceFieldStates($entity_type_id, $entity_id, $entity_instance_id, $this
->replaceLayoutBuilderFieldIdKeys($expected_field_states));
}
protected function assertEntityInstanceFieldMarkup($expected_field_attributes) : void {
if (func_num_args() === 4) {
$expected_field_attributes = func_get_arg(3);
@trigger_error('Calling ' . __METHOD__ . '() with 4 arguments is deprecated in drupal:9.1.0 and will throw an error in drupal:10.0.0. See https://www.drupal.org/project/drupal/issues/3037436', E_USER_DEPRECATED);
}
parent::assertEntityInstanceFieldMarkup($this
->replaceLayoutBuilderFieldIdKeys($expected_field_attributes));
}
protected function replaceLayoutBuilderFieldIdKeys(array $array) {
if (!$this->usingLayoutBuilder) {
return $array;
}
$replacement = [];
foreach ($array as $field_key => $value) {
$new_field_key = $this
->getQuickEditFieldId($field_key);
$replacement[$new_field_key] = $value;
}
return $replacement;
}
protected function loginLayoutAdmin() {
$this
->drupalLogin($this
->drupalCreateUser([
'configure any layout',
'access content',
'administer node display',
'administer node fields',
'administer blocks',
]));
}
protected function createLayoutOverride() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$user = $this->loggedInUser;
$this
->loginLayoutAdmin();
$this
->drupalGet('node/' . $this->article
->id() . '/layout');
$page
->pressButton('Save layout');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.messages--status'));
$assert_session
->pageTextContains('The layout override has been saved.');
$this
->drupalLogin($user);
}
protected function revertLayoutToDefaults() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$user = $this->loggedInUser;
$this
->loginLayoutAdmin();
$this
->drupalGet('node/' . $this->article
->id() . '/layout');
$assert_session
->buttonExists('Revert to defaults');
$page
->pressButton('Revert to defaults');
$page
->pressButton('Revert');
$assert_session
->pageTextContains('The layout has been reverted back to defaults.');
$this
->drupalLogin($user);
}
protected function disableLayoutBuilder($path) {
$page = $this
->getSession()
->getPage();
$user = $this->loggedInUser;
$this
->loginLayoutAdmin();
$this
->drupalGet($path);
$page
->uncheckField('layout[allow_custom]');
$page
->uncheckField('layout[enabled]');
$page
->pressButton('Save');
$page
->pressButton('Confirm');
$this
->drupalLogin($user);
}
private function assertQuickEditInit(array $fields) : void {
$this
->assertNotEmpty($fields);
$node = $this->article;
$this
->drupalGet('node/' . $node
->id());
$this
->awaitQuickEditForEntity('node', 1);
$this
->assertEntityInstanceStates([
'node/1[0]' => 'closed',
]);
$field_states = [];
foreach ($fields as $field) {
$field_states["node/1/{$field}/en/full"] = 'inactive';
}
$this
->assertEntityInstanceFieldStates('node', 1, 0, $field_states);
}
protected function getQuickEditFieldId($original_field_id) {
$page = $this
->getSession()
->getPage();
$parts = explode('/', $original_field_id);
array_pop($parts);
$field_key_without_view_mode = implode('/', $parts);
$element = $page
->find('css', "[data-quickedit-field-id^=\"{$field_key_without_view_mode}\"]");
$this
->assertNotEmpty($element, "Found Quick Edit-enabled field whose data-quickedit-field attribute starts with: {$field_key_without_view_mode}");
try {
$has_attribute = $element
->hasAttribute('data-quickedit-field-id');
} catch (\Exception $e) {
$has_attribute = FALSE;
}
$this
->assertTrue($has_attribute, $field_key_without_view_mode);
$new_field_id = $element
->getAttribute('data-quickedit-field-id');
return $new_field_id;
}
}