You are here

protected function FileUrlWidgetTest::addFileUrlItem in File URL 8

Same name and namespace in other branches
  1. 2.0.x tests/src/FunctionalJavascript/FileUrlWidgetTest.php \Drupal\Tests\file_url\FunctionalJavascript\FileUrlWidgetTest::addFileUrlItem()

Selects a radio button option from a file URL field.

Parameters

string $field_name: The file URL field name.

string $file_mode: The radio button option value with the file mode ('upload', 'remote').

int|string $value: Either the index of the test file to upload or the remote URL.

bool $check_presence: (optional) After creating the item, check also its presence. Defaults to TRUE.

Throws

\Behat\Mink\Exception\ElementNotFoundException If the field doesn't exist.

1 call to FileUrlWidgetTest::addFileUrlItem()
FileUrlWidgetTest::testFileUrlWidget in tests/src/FunctionalJavascript/FileUrlWidgetTest.php
Tests the file URL widget.

File

tests/src/FunctionalJavascript/FileUrlWidgetTest.php, line 170

Class

FileUrlWidgetTest
Tests the file URL widget.

Namespace

Drupal\Tests\file_url\FunctionalJavascript

Code

protected function addFileUrlItem($field_name, $file_mode, $value, $check_presence = TRUE) {
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();

  // Narrow the search to the field's wrapper.
  $wrapper = $page
    ->find('xpath', "//div[@data-drupal-selector='edit-{$field_name}-wrapper']");
  if (!$wrapper) {
    throw new ElementNotFoundException($session, $field_name);
  }

  /** @var \Behat\Mink\Element\NodeElement $radio */
  $radio = $wrapper
    ->find('xpath', "//input[@type='radio' and @value='{$file_mode}']");
  if (!$radio || $radio
    ->getTagName() !== 'input' || $radio
    ->getAttribute('type') !== 'radio') {
    throw new ElementNotFoundException($session, $field_name);
  }

  // Select the file mode.
  $radio
    ->click();
  if ($file_mode === 'upload') {

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = $this->container
      ->get('file_system');
    $wrapper
      ->attachFileToField('Choose a file', $file_system
      ->realpath($this->files[$value]->uri));

    // Wait for ajax to finish the upload.
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
    if ($check_presence) {

      // Check that the uploaded file has been added to the field item list.
      $this
        ->assertSession()
        ->linkExists($this->files[$value]->filename);
    }
  }
  elseif ($file_mode === 'remote') {
    $wrapper
      ->fillField('Remote URL', $value);

    // Wait for ajax to finish the job.
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
    if ($check_presence) {

      // Check that the remote URL has been added to the field item list.
      $this
        ->assertSession()
        ->linkExists($value);
    }
  }
}