Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / MoveTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the file move function.
9  *
10  * @group file
11  */
12 class MoveTest extends FileManagedUnitTestBase {
13
14   /**
15    * Move a normal file.
16    */
17   public function testNormal() {
18     $contents = $this->randomMachineName(10);
19     $source = $this->createFile(NULL, $contents);
20     $desired_filepath = '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_move(clone $source, $desired_filepath, FILE_EXISTS_ERROR);
25
26     // Check the return status and that the contents changed.
27     $this->assertTrue($result, 'File moved successfully.');
28     $this->assertFalse(file_exists($source->getFileUri()));
29     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
30
31     // Check that the correct hooks were called.
32     $this->assertFileHooksCalled(['move', 'load', 'update']);
33
34     // Make sure we got the same file back.
35     $this->assertEqual($source->id(), $result->id(), format_string("Source file id's' %fid is unchanged after move.", ['%fid' => $source->id()]));
36
37     // Reload the file from the database and check that the changes were
38     // actually saved.
39     $loaded_file = File::load($result->id());
40     $this->assertTrue($loaded_file, 'File can be loaded from the database.');
41     $this->assertFileUnchanged($result, $loaded_file);
42   }
43
44   /**
45    * Test renaming when moving onto a file that already exists.
46    */
47   public function testExistingRename() {
48     // Setup a file to overwrite.
49     $contents = $this->randomMachineName(10);
50     $source = $this->createFile(NULL, $contents);
51     $target = $this->createFile();
52     $this->assertDifferentFile($source, $target);
53
54     // Clone the object so we don't have to worry about the function changing
55     // our reference copy.
56     $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
57
58     // Check the return status and that the contents changed.
59     $this->assertTrue($result, 'File moved successfully.');
60     $this->assertFalse(file_exists($source->getFileUri()));
61     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
62
63     // Check that the correct hooks were called.
64     $this->assertFileHooksCalled(['move', 'load', 'update']);
65
66     // Compare the returned value to what made it into the database.
67     $this->assertFileUnchanged($result, File::load($result->id()));
68     // The target file should not have been altered.
69     $this->assertFileUnchanged($target, File::load($target->id()));
70     // Make sure we end up with two distinct files afterwards.
71     $this->assertDifferentFile($target, $result);
72
73     // Compare the source and results.
74     $loaded_source = File::load($source->id());
75     $this->assertEqual($loaded_source->id(), $result->id(), "Returned file's id matches the source.");
76     $this->assertNotEqual($loaded_source->getFileUri(), $source->getFileUri(), 'Returned file path has changed from the original.');
77   }
78
79   /**
80    * Test replacement when moving onto a file that already exists.
81    */
82   public function testExistingReplace() {
83     // Setup a file to overwrite.
84     $contents = $this->randomMachineName(10);
85     $source = $this->createFile(NULL, $contents);
86     $target = $this->createFile();
87     $this->assertDifferentFile($source, $target);
88
89     // Clone the object so we don't have to worry about the function changing
90     // our reference copy.
91     $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_REPLACE);
92
93     // Look at the results.
94     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
95     $this->assertFalse(file_exists($source->getFileUri()));
96     $this->assertTrue($result, 'File moved successfully.');
97
98     // Check that the correct hooks were called.
99     $this->assertFileHooksCalled(['move', 'update', 'delete', 'load']);
100
101     // Reload the file from the database and check that the changes were
102     // actually saved.
103     $loaded_result = File::load($result->id());
104     $this->assertFileUnchanged($result, $loaded_result);
105     // Check that target was re-used.
106     $this->assertSameFile($target, $loaded_result);
107     // Source and result should be totally different.
108     $this->assertDifferentFile($source, $loaded_result);
109   }
110
111   /**
112    * Test replacement when moving onto itself.
113    */
114   public function testExistingReplaceSelf() {
115     // Setup a file to overwrite.
116     $contents = $this->randomMachineName(10);
117     $source = $this->createFile(NULL, $contents);
118
119     // Copy the file over itself. Clone the object so we don't have to worry
120     // about the function changing our reference copy.
121     $result = file_move(clone $source, $source->getFileUri(), FILE_EXISTS_REPLACE);
122     $this->assertFalse($result, 'File move failed.');
123     $this->assertEqual($contents, file_get_contents($source->getFileUri()), 'Contents of file were not altered.');
124
125     // Check that no hooks were called while failing.
126     $this->assertFileHooksCalled([]);
127
128     // Load the file from the database and make sure it is identical to what
129     // was returned.
130     $this->assertFileUnchanged($source, File::load($source->id()));
131   }
132
133   /**
134    * Test that moving onto an existing file fails when FILE_EXISTS_ERROR is
135    * specified.
136    */
137   public function testExistingError() {
138     $contents = $this->randomMachineName(10);
139     $source = $this->createFile();
140     $target = $this->createFile(NULL, $contents);
141     $this->assertDifferentFile($source, $target);
142
143     // Clone the object so we don't have to worry about the function changing
144     // our reference copy.
145     $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_ERROR);
146
147     // Check the return status and that the contents did not change.
148     $this->assertFalse($result, 'File move failed.');
149     $this->assertTrue(file_exists($source->getFileUri()));
150     $this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
151
152     // Check that no hooks were called while failing.
153     $this->assertFileHooksCalled([]);
154
155     // Load the file from the database and make sure it is identical to what
156     // was returned.
157     $this->assertFileUnchanged($source, File::load($source->id()));
158     $this->assertFileUnchanged($target, File::load($target->id()));
159   }
160
161 }