View source
<?php
namespace Drupal\Tests\quickedit\FunctionalJavascript;
use Behat\Mink\Session;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\TestFileCreationTrait;
class QuickEditLoadingTest extends WebDriverTestBase {
use ContextualLinkClickTrait;
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected static $modules = [
'contextual',
'quickedit',
'filter',
'node',
'image',
];
protected $defaultTheme = 'classy';
protected $authorUser;
protected $testNode;
protected $editorUser;
protected function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$node_type = NodeType::load('article');
$node_type
->setNewRevision(FALSE);
$node_type
->save();
$this->testNode = $this
->drupalCreateNode([
'type' => 'article',
'body' => [
0 => [
'value' => '<p>How are you?</p>',
'format' => 'filtered_html',
],
],
'revision_log' => $this
->randomString(),
]);
$basic_permissions = [
'access content',
'create article content',
'edit any article content',
'use text format filtered_html',
'access contextual links',
];
$this->authorUser = $this
->drupalCreateUser($basic_permissions);
$this->editorUser = $this
->drupalCreateUser(array_merge($basic_permissions, [
'access in-place editing',
]));
}
public function testUserPermissions() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->authorUser);
$this
->drupalGet('node/1');
$this
->assertSession()
->responseNotContains('core/modules/quickedit/js/quickedit.js');
$this
->assertSession()
->responseNotContains('core/modules/quickedit/js/editors/formEditor.js');
$this
->assertSession()
->responseNotContains('data-quickedit-entity-id="node/1"');
$this
->assertSession()
->responseNotContains('data-quickedit-field-id="node/1/body/en/full"');
$this
->assertSession()
->elementNotExists('xpath', '//h1[contains(@class, "js-quickedit-page-title")]');
$assert
->linkNotExists('Quick edit');
$nid = $this->testNode
->id();
$node = Node::load($nid);
$vids = \Drupal::entityTypeManager()
->getStorage('node')
->revisionIds($node);
$this
->assertCount(1, $vids, 'The node has only one revision.');
$original_log = $node->revision_log->value;
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/' . $nid);
$page = $this
->getSession()
->getPage();
$assert
->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
$this
->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
$page
->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')
->click();
$body_field_locator = '[name="body[0][value]"]';
$body_text = 'Fine thanks.';
$assert
->waitForElementVisible('css', $body_field_locator)
->setValue('<p>' . $body_text . '</p>');
$assert
->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')
->click();
$assert
->waitForElementRemoved('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]');
$assert
->responseMatches("|\\s*{$body_text}\\s*|");
$this
->drupalGet('node/' . $nid);
$assert
->pageTextContains($body_text);
$node = Node::load($nid);
$vids = \Drupal::entityTypeManager()
->getStorage('node')
->revisionIds($node);
$this
->assertCount(1, $vids, 'The node has only one revision.');
$this
->assertSame($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
}
public function testWithPendingRevision() {
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/add/article');
$this
->submitForm([
'title[0][value]' => 'foo',
], 'Preview');
$this
->assertSession()
->responseNotContains('data-quickedit-entity-id="node/' . $this->testNode
->id() . '"');
$this
->assertSession()
->responseNotContains('data-quickedit-field-id="node/' . $this->testNode
->id() . '/title/' . $this->testNode
->language()
->getId() . '/full"');
$this
->drupalGet('node/' . $this->testNode
->id());
$this
->assertSession()
->responseContains('data-quickedit-entity-id="node/' . $this->testNode
->id() . '"');
$this
->assertSession()
->responseContains('data-quickedit-field-id="node/' . $this->testNode
->id() . '/title/' . $this->testNode
->language()
->getId() . '/full"');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this->testNode->title = 'Updated node';
$this->testNode
->setNewRevision(TRUE);
$this->testNode
->isDefaultRevision(FALSE);
$this->testNode
->save();
$this
->drupalGet('node/' . $this->testNode
->id());
$this
->assertSession()
->responseNotContains('data-quickedit-entity-id="node/' . $this->testNode
->id() . '"');
$this
->assertSession()
->responseNotContains('data-quickedit-field-id="node/' . $this->testNode
->id() . '/title/' . $this->testNode
->language()
->getId() . '/full"');
}
public function testTitleBaseField() {
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$nid = $this->testNode
->id();
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/' . $nid);
$assert
->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
$this
->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
$page
->find('css', '[data-quickedit-field-id="node/' . $nid . '/title/en/full"]')
->click();
$field_locator = '.field--name-title';
$text_new = 'Obligatory question';
$assert
->waitForElementVisible('css', $field_locator)
->setValue($text_new);
$this
->assertSession()
->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')
->click();
$assert
->waitForElementRemoved('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]');
$assert
->responseMatches("|\\s*{$text_new}\\s*|");
$this
->drupalGet('node/' . $nid);
$assert
->pageTextContains($text_new);
}
public function testDisplayOptions() {
$node = Node::load('1');
$display_settings = [
'label' => 'inline',
];
$build = $node->body
->view($display_settings);
$output = \Drupal::service('renderer')
->renderRoot($build);
$this
->assertStringNotContainsString('data-quickedit-field-id', $output, 'data-quickedit-field-id attribute not added when rendering field using dynamic display options.');
}
public function testConcurrentEdit() {
$nid = $this->testNode
->id();
$this
->drupalLogin($this->authorUser);
$this
->drupalGet('node/' . $nid . '/edit');
$logged_in_user = $this->loggedInUser;
$this->loggedInUser = FALSE;
$driver = $this
->getDefaultDriverInstance();
$session = new Session($driver);
$this->mink
->registerSession('concurrent', $session);
$this->mink
->setDefaultSessionName('concurrent');
$this
->initFrontPage();
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/' . $nid);
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$assert
->waitForElement('css', '[data-quickedit-entity-id="node/' . $nid . '"] .contextual .quickedit');
$this
->clickContextualLink('[data-quickedit-entity-id="node/' . $nid . '"]', 'Quick edit');
$page
->find('css', '[data-quickedit-field-id="node/' . $nid . '/body/en/full"]')
->click();
$body_field_locator = '[name="body[0][value]"]';
$body_text = 'Fine thanks.';
$assert
->waitForElementVisible('css', $body_field_locator)
->setValue('<p>' . $body_text . '</p>');
$assert
->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')
->click();
$assert
->waitForElementRemoved('css', $body_field_locator);
$assert
->responseMatches("|\\s*{$body_text}\\s*|");
$this->mink
->setDefaultSessionName('default');
$this->loggedInUser = $logged_in_user;
sleep(2);
$this
->submitForm([
'body[0][value]' => '<p>Concurrent edit!</p>',
], 'Save');
$this
->getSession()
->getPage()
->hasContent('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
}
public function testContentBlock() {
\Drupal::service('module_installer')
->install([
'block_content',
]);
$block = BlockContent::create([
'info' => $this
->randomMachineName(),
'type' => 'basic',
'langcode' => 'en',
]);
$block
->save();
$this
->drupalPlaceBlock('block_content:' . $block
->uuid());
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('');
$this
->assertSession()
->responseContains('data-quickedit-entity-id="block_content/1"');
}
public function testImageField() {
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
FieldStorageConfig::create([
'field_name' => 'field_image',
'type' => 'image',
'entity_type' => 'node',
])
->save();
FieldConfig::create([
'field_name' => 'field_image',
'field_type' => 'image',
'label' => t('Image'),
'entity_type' => 'node',
'bundle' => 'article',
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article', 'default')
->setComponent('field_image', [
'type' => 'image_image',
])
->save();
$display = EntityViewDisplay::load('node.article.default');
$display
->setComponent('field_image', [
'type' => 'image',
])
->save();
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/1/edit');
$image = $this
->drupalGetTestFiles('image')[0];
$image_path = $this->container
->get('file_system')
->realpath($image->uri);
$page
->attachFileToField('files[field_image_0]', $image_path);
$alt_field = $assert
->waitForField('field_image[0][alt]');
$this
->assertNotEmpty($alt_field);
$this
->submitForm([
'field_image[0][alt]' => 'Vivamus aliquet elit',
], 'Save');
$assert
->waitForElement('css', '[data-quickedit-entity-id="node/1"] .contextual .quickedit');
$this
->clickContextualLink('[data-quickedit-entity-id="node/1"]', 'Quick edit');
$assert
->waitForElement('css', '[data-quickedit-field-id="node/1/field_image/en/full"]')
->click();
$field_locator = '.field--name-field-image';
$assert
->waitForElementVisible('css', $field_locator);
}
}