3 namespace Drupal\KernelTests\Core\File;
5 use Drupal\Core\Site\Settings;
6 use Drupal\Core\File\FileSystem;
9 * Tests the unmanaged file move function.
13 class UnmanagedMoveTest extends FileTestBase {
17 public function testNormal() {
18 // Create a file for testing
19 $uri = $this->createUri();
21 // Moving to a new name.
22 $desired_filepath = 'public://' . $this->randomMachineName();
23 $new_filepath = file_unmanaged_move($uri, $desired_filepath, FILE_EXISTS_ERROR);
24 $this->assertTrue($new_filepath, 'Move was successful.');
25 $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
26 $this->assertTrue(file_exists($new_filepath), 'File exists at the new location.');
27 $this->assertFalse(file_exists($uri), 'No file remains at the old location.');
28 $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
30 // Moving with rename.
31 $desired_filepath = 'public://' . $this->randomMachineName();
32 $this->assertTrue(file_exists($new_filepath), 'File exists before moving.');
33 $this->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
34 $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME);
35 $this->assertTrue($newer_filepath, 'Move was successful.');
36 $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
37 $this->assertTrue(file_exists($newer_filepath), 'File exists at the new location.');
38 $this->assertFalse(file_exists($new_filepath), 'No file remains at the old location.');
39 $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
41 // TODO: test moving to a directory (rather than full directory/file path)
42 // TODO: test creating and moving normal files (rather than streams)
46 * Try to move a missing file.
48 public function testMissing() {
49 // Move non-existent file.
50 $new_filepath = file_unmanaged_move($this->randomMachineName(), $this->randomMachineName());
51 $this->assertFalse($new_filepath, 'Moving a missing file fails.');
55 * Try to move a file onto itself.
57 public function testOverwriteSelf() {
58 // Create a file for testing.
59 $uri = $this->createUri();
61 // Move the file onto itself without renaming shouldn't make changes.
62 $new_filepath = file_unmanaged_move($uri, $uri, FILE_EXISTS_REPLACE);
63 $this->assertFalse($new_filepath, 'Moving onto itself without renaming fails.');
64 $this->assertTrue(file_exists($uri), 'File exists after moving onto itself.');
66 // Move the file onto itself with renaming will result in a new filename.
67 $new_filepath = file_unmanaged_move($uri, $uri, FILE_EXISTS_RENAME);
68 $this->assertTrue($new_filepath, 'Moving onto itself with renaming works.');
69 $this->assertFalse(file_exists($uri), 'Original file has been removed.');
70 $this->assertTrue(file_exists($new_filepath), 'File exists after moving onto itself.');