function SaveDataTest::testWithoutFilename

Test the file_save_data() function when no filename is provided.

File

core/modules/file/tests/src/Kernel/SaveDataTest.php, line 19

Class

SaveDataTest
Tests the <a href="/api/drupal/core%21modules%21file%21file.module/function/file_save_data/8.9.x" title="Saves a file to the specified destination and creates a database entry." class="local">file_save_data</a>() function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testWithoutFilename() {
    $contents = $this->randomMachineName(8);
    $result = file_save_data($contents);
    $this->assertNotFalse($result, 'Unnamed file saved correctly.');
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
    $this->assertEqual(\Drupal::config('system.file')->get('default_scheme'), $stream_wrapper_manager::getScheme($result->getFileUri()), "File was placed in Drupal's files directory.");
    $this->assertEqual($result->getFilename(), \Drupal::service('file_system')->basename($result->getFileUri()), "Filename was set to the file's basename.");
    $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEqual($result->getMimeType(), 'application/octet-stream', '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.