3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file move function.
12 class MoveTest extends FileManagedUnitTestBase {
16 public function testNormal() {
17 $contents = $this->randomMachineName(10);
18 $source = $this->createFile(NULL, $contents);
19 $desired_filepath = 'public://' . $this->randomMachineName();
21 // Clone the object so we don't have to worry about the function changing
22 // our reference copy.
23 $result = file_move(clone $source, $desired_filepath, FILE_EXISTS_ERROR);
25 // Check the return status and that the contents changed.
26 $this->assertTrue($result, 'File moved successfully.');
27 $this->assertFalse(file_exists($source->getFileUri()));
28 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
30 // Check that the correct hooks were called.
31 $this->assertFileHooksCalled(['move', 'load', 'update']);
33 // Make sure we got the same file back.
34 $this->assertEqual($source->id(), $result->id(), format_string("Source file id's' %fid is unchanged after move.", ['%fid' => $source->id()]));
36 // Reload the file from the database and check that the changes were
38 $loaded_file = File::load($result->id());
39 $this->assertTrue($loaded_file, 'File can be loaded from the database.');
40 $this->assertFileUnchanged($result, $loaded_file);
44 * Test renaming when moving onto a file that already exists.
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);
53 // Clone the object so we don't have to worry about the function changing
54 // our reference copy.
55 $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
57 // Check the return status and that the contents changed.
58 $this->assertTrue($result, 'File moved successfully.');
59 $this->assertFalse(file_exists($source->getFileUri()));
60 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
62 // Check that the correct hooks were called.
63 $this->assertFileHooksCalled(['move', 'load', 'update']);
65 // Compare the returned value to what made it into the database.
66 $this->assertFileUnchanged($result, File::load($result->id()));
67 // The target file should not have been altered.
68 $this->assertFileUnchanged($target, File::load($target->id()));
69 // Make sure we end up with two distinct files afterwards.
70 $this->assertDifferentFile($target, $result);
72 // Compare the source and results.
73 $loaded_source = File::load($source->id());
74 $this->assertEqual($loaded_source->id(), $result->id(), "Returned file's id matches the source.");
75 $this->assertNotEqual($loaded_source->getFileUri(), $source->getFileUri(), 'Returned file path has changed from the original.');
79 * Test replacement when moving onto a file that already exists.
81 public function testExistingReplace() {
82 // Setup a file to overwrite.
83 $contents = $this->randomMachineName(10);
84 $source = $this->createFile(NULL, $contents);
85 $target = $this->createFile();
86 $this->assertDifferentFile($source, $target);
88 // Clone the object so we don't have to worry about the function changing
89 // our reference copy.
90 $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_REPLACE);
92 // Look at the results.
93 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
94 $this->assertFalse(file_exists($source->getFileUri()));
95 $this->assertTrue($result, 'File moved successfully.');
97 // Check that the correct hooks were called.
98 $this->assertFileHooksCalled(['move', 'update', 'delete', 'load']);
100 // Reload the file from the database and check that the changes were
102 $loaded_result = File::load($result->id());
103 $this->assertFileUnchanged($result, $loaded_result);
104 // Check that target was re-used.
105 $this->assertSameFile($target, $loaded_result);
106 // Source and result should be totally different.
107 $this->assertDifferentFile($source, $loaded_result);
111 * Test replacement when moving onto itself.
113 public function testExistingReplaceSelf() {
114 // Setup a file to overwrite.
115 $contents = $this->randomMachineName(10);
116 $source = $this->createFile(NULL, $contents);
118 // Copy the file over itself. Clone the object so we don't have to worry
119 // about the function changing our reference copy.
120 $result = file_move(clone $source, $source->getFileUri(), FILE_EXISTS_REPLACE);
121 $this->assertFalse($result, 'File move failed.');
122 $this->assertEqual($contents, file_get_contents($source->getFileUri()), 'Contents of file were not altered.');
124 // Check that no hooks were called while failing.
125 $this->assertFileHooksCalled([]);
127 // Load the file from the database and make sure it is identical to what
129 $this->assertFileUnchanged($source, File::load($source->id()));
133 * Test that moving onto an existing file fails when FILE_EXISTS_ERROR is
136 public function testExistingError() {
137 $contents = $this->randomMachineName(10);
138 $source = $this->createFile();
139 $target = $this->createFile(NULL, $contents);
140 $this->assertDifferentFile($source, $target);
142 // Clone the object so we don't have to worry about the function changing
143 // our reference copy.
144 $result = file_move(clone $source, $target->getFileUri(), FILE_EXISTS_ERROR);
146 // Check the return status and that the contents did not change.
147 $this->assertFalse($result, 'File move failed.');
148 $this->assertTrue(file_exists($source->getFileUri()));
149 $this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
151 // Check that no hooks were called while failing.
152 $this->assertFileHooksCalled([]);
154 // Load the file from the database and make sure it is identical to what
156 $this->assertFileUnchanged($source, File::load($source->id()));
157 $this->assertFileUnchanged($target, File::load($target->id()));