function MoveTest::testNormal

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

Move a normal file.

@covers ::move

File

core/modules/file/tests/src/Kernel/MoveTest.php, line 43

Class

MoveTest
Tests the file move function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testNormal() : void {
    $contents = $this->randomMachineName(10);
    $source = $this->createFile(NULL, $contents);
    $desired_filepath = 'public://' . $this->randomMachineName();
    // Clone the object so we don't have to worry about the function changing
    // our reference copy.
    $result = $this->fileRepository
        ->move(clone $source, $desired_filepath, FileExists::Error);
    // Check the return status and that the contents changed.
    $this->assertNotFalse($result, 'File moved successfully.');
    $this->assertFileDoesNotExist($source->getFileUri());
    $this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        'move',
        'load',
        'update',
    ]);
    // Make sure we got the same file back.
    $this->assertEquals($source->id(), $result->id(), "Source file ID {$source->id()} should be unchanged after move.");
    // Reload the file from the database and check that the changes were
    // actually saved.
    $loaded_file = File::load($result->id());
    $this->assertNotEmpty($loaded_file, 'File can be loaded from the database.');
    $this->assertFileUnchanged($result, $loaded_file);
}

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