function SaveUploadTest::testInvalidUtf8FilenameUpload

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testInvalidUtf8FilenameUpload()
  2. 8.9.x core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testInvalidUtf8FilenameUpload()
  3. 11.x 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 694

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testInvalidUtf8FilenameUpload() : void {
    $this->drupalGet('file-test/upload');
    // Filename containing invalid UTF-8.
    $filename = "x\xc0xx.gif";
    $page = $this->getSession()
        ->getPage();
    $data = [
        'multipart' => [
            [
                'name' => 'file_test_replace',
                'contents' => FileExists::Rename->name,
            ],
            [
                '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);
}

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