You are here

public function FileFieldWidgetTest::testUploadingMoreFilesThenAllowed in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testUploadingMoreFilesThenAllowed()

Tests uploading more files then allowed at once.

File

core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php, line 189

Class

FileFieldWidgetTest
Tests the file field widget, single and multi-valued, using AJAX upload.

Namespace

Drupal\Tests\file\FunctionalJavascript

Code

public function testUploadingMoreFilesThenAllowed() {
  $type_name = 'article';
  $field_name = 'test_file_field_1';
  $cardinality = 2;
  $this
    ->createFileField($field_name, 'node', $type_name, [
    'cardinality' => $cardinality,
  ]);
  $web_driver = $this
    ->getSession()
    ->getDriver();
  $file_system = \Drupal::service('file_system');
  $files = array_slice($this
    ->getTestFiles('text'), 0, 3);
  $real_paths = [];
  foreach ($files as $file) {
    $real_paths[] = $file_system
      ->realpath($file->uri);
  }
  $remote_paths = [];
  foreach ($real_paths as $path) {
    $remote_paths[] = $web_driver
      ->uploadFileAndGetRemoteFilePath($path);
  }

  // Tests that uploading multiple remote files works with remote path.
  $this
    ->drupalGet("node/add/{$type_name}");
  $multiple_field = $this
    ->getSession()
    ->getPage()
    ->findField('files[test_file_field_1_0][]');
  $multiple_field
    ->setValue(implode("\n", $remote_paths));
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains("Field {$field_name} can only hold {$cardinality} values but there were 3 uploaded. The following files have been omitted as a result: text-2.txt.");
}