View source
<?php
namespace Drupal\Tests\quickedit\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use WebDriver\Key;
class QuickEditJavascriptTestBase extends WebDriverTestBase {
protected static $modules = [
'contextual',
'quickedit',
'toolbar',
];
protected $contentAuthorUser;
protected static $expectedFieldStateAttributes = [
'inactive' => '.quickedit-field:not(.quickedit-editable):not(.quickedit-candidate):not(.quickedit-highlighted):not(.quickedit-editing):not(.quickedit-changed)',
'candidate' => '.quickedit-field.quickedit-editable.quickedit-candidate:not(.quickedit-highlighted):not(.quickedit-editing)',
'highlighted' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted:not(.quickedit-editing)',
'activating' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
'active' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
'changed' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
'saving' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
];
protected function startQuickEditViaToolbar($entity_type_id, $entity_id, $entity_instance_id) {
$page = $this
->getSession()
->getPage();
$toolbar_edit_button_selector = '#toolbar-bar .contextual-toolbar-tab button';
$entity_instance_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"][data-quickedit-entity-instance-id="' . $entity_instance_id . '"]';
$contextual_links_trigger_selector = '[data-contextual-id] > .trigger';
$toolbar_edit_button = $page
->find('css', $toolbar_edit_button_selector);
$this
->assertSame('false', $toolbar_edit_button
->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is not yet pressed.');
$this
->assertFalse($toolbar_edit_button
->hasClass('is-active'), 'The "Edit" button in the toolbar is not yet marked as active.');
foreach ($page
->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
$this
->assertTrue($dom_node
->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node
->getParent()
->getAttribute('data-contextual-id') . '" is hidden.');
}
$this
->assertTrue(TRUE, 'All contextual links triggers are hidden.');
$this
->click($toolbar_edit_button_selector);
$this
->assertSame('true', $toolbar_edit_button
->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is pressed.');
$this
->assertTrue($toolbar_edit_button
->hasClass('is-active'), 'The "Edit" button in the toolbar is marked as active.');
foreach ($page
->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
$this
->assertFalse($dom_node
->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node
->getParent()
->getAttribute('data-contextual-id') . '" is visible.');
}
$this
->assertTrue(TRUE, 'All contextual links triggers are visible.');
$entity_contextual_links_container = $page
->find('css', $entity_instance_selector)
->find('css', $contextual_links_trigger_selector)
->getParent();
$this
->assertFalse($entity_contextual_links_container
->hasClass('open'));
$this
->assertTrue($entity_contextual_links_container
->find('css', 'ul.contextual-links')
->hasAttribute('hidden'));
$this
->click($entity_instance_selector . ' ' . $contextual_links_trigger_selector);
$this
->assertTrue($entity_contextual_links_container
->hasClass('open'));
$this
->assertFalse($entity_contextual_links_container
->find('css', 'ul.contextual-links')
->hasAttribute('hidden'));
$this
->click($entity_instance_selector . ' [data-contextual-id] ul.contextual-links li.quickedit a');
$js_condition = <<<JS
Drupal.quickedit.collections.entities.where({isActive: true}).length === 1 && Drupal.quickedit.collections.entities.where({isActive: true})[0].get('entityID') === '{<span class="php-variable">$entity_type_id</span>}/{<span class="php-variable">$entity_id</span>}'
JS;
$this
->assertJsCondition($js_condition);
}
protected function saveQuickEdit() {
$quickedit_entity_toolbar = $this
->getSession()
->getPage()
->findById('quickedit-entity-toolbar');
$save_button = $quickedit_entity_toolbar
->find('css', 'button.action-save');
$save_button
->press();
$this
->assertSame('Saving', $save_button
->getText());
}
protected function awaitQuickEditForEntity($entity_type_id, $entity_id) {
$entity_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"]';
$condition = "document.querySelectorAll('" . $entity_selector . "').length === document.querySelectorAll('" . $entity_selector . " .quickedit').length";
$this
->assertJsCondition($condition, 10000);
}
protected function awaitEntityInstanceFieldState($entity_type_id, $entity_id, $entity_instance_id, $field_name, $langcode, $awaited_state) {
$entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
$logical_field_id = $entity_type_id . '/' . $entity_id . '/' . $field_name . '/' . $langcode;
$this
->assertJsCondition("Drupal.quickedit.collections.entities.get('{$entity_page_id}').get('fields').findWhere({logicalFieldID: '{$logical_field_id}'}).get('state') === '{$awaited_state}'");
}
protected function assertQuickEditEntityToolbar($expected_entity_label, $expected_field_label) {
$quickedit_entity_toolbar = $this
->getSession()
->getPage()
->findById('quickedit-entity-toolbar');
$this
->assertSame($expected_entity_label, $this
->getSession()
->evaluateScript("return window.jQuery('#quickedit-entity-toolbar .quickedit-toolbar-label').clone().children().remove().end().text();"));
if ($expected_field_label !== NULL) {
$field_label = $quickedit_entity_toolbar
->find('css', '.quickedit-toolbar-label > .field');
if ($field_label) {
$field_label = $field_label
->getText();
}
$this
->assertSame($expected_field_label, $field_label);
}
else {
$this
->assertEmpty($quickedit_entity_toolbar
->find('css', '.quickedit-toolbar-label > .field'));
}
}
protected function assertEntityInstanceStates(array $expected_entity_states) {
$js_get_all_field_states_for_entity = <<<JS
function () {
Drupal.quickedit.collections.entities.reduce(function (result, fieldModel) { result[fieldModel.get('id')] = fieldModel.get('state'); return result; }, {})
var entityCollection = Drupal.quickedit.collections.entities;
return entityCollection.reduce(function (result, entityModel) {
result[entityModel.id] = entityModel.get('state');
return result;
}, {});
}()
JS;
$this
->assertSame($expected_entity_states, $this
->getSession()
->evaluateScript($js_get_all_field_states_for_entity));
}
protected function assertEntityInstanceFieldStates($entity_type_id, $entity_id, $entity_instance_id, array $expected_field_states) {
$entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
$js_get_all_field_states_for_entity = <<<JS
function () {
var entityCollection = Drupal.quickedit.collections.entities;
var entityModel = entityCollection.get('{<span class="php-variable">$entity_page_id</span>}');
return entityModel.get('fields').reduce(function (result, fieldModel) {
result[fieldModel.get('fieldID')] = fieldModel.get('state');
return result;
}, {});
}()
JS;
$this
->assertEquals($expected_field_states, $this
->getSession()
->evaluateScript($js_get_all_field_states_for_entity));
$expected_field_attributes = [];
foreach ($expected_field_states as $quickedit_field_id => $expected_field_state) {
$expected_field_attributes[$quickedit_field_id] = static::$expectedFieldStateAttributes[$expected_field_state];
}
$this
->assertEntityInstanceFieldMarkup($expected_field_attributes);
}
protected function assertEntityInstanceFieldMarkup($expected_field_attributes) {
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);
}
if (!is_array($expected_field_attributes)) {
throw new \InvalidArgumentException('The $expected_field_attributes argument must be an array.');
}
foreach ($expected_field_attributes as $quickedit_field_id => $expectation) {
$element = $this
->assertSession()
->waitForElementVisible('css', '[data-quickedit-field-id="' . $quickedit_field_id . '"]' . $expectation);
$this
->assertNotEmpty($element, 'Field ' . $quickedit_field_id . ' did not match its expectation selector (' . $expectation . ')');
}
}
protected function typeInPlainTextEditor($css_selector, $text) {
$field = $this
->getSession()
->getPage()
->find('css', $css_selector);
$field
->setValue(Key::END . $text);
}
protected function typeInFormEditorTextInputField($input_name, $text) {
$input = $this
->cssSelect('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[type=text][name="' . $input_name . '"]')[0];
$input
->setValue($text);
$js_simulate_user_typing = <<<JS
function () {
var el = document.querySelector('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[name="{<span class="php-variable">$input_name</span>}"]');
window.jQuery(el).trigger('formUpdated');
}()
JS;
$this
->getSession()
->evaluateScript($js_simulate_user_typing);
}
}