You are here

protected function WebformElementManagedFileTest::checkFileUpload in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/Element/WebformElementManagedFileTest.php \Drupal\Tests\webform\Functional\Element\WebformElementManagedFileTest::checkFileUpload()

Check file upload.

Parameters

string $type: The type of file upload which can be either single or multiple.

object $first_file: The first file to be uploaded.

object $second_file: The second file that replaces the first file.

1 call to WebformElementManagedFileTest::checkFileUpload()
WebformElementManagedFileTest::testFileUpload in tests/src/Functional/Element/WebformElementManagedFileTest.php
Test single and multiple file upload.

File

tests/src/Functional/Element/WebformElementManagedFileTest.php, line 301

Class

WebformElementManagedFileTest
Test for webform element managed file handling.

Namespace

Drupal\Tests\webform\Functional\Element

Code

protected function checkFileUpload($type, $first_file, $second_file) {
  $key = 'managed_file_' . $type;
  $parameter_name = $type === 'multiple' ? "files[{$key}][]" : "files[{$key}]";
  $edit = [
    $parameter_name => \Drupal::service('file_system')
      ->realpath($first_file->uri),
  ];
  $sid = $this
    ->postSubmission($this->webform, $edit);

  /** @var \Drupal\webform\WebformSubmissionInterface $submission */
  $submission = WebformSubmission::load($sid);

  /** @var \Drupal\file\FileInterface $file */
  $fid = $this
    ->getLastFileId();
  $file = File::load($fid);

  // Check that test file was uploaded to the current submission.
  $second = $type === 'multiple' ? [
    $fid,
  ] : $fid;
  $this
    ->assertEqual($submission
    ->getElementData($key), $second, 'Test file was upload to the current submission');

  // Check test file file usage.
  $this
    ->assertIdentical([
    'webform' => [
      'webform_submission' => [
        $sid => '1',
      ],
    ],
  ], $this->fileUsage
    ->listUsage($file), 'The file has 1 usage.');

  // Check test file uploaded file path.
  $this
    ->assertEqual($file
    ->getFileUri(), 'private://webform/test_element_managed_file/' . $sid . '/' . $first_file->filename);

  // Check that test file exists.
  $this
    ->assertFileExists($file
    ->getFileUri());

  // Login admin user.
  $this
    ->drupalLogin($this->adminSubmissionUser);

  // Check managed file formatting.
  $this
    ->drupalGet('/admin/structure/webform/manage/test_element_managed_file/submission/' . $sid);
  if ($type === 'multiple') {
    $this
      ->assertRaw('<label>managed_file_multiple</label>');
    $this
      ->assertRaw('<ul>');
  }

  // @todo Remove once Drupal 9.1.x is only supported.
  if (floatval(\Drupal::VERSION) >= 9.1) {
    $this
      ->assertRaw('<span class="file file--mime-text-plain file--text"><a href="' . file_create_url($file
      ->getFileUri()) . '" type="text/plain">' . $file
      ->getFilename() . '</a></span>');
  }
  else {
    $this
      ->assertRaw('<span class="file file--mime-text-plain file--text"><a href="' . file_create_url($file
      ->getFileUri()) . '" type="text/plain; length=' . $file
      ->getSize() . '">' . $file
      ->getFilename() . '</a></span>');
  }

  // Remove the uploaded file.
  if ($type === 'multiple') {
    $edit = [
      'managed_file_multiple[file_' . $fid . '][selected]' => TRUE,
    ];
    $submit = 'Remove selected';
  }
  else {
    $edit = [];
    $submit = 'Remove';
  }
  $this
    ->drupalPostForm('/admin/structure/webform/manage/test_element_managed_file/submission/' . $sid . '/edit', $edit, $submit);

  // Upload new file.
  $edit = [
    $parameter_name => \Drupal::service('file_system')
      ->realpath($second_file->uri),
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Upload');

  // Submit the new file.
  $this
    ->drupalPostForm(NULL, [], 'Save');

  /** @var \Drupal\file\FileInterface $test_file_0 */
  $new_fid = $this
    ->getLastFileId();
  $new_file = File::load($new_fid);
  \Drupal::entityTypeManager()
    ->getStorage('webform_submission')
    ->resetCache();
  $submission = WebformSubmission::load($sid);

  // Check that test new file was uploaded to the current submission.
  $second = $type === 'multiple' ? [
    $new_fid,
  ] : $new_fid;
  $this
    ->assertEqual($submission
    ->getElementData($key), $second, 'Test new file was upload to the current submission');

  // Check that test file was deleted from the disk and database.
  $this
    ->assertFileNotExists($file
    ->getFileUri(), 'Test file deleted from disk');
  $this
    ->assertEqual(0, \Drupal::database()
    ->query('SELECT COUNT(fid) AS total FROM {file_managed} WHERE fid = :fid', [
    ':fid' => $fid,
  ])
    ->fetchField(), 'Test file 0 deleted from database');
  $this
    ->assertEqual(0, \Drupal::database()
    ->query('SELECT COUNT(fid) AS total FROM {file_usage} WHERE fid = :fid', [
    ':fid' => $fid,
  ])
    ->fetchField(), 'Test file 0 deleted from database');

  // Check test file 1 file usage.
  $this
    ->assertIdentical([
    'webform' => [
      'webform_submission' => [
        $sid => '1',
      ],
    ],
  ], $this->fileUsage
    ->listUsage($new_file), 'The new file has 1 usage.');

  // Check that file directory was create.
  $this
    ->assertFileExists('private://webform/test_element_managed_file/' . $sid . '/');

  // Delete the submission.
  $submission
    ->delete();

  // Check that test file 1 was deleted from the disk and database.
  $this
    ->assertFileNotExists($new_file
    ->getFileUri(), 'Test new file deleted from disk');
  $this
    ->assertEqual(0, \Drupal::database()
    ->query('SELECT COUNT(fid) AS total FROM {file_managed} WHERE fid = :fid', [
    ':fid' => $new_fid,
  ])
    ->fetchField(), 'Test new file deleted from database');

  // Check that empty file directory was deleted.
  $this
    ->assertFileNotExists('private://webform/test_element_managed_file/' . $sid . '/');
}