Debian Codegraph @

tests/phpunit/integration/includes/libs/filebackend/FileBackendIntegrationTestBase.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Http\HttpRequestFactory;
5 use MediaWiki\Logger\LoggerFactory;
6 use MediaWiki\Status\Status;
7 use Shellbox\Shellbox;
8 use Wikimedia\FileBackend\FileBackend;
9 use Wikimedia\FileBackend\FSFile\FSFile;
10 use Wikimedia\FileBackend\FSFile\TempFSFile;
11 use Wikimedia\FileBackend\FSFileBackend;
12
13 abstract class FileBackendIntegrationTestBase extends MediaWikiIntegrationTestCase {
14
15 /** @var FileBackend */
16 protected $backend;
17
18 abstract protected function getBackend();
19
20 protected function setUp(): void {
21 $this->backend = $this->getBackend();
22 }
23
24 protected function tearDown(): void {
25 $this->tearDownFiles();
26 }
27
28 private static function baseStorePath() {
29 return 'mwstore://localtesting';
30 }
31
32 private function backendClass() {
33 return get_class( $this->backend );
34 }
35
36 /**
37 * @dataProvider provider_testStore
38 */
39 public function testStore( $op ) {
40 $this->addTmpFiles( $op['src'] );
41 $backendName = $this->backendClass();
42
43 $source = $op['src'];
44 $dest = $op['dst'];
45 $this->prepare( [ 'dir' => dirname( $dest ) ] );
46
47 file_put_contents( $source, "Unit test file" );
48
49 if ( isset( $op['overwrite'] ) || isset( $op['overwriteSame'] ) ) {
50 $this->backend->store( $op );
51 }
52
53 $status = $this->backend->doOperation( $op );
54
55 $this->assertStatusGood( $status,
56 "Store from $source to $dest succeeded without warnings ($backendName)." );
57 $this->assertEquals( [ 0 => true ], $status->success,
58 "Store from $source to $dest has proper 'success' field in Status ($backendName)." );
59 $this->assertTrue( is_file( $source ),
60 "Source file $source still exists ($backendName)." );
61 $this->assertTrue( $this->backend->fileExists( [ 'src' => $dest ] ),
62 "Destination file $dest exists ($backendName)." );
63
64 $this->assertEquals( filesize( $source ),
65 $this->backend->getFileSize( [ 'src' => $dest ] ),
66 "Destination file $dest has correct size ($backendName)." );
67
68 $props1 = FSFile::getPropsFromPath( $source );
69 $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
70 $this->assertEquals( $props1, $props2,
71 "Source and destination have the same props ($backendName)." );
72
73 $this->assertBackendPathsConsistent( [ $dest ], true );
74 }
75
76 public static function provider_testStore() {
77 $cases = [];
78
79 $tmpName = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
80 $toPath = self::baseStorePath() . '/unittest-cont1/e/fun/obj1.txt';
81 $op = [ 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ];
82 $cases[] = [ $op ];
83
84 $op2 = $op;
85 $op2['overwrite'] = true;
86 $cases[] = [ $op2 ];
87
88 $op3 = $op;
89 $op3['overwriteSame'] = true;
90 $cases[] = [ $op3 ];
91
92 return $cases;
93 }
94
95 /**
96 * @dataProvider provider_testCopy
97 */
98 public function testCopy( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
99 $backendName = $this->backendClass();
100
101 $source = $op['src'];
102 $dest = $op['dst'];
103 $this->prepare( [ 'dir' => dirname( $source ) ] );
104 $this->prepare( [ 'dir' => dirname( $dest ) ] );
105
106 if ( is_string( $srcContent ) ) {
107 $status = $this->backend->create( [ 'content' => $srcContent, 'dst' => $source ] );
108 $this->assertStatusGood( $status, "Creation of $source succeeded ($backendName)." );
109 }
110 if ( is_string( $dstContent ) ) {
111 $status = $this->backend->create( [ 'content' => $dstContent, 'dst' => $dest ] );
112 $this->assertStatusGood( $status, "Creation of $dest succeeded ($backendName)." );
113 }
114
115 $status = $this->backend->doOperation( $op );
116
117 if ( $okStatus ) {
118 $this->assertStatusGood(
119 $status,
120 "Copy from $source to $dest succeeded without warnings ($backendName)." );
121 $this->assertEquals( [ 0 => true ], $status->success,
122 "Copy from $source to $dest has proper 'success' field in Status ($backendName)." );
123 if ( !is_string( $srcContent ) ) {
124 $this->assertSame(
125 is_string( $dstContent ),
126 $this->backend->fileExists( [ 'src' => $dest ] ),
127 "Destination file $dest unchanged after no-op copy ($backendName)." );
128 $this->assertSame(
129 $dstContent,
130 $this->backend->getFileContents( [ 'src' => $dest ] ),
131 "Destination file $dest unchanged after no-op copy ($backendName)." );
132 } else {
133 $this->assertEquals(
134 $this->backend->getFileSize( [ 'src' => $source ] ),
135 $this->backend->getFileSize( [ 'src' => $dest ] ),
136 "Destination file $dest has correct size ($backendName)." );
137 $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
138 $props2 = $this->backend->getFileProps( [ 'src' => $dest ] );
139 $this->assertEquals(
140 $props1,
141 $props2,
142 "Source and destination have the same props ($backendName)." );
143 }
144 } else {
145 $this->assertStatusNotOK(
146 $status,
147 "Copy from $source to $dest fails ($backendName)." );
148 $this->assertSame(
149 is_string( $dstContent ),
150 (bool)$this->backend->fileExists( [ 'src' => $dest ] ),
151 "Destination file $dest unchanged after failed copy ($backendName)." );
152 $this->assertSame(
153 $dstContent,
154 $this->backend->getFileContents( [ 'src' => $dest ] ),
155 "Destination file $dest unchanged after failed copy ($backendName)." );
156 }
157
158 $this->assertSame(
159 is_string( $srcContent ),
160 (bool)$this->backend->fileExists( [ 'src' => $source ] ),
161 "Source file $source unchanged after copy ($backendName)."
162 );
163 $this->assertSame(
164 $srcContent,
165 $this->backend->getFileContents( [ 'src' => $source ] ),
166 "Source file $source unchanged after copy ($backendName)."
167 );
168 if ( is_string( $dstContent ) ) {
169 $this->assertTrue(
170 (bool)$this->backend->fileExists( [ 'src' => $dest ] ),
171 "Destination file $dest exists after copy ($backendName)." );
172 }
173
174 $this->assertBackendPathsConsistent( [ $source, $dest ], $okSyncStatus );
175 }
176
177 /**
178 * @return array (op, source exists, dest exists, op succeeds, sync check succeeds)
179 */
180 public static function provider_testCopy() {
181 $cases = [];
182
183 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
184 $dest = self::baseStorePath() . '/unittest-cont2/a/fileCopied.txt';
185 $opBase = [ 'op' => 'copy', 'src' => $source, 'dst' => $dest ];
186
187 $op = $opBase;
188 $cases[] = [ $op, 'yyy', false, true, true ];
189
190 $op = $opBase;
191 $op['overwrite'] = true;
192 $cases[] = [ $op, 'yyy', false, true, true ];
193
194 $op = $opBase;
195 $op['overwrite'] = true;
196 $cases[] = [ $op, 'yyy', 'xxx', true, true ];
197
198 $op = $opBase;
199 $op['overwriteSame'] = true;
200 $cases[] = [ $op, 'yyy', false, true, true ];
201
202 $op = $opBase;
203 $op['overwriteSame'] = true;
204 $cases[] = [ $op, 'yyy', 'yyy', true, true ];
205
206 $op = $opBase;
207 $op['overwriteSame'] = true;
208 $cases[] = [ $op, 'yyy', 'zzz', false, true ];
209
210 $op = $opBase;
211 $op['ignoreMissingSource'] = true;
212 $cases[] = [ $op, 'xxx', false, true, true ];
213
214 $op = $opBase;
215 $op['ignoreMissingSource'] = true;
216 $cases[] = [ $op, false, false, true, true ];
217
218 $op = $opBase;
219 $op['ignoreMissingSource'] = true;
220 $cases[] = [ $op, false, 'xxx', true, true ];
221
222 $op = $opBase;
223 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
224 $op['ignoreMissingSource'] = true;
225 $cases[] = [ $op, false, false, false, false ];
226
227 return $cases;
228 }
229
230 /**
231 * @dataProvider provider_testMove
232 */
233 public function testMove( $op, $srcContent, $dstContent, $okStatus, $okSyncStatus ) {
234 $backendName = $this->backendClass();
235
236 $source = $op['src'];
237 $dest = $op['dst'];
238 $this->prepare( [ 'dir' => dirname( $source ) ] );
239 $this->prepare( [ 'dir' => dirname( $dest ) ] );
240
241 if ( is_string( $srcContent ) ) {
242 $status = $this->backend->create( [ 'content' => $srcContent, 'dst' => $source ] );
243 $this->assertStatusGood( $status, "Creation of $source succeeded ($backendName)." );
244 }
245 if ( is_string( $dstContent ) ) {
246 $status = $this->backend->create( [ 'content' => $dstContent, 'dst' => $dest ] );
247 $this->assertStatusGood( $status, "Creation of $dest succeeded ($backendName)." );
248 }
249
250 $oldSrcProps = $this->backend->getFileProps( [ 'src' => $source ] );
251
252 $status = $this->backend->doOperation( $op );
253
254 if ( $okStatus ) {
255 $this->assertStatusGood(
256 $status,
257 "Move from $source to $dest succeeded without warnings ($backendName)." );
258 $this->assertEquals( [ 0 => true ], $status->success,
259 "Move from $source to $dest has proper 'success' field in Status ($backendName)." );
260 if ( !is_string( $srcContent ) ) {
261 $this->assertSame(
262 is_string( $dstContent ),
263 $this->backend->fileExists( [ 'src' => $dest ] ),
264 "Destination file $dest unchanged after no-op move ($backendName)." );
265 $this->assertSame(
266 $dstContent,
267 $this->backend->getFileContents( [ 'src' => $dest ] ),
268 "Destination file $dest unchanged after no-op move ($backendName)." );
269 } else {
270 $this->assertEquals(
271 $this->backend->getFileSize( [ 'src' => $dest ] ),
272 strlen( $srcContent ),
273 "Destination file $dest has correct size ($backendName)." );
274 $this->assertEquals(
275 $oldSrcProps,
276 $this->backend->getFileProps( [ 'src' => $dest ] ),
277 "Source and destination have the same props ($backendName)." );
278 }
279 } else {
280 $this->assertStatusNotOK(
281 $status,
282 "Move from $source to $dest fails ($backendName)." );
283 $this->assertSame(
284 is_string( $dstContent ),
285 (bool)$this->backend->fileExists( [ 'src' => $dest ] ),
286 "Destination file $dest unchanged after failed move ($backendName)." );
287 $this->assertSame(
288 $dstContent,
289 $this->backend->getFileContents( [ 'src' => $dest ] ),
290 "Destination file $dest unchanged after failed move ($backendName)." );
291 $this->assertSame(
292 is_string( $srcContent ),
293 (bool)$this->backend->fileExists( [ 'src' => $source ] ),
294 "Source file $source unchanged after failed move ($backendName)."
295 );
296 $this->assertSame(
297 $srcContent,
298 $this->backend->getFileContents( [ 'src' => $source ] ),
299 "Source file $source unchanged after failed move ($backendName)."
300 );
301 }
302
303 if ( is_string( $dstContent ) ) {
304 $this->assertTrue(
305 (bool)$this->backend->fileExists( [ 'src' => $dest ] ),
306 "Destination file $dest exists after move ($backendName)." );
307 }
308
309 $this->assertBackendPathsConsistent( [ $source, $dest ], $okSyncStatus );
310 }
311
312 /**
313 * @return array (op, source exists, dest exists, op succeeds, sync check succeeds)
314 */
315 public static function provider_testMove() {
316 $cases = [];
317
318 $source = self::baseStorePath() . '/unittest-cont1/e/file.txt';
319 $dest = self::baseStorePath() . '/unittest-cont2/a/fileMoved.txt';
320 $opBase = [ 'op' => 'move', 'src' => $source, 'dst' => $dest ];
321
322 $op = $opBase;
323 $cases[] = [ $op, 'yyy', false, true, true ];
324
325 $op = $opBase;
326 $op['overwrite'] = true;
327 $cases[] = [ $op, 'yyy', false, true, true ];
328
329 $op = $opBase;
330 $op['overwrite'] = true;
331 $cases[] = [ $op, 'yyy', 'xxx', true, true ];
332
333 $op = $opBase;
334 $op['overwriteSame'] = true;
335 $cases[] = [ $op, 'yyy', false, true, true ];
336
337 $op = $opBase;
338 $op['overwriteSame'] = true;
339 $cases[] = [ $op, 'yyy', 'yyy', true, true ];
340
341 $op = $opBase;
342 $op['overwriteSame'] = true;
343 $cases[] = [ $op, 'yyy', 'zzz', false, true ];
344
345 $op = $opBase;
346 $op['ignoreMissingSource'] = true;
347 $cases[] = [ $op, 'xxx', false, true, true ];
348
349 $op = $opBase;
350 $op['ignoreMissingSource'] = true;
351 $cases[] = [ $op, false, false, true, true ];
352
353 $op = $opBase;
354 $op['ignoreMissingSource'] = true;
355 $cases[] = [ $op, false, 'xxx', true, true ];
356
357 $op = $opBase;
358 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
359 $op['ignoreMissingSource'] = true;
360 $cases[] = [ $op, false, false, false, false ];
361
362 return $cases;
363 }
364
365 /**
366 * @dataProvider provider_testDelete
367 */
368 public function testDelete( $op, $srcContent, $okStatus, $okSyncStatus ) {
369 $backendName = $this->backendClass();
370
371 $source = $op['src'];
372 $this->prepare( [ 'dir' => dirname( $source ) ] );
373
374 if ( is_string( $srcContent ) ) {
375 $status = $this->backend->doOperation(
376 [ 'op' => 'create', 'content' => $srcContent, 'dst' => $source ] );
377 $this->assertStatusGood( $status,
378 "Creation of file at $source succeeded ($backendName)." );
379 }
380
381 $status = $this->backend->doOperation( $op );
382 if ( $okStatus ) {
383 $this->assertStatusGood( $status,
384 "Deletion of file at $source succeeded without warnings ($backendName)." );
385 $this->assertEquals( [ 0 => true ], $status->success,
386 "Deletion of file at $source has proper 'success' field in Status ($backendName)." );
387 } else {
388 $this->assertStatusNotOK( $status,
389 "Deletion of file at $source failed ($backendName)." );
390 }
391
392 $this->assertFalse(
393 (bool)$this->backend->fileExists( [ 'src' => $source ] ),
394 "Source file $source does not exist after move ($backendName)." );
395
396 $this->assertFalse(
397 $this->backend->getFileSize( [ 'src' => $source ] ),
398 "Source file $source has correct size (false) ($backendName)." );
399
400 $props1 = $this->backend->getFileProps( [ 'src' => $source ] );
401 $this->assertFalse(
402 $props1['fileExists'],
403 "Source file $source does not exist according to props ($backendName)." );
404
405 $this->assertBackendPathsConsistent( [ $source ], $okSyncStatus );
406 }
407
408 /**
409 * @return array (op, source content, op succeeds, sync check succeeds)
410 */
411 public static function provider_testDelete() {
412 $cases = [];
413
414 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
415 $baseOp = [ 'op' => 'delete', 'src' => $source ];
416
417 $op = $baseOp;
418 $cases[] = [ $op, 'xxx', true, true ];
419
420 $op = $baseOp;
421 $op['ignoreMissingSource'] = true;
422 $cases[] = [ $op, 'xxx', true, true ];
423
424 $op = $baseOp;
425 $cases[] = [ $op, false, false, true ];
426
427 $op = $baseOp;
428 $op['ignoreMissingSource'] = true;
429 $cases[] = [ $op, false, true, true ];
430
431 $op = $baseOp;
432 $op['ignoreMissingSource'] = true;
433 $op['src'] = 'mwstore://wrongbackend/unittest-cont1/e/file.txt';
434 $cases[] = [ $op, false, false, false ];
435
436 return $cases;
437 }
438
439 /**
440 * @dataProvider provider_testDescribe
441 */
442 public function testDescribe( $op, $withSource, $okStatus ) {
443 $backendName = $this->backendClass();
444
445 $source = $op['src'];
446 $this->prepare( [ 'dir' => dirname( $source ) ] );
447
448 if ( $withSource ) {
449 $status = $this->backend->doOperation(
450 [ 'op' => 'create', 'content' => 'blahblah', 'dst' => $source,
451 'headers' => [ 'Content-Disposition' => 'xxx' ] ] );
452 $this->assertStatusGood( $status,
453 "Creation of file at $source succeeded ($backendName)." );
454 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
455 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
456 $this->assertHasHeaders( [ 'Content-Disposition' => 'xxx' ], $attr );
457 }
458
459 $status = $this->backend->describe( [ 'src' => $source,
460 'headers' => [ 'Content-Disposition' => '' ] ] ); // remove
461 $this->assertStatusGood( $status,
462 "Removal of header for $source succeeded ($backendName)." );
463
464 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
465 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
466 $this->assertFalse( isset( $attr['headers']['content-disposition'] ),
467 "File 'Content-Disposition' header removed." );
468 }
469 }
470
471 $status = $this->backend->doOperation( $op );
472 if ( $okStatus ) {
473 $this->assertStatusGood( $status,
474 "Describe of file at $source succeeded without warnings ($backendName)." );
475 $this->assertEquals( [ 0 => true ], $status->success,
476 "Describe of file at $source has proper 'success' field in Status ($backendName)." );
477 if ( $this->backend->hasFeatures( FileBackend::ATTR_HEADERS ) ) {
478 $attr = $this->backend->getFileXAttributes( [ 'src' => $source ] );
479 $this->assertHasHeaders( $op['headers'], $attr );
480 }
481 } else {
482 $this->assertStatusNotOK( $status,
483 "Describe of file at $source failed ($backendName)." );
484 }
485
486 $this->assertBackendPathsConsistent( [ $source ], true );
487 }
488
489 private function assertHasHeaders( array $headers, array $attr ) {
490 foreach ( $headers as $n => $v ) {
491 if ( $n !== '' ) {
492 $this->assertTrue( isset( $attr['headers'][strtolower( $n )] ),
493 "File has '$n' header." );
494 $this->assertEquals( $v, $attr['headers'][strtolower( $n )],
495 "File has '$n' header value." );
496 } else {
497 $this->assertFalse( isset( $attr['headers'][strtolower( $n )] ),
498 "File does not have '$n' header." );
499 }
500 }
501 }
502
503 public static function provider_testDescribe() {
504 $cases = [];
505
506 $source = self::baseStorePath() . '/unittest-cont1/e/myfacefile.txt';
507
508 $op = [ 'op' => 'describe', 'src' => $source,
509 'headers' => [ 'Content-Disposition' => 'inline' ], ];
510 $cases[] = [
511 $op, // operation
512 true, // with source
513 true // succeeds
514 ];
515
516 $cases[] = [
517 $op, // operation
518 false, // without source
519 false // fails
520 ];
521
522 return $cases;
523 }
524
525 /**
526 * @dataProvider provider_testCreate
527 */
528 public function testCreate( $op, $alreadyExists, $okStatus, $newSize ) {
529 $backendName = $this->backendClass();
530
531 $dest = $op['dst'];
532 $this->prepare( [ 'dir' => dirname( $dest ) ] );
533
534 $oldText = 'blah...blah...waahwaah';
535 if ( $alreadyExists ) {
536 $status = $this->backend->doOperation(
537 [ 'op' => 'create', 'content' => $oldText, 'dst' => $dest ] );
538 $this->assertStatusGood( $status,
539 "Creation of file at $dest succeeded ($backendName)." );
540 }
541
542 $status = $this->backend->doOperation( $op );
543 if ( $okStatus ) {
544 $this->assertStatusGood( $status,
545 "Creation of file at $dest succeeded without warnings ($backendName)." );
546 $this->assertEquals( [ 0 => true ], $status->success,
547 "Creation of file at $dest has proper 'success' field in Status ($backendName)." );
548 } else {
549 $this->assertStatusNotOK( $status,
550 "Creation of file at $dest failed ($backendName)." );
551 }
552
553 $this->assertTrue( $this->backend->fileExists( [ 'src' => $dest ] ),
554 "Destination file $dest exists after creation ($backendName)." );
555
556 $props1 = $this->backend->getFileProps( [ 'src' => $dest ] );
557 $this->assertTrue( $props1['fileExists'],
558 "Destination file $dest exists according to props ($backendName)." );
559 if ( $okStatus ) { // file content is what we saved
560 $this->assertEquals( $newSize, $props1['size'],
561 "Destination file $dest has expected size according to props ($backendName)." );
562 $this->assertEquals( $newSize,
563 $this->backend->getFileSize( [ 'src' => $dest ] ),
564 "Destination file $dest has correct size ($backendName)." );
565 } else { // file content is some other previous text
566 $this->assertEquals( strlen( $oldText ), $props1['size'],
567 "Destination file $dest has original size according to props ($backendName)." );
568 $this->assertEquals( strlen( $oldText ),
569 $this->backend->getFileSize( [ 'src' => $dest ] ),
570 "Destination file $dest has original size according to props ($backendName)." );
571 }
572
573 $this->assertBackendPathsConsistent( [ $dest ], true );
574 }
575
576 /**
577 * @dataProvider provider_testCreate
578 */
579 public static function provider_testCreate() {
580 $cases = [];
581
582 $dest = self::baseStorePath() . '/unittest-cont2/a/myspacefile.txt';
583
584 $op = [ 'op' => 'create', 'content' => 'test test testing', 'dst' => $dest ];
585 $cases[] = [
586 $op, // operation
587 false, // no dest already exists
588 true, // succeeds
589 strlen( $op['content'] )
590 ];
591
592 $op2 = $op;
593 $op2['content'] = "\n";
594 $cases[] = [
595 $op2, // operation
596 false, // no dest already exists
597 true, // succeeds
598 strlen( $op2['content'] )
599 ];
600
601 $op2 = $op;
602 $op2['content'] = "fsf\n waf 3kt";
603 $cases[] = [
604 $op2, // operation
605 true, // dest already exists
606 false, // fails
607 strlen( $op2['content'] )
608 ];
609
610 $op2 = $op;
611 $op2['content'] = "egm'g gkpe gpqg eqwgwqg";
612 $op2['overwrite'] = true;
613 $cases[] = [
614 $op2, // operation
615 true, // dest already exists
616 true, // succeeds
617 strlen( $op2['content'] )
618 ];
619
620 $op2 = $op;
621 $op2['content'] = "39qjmg3-qg";
622 $op2['overwriteSame'] = true;
623 $cases[] = [
624 $op2, // operation
625 true, // dest already exists
626 false, // succeeds
627 strlen( $op2['content'] )
628 ];
629
630 return $cases;
631 }
632
633 /**
634 * @dataProvider provider_quickOperations
635 */
636 public function testDoQuickOperations(
637 $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, $batchSize
638 ) {
639 $backendName = $this->backendClass();
640
641 foreach ( $files as $path ) {
642 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
643 $this->assertStatusGood( $status,
644 "Preparing $path succeeded without warnings ($backendName)." );
645 }
646
647 foreach ( array_chunk( $createOps, $batchSize ) as $batchOps ) {
648 $this->assertStatusGood(
649 $this->backend->doQuickOperations( $batchOps ),
650 "Creation of source files succeeded ($backendName)."
651 );
652 }
653 foreach ( $files as $file ) {
654 $this->assertTrue(
655 $this->backend->fileExists( [ 'src' => $file ] ),
656 "File $file exists."
657 );
658 }
659
660 foreach ( array_chunk( $copyOps, $batchSize ) as $batchOps ) {
661 $this->assertStatusGood(
662 $this->backend->doQuickOperations( $batchOps ),
663 "Quick copy of source files succeeded ($backendName)."
664 );
665 }
666 foreach ( $files as $file ) {
667 $this->assertTrue(
668 $this->backend->fileExists( [ 'src' => "$file-2" ] ),
669 "File $file-2 exists."
670 );
671 }
672
673 foreach ( array_chunk( $moveOps, $batchSize ) as $batchOps ) {
674 $this->assertStatusGood(
675 $this->backend->doQuickOperations( $batchOps ),
676 "Quick move of source files succeeded ($backendName)."
677 );
678 }
679 foreach ( $files as $file ) {
680 $this->assertTrue(
681 $this->backend->fileExists( [ 'src' => "$file-3" ] ),
682 "File $file-3 move in."
683 );
684 $this->assertFalse(
685 $this->backend->fileExists( [ 'src' => "$file-2" ] ),
686 "File $file-2 moved away."
687 );
688 }
689
690 foreach ( array_chunk( $overSelfOps, $batchSize ) as $batchOps ) {
691 $this->assertStatusGood(
692 $this->backend->doQuickOperations( $batchOps ),
693 "Quick copy/move of source files over themselves succeeded ($backendName)."
694 );
695 }
696 foreach ( $files as $file ) {
697 $this->assertTrue(
698 $this->backend->fileExists( [ 'src' => $file ] ),
699 "File $file still exists after copy/move over self."
700 );
701 }
702
703 foreach ( array_chunk( $deleteOps, $batchSize ) as $batchOps ) {
704 $this->assertStatusGood(
705 $this->backend->doQuickOperations( $batchOps ),
706 "Quick deletion of source files succeeded ($backendName)."
707 );
708 }
709 foreach ( $files as $file ) {
710 $this->assertFalse( $this->backend->fileExists( [ 'src' => $file ] ),
711 "File $file purged." );
712 $this->assertFalse( $this->backend->fileExists( [ 'src' => "$file-3" ] ),
713 "File $file-3 purged." );
714 }
715 }
716
717 public static function provider_quickOperations() {
718 $base = self::baseStorePath();
719 $files = [
720 "$base/unittest-cont1/e/fileA.a",
721 "$base/unittest-cont1/e/fileB.a",
722 "$base/unittest-cont1/e/fileC.a"
723 ];
724
725 $createOps = $copyOps = $moveOps = $overSelfOps = $deleteOps = [];
726 foreach ( $files as $path ) {
727 $createOps[] = [ 'op' => 'create', 'dst' => $path, 'content' => 52525 ];
728 $createOps[] = [ 'op' => 'create', 'dst' => "$path-x", 'content' => 832 ];
729 $createOps[] = [ 'op' => 'null' ];
730
731 $copyOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => "$path-2" ];
732 $copyOps[] = [
733 'op' => 'copy',
734 'src' => "$path-nothing",
735 'dst' => "$path-nowhere",
736 'ignoreMissingSource' => true
737 ];
738 $copyOps[] = [ 'op' => 'null' ];
739
740 $moveOps[] = [ 'op' => 'move', 'src' => "$path-2", 'dst' => "$path-3" ];
741 $moveOps[] = [
742 'op' => 'move',
743 'src' => "$path-nothing",
744 'dst' => "$path-nowhere",
745 'ignoreMissingSource' => true
746 ];
747 $moveOps[] = [ 'op' => 'null' ];
748
749 $overSelfOps[] = [ 'op' => 'copy', 'src' => $path, 'dst' => $path ];
750 $overSelfOps[] = [ 'op' => 'move', 'src' => $path, 'dst' => $path ];
751
752 $deleteOps[] = [ 'op' => 'delete', 'src' => $path ];
753 $deleteOps[] = [ 'op' => 'delete', 'src' => "$path-3" ];
754 $deleteOps[] = [
755 'op' => 'delete',
756 'src' => "$path-gone",
757 'ignoreMissingSource' => true
758 ];
759 $deleteOps[] = [ 'op' => 'null' ];
760 }
761
762 return [
763 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 1 ],
764 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 2 ],
765 [ $files, $createOps, $copyOps, $moveOps, $overSelfOps, $deleteOps, 100 ]
766 ];
767 }
768
769 /**
770 * @dataProvider provider_testConcatenate
771 */
772 public function testConcatenate( $params, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
773 $backendName = $this->backendClass();
774
775 $expContent = '';
776 // Create sources
777 $ops = [];
778 foreach ( $srcs as $i => $source ) {
779 $this->prepare( [ 'dir' => dirname( $source ) ] );
780 $ops[] = [
781 'op' => 'create', // operation
782 'dst' => $source, // source
783 'content' => $srcsContent[$i]
784 ];
785 $expContent .= $srcsContent[$i];
786 }
787 $status = $this->backend->doOperations( $ops );
788
789 $this->assertStatusGood( $status,
790 "Creation of source files succeeded ($backendName)." );
791
792 $dest = $params['dst'] = $this->getNewTempFile();
793 if ( $alreadyExists ) {
794 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
795 $this->assertTrue( $ok,
796 "Creation of file at $dest succeeded ($backendName)." );
797 } else {
798 $ok = file_put_contents( $dest, '' ) !== false;
799 $this->assertTrue( $ok,
800 "Creation of 0-byte file at $dest succeeded ($backendName)." );
801 }
802
803 // Combine the files into one
804 $status = $this->backend->concatenate( $params );
805 if ( $okStatus ) {
806 $this->assertStatusGood( $status,
807 "Creation of concat file at $dest succeeded without warnings ($backendName)." );
808 } else {
809 $this->assertStatusNotOK( $status,
810 "Creation of concat file at $dest failed ($backendName)." );
811 }
812
813 if ( $okStatus ) {
814 $this->assertTrue( is_file( $dest ),
815 "Dest concat file $dest exists after creation ($backendName)." );
816 } else {
817 $this->assertTrue( is_file( $dest ),
818 "Dest concat file $dest exists after failed creation ($backendName)." );
819 }
820
821 $contents = file_get_contents( $dest );
822 $this->assertIsString( $contents, "File at $dest exists ($backendName)." );
823
824 if ( $okStatus ) {
825 $this->assertEquals( $expContent, $contents,
826 "Concat file at $dest has correct contents ($backendName)." );
827 } else {
828 $this->assertNotEquals( $expContent, $contents,
829 "Concat file at $dest has correct contents ($backendName)." );
830 }
831 }
832
833 public static function provider_testConcatenate() {
834 $cases = [];
835
836 $srcs = [
837 self::baseStorePath() . '/unittest-cont1/e/file1.txt',
838 self::baseStorePath() . '/unittest-cont1/e/file2.txt',
839 self::baseStorePath() . '/unittest-cont1/e/file3.txt',
840 self::baseStorePath() . '/unittest-cont1/e/file4.txt',
841 self::baseStorePath() . '/unittest-cont1/e/file5.txt',
842 self::baseStorePath() . '/unittest-cont1/e/file6.txt',
843 self::baseStorePath() . '/unittest-cont1/e/file7.txt',
844 self::baseStorePath() . '/unittest-cont1/e/file8.txt',
845 self::baseStorePath() . '/unittest-cont1/e/file9.txt',
846 self::baseStorePath() . '/unittest-cont1/e/file10.txt'
847 ];
848 $content = [
849 'egfage',
850 'ageageag',
851 'rhokohlr',
852 'shgmslkg',
853 'kenga',
854 'owagmal',
855 'kgmae',
856 'g eak;g',
857 'lkaem;a',
858 'legma'
859 ];
860 $params = [ 'srcs' => $srcs ];
861
862 $cases[] = [
863 $params, // operation
864 $srcs, // sources
865 $content, // content for each source
866 false, // no dest already exists
867 true, // succeeds
868 ];
869
870 $cases[] = [
871 $params, // operation
872 $srcs, // sources
873 $content, // content for each source
874 true, // dest already exists
875 false, // succeeds
876 ];
877
878 return $cases;
879 }
880
881 /**
882 * @dataProvider provider_testGetFileStat
883 */
884 public function testGetFileStat( $path, $content, $alreadyExists ) {
885 $backendName = $this->backendClass();
886
887 if ( $alreadyExists ) {
888 $this->prepare( [ 'dir' => dirname( $path ) ] );
889 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
890 $this->assertStatusGood( $status,
891 "Creation of file at $path succeeded ($backendName)." );
892
893 $size = $this->backend->getFileSize( [ 'src' => $path ] );
894 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
895 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
896
897 $this->assertEquals( strlen( $content ), $size,
898 "Correct file size of '$path'" );
899 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
900 "Correct file timestamp of '$path'" );
901
902 $size = $stat['size'];
903 $time = $stat['mtime'];
904 $this->assertEquals( strlen( $content ), $size,
905 "Correct file size of '$path'" );
906 $this->assertTrue( abs( time() - wfTimestamp( TS_UNIX, $time ) ) < 10,
907 "Correct file timestamp of '$path'" );
908
909 $this->backend->clearCache( [ $path ] );
910
911 $size = $this->backend->getFileSize( [ 'src' => $path ] );
912
913 $this->assertEquals( strlen( $content ), $size,
914 "Correct file size of '$path'" );
915
916 $this->backend->preloadCache( [ $path ] );
917
918 $size = $this->backend->getFileSize( [ 'src' => $path ] );
919
920 $this->assertEquals( strlen( $content ), $size,
921 "Correct file size of '$path'" );
922 } else {
923 $size = $this->backend->getFileSize( [ 'src' => $path ] );
924 $time = $this->backend->getFileTimestamp( [ 'src' => $path ] );
925 $stat = $this->backend->getFileStat( [ 'src' => $path ] );
926
927 $this->assertFalse( $size, "Correct file size of '$path'" );
928 $this->assertFalse( $time, "Correct file timestamp of '$path'" );
929 $this->assertFalse( $stat, "Correct file stat of '$path'" );
930 }
931 }
932
933 public static function provider_testGetFileStat() {
934 $cases = [];
935
936 $base = self::baseStorePath();
937 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents", true ];
938 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "", true ];
939 $cases[] = [ "$base/unittest-cont1/e/b/some-diff_file.txt", null, false ];
940
941 return $cases;
942 }
943
944 /**
945 * @dataProvider provider_testGetFileStat
946 */
947 public function testStreamFile( $path, $content, $alreadyExists ) {
948 $backendName = $this->backendClass();
949
950 if ( $content !== null ) {
951 $this->prepare( [ 'dir' => dirname( $path ) ] );
952 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
953 $this->assertStatusGood( $status,
954 "Creation of file at $path succeeded ($backendName)." );
955
956 ob_start();
957 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
958 $data = ob_get_contents();
959 ob_end_clean();
960
961 $this->assertEquals( $content, $data, "Correct content streamed from '$path'" );
962 } else { // 404 case
963 ob_start();
964 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1 ] );
965 $data = ob_get_contents();
966 ob_end_clean();
967
968 $this->assertMatchesRegularExpression( '#<h1>File not found</h1>#', $data,
969 "Correct content streamed from '$path' ($backendName)" );
970 }
971 }
972
973 public static function provider_testStreamFile() {
974 $cases = [];
975
976 $base = self::baseStorePath();
977 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
978 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", null ];
979
980 return $cases;
981 }
982
983 public function testStreamFileRange() {
984 $backendName = $this->backendClass();
985
986 $base = self::baseStorePath();
987 $path = "$base/unittest-cont1/e/b/z/range_file.txt";
988 $content = "0123456789ABCDEF";
989
990 $this->prepare( [ 'dir' => dirname( $path ) ] );
991 $status = $this->create( [ 'dst' => $path, 'content' => $content ] );
992 $this->assertStatusGood( $status,
993 "Creation of file at $path succeeded ($backendName)." );
994
995 static $ranges = [
996 'bytes=0-0' => '0',
997 'bytes=0-3' => '0123',
998 'bytes=4-8' => '45678',
999 'bytes=15-15' => 'F',
1000 'bytes=14-15' => 'EF',
1001 'bytes=-5' => 'BCDEF',
1002 'bytes=-1' => 'F',
1003 'bytes=10-16' => 'ABCDEF',
1004 'bytes=10-99' => 'ABCDEF',
1005 ];
1006
1007 foreach ( $ranges as $range => $chunk ) {
1008 ob_start();
1009 $this->backend->streamFile( [ 'src' => $path, 'headless' => 1, 'allowOB' => 1,
1010 'options' => [ 'range' => $range ] ] );
1011 $data = ob_get_contents();
1012 ob_end_clean();
1013
1014 $this->assertEquals( $chunk, $data, "Correct chunk streamed from '$path' for '$range'" );
1015 }
1016 }
1017
1018 /**
1019 * @dataProvider provider_testGetFileContents
1020 */
1021 public function testGetFileContents( $source, $content ) {
1022 $backendName = $this->backendClass();
1023
1024 $srcs = (array)$source;
1025 $content = (array)$content;
1026 foreach ( $srcs as $i => $src ) {
1027 $this->prepare( [ 'dir' => dirname( $src ) ] );
1028 $status = $this->backend->doOperation(
1029 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1030 $this->assertStatusGood( $status,
1031 "Creation of file at $src succeeded ($backendName)." );
1032 }
1033
1034 if ( is_array( $source ) ) {
1035 $contents = $this->backend->getFileContentsMulti( [ 'srcs' => $source ] );
1036 foreach ( $contents as $path => $data ) {
1037 $this->assertIsString( $data, "Contents of $path exists ($backendName)." );
1038 $this->assertEquals(
1039 current( $content ),
1040 $data,
1041 "Contents of $path is correct ($backendName)."
1042 );
1043 next( $content );
1044 }
1045 $this->assertEquals(
1046 $source,
1047 array_keys( $contents ),
1048 "Contents in right order ($backendName)."
1049 );
1050 $this->assertSameSize(
1051 $source,
1052 $contents,
1053 "Contents array size correct ($backendName)."
1054 );
1055 } else {
1056 $data = $this->backend->getFileContents( [ 'src' => $source ] );
1057 $this->assertIsString( $data, "Contents of $source exists ($backendName)." );
1058 $this->assertEquals( $content[0], $data, "Contents of $source is correct ($backendName)." );
1059 }
1060 }
1061
1062 public static function provider_testGetFileContents() {
1063 $cases = [];
1064
1065 $base = self::baseStorePath();
1066 $cases[] = [ "$base/unittest-cont1/e/b/z/some_file.txt", "some file contents" ];
1067 $cases[] = [ "$base/unittest-cont1/e/b/some-other_file.txt", "more file contents" ];
1068 $cases[] = [
1069 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1070 "$base/unittest-cont1/e/a/z.txt" ],
1071 [ "contents xx", "contents xy", "contents xz" ]
1072 ];
1073
1074 return $cases;
1075 }
1076
1077 /**
1078 * @dataProvider provider_testGetLocalCopy
1079 */
1080 public function testGetLocalCopy( $source, $content ) {
1081 $backendName = $this->backendClass();
1082
1083 $srcs = (array)$source;
1084 $content = (array)$content;
1085 foreach ( $srcs as $i => $src ) {
1086 $this->prepare( [ 'dir' => dirname( $src ) ] );
1087 $status = $this->backend->doOperation(
1088 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1089 $this->assertStatusGood( $status,
1090 "Creation of file at $src succeeded ($backendName)." );
1091 }
1092
1093 if ( is_array( $source ) ) {
1094 $tmpFiles = $this->backend->getLocalCopyMulti( [ 'srcs' => $source ] );
1095 foreach ( $tmpFiles as $path => $tmpFile ) {
1096 $this->assertNotNull( $tmpFile,
1097 "Creation of local copy of $path succeeded ($backendName)." );
1098 $contents = file_get_contents( $tmpFile->getPath() );
1099 $this->assertIsString( $contents, "Local copy of $path exists ($backendName)." );
1100 $this->assertEquals(
1101 current( $content ),
1102 $contents,
1103 "Local copy of $path is correct ($backendName)."
1104 );
1105 next( $content );
1106 }
1107 $this->assertEquals(
1108 $source,
1109 array_keys( $tmpFiles ),
1110 "Local copies in right order ($backendName)."
1111 );
1112 $this->assertSameSize(
1113 $source,
1114 $tmpFiles,
1115 "Local copies array size correct ($backendName)."
1116 );
1117 } else {
1118 $tmpFile = $this->backend->getLocalCopy( [ 'src' => $source ] );
1119 $this->assertNotNull( $tmpFile,
1120 "Creation of local copy of $source succeeded ($backendName)." );
1121 $contents = file_get_contents( $tmpFile->getPath() );
1122 $this->assertIsString( $contents, "Local copy of $source exists ($backendName)." );
1123 $this->assertEquals(
1124 $content[0],
1125 $contents,
1126 "Local copy of $source is correct ($backendName)."
1127 );
1128 }
1129
1130 $obj = (object)[];
1131 $tmpFile->bind( $obj );
1132 }
1133
1134 public static function provider_testGetLocalCopy() {
1135 $cases = [];
1136
1137 $base = self::baseStorePath();
1138 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1139 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1140 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1141 $cases[] = [
1142 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1143 "$base/unittest-cont1/e/a/z.txt" ],
1144 [ "contents xx $", "contents xy 111", "contents xz" ]
1145 ];
1146
1147 return $cases;
1148 }
1149
1150 /**
1151 * @dataProvider provider_testGetLocalReference
1152 */
1153 public function testGetLocalReference( $source, $content ) {
1154 $backendName = $this->backendClass();
1155
1156 $srcs = (array)$source;
1157 $content = (array)$content;
1158 foreach ( $srcs as $i => $src ) {
1159 $this->prepare( [ 'dir' => dirname( $src ) ] );
1160 $status = $this->backend->doOperation(
1161 [ 'op' => 'create', 'content' => $content[$i], 'dst' => $src ] );
1162 $this->assertStatusGood( $status,
1163 "Creation of file at $src succeeded ($backendName)." );
1164 }
1165
1166 if ( is_array( $source ) ) {
1167 $tmpFiles = $this->backend->getLocalReferenceMulti( [ 'srcs' => $source ] );
1168 foreach ( $tmpFiles as $path => $tmpFile ) {
1169 $this->assertNotNull( $tmpFile,
1170 "Creation of local copy of $path succeeded ($backendName)." );
1171 $contents = file_get_contents( $tmpFile->getPath() );
1172 $this->assertIsString( $contents, "Local ref of $path exists ($backendName)." );
1173 $this->assertEquals(
1174 current( $content ),
1175 $contents,
1176 "Local ref of $path is correct ($backendName)."
1177 );
1178 next( $content );
1179 }
1180 $this->assertEquals(
1181 $source,
1182 array_keys( $tmpFiles ),
1183 "Local refs in right order ($backendName)."
1184 );
1185 $this->assertSameSize(
1186 $source,
1187 $tmpFiles,
1188 "Local refs array size correct ($backendName)."
1189 );
1190 } else {
1191 $tmpFile = $this->backend->getLocalReference( [ 'src' => $source ] );
1192 $this->assertNotNull( $tmpFile,
1193 "Creation of local copy of $source succeeded ($backendName)." );
1194 $contents = file_get_contents( $tmpFile->getPath() );
1195 $this->assertIsString( $contents, "Local ref of $source exists ($backendName)." );
1196 $this->assertEquals( $content[0], $contents, "Local ref of $source is correct ($backendName)." );
1197 }
1198 }
1199
1200 public static function provider_testGetLocalReference() {
1201 $cases = [];
1202
1203 $base = self::baseStorePath();
1204 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1205 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1206 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1207 $cases[] = [
1208 [ "$base/unittest-cont1/e/a/x.txt", "$base/unittest-cont1/e/a/y.txt",
1209 "$base/unittest-cont1/e/a/z.txt" ],
1210 [ "contents xx 1111", "contents xy %", "contents xz $" ]
1211 ];
1212
1213 return $cases;
1214 }
1215
1216 public function testGetLocalCopyAndReference404() {
1217 $backendName = $this->backendClass();
1218
1219 $base = self::baseStorePath();
1220
1221 $tmpFile = $this->backend->getLocalCopy( [
1222 'src' => "$base/unittest-cont1/a/not-there" ] );
1223 $this->assertFalse( $tmpFile, "Local copy of not existing file is false ($backendName)." );
1224
1225 $tmpFile = $this->backend->getLocalReference( [
1226 'src' => "$base/unittest-cont1/a/not-there" ] );
1227 $this->assertFalse( $tmpFile, "Local ref of not existing file is false ($backendName)." );
1228 }
1229
1230 /**
1231 * Get a real HTTP request factory, overriding the restrictions on tests that do HTTP requests
1232 * @return HttpRequestFactory
1233 */
1234 private function getRealHttpRequestFactory() {
1235 return new HttpRequestFactory(
1236 new ServiceOptions(
1237 HttpRequestFactory::CONSTRUCTOR_OPTIONS,
1238 $this->getServiceContainer()->getMainConfig()
1239 ),
1240 LoggerFactory::getInstance( 'http' )
1241 );
1242 }
1243
1244 /**
1245 * @dataProvider provider_testGetFileHttpUrl
1246 */
1247 public function testGetFileHttpUrl( $source, $content ) {
1248 $backendName = $this->backendClass();
1249
1250 $this->prepare( [ 'dir' => dirname( $source ) ] );
1251 $status = $this->backend->doOperation(
1252 [ 'op' => 'create', 'content' => $content, 'dst' => $source ] );
1253 $this->assertStatusGood( $status,
1254 "Creation of file at $source succeeded ($backendName)." );
1255
1256 $url = $this->backend->getFileHttpUrl( [ 'src' => $source ] );
1257
1258 if ( $url !== null ) { // supported
1259 $data = $this->getRealHttpRequestFactory()->get( $url, [], __METHOD__ );
1260 $this->assertEquals( $content, $data,
1261 "HTTP GET of URL has right contents ($backendName)." );
1262 }
1263 }
1264
1265 public static function provider_testGetFileHttpUrl() {
1266 $cases = [];
1267
1268 $base = self::baseStorePath();
1269 $cases[] = [ "$base/unittest-cont1/e/a/z/some_file.txt", "some file contents" ];
1270 $cases[] = [ "$base/unittest-cont1/e/a/some-other_file.txt", "more file contents" ];
1271 $cases[] = [ "$base/unittest-cont1/e/a/\$odd&.txt", "test file contents" ];
1272
1273 return $cases;
1274 }
1275
1276 public function testAddShellboxInputFile() {
1277 if ( wfIsWindows() ) {
1278 $this->markTestSkipped( 'This test requires a POSIX environment.' );
1279 }
1280 $backendName = $this->backendClass();
1281 $base = self::baseStorePath();
1282 $src = "$base/unittest-cont1/e/a/b/fileA.txt";
1283 $contents = 'test';
1284 $this->prepare( [ 'dir' => dirname( $src ) ] );
1285 $status = $this->backend->create( [ 'content' => $contents, 'dst' => $src ] );
1286 $this->assertStatusGood( $status,
1287 "Creation of file at $src succeeded ($backendName)." );
1288 $executor = Shellbox::createBoxedExecutor();
1289 $executor->setUrlFileClient( $this->getRealHttpRequestFactory()->createGuzzleClient() );
1290 $command = $executor->createCommand();
1291 $this->backend->addShellboxInputFile( $command, 'fileA.txt', [ 'src' => $src ] );
1292 $result = $command
1293 ->params( 'cat', 'fileA.txt' )
1294 ->routeName( 'test' )
1295 ->execute();
1296 $this->assertSame( 'test', $result->getStdout() );
1297 }
1298
1299 public static function provider_testPrepareAndClean() {
1300 $base = self::baseStorePath();
1301
1302 return [
1303 [ "$base/unittest-cont1/e/a/z/some_file1.txt", true ],
1304 [ "$base/unittest-cont2/a/z/some_file2.txt", true ],
1305 # Specific to FS backend with no basePath field set
1306 # [ "$base/unittest-cont3/a/z/some_file3.txt", false ],
1307 ];
1308 }
1309
1310 /**
1311 * @dataProvider provider_testPrepareAndClean
1312 */
1313 public function testPrepareAndClean( $path, $isOK ) {
1314 $backendName = $this->backendClass();
1315
1316 $status = $this->prepare( [ 'dir' => dirname( $path ) ] );
1317 if ( $isOK ) {
1318 $this->assertStatusGood( $status,
1319 "Preparing dir $path succeeded without warnings ($backendName)." );
1320 } else {
1321 $this->assertStatusNotOK( $status,
1322 "Preparing dir $path failed ($backendName)." );
1323 }
1324
1325 $status = $this->backend->secure( [ 'dir' => dirname( $path ) ] );
1326 if ( $isOK ) {
1327 $this->assertStatusGood( $status,
1328 "Securing dir $path succeeded without warnings ($backendName)." );
1329 } else {
1330 $this->assertStatusNotOK( $status,
1331 "Securing dir $path failed ($backendName)." );
1332 }
1333
1334 $status = $this->backend->publish( [ 'dir' => dirname( $path ) ] );
1335 if ( $isOK ) {
1336 $this->assertStatusGood( $status,
1337 "Publishing dir $path succeeded without warnings ($backendName)." );
1338 } else {
1339 $this->assertStatusNotOK( $status,
1340 "Publishing dir $path failed ($backendName)." );
1341 }
1342
1343 $status = $this->backend->clean( [ 'dir' => dirname( $path ) ] );
1344 if ( $isOK ) {
1345 $this->assertStatusGood( $status,
1346 "Cleaning dir $path succeeded without warnings ($backendName)." );
1347 } else {
1348 $this->assertStatusNotOK( $status,
1349 "Cleaning dir $path failed ($backendName)." );
1350 }
1351 }
1352
1353 public function testRecursiveClean() {
1354 $backendName = $this->backendClass();
1355
1356 $base = self::baseStorePath();
1357 $dirs = [
1358 "$base/unittest-cont1",
1359 "$base/unittest-cont1/e",
1360 "$base/unittest-cont1/e/a",
1361 "$base/unittest-cont1/e/a/b",
1362 "$base/unittest-cont1/e/a/b/c",
1363 "$base/unittest-cont1/e/a/b/c/d0",
1364 "$base/unittest-cont1/e/a/b/c/d1",
1365 "$base/unittest-cont1/e/a/b/c/d2",
1366 "$base/unittest-cont1/e/a/b/c/d0/1",
1367 "$base/unittest-cont1/e/a/b/c/d0/2",
1368 "$base/unittest-cont1/e/a/b/c/d1/3",
1369 "$base/unittest-cont1/e/a/b/c/d1/4",
1370 "$base/unittest-cont1/e/a/b/c/d2/5",
1371 "$base/unittest-cont1/e/a/b/c/d2/6"
1372 ];
1373 foreach ( $dirs as $dir ) {
1374 $status = $this->prepare( [ 'dir' => $dir ] );
1375 $this->assertStatusGood( $status,
1376 "Preparing dir $dir succeeded without warnings ($backendName)." );
1377 }
1378
1379 if ( $this->backend instanceof FSFileBackend ) {
1380 foreach ( $dirs as $dir ) {
1381 $this->assertTrue( $this->backend->directoryExists( [ 'dir' => $dir ] ),
1382 "Dir $dir exists ($backendName)." );
1383 }
1384 }
1385
1386 $status = $this->backend->clean(
1387 [ 'dir' => "$base/unittest-cont1", 'recursive' => 1 ] );
1388 $this->assertStatusGood( $status,
1389 "Recursive cleaning of dir $dir succeeded without warnings ($backendName)." );
1390
1391 foreach ( $dirs as $dir ) {
1392 $this->assertFalse( $this->backend->directoryExists( [ 'dir' => $dir ] ),
1393 "Dir $dir no longer exists ($backendName)." );
1394 }
1395 }
1396
1397 public function testDoOperationsSuccessful() {
1398 $base = self::baseStorePath();
1399
1400 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1401 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1402 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1403 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1404 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1405 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1406 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1407
1408 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1409 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1410 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1411 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1412 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1413 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1414 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1415
1416 $status = $this->backend->doOperations( [
1417 [ 'op' => 'describe', 'src' => $fileA,
1418 'headers' => [ 'X-Content-Length' => '91.3' ], 'disposition' => 'inline' ],
1419 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1420 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1421 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1422 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1423 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1424 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1425 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1426 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1427 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1428 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1429 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1430 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1431 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1432 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1433 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1434 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1435 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1436 // Does nothing
1437 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1438 // Does nothing
1439 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1440 // Does nothing
1441 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1442 // Does nothing
1443 [ 'op' => 'null' ],
1444 // Does nothing
1445 ] );
1446
1447 $this->assertStatusGood( $status, "Operation batch succeeded" );
1448 $this->assertCount( 14, $status->success,
1449 "Operation batch has correct success array" );
1450
1451 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileA ] ),
1452 "File does not exist at $fileA" );
1453 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileB ] ),
1454 "File does not exist at $fileB" );
1455 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileD ] ),
1456 "File does not exist at $fileD" );
1457
1458 $this->assertTrue( $this->backend->fileExists( [ 'src' => $fileC ] ),
1459 "File exists at $fileC" );
1460 $this->assertEquals( $fileBContents,
1461 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1462 "Correct file contents of $fileC" );
1463 $this->assertEquals( strlen( $fileBContents ),
1464 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1465 "Correct file size of $fileC" );
1466 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1467 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1468 "Correct file SHA-1 of $fileC" );
1469 }
1470
1471 public function testDoOperationsPipeline() {
1472 $base = self::baseStorePath();
1473
1474 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1475 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1476 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1477
1478 $tmpNameA = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1479 $tmpNameB = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1480 $tmpNameC = TempFSFile::factory( "unittests_", 'txt', wfTempDir() )->getPath();
1481 $this->addTmpFiles( [ $tmpNameA, $tmpNameB, $tmpNameC ] );
1482 file_put_contents( $tmpNameA, $fileAContents );
1483 file_put_contents( $tmpNameB, $fileBContents );
1484 file_put_contents( $tmpNameC, $fileCContents );
1485
1486 $fileA = "$base/unittest-cont1/e/a/b/fileA.txt";
1487 $fileB = "$base/unittest-cont1/e/a/b/fileB.txt";
1488 $fileC = "$base/unittest-cont1/e/a/b/fileC.txt";
1489 $fileD = "$base/unittest-cont1/e/a/b/fileD.txt";
1490
1491 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1492 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1493 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1494 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1495 $this->prepare( [ 'dir' => dirname( $fileD ) ] );
1496
1497 $status = $this->backend->doOperations( [
1498 [ 'op' => 'store', 'src' => $tmpNameA, 'dst' => $fileA, 'overwriteSame' => 1 ],
1499 [ 'op' => 'store', 'src' => $tmpNameB, 'dst' => $fileB, 'overwrite' => 1 ],
1500 [ 'op' => 'store', 'src' => $tmpNameC, 'dst' => $fileC, 'overwrite' => 1 ],
1501 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1502 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1503 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1504 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1505 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD, 'overwrite' => 1 ],
1506 // Now: A:<A>, B:<B>, C:<empty>, D:<A>
1507 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC ],
1508 // Now: A:<A>, B:<empty>, C:<B>, D:<A>
1509 [ 'op' => 'move', 'src' => $fileD, 'dst' => $fileA, 'overwriteSame' => 1 ],
1510 // Now: A:<A>, B:<empty>, C:<B>, D:<empty>
1511 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileA, 'overwrite' => 1 ],
1512 // Now: A:<B>, B:<empty>, C:<empty>, D:<empty>
1513 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC ],
1514 // Now: A:<B>, B:<empty>, C:<B>, D:<empty>
1515 [ 'op' => 'move', 'src' => $fileA, 'dst' => $fileC, 'overwriteSame' => 1 ],
1516 // Now: A:<empty>, B:<empty>, C:<B>, D:<empty>
1517 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1518 // Does nothing
1519 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1520 // Does nothing
1521 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwrite' => 1 ],
1522 // Does nothing
1523 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ],
1524 // Does nothing
1525 [ 'op' => 'null' ],
1526 // Does nothing
1527 ] );
1528
1529 $this->assertStatusGood( $status, "Operation batch succeeded" );
1530 $this->assertCount( 16, $status->success,
1531 "Operation batch has correct success array" );
1532
1533 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileA ] ),
1534 "File does not exist at $fileA" );
1535 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileB ] ),
1536 "File does not exist at $fileB" );
1537 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileD ] ),
1538 "File does not exist at $fileD" );
1539
1540 $this->assertTrue( $this->backend->fileExists( [ 'src' => $fileC ] ),
1541 "File exists at $fileC" );
1542 $this->assertEquals( $fileBContents,
1543 $this->backend->getFileContents( [ 'src' => $fileC ] ),
1544 "Correct file contents of $fileC" );
1545 $this->assertEquals( strlen( $fileBContents ),
1546 $this->backend->getFileSize( [ 'src' => $fileC ] ),
1547 "Correct file size of $fileC" );
1548 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1549 $this->backend->getFileSha1Base36( [ 'src' => $fileC ] ),
1550 "Correct file SHA-1 of $fileC" );
1551 }
1552
1553 public function testDoOperationsFailing() {
1554 $base = self::baseStorePath();
1555
1556 $fileA = "$base/unittest-cont2/a/b/fileA.txt";
1557 $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq';
1558 $fileB = "$base/unittest-cont2/a/b/fileB.txt";
1559 $fileBContents = 'g-jmq3gpqgt3qtg q3GT ';
1560 $fileC = "$base/unittest-cont2/a/b/fileC.txt";
1561 $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag';
1562 $fileD = "$base/unittest-cont2/a/b/fileD.txt";
1563
1564 $this->prepare( [ 'dir' => dirname( $fileA ) ] );
1565 $this->create( [ 'dst' => $fileA, 'content' => $fileAContents ] );
1566 $this->prepare( [ 'dir' => dirname( $fileB ) ] );
1567 $this->create( [ 'dst' => $fileB, 'content' => $fileBContents ] );
1568 $this->prepare( [ 'dir' => dirname( $fileC ) ] );
1569 $this->create( [ 'dst' => $fileC, 'content' => $fileCContents ] );
1570
1571 $status = $this->backend->doOperations( [
1572 [ 'op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1 ],
1573 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (file:<orginal contents>)
1574 [ 'op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1 ],
1575 // Now: A:<A>, B:<B>, C:<A>, D:<empty>
1576 [ 'op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1 ],
1577 // Now: A:<A>, B:<B>, C:<A>, D:<B>
1578 [ 'op' => 'move', 'src' => $fileC, 'dst' => $fileD ],
1579 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1580 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1 ],
1581 // Now: A:<A>, B:<B>, C:<A>, D:<empty> (failed)
1582 [ 'op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1 ],
1583 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1584 [ 'op' => 'delete', 'src' => $fileD ],
1585 // Now: A:<B>, B:<empty>, C:<A>, D:<empty>
1586 [ 'op' => 'null' ],
1587 // Does nothing
1588 ], [ 'force' => 1 ] );
1589
1590 $this->assertStatusWarning( 'backend-fail-alreadyexists', $status );
1591 $this->assertStatusWarning( 'backend-fail-notsame', $status );
1592 $this->assertCount( 8, $status->success,
1593 "Operation batch has correct success array" );
1594
1595 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileB ] ),
1596 "File does not exist at $fileB" );
1597 $this->assertFalse( $this->backend->fileExists( [ 'src' => $fileD ] ),
1598 "File does not exist at $fileD" );
1599
1600 $this->assertTrue( $this->backend->fileExists( [ 'src' => $fileA ] ),
1601 "File does not exist at $fileA" );
1602 $this->assertTrue( $this->backend->fileExists( [ 'src' => $fileC ] ),
1603 "File exists at $fileC" );
1604 $this->assertEquals( $fileBContents,
1605 $this->backend->getFileContents( [ 'src' => $fileA ] ),
1606 "Correct file contents of $fileA" );
1607 $this->assertEquals( strlen( $fileBContents ),
1608 $this->backend->getFileSize( [ 'src' => $fileA ] ),
1609 "Correct file size of $fileA" );
1610 $this->assertEquals( Wikimedia\base_convert( sha1( $fileBContents ), 16, 36, 31 ),
1611 $this->backend->getFileSha1Base36( [ 'src' => $fileA ] ),
1612 "Correct file SHA-1 of $fileA" );
1613 }
1614
1615 public function testGetFileList() {
1616 $backendName = $this->backendClass();
1617 $base = self::baseStorePath();
1618
1619 // This is null on FSFileBackend, because it knows about all containers
1620 // that exist, whereas Swift would need to do a remote request, so it
1621 // just returns an empty iterator.
1622 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont-notexists" ] );
1623 $this->assertThat( $iter, $this->logicalOr( $this->isNull(), $this->countOf( 0 ) ) );
1624
1625 $files = [
1626 "$base/unittest-cont1/e/test1.txt",
1627 "$base/unittest-cont1/e/test2.txt",
1628 "$base/unittest-cont1/e/test3.txt",
1629 "$base/unittest-cont1/e/subdir1/test1.txt",
1630 "$base/unittest-cont1/e/subdir1/test2.txt",
1631 "$base/unittest-cont1/e/subdir2/test3.txt",
1632 "$base/unittest-cont1/e/subdir2/test4.txt",
1633 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1634 "$base/unittest-cont1/e/subdir2/subdir/test2.txt",
1635 "$base/unittest-cont1/e/subdir2/subdir/test3.txt",
1636 "$base/unittest-cont1/e/subdir2/subdir/test4.txt",
1637 "$base/unittest-cont1/e/subdir2/subdir/test5.txt",
1638 "$base/unittest-cont1/e/subdir2/subdir/sub/test0.txt",
1639 "$base/unittest-cont1/e/subdir2/subdir/sub/120-px-file.txt",
1640 ];
1641
1642 // Add the files
1643 $ops = [];
1644 foreach ( $files as $file ) {
1645 $this->prepare( [ 'dir' => dirname( $file ) ] );
1646 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
1647 }
1648 $status = $this->backend->doQuickOperations( $ops );
1649 $this->assertStatusGood( $status,
1650 "Creation of files succeeded ($backendName)." );
1651
1652 // Expected listing at root
1653 $expected = [
1654 "e/test1.txt",
1655 "e/test2.txt",
1656 "e/test3.txt",
1657 "e/subdir1/test1.txt",
1658 "e/subdir1/test2.txt",
1659 "e/subdir2/test3.txt",
1660 "e/subdir2/test4.txt",
1661 "e/subdir2/subdir/test1.txt",
1662 "e/subdir2/subdir/test2.txt",
1663 "e/subdir2/subdir/test3.txt",
1664 "e/subdir2/subdir/test4.txt",
1665 "e/subdir2/subdir/test5.txt",
1666 "e/subdir2/subdir/sub/test0.txt",
1667 "e/subdir2/subdir/sub/120-px-file.txt",
1668 ];
1669 sort( $expected );
1670
1671 // Actual listing (no trailing slash) at root
1672 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1" ] );
1673 $this->assertNotNull( $iter );
1674 $list = $this->listToArray( $iter );
1675 sort( $list );
1676 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1677
1678 // Actual listing (no trailing slash) at root with advise
1679 $iter = $this->backend->getFileList( [
1680 'dir' => "$base/unittest-cont1",
1681 'adviseStat' => 1
1682 ] );
1683 $this->assertNotNull( $iter );
1684 $list = $this->listToArray( $iter );
1685 sort( $list );
1686 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1687
1688 // Actual listing (with trailing slash) at root
1689 $list = [];
1690 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/" ] );
1691 $this->assertNotNull( $iter );
1692 foreach ( $iter as $file ) {
1693 $list[] = $file;
1694 }
1695 sort( $list );
1696 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1697
1698 // Expected listing at subdir
1699 $expected = [
1700 "test1.txt",
1701 "test2.txt",
1702 "test3.txt",
1703 "test4.txt",
1704 "test5.txt",
1705 "sub/test0.txt",
1706 "sub/120-px-file.txt",
1707 ];
1708 sort( $expected );
1709
1710 // Actual listing (no trailing slash) at subdir
1711 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] );
1712 $this->assertNotNull( $iter );
1713 $list = $this->listToArray( $iter );
1714 sort( $list );
1715 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1716
1717 // Actual listing (no trailing slash) at subdir with advise
1718 $iter = $this->backend->getFileList( [
1719 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
1720 'adviseStat' => 1
1721 ] );
1722 $this->assertNotNull( $iter );
1723 $list = $this->listToArray( $iter );
1724 sort( $list );
1725 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1726
1727 // Actual listing (with trailing slash) at subdir
1728 $list = [];
1729 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir/" ] );
1730 $this->assertNotNull( $iter );
1731 foreach ( $iter as $file ) {
1732 $list[] = $file;
1733 }
1734 sort( $list );
1735 $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." );
1736
1737 // Actual listing (using iterator second time)
1738 $list = $this->listToArray( $iter );
1739 sort( $list );
1740 $this->assertEquals( $expected, $list, "Correct file listing ($backendName), second iteration." );
1741
1742 // Actual listing (top files only) at root
1743 $iter = $this->backend->getTopFileList( [ 'dir' => "$base/unittest-cont1" ] );
1744 $this->assertNotNull( $iter );
1745 $list = $this->listToArray( $iter );
1746 sort( $list );
1747 $this->assertEquals( [], $list, "Correct top file listing ($backendName)." );
1748
1749 // Expected listing (top files only) at subdir
1750 $expected = [
1751 "test1.txt",
1752 "test2.txt",
1753 "test3.txt",
1754 "test4.txt",
1755 "test5.txt"
1756 ];
1757 sort( $expected );
1758
1759 // Actual listing (top files only) at subdir
1760 $iter = $this->backend->getTopFileList(
1761 [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ]
1762 );
1763 $this->assertNotNull( $iter );
1764 $list = $this->listToArray( $iter );
1765 sort( $list );
1766 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1767
1768 // Actual listing (top files only) at subdir with advise
1769 $iter = $this->backend->getTopFileList( [
1770 'dir' => "$base/unittest-cont1/e/subdir2/subdir",
1771 'adviseStat' => 1
1772 ] );
1773 $this->assertNotNull( $iter );
1774 $list = $this->listToArray( $iter );
1775 sort( $list );
1776 $this->assertEquals( $expected, $list, "Correct top file listing ($backendName)." );
1777
1778 foreach ( $files as $file ) { // clean up
1779 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
1780 }
1781
1782 $iter = $this->backend->getFileList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
1783 $this->assertNotNull( $iter );
1784 foreach ( $iter as $item ) {
1785 // no errors
1786 }
1787 }
1788
1789 public function testGetDirectoryList() {
1790 $backendName = $this->backendClass();
1791
1792 $base = self::baseStorePath();
1793 $files = [
1794 "$base/unittest-cont1/e/test1.txt",
1795 "$base/unittest-cont1/e/test2.txt",
1796 "$base/unittest-cont1/e/test3.txt",
1797 "$base/unittest-cont1/e/subdir1/test1.txt",
1798 "$base/unittest-cont1/e/subdir1/test2.txt",
1799 "$base/unittest-cont1/e/subdir2/test3.txt",
1800 "$base/unittest-cont1/e/subdir2/test4.txt",
1801 "$base/unittest-cont1/e/subdir2/subdir/test1.txt",
1802 "$base/unittest-cont1/e/subdir3/subdir/test2.txt",
1803 "$base/unittest-cont1/e/subdir4/subdir/test3.txt",
1804 "$base/unittest-cont1/e/subdir4/subdir/test4.txt",
1805 "$base/unittest-cont1/e/subdir4/subdir/test5.txt",
1806 "$base/unittest-cont1/e/subdir4/subdir/sub/test0.txt",
1807 "$base/unittest-cont1/e/subdir4/subdir/sub/120-px-file.txt",
1808 ];
1809
1810 // Add the files
1811 $ops = [];
1812 foreach ( $files as $file ) {
1813 $this->prepare( [ 'dir' => dirname( $file ) ] );
1814 $ops[] = [ 'op' => 'create', 'content' => 'xxy', 'dst' => $file ];
1815 }
1816 $status = $this->backend->doQuickOperations( $ops );
1817 $this->assertStatusGood( $status,
1818 "Creation of files succeeded ($backendName)." );
1819
1820 $this->assertTrue(
1821 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] ),
1822 "Directory exists in ($backendName)."
1823 );
1824 $this->assertTrue(
1825 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/subdir" ] ),
1826 "Directory exists in ($backendName)."
1827 );
1828 $this->assertFalse(
1829 $this->backend->directoryExists( [ 'dir' => "$base/unittest-cont1/e/subdir2/test1.txt" ] ),
1830 "Directory does not exists in ($backendName)."
1831 );
1832
1833 // Expected listing
1834 $expected = [
1835 "e",
1836 ];
1837 sort( $expected );
1838
1839 // Actual listing (no trailing slash)
1840 $list = [];
1841 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1" ] );
1842 $this->assertNotNull( $iter );
1843 foreach ( $iter as $file ) {
1844 $list[] = $file;
1845 }
1846 sort( $list );
1847
1848 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1849
1850 // Expected listing
1851 $expected = [
1852 "subdir1",
1853 "subdir2",
1854 "subdir3",
1855 "subdir4",
1856 ];
1857 sort( $expected );
1858
1859 // Actual listing (no trailing slash)
1860 $list = [];
1861 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e" ] );
1862 $this->assertNotNull( $iter );
1863 foreach ( $iter as $file ) {
1864 $list[] = $file;
1865 }
1866 sort( $list );
1867
1868 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1869
1870 // Actual listing (with trailing slash)
1871 $list = [];
1872 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/" ] );
1873 $this->assertNotNull( $iter );
1874 foreach ( $iter as $file ) {
1875 $list[] = $file;
1876 }
1877 sort( $list );
1878
1879 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1880
1881 // Expected listing
1882 $expected = [
1883 "subdir",
1884 ];
1885 sort( $expected );
1886
1887 // Actual listing (no trailing slash)
1888 $list = [];
1889 $iter = $this->backend->getTopDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir2" ] );
1890 $this->assertNotNull( $iter );
1891 foreach ( $iter as $file ) {
1892 $list[] = $file;
1893 }
1894 sort( $list );
1895
1896 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1897
1898 // Actual listing (with trailing slash)
1899 $list = [];
1900 $iter = $this->backend->getTopDirectoryList(
1901 [ 'dir' => "$base/unittest-cont1/e/subdir2/" ]
1902 );
1903 $this->assertNotNull( $iter );
1904
1905 foreach ( $iter as $file ) {
1906 $list[] = $file;
1907 }
1908 sort( $list );
1909
1910 $this->assertEquals( $expected, $list, "Correct top dir listing ($backendName)." );
1911
1912 // Actual listing (using iterator second time)
1913 $list = [];
1914 foreach ( $iter as $file ) {
1915 $list[] = $file;
1916 }
1917 sort( $list );
1918
1919 $this->assertEquals(
1920 $expected,
1921 $list,
1922 "Correct top dir listing ($backendName), second iteration."
1923 );
1924
1925 // Expected listing (recursive)
1926 $expected = [
1927 "e",
1928 "e/subdir1",
1929 "e/subdir2",
1930 "e/subdir3",
1931 "e/subdir4",
1932 "e/subdir2/subdir",
1933 "e/subdir3/subdir",
1934 "e/subdir4/subdir",
1935 "e/subdir4/subdir/sub",
1936 ];
1937 sort( $expected );
1938
1939 // Actual listing (recursive)
1940 $list = [];
1941 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/" ] );
1942 $this->assertNotNull( $iter );
1943 foreach ( $iter as $file ) {
1944 $list[] = $file;
1945 }
1946 sort( $list );
1947
1948 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1949
1950 // Expected listing (recursive)
1951 $expected = [
1952 "subdir",
1953 "subdir/sub",
1954 ];
1955 sort( $expected );
1956
1957 // Actual listing (recursive)
1958 $list = [];
1959 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir4" ] );
1960 $this->assertNotNull( $iter );
1961 foreach ( $iter as $file ) {
1962 $list[] = $file;
1963 }
1964 sort( $list );
1965
1966 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1967
1968 // Actual listing (recursive, second time)
1969 $list = [];
1970 foreach ( $iter as $file ) {
1971 $list[] = $file;
1972 }
1973 sort( $list );
1974
1975 $this->assertEquals( $expected, $list, "Correct dir listing ($backendName)." );
1976
1977 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/subdir1" ] );
1978 $this->assertNotNull( $iter );
1979 $items = $this->listToArray( $iter );
1980 $this->assertEquals( [], $items, "Directory listing is empty." );
1981
1982 foreach ( $files as $file ) { // clean up
1983 $this->backend->doOperation( [ 'op' => 'delete', 'src' => $file ] );
1984 }
1985
1986 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/not/exists" ] );
1987 $this->assertNotNull( $iter );
1988 foreach ( $iter as $file ) {
1989 // no errors
1990 }
1991
1992 $items = $this->listToArray( $iter );
1993 $this->assertEquals( [], $items, "Directory listing is empty." );
1994
1995 $iter = $this->backend->getDirectoryList( [ 'dir' => "$base/unittest-cont1/e/not/exists" ] );
1996 $this->assertNotNull( $iter );
1997 $items = $this->listToArray( $iter );
1998 $this->assertEquals( [], $items, "Directory listing is empty." );
1999 }
2000
2001 public function testLockCalls() {
2002 $backendName = $this->backendClass();
2003 $base = $this->backend->getContainerStoragePath( 'test' );
2004
2005 $paths = [
2006 "$base/test1.txt",
2007 "$base/test2.txt",
2008 "$base/test3.txt",
2009 "$base/subdir1",
2010 "$base/subdir1", // duplicate
2011 "$base/subdir1/test1.txt",
2012 "$base/subdir1/test2.txt",
2013 "$base/subdir2",
2014 "$base/subdir2", // duplicate
2015 "$base/subdir2/test3.txt",
2016 "$base/subdir2/test4.txt",
2017 "$base/subdir2/subdir",
2018 "$base/subdir2/subdir/test1.txt",
2019 "$base/subdir2/subdir/test2.txt",
2020 "$base/subdir2/subdir/test3.txt",
2021 "$base/subdir2/subdir/test4.txt",
2022 "$base/subdir2/subdir/test5.txt",
2023 "$base/subdir2/subdir/sub",
2024 "$base/subdir2/subdir/sub/test0.txt",
2025 "$base/subdir2/subdir/sub/120-px-file.txt",
2026 ];
2027
2028 for ( $i = 0; $i < 2; $i++ ) {
2029 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2030 $this->assertStatusGood( $status,
2031 "Locking of files succeeded ($backendName) ($i)." );
2032
2033 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2034 $this->assertStatusGood( $status,
2035 "Locking of files succeeded ($backendName) ($i)." );
2036
2037 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2038 $this->assertStatusGood( $status,
2039 "Locking of files succeeded ($backendName) ($i)." );
2040
2041 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2042 $this->assertStatusGood( $status,
2043 "Locking of files succeeded ($backendName). ($i)" );
2044
2045 # # Flip the acquire/release ordering around ##
2046
2047 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_SH );
2048 $this->assertStatusGood( $status,
2049 "Locking of files succeeded ($backendName) ($i)." );
2050
2051 $status = $this->backend->lockFiles( $paths, LockManager::LOCK_EX );
2052 $this->assertStatusGood( $status,
2053 "Locking of files succeeded ($backendName) ($i)." );
2054
2055 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_EX );
2056 $this->assertStatusGood( $status,
2057 "Locking of files succeeded ($backendName). ($i)" );
2058
2059 $status = $this->backend->unlockFiles( $paths, LockManager::LOCK_SH );
2060 $this->assertStatusGood( $status,
2061 "Locking of files succeeded ($backendName) ($i)." );
2062 }
2063
2064 $status = Status::newGood();
2065 $sl = $this->backend->getScopedFileLocks( $paths, LockManager::LOCK_EX, $status );
2066 $this->assertInstanceOf( ScopedLock::class, $sl,
2067 "Scoped locking of files succeeded ($backendName)." );
2068 $this->assertStatusGood( $status,
2069 "Scoped locking of files succeeded ($backendName)." );
2070
2071 ScopedLock::release( $sl );
2072 $this->assertNull( $sl,
2073 "Scoped unlocking of files succeeded ($backendName)." );
2074 $this->assertStatusGood( $status,
2075 "Scoped unlocking of files succeeded ($backendName)." );
2076 }
2077
2078 // helper function
2079 private function listToArray( $iter ) {
2080 return is_array( $iter ) ? $iter : iterator_to_array( $iter );
2081 }
2082
2083 // test helper wrapper for backend prepare() function
2084 private function prepare( array $params ) {
2085 return $this->backend->prepare( $params );
2086 }
2087
2088 // test helper wrapper for backend prepare() function
2089 private function create( array $params ) {
2090 $params['op'] = 'create';
2091
2092 return $this->backend->doQuickOperations( [ $params ] );
2093 }
2094
2095 public function tearDownFiles() {
2096 $containers = [ 'unittest-cont1', 'unittest-cont2' ];
2097 foreach ( $containers as $container ) {
2098 $this->deleteFiles( $container );
2099 }
2100 }
2101
2102 private function deleteFiles( $container ) {
2103 $base = self::baseStorePath();
2104 $iter = $this->backend->getFileList( [ 'dir' => "$base/$container" ] );
2105 if ( $iter ) {
2106 foreach ( $iter as $file ) {
2107 $this->backend->quickDelete( [ 'src' => "$base/$container/$file" ] );
2108 }
2109 // free the directory, to avoid Permission denied under windows on rmdir
2110 unset( $iter );
2111 }
2112 $this->backend->clean( [ 'dir' => "$base/$container", 'recursive' => 1 ] );
2113 }
2114
2115 protected function assertBackendPathsConsistent( array $paths, $okSyncStatus ) {
2116 }
2117
2118 }