3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file copy function.
12 class CopyTest extends FileManagedUnitTestBase {
15 * Test file copying in the normal, base case.
17 public function testNormal() {
18 $contents = $this->randomMachineName(10);
19 $source = $this->createFile(NULL, $contents);
20 $desired_uri = '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_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
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.');
30 // Check that the correct hooks were called.
31 $this->assertFileHooksCalled(['copy', 'insert']);
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.');
38 // Reload the file from the database and check that the changes were
40 $this->assertFileUnchanged($result, File::load($result->id()));
44 * Test renaming when copying over 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_copy(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
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.');
62 // Check that the correct hooks were called.
63 $this->assertFileHooksCalled(['copy', 'insert']);
65 // Load all the affected files to check the changes that actually made it
67 $loaded_source = File::load($source->id());
68 $loaded_target = File::load($target->id());
69 $loaded_result = File::load($result->id());
71 // Verify that the source file wasn't changed.
72 $this->assertFileUnchanged($source, $loaded_source);
74 // Verify that what was returned is what's in the database.
75 $this->assertFileUnchanged($result, $loaded_result);
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);
84 * Test replacement when copying over a file that already exists.
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);
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);
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);
102 // Check that the correct hooks were called.
103 $this->assertFileHooksCalled(['load', 'copy', 'update']);
105 // Load all the affected files to check the changes that actually made it
107 $loaded_source = File::load($source->id());
108 $loaded_target = File::load($target->id());
109 $loaded_result = File::load($result->id());
111 // Verify that the source file wasn't changed.
112 $this->assertFileUnchanged($source, $loaded_source);
114 // Verify that what was returned is what's in the database.
115 $this->assertFileUnchanged($result, $loaded_result);
117 // Target file was reused for the result.
118 $this->assertFileUnchanged($loaded_target, $loaded_result);
122 * Test that copying over an existing file fails when FILE_EXISTS_ERROR is
125 public function testExistingError() {
126 $contents = $this->randomMachineName(10);
127 $source = $this->createFile();
128 $target = $this->createFile(NULL, $contents);
129 $this->assertDifferentFile($source, $target);
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);
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.');
139 // Check that the correct hooks were called.
140 $this->assertFileHooksCalled([]);
142 $this->assertFileUnchanged($source, File::load($source->id()));
143 $this->assertFileUnchanged($target, File::load($target->id()));