1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\Exception\FileExistsException;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileExists;
use Drupal\file\Entity\File;
use Drupal\file\FileRepository;
/**
* Tests the file copy function.
*
* @coversDefaultClass \Drupal\file\FileRepository
* @group file
*/
class CopyTest extends FileManagedUnitTestBase {
/**
* The file repository service under test.
*
* @var \Drupal\file\FileRepository
*/
protected $fileRepository;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->fileRepository = $this->container->get('file.repository');
}
/**
* Tests file copying in the normal, base case.
*
* @covers ::copy
*/
public function testNormal(): void {
$contents = $this->randomMachineName(10);
$source = $this->createFile(NULL, $contents);
$desired_uri = 'public://' . $this->randomMachineName();
// Clone the object so we don't have to worry about the function changing
// our reference copy.
$result = $this->fileRepository->copy(clone $source, $desired_uri, FileExists::Error);
// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File copied successfully.');
$this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
// Check that the correct hooks were called.
$this->assertFileHooksCalled(['copy', 'insert']);
$this->assertDifferentFile($source, $result);
$this->assertEquals($result->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
$this->assertFileExists($source->getFileUri());
$this->assertFileExists($result->getFileUri());
// Reload the file from the database and check that the changes were
// actually saved.
$this->assertFileUnchanged($result, File::load($result->id()));
}
/**
* Tests renaming when copying over a file that already exists.
*
* @covers ::copy
*/
public function testExistingRename(): void {
// Setup a file to overwrite.
$contents = $this->randomMachineName(10);
$source = $this->createFile(NULL, $contents);
$target = $this->createFile();
$this->assertDifferentFile($source, $target);
// Clone the object so we don't have to worry about the function changing
// our reference copy.
$result = $this->fileRepository->copy(clone $source, $target->getFileUri(), FileExists::Rename);
// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File copied successfully.');
$this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file were copied correctly.');
$this->assertNotEquals($source->getFileUri(), $result->getFileUri(), 'Returned file path has changed from the original.');
// Check that the correct hooks were called.
$this->assertFileHooksCalled(['copy', 'insert']);
// Load all the affected files to check the changes that actually made it
// to the database.
$loaded_source = File::load($source->id());
$loaded_target = File::load($target->id());
$loaded_result = File::load($result->id());
// Verify that the source file wasn't changed.
$this->assertFileUnchanged($source, $loaded_source);
// Verify that what was returned is what's in the database.
$this->assertFileUnchanged($result, $loaded_result);
// Make sure we end up with three distinct files afterwards.
$this->assertDifferentFile($loaded_source, $loaded_target);
$this->assertDifferentFile($loaded_target, $loaded_result);
$this->assertDifferentFile($loaded_source, $loaded_result);
}
/**
* Tests replacement when copying over a file that already exists.
*
* @covers ::copy
*/
public function testExistingReplace(): void {
// Setup a file to overwrite.
$contents = $this->randomMachineName(10);
$source = $this->createFile(NULL, $contents);
$target = $this->createFile();
$this->assertDifferentFile($source, $target);
// Clone the object so we don't have to worry about the function changing
// our reference copy.
$result = $this->fileRepository->copy(clone $source, $target->getFileUri(), FileExists::Replace);
// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File copied successfully.');
$this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
$this->assertDifferentFile($source, $result);
// Check that the correct hooks were called.
$this->assertFileHooksCalled(['load', 'copy', 'update']);
// Load all the affected files to check the changes that actually made it
// to the database.
$loaded_source = File::load($source->id());
$loaded_target = File::load($target->id());
$loaded_result = File::load($result->id());
// Verify that the source file wasn't changed.
$this->assertFileUnchanged($source, $loaded_source);
// Verify that what was returned is what's in the database.
$this->assertFileUnchanged($result, $loaded_result);
// Target file was reused for the result.
$this->assertFileUnchanged($loaded_target, $loaded_result);
}
/**
* Tests that copying over an existing file fails when instructed to do so.
*
* @covers ::copy
*/
public function testExistingError(): void {
$contents = $this->randomMachineName(10);
$source = $this->createFile();
$target = $this->createFile(NULL, $contents);
$this->assertDifferentFile($source, $target);
// Clone the object so we don't have to worry about the function changing
// our reference copy.
try {
$this->fileRepository->copy(clone $source, $target->getFileUri(), FileExists::Error);
$this->fail('expected FileExistsException');
}
// FileExistsException is a subclass of FileException.
catch (FileExistsException $e) {
$this->assertStringContainsString("could not be copied because a file by that name already exists in the destination directory", $e->getMessage());
}
// Check the contents were not changed.
$this->assertEquals($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
// Check that the correct hooks were called.
$this->assertFileHooksCalled([]);
$this->assertFileUnchanged($source, File::load($source->id()));
$this->assertFileUnchanged($target, File::load($target->id()));
}
/**
* Tests for an invalid stream wrapper.
*
* @covers ::copy
*/
public function testInvalidStreamWrapper(): void {
$this->expectException(InvalidStreamWrapperException::class);
$this->expectExceptionMessage('Invalid stream wrapper: foo://');
$source = $this->createFile();
$this->fileRepository->copy($source, 'foo://');
}
/**
* Tests for entity storage exception.
*
* @covers ::copy
*/
public function testEntityStorageException(): void {
/** @var \Drupal\Core\Entity\EntityTypeManager $entityTypeManager */
$entityTypeManager = $this->prophesize(EntityTypeManager::class);
$entityTypeManager->getStorage('file')
->willThrow(EntityStorageException::class);
$fileRepository = new FileRepository(
$this->container->get('file_system'),
$this->container->get('stream_wrapper_manager'),
$entityTypeManager->reveal(),
$this->container->get('module_handler'),
$this->container->get('file.usage'),
$this->container->get('current_user')
);
$this->expectException(EntityStorageException::class);
$source = $this->createFile();
$target = $this->createFile();
$fileRepository->copy($source, $target->getFileUri(), FileExists::Replace);
}
}
|