function FileMoveTest::testNormal

Same name in this branch
  1. 8.9.x core/modules/image/tests/src/Functional/FileMoveTest.php \Drupal\Tests\image\Functional\FileMoveTest::testNormal()
Same name in other branches
  1. 7.x modules/simpletest/tests/file.test \FileMoveTest::testNormal()
  2. 9 core/modules/image/tests/src/Functional/FileMoveTest.php \Drupal\Tests\image\Functional\FileMoveTest::testNormal()
  3. 9 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest::testNormal()
  4. 10 core/modules/image/tests/src/Kernel/FileMoveTest.php \Drupal\Tests\image\Kernel\FileMoveTest::testNormal()
  5. 10 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest::testNormal()
  6. 11.x core/modules/image/tests/src/Kernel/FileMoveTest.php \Drupal\Tests\image\Kernel\FileMoveTest::testNormal()
  7. 11.x core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest::testNormal()

Move a normal file.

File

core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php, line 21

Class

FileMoveTest
Tests the unmanaged file move function.

Namespace

Drupal\KernelTests\Core\File

Code

public function testNormal() {
    // Create a file for testing
    $uri = $this->createUri();
    // Moving to a new name.
    $desired_filepath = 'public://' . $this->randomMachineName();
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $new_filepath = $file_system->move($uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
    $this->assertNotFalse($new_filepath, 'Move was successful.');
    $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
    $this->assertFileExists($new_filepath);
    $this->assertFileNotExists($uri);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // Moving with rename.
    $desired_filepath = 'public://' . $this->randomMachineName();
    $this->assertFileExists($new_filepath);
    $this->assertNotFalse(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
    $newer_filepath = $file_system->move($new_filepath, $desired_filepath, FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($newer_filepath, 'Move was successful.');
    $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
    $this->assertFileExists($newer_filepath);
    $this->assertFileNotExists($new_filepath);
    $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // TODO: test moving to a directory (rather than full directory/file path)
    // TODO: test creating and moving normal files (rather than streams)
}

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