You are here

public function SaveUploadTest::testInvalidUtf8FilenameUpload in Drupal 9

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

Tests that filenames containing invalid UTF-8 are rejected.

File

core/modules/file/tests/src/Functional/SaveUploadTest.php, line 677

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testInvalidUtf8FilenameUpload() {
  $this
    ->drupalGet('file-test/upload');

  // Filename containing invalid UTF-8.
  $filename = "";
  $page = $this
    ->getSession()
    ->getPage();
  $data = [
    'multipart' => [
      [
        'name' => 'file_test_replace',
        'contents' => FileSystemInterface::EXISTS_RENAME,
      ],
      [
        'name' => 'form_id',
        'contents' => '_file_test_form',
      ],
      [
        'name' => 'form_build_id',
        'contents' => $page
          ->find('hidden_field_selector', [
          'hidden_field',
          'form_build_id',
        ])
          ->getAttribute('value'),
      ],
      [
        'name' => 'form_token',
        'contents' => $page
          ->find('hidden_field_selector', [
          'hidden_field',
          'form_token',
        ])
          ->getAttribute('value'),
      ],
      [
        'name' => 'op',
        'contents' => 'Submit',
      ],
      [
        'name' => 'files[file_test_upload]',
        'contents' => 'Test content',
        'filename' => $filename,
      ],
    ],
    'cookies' => $this
      ->getSessionCookies(),
    'http_errors' => FALSE,
  ];
  $this
    ->assertFileDoesNotExist('temporary://' . $filename);

  // Use Guzzle's HTTP client directly so we can POST files without having to
  // write them to disk. Not all filesystem support writing files with invalid
  // UTF-8 filenames.
  $response = $this
    ->getHttpClient()
    ->request('POST', Url::fromUri('base:file-test/upload')
    ->setAbsolute()
    ->toString(), $data);
  $content = (string) $response
    ->getBody();
  $this
    ->htmlOutput($content);
  $error_text = new FormattableMarkup('The file %filename could not be uploaded because the name is invalid.', [
    '%filename' => $filename,
  ]);
  $this
    ->assertStringContainsString((string) $error_text, $content);
  $this
    ->assertStringContainsString('Epic upload FAIL!', $content);
  $this
    ->assertFileDoesNotExist('temporary://' . $filename);
}