Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / CopyTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the file copy function.
9  *
10  * @group file
11  */
12 class CopyTest extends FileManagedUnitTestBase {
13
14   /**
15    * Test file copying in the normal, base case.
16    */
17   public function testNormal() {
18     $contents = $this->randomMachineName(10);
19     $source = $this->createFile(NULL, $contents);
20     $desired_uri = 'public://' . $this->randomMachineName();
21
22     // Clone the object so we don't have to worry about the function changing
23     // our reference copy.
24     $result = file_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
25
26     // Check the return status and that the contents changed.
27     $this->assertTrue($result, 'File copied successfully.');
28     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
29
30     // Check that the correct hooks were called.
31     $this->assertFileHooksCalled(['copy', 'insert']);
32
33     $this->assertDifferentFile($source, $result);
34     $this->assertEqual($result->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
35     $this->assertTrue(file_exists($source->getFileUri()), 'The original file still exists.');
36     $this->assertTrue(file_exists($result->getFileUri()), 'The copied file exists.');
37
38     // Reload the file from the database and check that the changes were
39     // actually saved.
40     $this->assertFileUnchanged($result, File::load($result->id()));
41   }
42
43   /**
44    * Test renaming when copying over a file that already exists.
45    */
46   public function testExistingRename() {
47     // Setup a file to overwrite.
48     $contents = $this->randomMachineName(10);
49     $source = $this->createFile(NULL, $contents);
50     $target = $this->createFile();
51     $this->assertDifferentFile($source, $target);
52
53     // Clone the object so we don't have to worry about the function changing
54     // our reference copy.
55     $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
56
57     // Check the return status and that the contents changed.
58     $this->assertTrue($result, 'File copied successfully.');
59     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
60     $this->assertNotEqual($result->getFileUri(), $source->getFileUri(), 'Returned file path has changed from the original.');
61
62     // Check that the correct hooks were called.
63     $this->assertFileHooksCalled(['copy', 'insert']);
64
65     // Load all the affected files to check the changes that actually made it
66     // to the database.
67     $loaded_source = File::load($source->id());
68     $loaded_target = File::load($target->id());
69     $loaded_result = File::load($result->id());
70
71     // Verify that the source file wasn't changed.
72     $this->assertFileUnchanged($source, $loaded_source);
73
74     // Verify that what was returned is what's in the database.
75     $this->assertFileUnchanged($result, $loaded_result);
76
77     // Make sure we end up with three distinct files afterwards.
78     $this->assertDifferentFile($loaded_source, $loaded_target);
79     $this->assertDifferentFile($loaded_target, $loaded_result);
80     $this->assertDifferentFile($loaded_source, $loaded_result);
81   }
82
83   /**
84    * Test replacement when copying over a file that already exists.
85    */
86   public function testExistingReplace() {
87     // Setup a file to overwrite.
88     $contents = $this->randomMachineName(10);
89     $source = $this->createFile(NULL, $contents);
90     $target = $this->createFile();
91     $this->assertDifferentFile($source, $target);
92
93     // Clone the object so we don't have to worry about the function changing
94     // our reference copy.
95     $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_REPLACE);
96
97     // Check the return status and that the contents changed.
98     $this->assertTrue($result, 'File copied successfully.');
99     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
100     $this->assertDifferentFile($source, $result);
101
102     // Check that the correct hooks were called.
103     $this->assertFileHooksCalled(['load', 'copy', 'update']);
104
105     // Load all the affected files to check the changes that actually made it
106     // to the database.
107     $loaded_source = File::load($source->id());
108     $loaded_target = File::load($target->id());
109     $loaded_result = File::load($result->id());
110
111     // Verify that the source file wasn't changed.
112     $this->assertFileUnchanged($source, $loaded_source);
113
114     // Verify that what was returned is what's in the database.
115     $this->assertFileUnchanged($result, $loaded_result);
116
117     // Target file was reused for the result.
118     $this->assertFileUnchanged($loaded_target, $loaded_result);
119   }
120
121   /**
122    * Test that copying over an existing file fails when FILE_EXISTS_ERROR is
123    * specified.
124    */
125   public function testExistingError() {
126     $contents = $this->randomMachineName(10);
127     $source = $this->createFile();
128     $target = $this->createFile(NULL, $contents);
129     $this->assertDifferentFile($source, $target);
130
131     // Clone the object so we don't have to worry about the function changing
132     // our reference copy.
133     $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_ERROR);
134
135     // Check the return status and that the contents were not changed.
136     $this->assertFalse($result, 'File copy failed.');
137     $this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
138
139     // Check that the correct hooks were called.
140     $this->assertFileHooksCalled([]);
141
142     $this->assertFileUnchanged($source, File::load($source->id()));
143     $this->assertFileUnchanged($target, File::load($target->id()));
144   }
145
146 }