You are here

public function FileFieldPathsGeneralTest::testUploadFileMultivalue in File (Field) Paths 8

Tests a multivalue file upload with File (Field) Paths.

File

tests/src/Functional/FileFieldPathsGeneralTest.php, line 102

Class

FileFieldPathsGeneralTest
Test general functionality.

Namespace

Drupal\Tests\filefield_paths\Functional

Code

public function testUploadFileMultivalue() {
  $file_system = \Drupal::service('file_system');

  // Create a multivalue File field with 'node/[node:nid]' as the File path
  // and '[file:fid].txt' as the File name.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $storage_settings['cardinality'] = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED;
  $third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  $third_party_settings['filefield_paths']['file_name']['value'] = '[file:fid].txt';
  $this
    ->createFileField($field_name, 'node', $this->contentType, $storage_settings, [], $third_party_settings);

  // Create a node with three (3) test files.
  $text_files = $this
    ->drupalGetTestFiles('text');
  $this
    ->drupalGet("node/add/{$this->contentType}");
  $this
    ->submitForm([
    "files[{$field_name}_0][]" => $file_system
      ->realpath($text_files[0]->uri),
  ], 'Upload');
  $this
    ->submitForm([
    "files[{$field_name}_1][]" => $file_system
      ->realpath($text_files[1]->uri),
  ], 'Upload');
  $edit = [
    'title[0][value]' => $this
      ->randomMachineName(),
    "files[{$field_name}_2][]" => $file_system
      ->realpath($text_files[1]->uri),
  ];
  $this
    ->submitForm($edit, 'Save');

  // Get created Node ID.
  $matches = [];
  preg_match('/node\\/([0-9]+)/', $this
    ->getUrl(), $matches);
  $nid = $matches[1];
  $session = $this
    ->assertSession();

  // Ensure that the File path has been processed correctly.
  $session
    ->responseContains("{$this->publicFilesDirectory}/node/{$nid}/1.txt", 'The first File path has been processed correctly.');
  $session
    ->responseContains("{$this->publicFilesDirectory}/node/{$nid}/2.txt", 'The second File path has been processed correctly.');
  $session
    ->responseContains("{$this->publicFilesDirectory}/node/{$nid}/3.txt", 'The third File path has been processed correctly.');
}