3 namespace Drupal\Tests\quickedit\FunctionalJavascript;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
9 * Base class for testing the QuickEdit.
11 class QuickEditJavascriptTestBase extends WebDriverTestBase {
16 public static $modules = ['contextual', 'quickedit', 'toolbar'];
19 * A user with permissions to edit Articles and use Quick Edit.
21 * @var \Drupal\user\UserInterface
23 protected $contentAuthorUser;
25 protected static $expectedFieldStateAttributes = [
26 'inactive' => '.quickedit-field:not(.quickedit-editable):not(.quickedit-candidate):not(.quickedit-highlighted):not(.quickedit-editing):not(.quickedit-changed)',
27 // A field in 'candidate' state may still have the .quickedit-changed class
28 // because when its changes were saved to tempstore, it'll still be changed.
29 // It's just not currently being edited, so that's why it is not in the
31 'candidate' => '.quickedit-field.quickedit-editable.quickedit-candidate:not(.quickedit-highlighted):not(.quickedit-editing)',
32 'highlighted' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted:not(.quickedit-editing)',
33 'activating' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
34 'active' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
35 'changed' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
36 'saving' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
40 * Starts in-place editing of the given entity instance.
42 * @param string $entity_type_id
44 * @param int $entity_id
46 * @param int $entity_instance_id
47 * The entity instance ID. (Instance on the page.)
49 protected function startQuickEditViaToolbar($entity_type_id, $entity_id, $entity_instance_id) {
50 $page = $this->getSession()->getPage();
52 $toolbar_edit_button_selector = '#toolbar-bar .contextual-toolbar-tab button';
53 $entity_instance_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"][data-quickedit-entity-instance-id="' . $entity_instance_id . '"]';
54 $contextual_links_trigger_selector = '[data-contextual-id] > .trigger';
56 // Assert the original page state does not have the toolbar's "Edit" button
57 // pressed/activated, and hence none of the contextual link triggers should
59 $toolbar_edit_button = $page->find('css', $toolbar_edit_button_selector);
60 $this->assertSame('false', $toolbar_edit_button->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is not yet pressed.');
61 $this->assertFalse($toolbar_edit_button->hasClass('is-active'), 'The "Edit" button in the toolbar is not yet marked as active.');
62 foreach ($page->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
63 /** @var \Behat\Mink\Element\NodeElement $dom_node */
64 $this->assertTrue($dom_node->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node->getParent()->getAttribute('data-contextual-id') . '" is hidden.');
66 $this->assertTrue(TRUE, 'All contextual links triggers are hidden.');
68 // Click the "Edit" button in the toolbar.
69 $this->click($toolbar_edit_button_selector);
71 // Assert the toolbar's "Edit" button is now pressed/activated, and hence
72 // all of the contextual link triggers should be visible.
73 $this->assertSame('true', $toolbar_edit_button->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is pressed.');
74 $this->assertTrue($toolbar_edit_button->hasClass('is-active'), 'The "Edit" button in the toolbar is marked as active.');
75 foreach ($page->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
76 /** @var \Behat\Mink\Element\NodeElement $dom_node */
77 $this->assertFalse($dom_node->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node->getParent()->getAttribute('data-contextual-id') . '" is visible.');
79 $this->assertTrue(TRUE, 'All contextual links triggers are visible.');
81 // @todo Press tab key to verify that tabbing is now contrained to only
82 // contextual links triggers: https://www.drupal.org/node/2834776
84 // Assert that the contextual links associated with the entity's contextual
85 // links trigger are not visible.
86 /** @var \Behat\Mink\Element\NodeElement $entity_contextual_links_container */
87 $entity_contextual_links_container = $page->find('css', $entity_instance_selector)
88 ->find('css', $contextual_links_trigger_selector)
90 $this->assertFalse($entity_contextual_links_container->hasClass('open'));
91 $this->assertTrue($entity_contextual_links_container->find('css', 'ul.contextual-links')->hasAttribute('hidden'));
93 // Click the contextual link trigger for the entity we want to Quick Edit.
94 $this->click($entity_instance_selector . ' ' . $contextual_links_trigger_selector);
96 $this->assertTrue($entity_contextual_links_container->hasClass('open'));
97 $this->assertFalse($entity_contextual_links_container->find('css', 'ul.contextual-links')->hasAttribute('hidden'));
99 // Click the "Quick edit" contextual link.
100 $this->click($entity_instance_selector . ' [data-contextual-id] ul.contextual-links li.quickedit a');
102 // Assert the Quick Edit internal state is correct.
103 $js_condition = <<<JS
104 Drupal.quickedit.collections.entities.where({isActive: true}).length === 1 && Drupal.quickedit.collections.entities.where({isActive: true})[0].get('entityID') === '$entity_type_id/$entity_id';
106 $this->assertJsCondition($js_condition);
110 * Clicks the 'Save' button in the Quick Edit entity toolbar.
112 protected function saveQuickEdit() {
113 $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
114 $save_button = $quickedit_entity_toolbar->find('css', 'button.action-save');
115 $save_button->press();
116 $this->assertSame('Saving', $save_button->getText());
120 * Awaits Quick Edit to be initiated for all instances of the given entity.
122 * @param string $entity_type_id
123 * The entity type ID.
124 * @param int $entity_id
127 protected function awaitQuickEditForEntity($entity_type_id, $entity_id) {
128 $entity_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"]';
129 $condition = "document.querySelectorAll('" . $entity_selector . "').length === document.querySelectorAll('" . $entity_selector . " .quickedit').length";
130 $this->assertJsCondition($condition, 10000);
134 * Awaits a particular field instance to reach a particular state.
136 * @param string $entity_type_id
137 * The entity type ID.
138 * @param int $entity_id
140 * @param int $entity_instance_id
141 * The entity instance ID. (Instance on the page.)
142 * @param string $field_name
144 * @param string $langcode
146 * @param string $awaited_state
147 * One of the possible field states.
149 protected function awaitEntityInstanceFieldState($entity_type_id, $entity_id, $entity_instance_id, $field_name, $langcode, $awaited_state) {
150 $entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
151 $logical_field_id = $entity_type_id . '/' . $entity_id . '/' . $field_name . '/' . $langcode;
152 $this->assertJsCondition("Drupal.quickedit.collections.entities.get('$entity_page_id').get('fields').findWhere({logicalFieldID: '$logical_field_id'}).get('state') === '$awaited_state';");
156 * Asserts the state of the Quick Edit entity toolbar.
158 * @param string $expected_entity_label
159 * The expected label in the Quick Edit Entity Toolbar.
161 protected function assertQuickEditEntityToolbar($expected_entity_label, $expected_field_label) {
162 $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
163 // We cannot use ->getText() because it also returns the text of all child
164 // nodes. We also cannot use XPath to select text node in Selenium. So we
165 // use JS expression to select only the text node.
166 $this->assertSame($expected_entity_label, $this->getSession()->evaluateScript("return window.jQuery('#quickedit-entity-toolbar .quickedit-toolbar-label').clone().children().remove().end().text();"));
167 if ($expected_field_label !== NULL) {
168 $field_label = $quickedit_entity_toolbar->find('css', '.quickedit-toolbar-label > .field');
169 // Only try to find the text content of the element if it was actually
170 // found; otherwise use the returned value for assertion. This helps
171 // us find a more useful stack/error message from testbot instead of the
172 // trimmed partial exception stack.
174 $field_label = $field_label->getText();
176 $this->assertSame($expected_field_label, $field_label);
179 $this->assertFalse($quickedit_entity_toolbar->find('css', '.quickedit-toolbar-label > .field'));
184 * Asserts all EntityModels (entity instances) on the page.
186 * @param array $expected_entity_states
187 * Must describe the expected state of all in-place editable entity
188 * instances on the page.
190 * @see Drupal.quickedit.EntityModel
192 protected function assertEntityInstanceStates(array $expected_entity_states) {
193 $js_get_all_field_states_for_entity = <<<JS
195 Drupal.quickedit.collections.entities.reduce(function (result, fieldModel) { result[fieldModel.get('id')] = fieldModel.get('state'); return result; }, {})
196 var entityCollection = Drupal.quickedit.collections.entities;
197 return entityCollection.reduce(function (result, entityModel) {
198 result[entityModel.id] = entityModel.get('state');
203 $this->assertSame($expected_entity_states, $this->getSession()->evaluateScript($js_get_all_field_states_for_entity));
207 * Asserts all FieldModels for the given entity instance.
209 * @param string $entity_type_id
210 * The entity type ID.
211 * @param int $entity_id
213 * @param int $entity_instance_id
214 * The entity instance ID. (Instance on the page.)
215 * @param array $expected_field_states
216 * Must describe the expected state of all in-place editable fields of the
217 * given entity instance.
219 protected function assertEntityInstanceFieldStates($entity_type_id, $entity_id, $entity_instance_id, array $expected_field_states) {
220 // Get all FieldModel states for the entity instance being asserted. This
221 // ensures that $expected_field_states must describe the state of all fields
222 // of the entity instance.
223 $entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
224 $js_get_all_field_states_for_entity = <<<JS
226 var entityCollection = Drupal.quickedit.collections.entities;
227 var entityModel = entityCollection.get('$entity_page_id');
228 return entityModel.get('fields').reduce(function (result, fieldModel) {
229 result[fieldModel.get('fieldID')] = fieldModel.get('state');
234 $this->assertEquals($expected_field_states, $this->getSession()->evaluateScript($js_get_all_field_states_for_entity));
236 // Assert that those fields also have the appropriate DOM decorations.
237 $expected_field_attributes = [];
238 foreach ($expected_field_states as $quickedit_field_id => $expected_field_state) {
239 $expected_field_attributes[$quickedit_field_id] = static::$expectedFieldStateAttributes[$expected_field_state];
241 $this->assertEntityInstanceFieldMarkup($entity_type_id, $entity_id, $entity_instance_id, $expected_field_attributes);
245 * Asserts all in-place editable fields with markup expectations.
247 * @param string $entity_type_id
248 * The entity type ID.
249 * @param int $entity_id
251 * @param int $entity_instance_id
252 * The entity instance ID. (Instance on the page.)
253 * @param array $expected_field_attributes
254 * Must describe the expected markup attributes for all given in-place
257 protected function assertEntityInstanceFieldMarkup($entity_type_id, $entity_id, $entity_instance_id, array $expected_field_attributes) {
258 $entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
259 $expected_field_attributes_json = json_encode($expected_field_attributes);
260 $js_match_field_element_attributes = <<<JS
262 var expectations = $expected_field_attributes_json;
263 var entityCollection = Drupal.quickedit.collections.entities;
264 var entityModel = entityCollection.get('$entity_page_id');
265 return entityModel.get('fields').reduce(function (result, fieldModel) {
266 var fieldID = fieldModel.get('fieldID');
267 var element = fieldModel.get('el');
268 var matches = element.webkitMatchesSelector(expectations[fieldID]);
269 result[fieldID] = matches ? matches : element.outerHTML;
274 $result = $this->getSession()->evaluateScript($js_match_field_element_attributes);
275 foreach ($expected_field_attributes as $quickedit_field_id => $expectation) {
276 $this->assertSame(TRUE, $result[$quickedit_field_id], 'Field ' . $quickedit_field_id . ' did not match its expectation selector (' . $expectation . '), actual HTML: ' . $result[$quickedit_field_id]);
281 * Simulates typing in a 'plain_text' in-place editor.
283 * @param string $css_selector
284 * The CSS selector to find the DOM element (with the 'contenteditable=true'
285 * attribute set), to type in.
286 * @param string $text
289 * @see \Drupal\quickedit\Plugin\InPlaceEditor\PlainTextEditor
291 protected function typeInPlainTextEditor($css_selector, $text) {
292 $field = $this->getSession()->getPage()->find('css', $css_selector);
293 $field->setValue(Key::END . $text);
297 * Simulates typing in an input[type=text] inside a 'form' in-place editor.
299 * @param string $input_name
300 * The "name" attribute of the input[type=text] to type in.
301 * @param string $text
304 * @see \Drupal\quickedit\Plugin\InPlaceEditor\FormEditor
306 protected function typeInFormEditorTextInputField($input_name, $text) {
307 $input = $this->cssSelect('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[type=text][name="' . $input_name . '"]')[0];
308 $input->setValue($text);
309 $js_simulate_user_typing = <<<JS
311 var el = document.querySelector('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[name="$input_name"]');
312 window.jQuery(el).trigger('formUpdated');
315 $this->getSession()->evaluateScript($js_simulate_user_typing);