You are here

public function TestSubContext::iDropTheFileTo in Panopoly 8.2

Drops a file into the given CSS selector.

@Given I drop the file :path to :selector

See also

\Drupal\Tests\dropzonejs\FunctionalJavascript\DropzoneJsWebDriverTestBase::dropFile

\Behat\MinkExtension\Context\MinkContext::attachFileToField

File

modules/panopoly/panopoly_test/behat/steps/panopoly_test.behat.inc, line 1180
Provide Behat step-definitions for generic Panopoly tests.

Class

TestSubContext
Behat sub-context for Panopoly.

Code

public function iDropTheFileTo($path, $selector) {
  $input = <<<JS
      jQuery('.form-type-dropzonejs').append('<input type="file" name="fakefile">');
JS;
  $this
    ->getSession()
    ->evaluateScript($input);
  if ($this
    ->getMinkParameter('files_path')) {
    $fullPath = rtrim(realpath($this
      ->getMinkParameter('files_path')), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
    if (is_file($fullPath)) {
      $path = $fullPath;
    }
  }
  $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 } });
      document.getElementById('{<span class="php-variable">$selector</span>}').dropzone.listeners[0].events.drop(e);
      fakeFileInputs.map(function (i, el) { return el.remove(); });
    })(jQuery);
JS;
  $this
    ->getSession()
    ->evaluateScript($drop);
}