You are here

function SaveUploadTest::testHandleFileMunge in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Tests/SaveUploadTest.php \Drupal\file\Tests\SaveUploadTest::testHandleFileMunge()

Test file munge handling.

File

core/modules/file/src/Tests/SaveUploadTest.php, line 236
Contains \Drupal\file\Tests\SaveUploadTest.

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\file\Tests

Code

function testHandleFileMunge() {

  // Ensure insecure uploads are disabled for this test.
  $this
    ->config('system.file')
    ->set('allow_insecure_uploads', 0)
    ->save();
  $this->image = file_move($this->image, $this->image
    ->getFileUri() . '.foo.' . $this->imageExtension);

  // Reset the hook counters to get rid of the 'move' we just called.
  file_test_reset();
  $extensions = $this->imageExtension;
  $edit = array(
    'files[file_test_upload]' => drupal_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
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertResponse(200, 'Received a 200 response for posted test file.');
  $this
    ->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('File name is @filename', array(
    '@filename' => $munged_filename,
  )), 'File was successfully munged.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

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

  // Ensure we don't munge files if we're allowing any extension.
  // Reset the hook counters.
  file_test_reset();
  $edit = array(
    'files[file_test_upload]' => drupal_realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => TRUE,
  );
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertResponse(200, 'Received a 200 response for posted test file.');
  $this
    ->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
  $this
    ->assertRaw(t('File name is @filename', array(
    '@filename' => $this->image
      ->getFilename(),
  )), 'File was not munged when allowing any extension.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

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