3193cb6619123d9a39ec17e9e227d5ef2ab02d23
[yaffs-website] / web / core / modules / file / tests / src / Kernel / CopyTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the file copy function.
9  *
10  * @group file
11  */
12 class CopyTest extends FileManagedUnitTestBase {
13   /**
14    * Test file copying in the normal, base case.
15    */
16   public function testNormal() {
17     $contents = $this->randomMachineName(10);
18     $source = $this->createFile(NULL, $contents);
19     $desired_uri = '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_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
24
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.');
28
29     // Check that the correct hooks were called.
30     $this->assertFileHooksCalled(['copy', 'insert']);
31
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.');
36
37     // Reload the file from the database and check that the changes were
38     // actually saved.
39     $this->assertFileUnchanged($result, File::load($result->id()));
40   }
41
42   /**
43    * Test renaming when copying over a file that already exists.
44    */
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);
51
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);
55
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.');
60
61     // Check that the correct hooks were called.
62     $this->assertFileHooksCalled(['copy', 'insert']);
63
64     // Load all the affected files to check the changes that actually made it
65     // to the database.
66     $loaded_source = File::load($source->id());
67     $loaded_target = File::load($target->id());
68     $loaded_result = File::load($result->id());
69
70     // Verify that the source file wasn't changed.
71     $this->assertFileUnchanged($source, $loaded_source);
72
73     // Verify that what was returned is what's in the database.
74     $this->assertFileUnchanged($result, $loaded_result);
75
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);
80   }
81
82   /**
83    * Test replacement when copying over a file that already exists.
84    */
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);
91
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);
95
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);
100
101     // Check that the correct hooks were called.
102     $this->assertFileHooksCalled(['load', 'copy', 'update']);
103
104     // Load all the affected files to check the changes that actually made it
105     // to the database.
106     $loaded_source = File::load($source->id());
107     $loaded_target = File::load($target->id());
108     $loaded_result = File::load($result->id());
109
110     // Verify that the source file wasn't changed.
111     $this->assertFileUnchanged($source, $loaded_source);
112
113     // Verify that what was returned is what's in the database.
114     $this->assertFileUnchanged($result, $loaded_result);
115
116     // Target file was reused for the result.
117     $this->assertFileUnchanged($loaded_target, $loaded_result);
118   }
119
120   /**
121    * Test that copying over an existing file fails when FILE_EXISTS_ERROR is
122    * specified.
123    */
124   public function testExistingError() {
125     $contents = $this->randomMachineName(10);
126     $source = $this->createFile();
127     $target = $this->createFile(NULL, $contents);
128     $this->assertDifferentFile($source, $target);
129
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);
133
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.');
137
138     // Check that the correct hooks were called.
139     $this->assertFileHooksCalled([]);
140
141     $this->assertFileUnchanged($source, File::load($source->id()));
142     $this->assertFileUnchanged($target, File::load($target->id()));
143   }
144
145 }