3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file copy function.
12 class CopyTest extends FileManagedUnitTestBase {
14 * Test file copying in the normal, base case.
16 public function testNormal() {
17 $contents = $this->randomMachineName(10);
18 $source = $this->createFile(NULL, $contents);
19 $desired_uri = '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_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
25 // Check the return status and that the contents changed.
26 $this->assertTrue($result, 'File copied successfully.');
27 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
29 // Check that the correct hooks were called.
30 $this->assertFileHooksCalled(['copy', 'insert']);
32 $this->assertDifferentFile($source, $result);
33 $this->assertEqual($result->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
34 $this->assertTrue(file_exists($source->getFileUri()), 'The original file still exists.');
35 $this->assertTrue(file_exists($result->getFileUri()), 'The copied file exists.');
37 // Reload the file from the database and check that the changes were
39 $this->assertFileUnchanged($result, File::load($result->id()));
43 * Test renaming when copying over a file that already exists.
45 public function testExistingRename() {
46 // Setup a file to overwrite.
47 $contents = $this->randomMachineName(10);
48 $source = $this->createFile(NULL, $contents);
49 $target = $this->createFile();
50 $this->assertDifferentFile($source, $target);
52 // Clone the object so we don't have to worry about the function changing
53 // our reference copy.
54 $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_RENAME);
56 // Check the return status and that the contents changed.
57 $this->assertTrue($result, 'File copied successfully.');
58 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
59 $this->assertNotEqual($result->getFileUri(), $source->getFileUri(), 'Returned file path has changed from the original.');
61 // Check that the correct hooks were called.
62 $this->assertFileHooksCalled(['copy', 'insert']);
64 // Load all the affected files to check the changes that actually made it
66 $loaded_source = File::load($source->id());
67 $loaded_target = File::load($target->id());
68 $loaded_result = File::load($result->id());
70 // Verify that the source file wasn't changed.
71 $this->assertFileUnchanged($source, $loaded_source);
73 // Verify that what was returned is what's in the database.
74 $this->assertFileUnchanged($result, $loaded_result);
76 // Make sure we end up with three distinct files afterwards.
77 $this->assertDifferentFile($loaded_source, $loaded_target);
78 $this->assertDifferentFile($loaded_target, $loaded_result);
79 $this->assertDifferentFile($loaded_source, $loaded_result);
83 * Test replacement when copying over a file that already exists.
85 public function testExistingReplace() {
86 // Setup a file to overwrite.
87 $contents = $this->randomMachineName(10);
88 $source = $this->createFile(NULL, $contents);
89 $target = $this->createFile();
90 $this->assertDifferentFile($source, $target);
92 // Clone the object so we don't have to worry about the function changing
93 // our reference copy.
94 $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_REPLACE);
96 // Check the return status and that the contents changed.
97 $this->assertTrue($result, 'File copied successfully.');
98 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
99 $this->assertDifferentFile($source, $result);
101 // Check that the correct hooks were called.
102 $this->assertFileHooksCalled(['load', 'copy', 'update']);
104 // Load all the affected files to check the changes that actually made it
106 $loaded_source = File::load($source->id());
107 $loaded_target = File::load($target->id());
108 $loaded_result = File::load($result->id());
110 // Verify that the source file wasn't changed.
111 $this->assertFileUnchanged($source, $loaded_source);
113 // Verify that what was returned is what's in the database.
114 $this->assertFileUnchanged($result, $loaded_result);
116 // Target file was reused for the result.
117 $this->assertFileUnchanged($loaded_target, $loaded_result);
121 * Test that copying over an existing file fails when FILE_EXISTS_ERROR is
124 public function testExistingError() {
125 $contents = $this->randomMachineName(10);
126 $source = $this->createFile();
127 $target = $this->createFile(NULL, $contents);
128 $this->assertDifferentFile($source, $target);
130 // Clone the object so we don't have to worry about the function changing
131 // our reference copy.
132 $result = file_copy(clone $source, $target->getFileUri(), FILE_EXISTS_ERROR);
134 // Check the return status and that the contents were not changed.
135 $this->assertFalse($result, 'File copy failed.');
136 $this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
138 // Check that the correct hooks were called.
139 $this->assertFileHooksCalled([]);
141 $this->assertFileUnchanged($source, File::load($source->id()));
142 $this->assertFileUnchanged($target, File::load($target->id()));