-
Notifications
You must be signed in to change notification settings - Fork 63
/
LocalTest.php
359 lines (295 loc) · 14.7 KB
/
LocalTest.php
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace Utopia\Tests\Storage\Device;
use PHPUnit\Framework\TestCase;
use Utopia\Storage\Device\Local;
use Utopia\Storage\Device\S3;
class LocalTest extends TestCase
{
/**
* @var Local
*/
protected $object = null;
public function setUp(): void
{
$this->object = new Local(realpath(__DIR__.'/../../resources/disk-a'));
}
public function tearDown(): void
{
}
public function testPaths()
{
$this->assertEquals($this->object->getAbsolutePath('////storage/functions'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('storage/functions'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('/storage/functions'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('//storage///functions//'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('\\\\\storage\functions'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('..\\\\\//storage\\//functions'), '/storage/functions');
$this->assertEquals($this->object->getAbsolutePath('./..\\\\\//storage\\//functions'), '/storage/functions');
}
public function testName()
{
$this->assertEquals($this->object->getName(), 'Local Storage');
}
public function testType()
{
$this->assertEquals($this->object->getType(), 'local');
}
public function testDescription()
{
$this->assertEquals($this->object->getDescription(), 'Adapter for Local storage that is in the physical or virtual machine or mounted to it.');
}
public function testRoot()
{
$this->assertEquals($this->object->getRoot(), $this->object->getAbsolutePath(__DIR__.'/../../resources/disk-a'));
}
public function testPath()
{
$this->assertEquals($this->object->getPath('image.png'), $this->object->getAbsolutePath(__DIR__.'/../../resources/disk-a').'/image.png');
}
public function testWrite()
{
$this->assertEquals($this->object->write($this->object->getPath('text.txt'), 'Hello World'), true);
$this->assertEquals(file_exists($this->object->getPath('text.txt')), true);
$this->assertEquals(is_readable($this->object->getPath('text.txt')), true);
$this->object->delete($this->object->getPath('text.txt'));
}
public function testRead()
{
$this->assertEquals($this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World'), true);
$this->assertEquals($this->object->read($this->object->getPath('text-for-read.txt')), 'Hello World');
$this->object->delete($this->object->getPath('text-for-read.txt'));
}
public function testFileExists()
{
$this->assertEquals($this->object->write($this->object->getPath('text-for-test-exists.txt'), 'Hello World'), true);
$this->assertEquals($this->object->exists($this->object->getPath('text-for-test-exists.txt')), true);
$this->assertEquals($this->object->exists($this->object->getPath('text-for-test-doesnt-exist.txt')), false);
$this->object->delete($this->object->getPath('text-for-test-exists.txt'));
}
public function testMove()
{
$this->assertEquals($this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World'), true);
$this->assertEquals($this->object->read($this->object->getPath('text-for-move.txt')), 'Hello World');
$this->assertEquals($this->object->move($this->object->getPath('text-for-move.txt'), $this->object->getPath('text-for-move-new.txt')), true);
$this->assertEquals($this->object->read($this->object->getPath('text-for-move-new.txt')), 'Hello World');
$this->assertEquals(file_exists($this->object->getPath('text-for-move.txt')), false);
$this->assertEquals(is_readable($this->object->getPath('text-for-move.txt')), false);
$this->assertEquals(file_exists($this->object->getPath('text-for-move-new.txt')), true);
$this->assertEquals(is_readable($this->object->getPath('text-for-move-new.txt')), true);
$this->object->delete($this->object->getPath('text-for-move-new.txt'));
}
public function testDelete()
{
$this->assertEquals($this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World'), true);
$this->assertEquals($this->object->read($this->object->getPath('text-for-delete.txt')), 'Hello World');
$this->assertEquals($this->object->delete($this->object->getPath('text-for-delete.txt')), true);
$this->assertEquals(file_exists($this->object->getPath('text-for-delete.txt')), false);
$this->assertEquals(is_readable($this->object->getPath('text-for-delete.txt')), false);
}
public function testFileSize()
{
$this->assertEquals($this->object->getFileSize(__DIR__.'/../../resources/disk-a/kitten-1.jpg'), 599639);
$this->assertEquals($this->object->getFileSize(__DIR__.'/../../resources/disk-a/kitten-2.jpg'), 131958);
}
public function testFileMimeType()
{
$this->assertEquals($this->object->getFileMimeType(__DIR__.'/../../resources/disk-a/kitten-1.jpg'), 'image/jpeg');
$this->assertEquals($this->object->getFileMimeType(__DIR__.'/../../resources/disk-a/kitten-2.jpg'), 'image/jpeg');
$this->assertEquals($this->object->getFileMimeType(__DIR__.'/../../resources/disk-b/kitten-1.png'), 'image/png');
$this->assertEquals($this->object->getFileMimeType(__DIR__.'/../../resources/disk-b/kitten-2.png'), 'image/png');
}
public function testFileHash()
{
$this->assertEquals($this->object->getFileHash(__DIR__.'/../../resources/disk-a/kitten-1.jpg'), '7551f343143d2e24ab4aaf4624996b6a');
$this->assertEquals($this->object->getFileHash(__DIR__.'/../../resources/disk-a/kitten-2.jpg'), '81702fdeef2e55b1a22617bce4951cb5');
$this->assertEquals($this->object->getFileHash(__DIR__.'/../../resources/disk-b/kitten-1.png'), '03010f4f02980521a8fd6213b52ec313');
$this->assertEquals($this->object->getFileHash(__DIR__.'/../../resources/disk-b/kitten-2.png'), '8a9ed992b77e4b62b10e3a5c8ed72062');
}
public function testDirectoryCreate()
{
$directory = uniqid();
$this->assertTrue($this->object->createDirectory(__DIR__."/$directory"));
$this->assertTrue($this->object->exists(__DIR__."/$directory"));
}
public function testDirectorySize()
{
$this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__.'/../../resources/disk-a/'));
$this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__.'/../../resources/disk-b/'));
}
public function testPartUpload()
{
$source = __DIR__.'/../../resources/disk-a/large_file.mp4';
$dest = $this->object->getPath('uploaded.mp4');
$totalSize = $this->object->getFileSize($source);
$chunkSize = 2097152;
$chunks = ceil($totalSize / $chunkSize);
$chunk = 1;
$start = 0;
$handle = @fopen($source, 'rb');
while ($start < $totalSize) {
$contents = fread($handle, $chunkSize);
$op = __DIR__.'/chunk.part';
$cc = fopen($op, 'wb');
fwrite($cc, $contents);
fclose($cc);
$this->object->upload($op, $dest, $chunk, $chunks);
$start += strlen($contents);
$chunk++;
fseek($handle, $start);
}
@fclose($handle);
$this->assertEquals(\filesize($source), $this->object->getFileSize($dest));
$this->assertEquals(\md5_file($source), $this->object->getFileHash($dest));
return $dest;
}
public function testAbort()
{
$source = __DIR__.'/../../resources/disk-a/large_file.mp4';
$dest = $this->object->getPath('abcduploaded.mp4');
$totalSize = $this->object->getFileSize($source);
$chunkSize = 2097152;
$chunks = ceil($totalSize / $chunkSize);
$chunk = 1;
$start = 0;
$handle = @fopen($source, 'rb');
while ($chunk < 3) { // only upload two chunks
$contents = fread($handle, $chunkSize);
$op = __DIR__.'/chunk.part';
$cc = fopen($op, 'wb');
fwrite($cc, $contents);
fclose($cc);
$this->object->upload($op, $dest, $chunk, $chunks);
$start += strlen($contents);
$chunk++;
fseek($handle, $start);
}
@fclose($handle);
// using file name with same first four chars
$source = __DIR__.'/../../resources/disk-a/large_file.mp4';
$dest1 = $this->object->getPath('abcduploaded2.mp4');
$totalSize = $this->object->getFileSize($source);
$chunkSize = 2097152;
$chunks = ceil($totalSize / $chunkSize);
$chunk = 1;
$start = 0;
$handle = @fopen($source, 'rb');
while ($chunk < 3) { // only upload two chunks
$contents = fread($handle, $chunkSize);
$op = __DIR__.'/chunk.part';
$cc = fopen($op, 'wb');
fwrite($cc, $contents);
fclose($cc);
$this->object->upload($op, $dest1, $chunk, $chunks);
$start += strlen($contents);
$chunk++;
fseek($handle, $start);
}
@fclose($handle);
$this->assertTrue($this->object->abort($dest));
$this->assertTrue($this->object->abort($dest1));
}
/**
* @depends testPartUpload
*/
public function testPartRead($path)
{
$source = __DIR__.'/../../resources/disk-a/large_file.mp4';
$chunk = file_get_contents($source, false, null, 0, 500);
$readChunk = $this->object->read($path, 0, 500);
$this->assertEquals($chunk, $readChunk);
}
public function testPartitionFreeSpace()
{
$this->assertGreaterThan(0, $this->object->getPartitionFreeSpace());
}
public function testPartitionTotalSpace()
{
$this->assertGreaterThan(0, $this->object->getPartitionTotalSpace());
}
/**
* @depends testPartUpload
*/
public function testTransferLarge($path)
{
// chunked file
$this->object->setTransferChunkSize(10000000); //10 mb
$key = $_SERVER['S3_ACCESS_KEY'] ?? '';
$secret = $_SERVER['S3_SECRET'] ?? '';
$bucket = 'utopia-storage-test';
$device = new S3('/root', $key, $secret, $bucket, S3::EU_CENTRAL_1, S3::ACL_PRIVATE);
$destination = $device->getPath('largefile.mp4');
$this->assertTrue($this->object->transfer($path, $destination, $device));
$this->assertTrue($device->exists($destination));
$this->assertEquals($device->getFileMimeType($destination), 'video/mp4');
$device->delete($destination);
$this->object->delete($path);
}
public function testTransferSmall()
{
$this->object->setTransferChunkSize(10000000); //10 mb
$key = $_SERVER['S3_ACCESS_KEY'] ?? '';
$secret = $_SERVER['S3_SECRET'] ?? '';
$bucket = 'utopia-storage-test';
$device = new S3('/root', $key, $secret, $bucket, S3::EU_CENTRAL_1, S3::ACL_PRIVATE);
$path = $this->object->getPath('text-for-read.txt');
$this->object->write($path, 'Hello World');
$destination = $device->getPath('hello.txt');
$this->assertTrue($this->object->transfer($path, $destination, $device));
$this->assertTrue($device->exists($destination));
$this->assertEquals($device->read($destination), 'Hello World');
$this->object->delete($path);
$device->delete($destination);
}
public function testDeletePath()
{
// Test Single Object
$path = $this->object->getPath('text-for-delete-path.txt');
$path = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path);
$this->assertEquals(true, $this->object->write($path, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path));
$this->assertEquals(true, $this->object->deletePath('bucket'));
$this->assertEquals(false, $this->object->exists($path));
// Test Multiple Objects
$path = $this->object->getPath('text-for-delete-path1.txt');
$path = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path);
$this->assertEquals(true, $this->object->write($path, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path));
$path2 = $this->object->getPath('text-for-delete-path2.txt');
$path2 = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path2);
$this->assertEquals(true, $this->object->write($path2, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path2));
$path3 = $this->object->getPath('.hidden.txt');
$path3 = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.'bucket', $path3);
$this->assertEquals(true, $this->object->write($path3, 'Hello World', 'text/plain'));
$this->assertEquals(true, $this->object->exists($path3));
$this->assertEquals(true, $this->object->deletePath('bucket/'));
$this->assertEquals(false, $this->object->exists($path));
$this->assertEquals(false, $this->object->exists($path2));
$this->assertEquals(false, $this->object->exists($path3));
}
public function testGetFiles()
{
$dir = DIRECTORY_SEPARATOR.'get-files-test';
$this->assertTrue($this->object->createDirectory($dir));
$files = $this->object->getFiles($dir);
$this->assertEquals(0, \count($files));
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file-two.txt', 'Hello World');
$files = $this->object->getFiles($dir);
$this->assertEquals(2, \count($files));
}
public function testNestedDeletePath()
{
$dir = $this->object->getPath('nested-delete-path-test');
$dir2 = $dir.DIRECTORY_SEPARATOR.'dir2';
$dir3 = $dir2.DIRECTORY_SEPARATOR.'dir3';
$this->assertTrue($this->object->createDirectory($dir));
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir2));
$this->object->write($dir2.DIRECTORY_SEPARATOR.'new-file-2.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir3));
$this->object->write($dir3.DIRECTORY_SEPARATOR.'new-file-3.txt', 'Hello World');
$this->assertTrue($this->object->deletePath('nested-delete-path-test'));
$this->assertFalse($this->object->exists($dir));
}
}