trait SortableTestTrait

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/SortableTestTrait.php \Drupal\FunctionalJavascriptTests\SortableTestTrait
  2. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/SortableTestTrait.php \Drupal\FunctionalJavascriptTests\SortableTestTrait
  3. 11.x core/tests/Drupal/FunctionalJavascriptTests/SortableTestTrait.php \Drupal\FunctionalJavascriptTests\SortableTestTrait

Provides functions for simulating sort changes.

Selenium uses ChromeDriver for FunctionalJavascript tests, but it does not currently support HTML5 drag and drop. These methods manipulate the DOM. This trait should be deprecated when the Chromium bug is fixed.

Hierarchy

See also

https://www.drupal.org/project/drupal/issues/3078152

2 files declare their use of SortableTestTrait
EntityReferenceWidgetTest.php in core/modules/media_library/tests/src/FunctionalJavascript/EntityReferenceWidgetTest.php
LayoutBuilderSortTrait.php in core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderSortTrait.php

File

core/tests/Drupal/FunctionalJavascriptTests/SortableTestTrait.php, line 16

Namespace

Drupal\FunctionalJavascriptTests
View source
trait SortableTestTrait {
    
    /**
     * Define to provide any necessary callback following layout change.
     *
     * @param string $item
     *   The HTML selector for the element to be moved.
     * @param string $from
     *   The HTML selector for the previous container element.
     * @param null|string $to
     *   The HTML selector for the target container.
     */
    protected abstract function sortableUpdate($item, $from, $to = NULL);
    
    /**
     * Simulates a drag on an element from one container to another.
     *
     * @param string $item
     *   The HTML selector for the element to be moved.
     * @param string $from
     *   The HTML selector for the previous container element.
     * @param null|string $to
     *   The HTML selector for the target container.
     */
    protected function sortableTo($item, $from, $to) {
        $item = addslashes($item);
        $from = addslashes($from);
        $to = addslashes($to);
        $script = <<<JS
(function (src, to) {
  var sourceElement = document.querySelector(src);
  var toElement = document.querySelector(to);

  toElement.insertBefore(sourceElement, toElement.firstChild);
})('{<span class="php-variable">$item</span>}', '{<span class="php-variable">$to</span>}')

JS;
        $options = [
            'script' => $script,
            'args' => [],
        ];
        $this->getSession()
            ->getDriver()
            ->getWebDriverSession()
            ->execute($options);
        $this->sortableUpdate($item, $from, $to);
    }
    
    /**
     * Simulates a drag moving an element after its sibling in the same container.
     *
     * @param string $item
     *   The HTML selector for the element to be moved.
     * @param string $target
     *   The HTML selector for the sibling element.
     * @param string $from
     *   The HTML selector for the element container.
     */
    protected function sortableAfter($item, $target, $from) {
        $item = addslashes($item);
        $target = addslashes($target);
        $from = addslashes($from);
        $script = <<<JS
(function (src, to) {
  var sourceElement = document.querySelector(src);
  var toElement = document.querySelector(to);

  toElement.insertAdjacentElement('afterend', sourceElement);
})('{<span class="php-variable">$item</span>}', '{<span class="php-variable">$target</span>}')

JS;
        $options = [
            'script' => $script,
            'args' => [],
        ];
        $this->getSession()
            ->getDriver()
            ->getWebDriverSession()
            ->execute($options);
        $this->sortableUpdate($item, $from);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
SortableTestTrait::sortableAfter protected function Simulates a drag moving an element after its sibling in the same container.
SortableTestTrait::sortableTo protected function Simulates a drag on an element from one container to another.
SortableTestTrait::sortableUpdate abstract protected function Define to provide any necessary callback following layout change. 2

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.