3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file move function.
12 class MoveTest extends FileManagedUnitTestBase {
17 public function testNormal() {
18 $contents = $this->randomMachineName(10);
19 $source = $this->createFile(NULL, $contents);
20 $desired_filepath = 'public://' . $this->randomMachineName();
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);
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.');
31 // Check that the correct hooks were called.
32 $this->assertFileHooksCalled(['move', 'load', 'update']);
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()]));
37 // Reload the file from the database and check that the changes were
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);
45 * Test renaming when moving onto a file that already exists.
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);
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);
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.');
63 // Check that the correct hooks were called.
64 $this->assertFileHooksCalled(['move', 'load', 'update']);
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);
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.');
80 * Test replacement when moving onto a file that already exists.
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);
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);
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.');
98 // Check that the correct hooks were called.
99 $this->assertFileHooksCalled(['move', 'update', 'delete', 'load']);
101 // Reload the file from the database and check that the changes were
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);
112 * Test replacement when moving onto itself.
114 public function testExistingReplaceSelf() {
115 // Setup a file to overwrite.
116 $contents = $this->randomMachineName(10);
117 $source = $this->createFile(NULL, $contents);
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.');
125 // Check that no hooks were called while failing.
126 $this->assertFileHooksCalled([]);
128 // Load the file from the database and make sure it is identical to what
130 $this->assertFileUnchanged($source, File::load($source->id()));
134 * Test that moving onto an existing file fails when FILE_EXISTS_ERROR is
137 public function testExistingError() {
138 $contents = $this->randomMachineName(10);
139 $source = $this->createFile();
140 $target = $this->createFile(NULL, $contents);
141 $this->assertDifferentFile($source, $target);
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);
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.');
152 // Check that no hooks were called while failing.
153 $this->assertFileHooksCalled([]);
155 // Load the file from the database and make sure it is identical to what
157 $this->assertFileUnchanged($source, File::load($source->id()));
158 $this->assertFileUnchanged($target, File::load($target->id()));