You are here

function UnmanagedMoveTest::testNormal in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/File/UnmanagedMoveTest.php \Drupal\system\Tests\File\UnmanagedMoveTest::testNormal()

Move a normal file.

File

core/modules/system/src/Tests/File/UnmanagedMoveTest.php, line 22
Contains \Drupal\system\Tests\File\UnmanagedMoveTest.

Class

UnmanagedMoveTest
Tests the unmanaged file move function.

Namespace

Drupal\system\Tests\File

Code

function testNormal() {

  // Create a file for testing
  $uri = $this
    ->createUri();

  // Moving to a new name.
  $desired_filepath = 'public://' . $this
    ->randomMachineName();
  $new_filepath = file_unmanaged_move($uri, $desired_filepath, FILE_EXISTS_ERROR);
  $this
    ->assertTrue($new_filepath, 'Move was successful.');
  $this
    ->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
  $this
    ->assertTrue(file_exists($new_filepath), 'File exists at the new location.');
  $this
    ->assertFalse(file_exists($uri), 'No file remains at the old location.');
  $this
    ->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));

  // Moving with rename.
  $desired_filepath = 'public://' . $this
    ->randomMachineName();
  $this
    ->assertTrue(file_exists($new_filepath), 'File exists before moving.');
  $this
    ->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
  $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME);
  $this
    ->assertTrue($newer_filepath, 'Move was successful.');
  $this
    ->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
  $this
    ->assertTrue(file_exists($newer_filepath), 'File exists at the new location.');
  $this
    ->assertFalse(file_exists($new_filepath), 'No file remains at the old location.');
  $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)
}