function FileRepositoryTest::testWithFilename

Same name in other branches
  1. 9 core/modules/file/tests/src/Kernel/FileRepositoryTest.php \Drupal\Tests\file\Kernel\FileRepositoryTest::testWithFilename()
  2. 11.x core/modules/file/tests/src/Kernel/FileRepositoryTest.php \Drupal\Tests\file\Kernel\FileRepositoryTest::testWithFilename()

Tests the writeData() method.

@covers ::writeData

File

core/modules/file/tests/src/Kernel/FileRepositoryTest.php, line 52

Class

FileRepositoryTest
Tests the FileRepository.

Namespace

Drupal\Tests\file\Kernel

Code

public function testWithFilename() : void {
    $contents = $this->randomMachineName();
    // Using filename with non-latin characters.
    // cSpell:disable-next-line
    $filename = 'Текстовый файл.txt';
    $result = $this->fileRepository
        ->writeData($contents, 'public://' . $filename);
    $this->assertNotFalse($result, 'Unnamed file saved correctly.');
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
    $this->assertEquals('public', $stream_wrapper_manager::getScheme($result->getFileUri()), "File was placed in Drupal's files directory.");
    $this->assertEquals($filename, \Drupal::service('file_system')->basename($result->getFileUri()), 'File was named correctly.');
    $this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEquals('text/plain', $result->getMimeType(), 'A MIME type was set.');
    $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'insert',
    ]);
    // Verify that what was returned is what's in the database.
    $this->assertFileUnchanged($result, File::load($result->id()));
}

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