You are here

public function FileUrlWidgetTest::testFileUrlWidget in File URL 2.0.x

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

Tests the file URL widget.

File

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

Class

FileUrlWidgetTest
Tests the file URL widget.

Namespace

Drupal\Tests\file_url\FunctionalJavascript

Code

public function testFileUrlWidget() {

  // Add a new entity.
  $this
    ->drupalGet('/entity_test/add');
  $page = $this
    ->getSession()
    ->getPage();

  // Upload a file to the 'single' file URL field and save.
  $this
    ->addFileUrlItem('single', 'upload', 0);
  $page
    ->pressButton('Save');

  // Remove the file.
  $this
    ->removeFileUrlItem('single');

  // Check that the file mode selector is shown.
  $this
    ->assertSession()
    ->fieldExists('single[0][file_url_type]');

  // Add a remote URL.
  $url1 = $this
    ->randomUrl();
  $this
    ->addFileUrlItem('single', 'remote', $url1);
  $page
    ->pressButton('Save');

  // Check that the link is still there after saving the form.
  $this
    ->assertSession()
    ->linkExists($url1);

  // Upload two files to the 'multiple' file URL.
  $this
    ->addFileUrlItem('multiple', 'upload', 1);
  $this
    ->addFileUrlItem('multiple', 'upload', 2);

  // Append a remote URL.
  $url2 = $this
    ->randomUrl();
  $this
    ->addFileUrlItem('multiple', 'remote', $url2);

  // Check that file URL items are in the correct order.
  $this
    ->assertOrderInPageText([
    $this->files[1]->filename,
    $this->files[2]->filename,
    $url2,
  ]);

  // Swap the two items from top.
  $dragged = $this
    ->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[1]//a[@class='tabledrag-handle']")[0];
  $target = $this
    ->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[2]//a[@class='tabledrag-handle']")[0];
  $dragged
    ->dragTo($target);

  // Check that file URL items are in the correct order after reorder.
  $this
    ->assertOrderInPageText([
    $this->files[2]->filename,
    $this->files[1]->filename,
    $url2,
  ]);

  // Swap row 1 with row 2. The remote URL should be in the middle.
  $dragged = $this
    ->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[2]//a[@class='tabledrag-handle']")[0];
  $target = $this
    ->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[3]//a[@class='tabledrag-handle']")[0];
  $dragged
    ->dragTo($target);

  // Check that file URL items are in the correct order after reorder.
  $this
    ->assertOrderInPageText([
    $this->files[2]->filename,
    $url2,
    $this->files[1]->filename,
  ]);

  // Check that the order is preserved after save.
  $page
    ->pressButton('Save');
  $this
    ->assertOrderInPageText([
    $this->files[2]->filename,
    $url2,
    $this->files[1]->filename,
  ]);

  // Append an additional remote URL.
  $url3 = $this
    ->randomUrl();
  $this
    ->addFileUrlItem('multiple', 'remote', $url3);
  $this
    ->assertOrderInPageText([
    $this->files[2]->filename,
    $url2,
    $this->files[1]->filename,
    $url3,
  ]);

  // Check that the order is preserved after save.
  $page
    ->pressButton('Save');
  $this
    ->assertOrderInPageText([
    $this->files[2]->filename,
    $url2,
    $this->files[1]->filename,
    $url3,
  ]);

  // Test handling of invalid remote URLs.
  $this
    ->drupalGet('/entity_test/add');
  $url4 = $this
    ->randomUrl();

  // Add a valid URL.
  $this
    ->addFileUrlItem('multiple', 'remote', $url4);

  // Add an invalid URL.
  $url5 = 'invalid url';
  $this
    ->addFileUrlItem('multiple', 'remote', $url5, FALSE);

  // Check that the proper validation error has been displayed.
  $this
    ->assertSession()
    ->pageTextContains("The URL {$url5} is not valid.");
}