function SaveUploadFormTest::testHandleFileMunge

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testHandleFileMunge()
  2. 8.9.x core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testHandleFileMunge()
  3. 11.x core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testHandleFileMunge()

Tests file munge handling.

File

core/modules/file/tests/src/Functional/SaveUploadFormTest.php, line 296

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testHandleFileMunge() : void {
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  // Ensure insecure uploads are disabled for this test.
  $this->config('system.file')
    ->set('allow_insecure_uploads', 0)
    ->save();
  $original_uri = $this->image
    ->getFileUri();
  /** @var \Drupal\file\FileRepositoryInterface $file_repository */
  $file_repository = \Drupal::service('file.repository');
  $this->image = $file_repository->move($this->image, $original_uri . '.foo.' . $this->imageExtension);
  // Reset the hook counters to get rid of the 'move' we just called.
  file_test_reset();
  $extensions = $this->imageExtension;
  $edit = [
    'files[file_test_upload][]' => $file_system->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $munged_filename = $this->image
    ->getFilename();
  $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
  $munged_filename .= '_.' . $this->imageExtension;
  $this->drupalGet('file-test/save_upload_from_form_test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextContains('For security reasons, your upload has been renamed');
  $this->assertSession()
    ->pageTextContains("File name is {$munged_filename}");
  $this->assertSession()
    ->pageTextContains("You WIN!");
  // Check that the correct hooks were called.
  $this->assertFileHooksCalled([
    'validate',
    'insert',
  ]);
  // Test with uppercase extensions.
  $this->image = $file_repository->move($this->image, $original_uri . '.foo2.' . $this->imageExtension);
  // Reset the hook counters.
  file_test_reset();
  $extensions = $this->imageExtension;
  $edit = [
    'files[file_test_upload][]' => $file_system->realpath($this->image
      ->getFileUri()),
    'extensions' => mb_strtoupper($extensions),
  ];
  $munged_filename = $this->image
    ->getFilename();
  $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
  $munged_filename .= '_.' . $this->imageExtension;
  $this->drupalGet('file-test/save_upload_from_form_test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextContains('For security reasons, your upload has been renamed');
  $this->assertSession()
    ->pageTextContains("File name is {$munged_filename}");
  $this->assertSession()
    ->pageTextContains("You WIN!");
  // Check that the correct hooks were called.
  $this->assertFileHooksCalled([
    'validate',
    'insert',
  ]);
  // Ensure we don't munge files if we're allowing any extension.
  // Reset the hook counters.
  file_test_reset();
  // Ensure we don't munge files if we're allowing any extension.
  $edit = [
    'files[file_test_upload][]' => $file_system->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_array',
  ];
  $this->drupalGet('file-test/save_upload_from_form_test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextNotContains('For security reasons, your upload has been renamed');
  $this->assertSession()
    ->pageTextContains("File name is {$this->image->getFilename()}");
  $this->assertSession()
    ->pageTextContains("You WIN!");
  // Check that the correct hooks were called.
  $this->assertFileHooksCalled([
    'validate',
    'insert',
  ]);
  // Ensure that setting $validators['file_validate_extensions'] = ['']
  // rejects all files.
  // Reset the hook counters.
  file_test_reset();
  $edit = [
    'files[file_test_upload][]' => $file_system->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_string',
  ];
  $this->drupalGet('file-test/save_upload_from_form_test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextNotContains('For security reasons, your upload has been renamed');
  $this->assertSession()
    ->pageTextContains("Epic upload FAIL!");
  // Check that the correct hooks were called.
  $this->assertFileHooksCalled([
    'validate',
  ]);
}

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