3 namespace Drupal\KernelTests\Core\File;
5 use Drupal\Core\Site\Settings;
6 use Drupal\Core\File\FileSystem;
9 * Tests the unmanaged file copy function.
13 class UnmanagedCopyTest extends FileTestBase {
17 public function testNormal() {
18 // Create a file for testing
19 $uri = $this->createUri();
21 // Copying to a new name.
22 $desired_filepath = 'public://' . $this->randomMachineName();
23 $new_filepath = file_unmanaged_copy($uri, $desired_filepath, FILE_EXISTS_ERROR);
24 $this->assertTrue($new_filepath, 'Copy was successful.');
25 $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
26 $this->assertTrue(file_exists($uri), 'Original file remains.');
27 $this->assertTrue(file_exists($new_filepath), 'New file exists.');
28 $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
30 // Copying with rename.
31 $desired_filepath = 'public://' . $this->randomMachineName();
32 $this->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
33 $newer_filepath = file_unmanaged_copy($uri, $desired_filepath, FILE_EXISTS_RENAME);
34 $this->assertTrue($newer_filepath, 'Copy was successful.');
35 $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
36 $this->assertTrue(file_exists($uri), 'Original file remains.');
37 $this->assertTrue(file_exists($newer_filepath), 'New file exists.');
38 $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
40 // TODO: test copying to a directory (rather than full directory/file path)
41 // TODO: test copying normal files using normal paths (rather than only streams)
45 * Copy a non-existent file.
47 public function testNonExistent() {
48 // Copy non-existent file
49 $desired_filepath = $this->randomMachineName();
50 $this->assertFalse(file_exists($desired_filepath), "Randomly named file doesn't exist.");
51 $new_filepath = file_unmanaged_copy($desired_filepath, $this->randomMachineName());
52 $this->assertFalse($new_filepath, 'Copying a missing file fails.');
56 * Copy a file onto itself.
58 public function testOverwriteSelf() {
59 // Create a file for testing
60 $uri = $this->createUri();
62 // Copy the file onto itself with renaming works.
63 $new_filepath = file_unmanaged_copy($uri, $uri, FILE_EXISTS_RENAME);
64 $this->assertTrue($new_filepath, 'Copying onto itself with renaming works.');
65 $this->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
66 $this->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.');
67 $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
68 $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
70 // Copy the file onto itself without renaming fails.
71 $new_filepath = file_unmanaged_copy($uri, $uri, FILE_EXISTS_ERROR);
72 $this->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
73 $this->assertTrue(file_exists($uri), 'File exists after copying onto itself.');
75 // Copy the file into same directory without renaming fails.
76 $new_filepath = file_unmanaged_copy($uri, drupal_dirname($uri), FILE_EXISTS_ERROR);
77 $this->assertFalse($new_filepath, 'Copying onto itself fails.');
78 $this->assertTrue(file_exists($uri), 'File exists after copying onto itself.');
80 // Copy the file into same directory with renaming works.
81 $new_filepath = file_unmanaged_copy($uri, drupal_dirname($uri), FILE_EXISTS_RENAME);
82 $this->assertTrue($new_filepath, 'Copying into same directory works.');
83 $this->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
84 $this->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.');
85 $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
86 $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));