function FileFieldWidgetTest::testUploadingMoreFilesThanAllowed

Same name in other branches
  1. 10 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testUploadingMoreFilesThanAllowed()
  2. 11.x core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testUploadingMoreFilesThanAllowed()

Tests uploading more files than 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 testUploadingMoreFilesThanAllowed() {
    $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.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.