function FileRepositoryTest::testExistingRename

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

Tests writeData() when renaming around an existing file.

@covers ::writeData

File

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

Class

FileRepositoryTest
Tests the FileRepository.

Namespace

Drupal\Tests\file\Kernel

Code

public function testExistingRename() {
    // Setup a file to overwrite.
    $existing = $this->createFile();
    $contents = $this->randomMachineName();
    $result = $this->fileRepository
        ->writeData($contents, $existing->getFileUri());
    $this->assertNotFalse($result, 'File saved successfully.');
    $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($existing->getFilename(), $result->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
    $this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEquals('application/octet-stream', $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',
    ]);
    // Ensure that the existing file wasn't overwritten.
    $this->assertDifferentFile($existing, $result);
    $this->assertFileUnchanged($existing, File::load($existing->id()));
    // Verify that 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.