You are here

protected function DropzoneJsWebDriverTestBase::dropFile in DropzoneJS 8.2

Drop a predefined file to dropzone.

Throws

\Behat\Mink\Exception\ElementNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to DropzoneJsWebDriverTestBase::dropFile()
DropzoneJsEbWidgetTest::testUploadFile in tests/src/FunctionalJavascript/DropzoneJsEbWidgetTest.php
Tests the add widget with iframe form.

File

tests/src/FunctionalJavascript/DropzoneJsWebDriverTestBase.php, line 82

Class

DropzoneJsWebDriverTestBase
Base class for DropzoneJS Web driver functional test base.

Namespace

Drupal\Tests\dropzonejs\FunctionalJavascript

Code

protected function dropFile() {

  // Sometimes we are not yet switched to the iframe. Stop the horses a bit
  // here.
  sleep(1);
  $file = $this
    ->getFile('notalama');
  $input = <<<JS
      jQuery('.form-type-dropzonejs').append('<input type=\\'file\\' name=\\'fakefile\\'>');
JS;
  $this
    ->getSession()
    ->evaluateScript($input);
  $full_path = \Drupal::service('file_system')
    ->realPath($file
    ->getFileUri());
  if (is_file($full_path)) {
    $path = $full_path;
  }
  if (!isset($path) || !file_exists($path)) {
    throw new \RuntimeException("File {$path} does not exist");
  }
  $this
    ->getSession()
    ->getPage()
    ->attachFileToField('fakefile', $path);
  $drop = <<<JS
    (function(jQuery) {
      var fakeFileInputs = jQuery('input[name=fakefile]' );
      var fileList = fakeFileInputs.map(function (i, el) { return el.files[0] });
      var e = jQuery.Event('drop', { dataTransfer : { files : fileList } });
      jQuery('.dropzone' )[0].dropzone.listeners[0].events.drop(e);
      fakeFileInputs.map(function (i, el) { return el.remove(); });
    })(jQuery);
JS;
  $this
    ->getSession()
    ->evaluateScript($drop);
}