You are here

protected function QuickEditImageEditorTestTrait::dropImageOnImageEditor in Drupal 8

Simulates dragging and dropping an image on the 'image' in-place editor.

Parameters

string $file_uri: The URI of the image file to drag and drop.

1 call to QuickEditImageEditorTestTrait::dropImageOnImageEditor()
QuickEditImageTest::testImageInPlaceEditor in core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php
Test that quick editor works correctly with images.

File

core/modules/image/tests/src/FunctionalJavascript/QuickEditImageEditorTestTrait.php, line 39

Class

QuickEditImageEditorTestTrait

Namespace

Drupal\Tests\image\FunctionalJavascript

Code

protected function dropImageOnImageEditor($file_uri) {

  // Our headless browser can't drag+drop files, but we can mock the event.
  // Append a hidden upload element to the DOM.
  $script = 'jQuery("<input id=\\"quickedit-image-test-input\\" type=\\"file\\" />").appendTo("body")';
  $this
    ->getSession()
    ->executeScript($script);

  // Find the element, and set its value to our new image.
  $input = $this
    ->assertSession()
    ->elementExists('css', '#quickedit-image-test-input');
  $filepath = $this->container
    ->get('file_system')
    ->realpath($file_uri);
  $input
    ->attachFile($filepath);

  // Trigger the upload logic with a mock "drop" event.
  $script = 'var e = jQuery.Event("drop");' . 'e.originalEvent = {dataTransfer: {files: jQuery("#quickedit-image-test-input").get(0).files}};' . 'e.preventDefault = e.stopPropagation = function () {};' . 'jQuery(".quickedit-image-dropzone").trigger(e);';
  $this
    ->getSession()
    ->executeScript($script);

  // Wait for the dropzone element to be removed (i.e. loading is done).
  $js_condition = <<<JS
function () {
  var activeFieldID = Drupal.quickedit.collections.entities
    .findWhere({state:'opened'})
    .get('fields')
    .filter(function (fieldModel) {
      var state = fieldModel.get('state');
        return state === 'active' || state === 'changed';
    })[0]
    .get('fieldID')
  return document.querySelector('[data-quickedit-field-id="' + activeFieldID + '"] .quickedit-image-dropzone') === null;
}();
JS;
  $this
    ->assertJsCondition($js_condition, 20000);
}