You are here

function FileUnmanagedMoveTest::testNormal in SimpleTest 7

Move a normal file.

File

tests/file.test, line 1076
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileUnmanagedMoveTest
Unmanaged move related tests.

Code

function testNormal() {

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

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

  // Moving with rename.
  $desired_filepath = 'public://' . $this
    ->randomName();
  $this
    ->assertTrue(file_exists($new_filepath), t('File exists before moving.'));
  $this
    ->assertTrue(file_put_contents($desired_filepath, ' '), t('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, t('Move was successful.'));
  $this
    ->assertNotEqual($newer_filepath, $desired_filepath, t('Returned expected filepath.'));
  $this
    ->assertTrue(file_exists($newer_filepath), t('File exists at the new location.'));
  $this
    ->assertFalse(file_exists($new_filepath), t('No file remains at the old location.'));
  $this
    ->assertFilePermissions($newer_filepath, variable_get('file_chmod_file', 0664));

  // TODO: test moving to a directory (rather than full directory/file path)
  // TODO: test creating and moving normal files (rather than streams)
}