PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditJavascriptTestBase.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 309 lines | 140 code | 28 blank | 141 comment | 6 complexity | e1a0b0008fad8bd4e3d8085ec701f4d2 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\quickedit\FunctionalJavascript;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. use WebDriver\Key;
  5. /**
  6. * Base class for testing the QuickEdit.
  7. */
  8. class QuickEditJavascriptTestBase extends WebDriverTestBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. protected static $modules = ['contextual', 'quickedit', 'toolbar'];
  13. /**
  14. * A user with permissions to edit Articles and use Quick Edit.
  15. *
  16. * @var \Drupal\user\UserInterface
  17. */
  18. protected $contentAuthorUser;
  19. protected static $expectedFieldStateAttributes = [
  20. 'inactive' => '.quickedit-field:not(.quickedit-editable):not(.quickedit-candidate):not(.quickedit-highlighted):not(.quickedit-editing):not(.quickedit-changed)',
  21. // A field in 'candidate' state may still have the .quickedit-changed class
  22. // because when its changes were saved to tempstore, it'll still be changed.
  23. // It's just not currently being edited, so that's why it is not in the
  24. // 'changed' state.
  25. 'candidate' => '.quickedit-field.quickedit-editable.quickedit-candidate:not(.quickedit-highlighted):not(.quickedit-editing)',
  26. 'highlighted' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted:not(.quickedit-editing)',
  27. 'activating' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
  28. 'active' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing:not(.quickedit-changed)',
  29. 'changed' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
  30. 'saving' => '.quickedit-field.quickedit-editable.quickedit-candidate.quickedit-highlighted.quickedit-editing.quickedit-changed',
  31. ];
  32. /**
  33. * Starts in-place editing of the given entity instance.
  34. *
  35. * @param string $entity_type_id
  36. * The entity type ID.
  37. * @param int $entity_id
  38. * The entity ID.
  39. * @param int $entity_instance_id
  40. * The entity instance ID. (Instance on the page.)
  41. */
  42. protected function startQuickEditViaToolbar($entity_type_id, $entity_id, $entity_instance_id) {
  43. $page = $this->getSession()->getPage();
  44. $toolbar_edit_button_selector = '#toolbar-bar .contextual-toolbar-tab button';
  45. $entity_instance_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"][data-quickedit-entity-instance-id="' . $entity_instance_id . '"]';
  46. $contextual_links_trigger_selector = '[data-contextual-id] > .trigger';
  47. // Assert the original page state does not have the toolbar's "Edit" button
  48. // pressed/activated, and hence none of the contextual link triggers should
  49. // be visible.
  50. $toolbar_edit_button = $page->find('css', $toolbar_edit_button_selector);
  51. $this->assertSame('false', $toolbar_edit_button->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is not yet pressed.');
  52. $this->assertFalse($toolbar_edit_button->hasClass('is-active'), 'The "Edit" button in the toolbar is not yet marked as active.');
  53. foreach ($page->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
  54. /** @var \Behat\Mink\Element\NodeElement $dom_node */
  55. $this->assertTrue($dom_node->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node->getParent()->getAttribute('data-contextual-id') . '" is hidden.');
  56. }
  57. $this->assertTrue(TRUE, 'All contextual links triggers are hidden.');
  58. // Click the "Edit" button in the toolbar.
  59. $this->click($toolbar_edit_button_selector);
  60. // Assert the toolbar's "Edit" button is now pressed/activated, and hence
  61. // all of the contextual link triggers should be visible.
  62. $this->assertSame('true', $toolbar_edit_button->getAttribute('aria-pressed'), 'The "Edit" button in the toolbar is pressed.');
  63. $this->assertTrue($toolbar_edit_button->hasClass('is-active'), 'The "Edit" button in the toolbar is marked as active.');
  64. foreach ($page->findAll('css', $contextual_links_trigger_selector) as $dom_node) {
  65. /** @var \Behat\Mink\Element\NodeElement $dom_node */
  66. $this->assertFalse($dom_node->hasClass('visually-hidden'), 'The contextual links trigger "' . $dom_node->getParent()->getAttribute('data-contextual-id') . '" is visible.');
  67. }
  68. $this->assertTrue(TRUE, 'All contextual links triggers are visible.');
  69. // @todo Press tab key to verify that tabbing is now contrained to only
  70. // contextual links triggers: https://www.drupal.org/node/2834776
  71. // Assert that the contextual links associated with the entity's contextual
  72. // links trigger are not visible.
  73. /** @var \Behat\Mink\Element\NodeElement $entity_contextual_links_container */
  74. $entity_contextual_links_container = $page->find('css', $entity_instance_selector)
  75. ->find('css', $contextual_links_trigger_selector)
  76. ->getParent();
  77. $this->assertFalse($entity_contextual_links_container->hasClass('open'));
  78. $this->assertTrue($entity_contextual_links_container->find('css', 'ul.contextual-links')->hasAttribute('hidden'));
  79. // Click the contextual link trigger for the entity we want to Quick Edit.
  80. $this->click($entity_instance_selector . ' ' . $contextual_links_trigger_selector);
  81. $this->assertTrue($entity_contextual_links_container->hasClass('open'));
  82. $this->assertFalse($entity_contextual_links_container->find('css', 'ul.contextual-links')->hasAttribute('hidden'));
  83. // Click the "Quick edit" contextual link.
  84. $this->click($entity_instance_selector . ' [data-contextual-id] ul.contextual-links li.quickedit a');
  85. // Assert the Quick Edit internal state is correct.
  86. $js_condition = <<<JS
  87. Drupal.quickedit.collections.entities.where({isActive: true}).length === 1 && Drupal.quickedit.collections.entities.where({isActive: true})[0].get('entityID') === '$entity_type_id/$entity_id'
  88. JS;
  89. $this->assertJsCondition($js_condition);
  90. }
  91. /**
  92. * Clicks the 'Save' button in the Quick Edit entity toolbar.
  93. */
  94. protected function saveQuickEdit() {
  95. $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
  96. $save_button = $quickedit_entity_toolbar->find('css', 'button.action-save');
  97. $save_button->press();
  98. $this->assertSame('Saving', $save_button->getText());
  99. }
  100. /**
  101. * Awaits Quick Edit to be initiated for all instances of the given entity.
  102. *
  103. * @param string $entity_type_id
  104. * The entity type ID.
  105. * @param int $entity_id
  106. * The entity ID.
  107. */
  108. protected function awaitQuickEditForEntity($entity_type_id, $entity_id) {
  109. $entity_selector = '[data-quickedit-entity-id="' . $entity_type_id . '/' . $entity_id . '"]';
  110. $condition = "document.querySelectorAll('" . $entity_selector . "').length === document.querySelectorAll('" . $entity_selector . " .quickedit').length";
  111. $this->assertJsCondition($condition, 10000);
  112. }
  113. /**
  114. * Awaits a particular field instance to reach a particular state.
  115. *
  116. * @param string $entity_type_id
  117. * The entity type ID.
  118. * @param int $entity_id
  119. * The entity ID.
  120. * @param int $entity_instance_id
  121. * The entity instance ID. (Instance on the page.)
  122. * @param string $field_name
  123. * The field name.
  124. * @param string $langcode
  125. * The language code.
  126. * @param string $awaited_state
  127. * One of the possible field states.
  128. */
  129. protected function awaitEntityInstanceFieldState($entity_type_id, $entity_id, $entity_instance_id, $field_name, $langcode, $awaited_state) {
  130. $entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
  131. $logical_field_id = $entity_type_id . '/' . $entity_id . '/' . $field_name . '/' . $langcode;
  132. $this->assertJsCondition("Drupal.quickedit.collections.entities.get('$entity_page_id').get('fields').findWhere({logicalFieldID: '$logical_field_id'}).get('state') === '$awaited_state'");
  133. }
  134. /**
  135. * Asserts the state of the Quick Edit entity toolbar.
  136. *
  137. * @param string $expected_entity_label
  138. * The expected entity label in the Quick Edit Entity Toolbar.
  139. * @param string|null $expected_field_label
  140. * The expected field label in the Quick Edit Entity Toolbar, or NULL
  141. * if no field label is expected.
  142. */
  143. protected function assertQuickEditEntityToolbar($expected_entity_label, $expected_field_label) {
  144. $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
  145. // We cannot use ->getText() because it also returns the text of all child
  146. // nodes. We also cannot use XPath to select text node in Selenium. So we
  147. // use JS expression to select only the text node.
  148. $this->assertSame($expected_entity_label, $this->getSession()->evaluateScript("return window.jQuery('#quickedit-entity-toolbar .quickedit-toolbar-label').clone().children().remove().end().text();"));
  149. if ($expected_field_label !== NULL) {
  150. $field_label = $quickedit_entity_toolbar->find('css', '.quickedit-toolbar-label > .field');
  151. // Only try to find the text content of the element if it was actually
  152. // found; otherwise use the returned value for assertion. This helps
  153. // us find a more useful stack/error message from testbot instead of the
  154. // trimmed partial exception stack.
  155. if ($field_label) {
  156. $field_label = $field_label->getText();
  157. }
  158. $this->assertSame($expected_field_label, $field_label);
  159. }
  160. else {
  161. $this->assertEmpty($quickedit_entity_toolbar->find('css', '.quickedit-toolbar-label > .field'));
  162. }
  163. }
  164. /**
  165. * Asserts all EntityModels (entity instances) on the page.
  166. *
  167. * @param array $expected_entity_states
  168. * Must describe the expected state of all in-place editable entity
  169. * instances on the page.
  170. *
  171. * @see Drupal.quickedit.EntityModel
  172. */
  173. protected function assertEntityInstanceStates(array $expected_entity_states) {
  174. $js_get_all_field_states_for_entity = <<<JS
  175. function () {
  176. Drupal.quickedit.collections.entities.reduce(function (result, fieldModel) { result[fieldModel.get('id')] = fieldModel.get('state'); return result; }, {})
  177. var entityCollection = Drupal.quickedit.collections.entities;
  178. return entityCollection.reduce(function (result, entityModel) {
  179. result[entityModel.id] = entityModel.get('state');
  180. return result;
  181. }, {});
  182. }()
  183. JS;
  184. $this->assertSame($expected_entity_states, $this->getSession()->evaluateScript($js_get_all_field_states_for_entity));
  185. }
  186. /**
  187. * Asserts all FieldModels for the given entity instance.
  188. *
  189. * @param string $entity_type_id
  190. * The entity type ID.
  191. * @param int $entity_id
  192. * The entity ID.
  193. * @param int $entity_instance_id
  194. * The entity instance ID. (Instance on the page.)
  195. * @param array $expected_field_states
  196. * Must describe the expected state of all in-place editable fields of the
  197. * given entity instance.
  198. */
  199. protected function assertEntityInstanceFieldStates($entity_type_id, $entity_id, $entity_instance_id, array $expected_field_states) {
  200. // Get all FieldModel states for the entity instance being asserted. This
  201. // ensures that $expected_field_states must describe the state of all fields
  202. // of the entity instance.
  203. $entity_page_id = $entity_type_id . '/' . $entity_id . '[' . $entity_instance_id . ']';
  204. $js_get_all_field_states_for_entity = <<<JS
  205. function () {
  206. var entityCollection = Drupal.quickedit.collections.entities;
  207. var entityModel = entityCollection.get('$entity_page_id');
  208. return entityModel.get('fields').reduce(function (result, fieldModel) {
  209. result[fieldModel.get('fieldID')] = fieldModel.get('state');
  210. return result;
  211. }, {});
  212. }()
  213. JS;
  214. $this->assertEquals($expected_field_states, $this->getSession()->evaluateScript($js_get_all_field_states_for_entity));
  215. // Assert that those fields also have the appropriate DOM decorations.
  216. $expected_field_attributes = [];
  217. foreach ($expected_field_states as $quickedit_field_id => $expected_field_state) {
  218. $expected_field_attributes[$quickedit_field_id] = static::$expectedFieldStateAttributes[$expected_field_state];
  219. }
  220. $this->assertEntityInstanceFieldMarkup($expected_field_attributes);
  221. }
  222. /**
  223. * Asserts all in-place editable fields with markup expectations.
  224. *
  225. * @param array $expected_field_attributes
  226. * Must describe the expected markup attributes for all given in-place
  227. * editable fields.
  228. *
  229. * @todo https://www.drupal.org/project/drupal/issues/3178758 Remove
  230. * deprecation layer and add array typehint.
  231. */
  232. protected function assertEntityInstanceFieldMarkup($expected_field_attributes) {
  233. if (func_num_args() === 4) {
  234. $expected_field_attributes = func_get_arg(3);
  235. @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);
  236. }
  237. if (!is_array($expected_field_attributes)) {
  238. throw new \InvalidArgumentException('The $expected_field_attributes argument must be an array.');
  239. }
  240. foreach ($expected_field_attributes as $quickedit_field_id => $expectation) {
  241. $element = $this->assertSession()->waitForElementVisible('css', '[data-quickedit-field-id="' . $quickedit_field_id . '"]' . $expectation);
  242. $this->assertNotEmpty($element, 'Field ' . $quickedit_field_id . ' did not match its expectation selector (' . $expectation . ')');
  243. }
  244. }
  245. /**
  246. * Simulates typing in a 'plain_text' in-place editor.
  247. *
  248. * @param string $css_selector
  249. * The CSS selector to find the DOM element (with the 'contenteditable=true'
  250. * attribute set), to type in.
  251. * @param string $text
  252. * The text to type.
  253. *
  254. * @see \Drupal\quickedit\Plugin\InPlaceEditor\PlainTextEditor
  255. */
  256. protected function typeInPlainTextEditor($css_selector, $text) {
  257. $field = $this->getSession()->getPage()->find('css', $css_selector);
  258. $field->setValue(Key::END . $text);
  259. }
  260. /**
  261. * Simulates typing in an input[type=text] inside a 'form' in-place editor.
  262. *
  263. * @param string $input_name
  264. * The "name" attribute of the input[type=text] to type in.
  265. * @param string $text
  266. * The text to type.
  267. *
  268. * @see \Drupal\quickedit\Plugin\InPlaceEditor\FormEditor
  269. */
  270. protected function typeInFormEditorTextInputField($input_name, $text) {
  271. $input = $this->cssSelect('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[type=text][name="' . $input_name . '"]')[0];
  272. $input->setValue($text);
  273. $js_simulate_user_typing = <<<JS
  274. function () {
  275. var el = document.querySelector('.quickedit-form-container > .quickedit-form[role="dialog"] form.quickedit-field-form input[name="$input_name"]');
  276. window.jQuery(el).trigger('formUpdated');
  277. }()
  278. JS;
  279. $this->getSession()->evaluateScript($js_simulate_user_typing);
  280. }
  281. }