SortableTestTrait.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests;
  3. /**
  4. * Provides functions for simulating sort changes.
  5. *
  6. * Selenium uses ChromeDriver for FunctionalJavascript tests, but it does not
  7. * currently support HTML5 drag and drop. These methods manipulate the DOM.
  8. * This trait should be deprecated when the Chromium bug is fixed.
  9. *
  10. * @see https://www.drupal.org/project/drupal/issues/3078152
  11. */
  12. trait SortableTestTrait {
  13. /**
  14. * Define to provide any necessary callback following layout change.
  15. *
  16. * @param string $item
  17. * The HTML selector for the element to be moved.
  18. * @param string $from
  19. * The HTML selector for the previous container element.
  20. * @param null|string $to
  21. * The HTML selector for the target container.
  22. */
  23. abstract protected function sortableUpdate($item, $from, $to = NULL);
  24. /**
  25. * Simulates a drag on an element from one container to another.
  26. *
  27. * @param string $item
  28. * The HTML selector for the element to be moved.
  29. * @param string $from
  30. * The HTML selector for the previous container element.
  31. * @param null|string $to
  32. * The HTML selector for the target container.
  33. */
  34. protected function sortableTo($item, $from, $to) {
  35. $item = addslashes($item);
  36. $from = addslashes($from);
  37. $to = addslashes($to);
  38. $script = <<<JS
  39. (function (src, to) {
  40. var sourceElement = document.querySelector(src);
  41. var toElement = document.querySelector(to);
  42. toElement.insertBefore(sourceElement, toElement.firstChild);
  43. })('{$item}', '{$to}')
  44. JS;
  45. $options = [
  46. 'script' => $script,
  47. 'args' => [],
  48. ];
  49. $this->getSession()->getDriver()->getWebDriverSession()->execute($options);
  50. $this->sortableUpdate($item, $from, $to);
  51. }
  52. /**
  53. * Simulates a drag moving an element after its sibling in the same container.
  54. *
  55. * @param string $item
  56. * The HTML selector for the element to be moved.
  57. * @param string $target
  58. * The HTML selector for the sibling element.
  59. * @param string $from
  60. * The HTML selector for the element container.
  61. */
  62. protected function sortableAfter($item, $target, $from) {
  63. $item = addslashes($item);
  64. $target = addslashes($target);
  65. $from = addslashes($from);
  66. $script = <<<JS
  67. (function (src, to) {
  68. var sourceElement = document.querySelector(src);
  69. var toElement = document.querySelector(to);
  70. toElement.insertAdjacentElement('afterend', sourceElement);
  71. })('{$item}', '{$target}')
  72. JS;
  73. $options = [
  74. 'script' => $script,
  75. 'args' => [],
  76. ];
  77. $this->getSession()->getDriver()->getWebDriverSession()->execute($options);
  78. $this->sortableUpdate($item, $from);
  79. }
  80. }