You are here

public function SaveUploadFormTest::testHandleExtension in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testHandleExtension()
  2. 9 core/modules/file/tests/src/Functional/SaveUploadFormTest.php \Drupal\Tests\file\Functional\SaveUploadFormTest::testHandleExtension()

Tests extension handling.

File

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

Class

SaveUploadFormTest
Tests the _file_save_upload_from_form() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testHandleExtension() {

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');

  // The file being tested is a .gif which is in the default safe list
  // of extensions to allow when the extension validator isn't used. This is
  // implicitly tested at the testNormal() test. Here we tell
  // _file_save_upload_from_form() to only allow ".foo".
  $extensions = 'foo';
  $edit = [
    'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
    'files[file_test_upload][]' => $file_system
      ->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $this
    ->drupalGet('file-test/save_upload_from_form_test');
  $this
    ->submitForm($edit, 'Submit');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains('Only files with the following extensions are allowed: <em class="placeholder">' . $extensions . '</em>');
  $this
    ->assertSession()
    ->pageTextContains("Epic upload FAIL!");

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
  ]);

  // Reset the hook counters.
  file_test_reset();
  $extensions = 'foo ' . $this->imageExtension;

  // Now tell _file_save_upload_from_form() to allow the extension of our test image.
  $edit = [
    'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
    'files[file_test_upload][]' => $file_system
      ->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $this
    ->drupalGet('file-test/save_upload_from_form_test');
  $this
    ->submitForm($edit, 'Submit');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('Only files with the following extensions are allowed:');
  $this
    ->assertSession()
    ->pageTextContains("You WIN!");

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'load',
    'update',
  ]);

  // Reset the hook counters.
  file_test_reset();

  // Now tell _file_save_upload_from_form() to allow any extension.
  $edit = [
    'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
    '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('Only files with the following extensions are allowed:');
  $this
    ->assertSession()
    ->pageTextContains("You WIN!");

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'load',
    'update',
  ]);
}