3183ecfe42d83789e75b14c931656f29e83d9fe1
[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    * Move a normal file.
15    */
16   public function testNormal() {
17     $contents = $this->randomMachineName(10);
18     $source = $this->createFile(NULL, $contents);
19     $desired_filepath = 'public://' . $this->randomMachineName();
20
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);
24
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.');
29
30     // Check that the correct hooks were called.
31     $this->assertFileHooksCalled(['move', 'load', 'update']);
32
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()]));
35
36     // Reload the file from the database and check that the changes were
37     // actually saved.
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);
41   }
42
43   /**
44    * Test renaming when moving onto 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_move(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
56
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.');
61
62     // Check that the correct hooks were called.
63     $this->assertFileHooksCalled(['move', 'load', 'update']);
64
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);
71
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.');
76   }
77
78   /**
79    * Test replacement when moving onto a file that already exists.
80    */
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);
87
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);
91
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.');
96
97     // Check that the correct hooks were called.
98     $this->assertFileHooksCalled(['move', 'update', 'delete', 'load']);
99
100     // Reload the file from the database and check that the changes were
101     // actually saved.
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);
108   }
109
110   /**
111    * Test replacement when moving onto itself.
112    */
113   public function testExistingReplaceSelf() {
114     // Setup a file to overwrite.
115     $contents = $this->randomMachineName(10);
116     $source = $this->createFile(NULL, $contents);
117
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.');
123
124     // Check that no hooks were called while failing.
125     $this->assertFileHooksCalled([]);
126
127     // Load the file from the database and make sure it is identical to what
128     // was returned.
129     $this->assertFileUnchanged($source, File::load($source->id()));
130   }
131
132   /**
133    * Test that moving onto an existing file fails when FILE_EXISTS_ERROR is
134    * specified.
135    */
136   public function testExistingError() {
137     $contents = $this->randomMachineName(10);
138     $source = $this->createFile();
139     $target = $this->createFile(NULL, $contents);
140     $this->assertDifferentFile($source, $target);
141
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);
145
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.');
150
151     // Check that no hooks were called while failing.
152     $this->assertFileHooksCalled([]);
153
154     // Load the file from the database and make sure it is identical to what
155     // was returned.
156     $this->assertFileUnchanged($source, File::load($source->id()));
157     $this->assertFileUnchanged($target, File::load($target->id()));
158   }
159
160 }