Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / File / UnmanagedCopyTest.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 copy function.
10  *
11  * @group File
12  */
13 class UnmanagedCopyTest extends FileTestBase {
14   /**
15    * Copy a normal file.
16    */
17   public function testNormal() {
18     // Create a file for testing
19     $uri = $this->createUri();
20
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));
29
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));
39
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)
42   }
43
44   /**
45    * Copy a non-existent file.
46    */
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.');
53   }
54
55   /**
56    * Copy a file onto itself.
57    */
58   public function testOverwriteSelf() {
59     // Create a file for testing
60     $uri = $this->createUri();
61
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));
69
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.');
74
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.');
79
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));
87   }
88
89 }