You are here

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

Test a basic file upload with File (Field) Paths.

File

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

Class

FileFieldPathsGeneralTest
Test general functionality.

Namespace

Drupal\Tests\filefield_paths\Functional

Code

public function testUploadFile() {
  $session = $this
    ->assertSession();
  $file_system = \Drupal::service('file_system');

  // Create a File field with 'node/[node:nid]' as the File path and
  // '[node:nid].[file:ffp-extension-original]' as the File name.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $third_party_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  $third_party_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
  $this
    ->createFileField($field_name, 'node', $this->contentType, [], [], $third_party_settings);

  // Create a node with a test file.

  /** @var \Drupal\file\Entity\File $test_file */
  $test_file = $this
    ->getTestFile('text');
  $this
    ->drupalGet("node/add/{$this->contentType}");
  $edit['title[0][value]'] = $this
    ->randomMachineName();
  $edit["files[{$field_name}_0]"] = $file_system
    ->realpath($test_file
    ->getFileUri());
  $this
    ->submitForm($edit, 'Upload');

  // Ensure that the file was put into the Temporary file location.
  $config = $this
    ->config('filefield_paths.settings');
  $session
    ->responseContains(file_create_url("{$config->get('temp_location')}/{$test_file->getFilename()}"), 'File has been uploaded to the temporary file location.');

  // Save the node.
  $this
    ->submitForm([], 'Save');

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

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