8619a1507d07eb68add73accf829f3218f42f19b
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / File / UnmanagedMoveTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\File;
4
5 use Drupal\Core\Site\Settings;
6 use Drupal\Core\File\FileSystem;
7
8 /**
9  * Tests the unmanaged file move function.
10  *
11  * @group File
12  */
13 class UnmanagedMoveTest extends FileTestBase {
14   /**
15    * Move a normal file.
16    */
17   public function testNormal() {
18     // Create a file for testing
19     $uri = $this->createUri();
20
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));
29
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));
40
41     // TODO: test moving to a directory (rather than full directory/file path)
42     // TODO: test creating and moving normal files (rather than streams)
43   }
44
45   /**
46    * Try to move a missing file.
47    */
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.');
52   }
53
54   /**
55    * Try to move a file onto itself.
56    */
57   public function testOverwriteSelf() {
58     // Create a file for testing.
59     $uri = $this->createUri();
60
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.');
65
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.');
71   }
72
73 }