Fixes for r106752:
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / FileBackendTest.php
1 <?php
2
3 // @TODO: fix empty dir leakage
4 class FileBackendTest extends MediaWikiTestCase {
5 private $backend, $multiBackend;
6 private $filesToPrune, $pathsToPrune;
7
8 function setUp() {
9 parent::setUp();
10 $this->backend = new FSFileBackend( array(
11 'name' => 'localtesting',
12 'lockManager' => 'fsLockManager',
13 'containerPaths' => array(
14 'cont1' => wfTempDir() . '/localtesting/cont1',
15 'cont2' => wfTempDir() . '/localtesting/cont2' )
16 ) );
17 $this->multiBackend = new FileBackendMultiWrite( array(
18 'name' => 'localtestingmulti',
19 'lockManager' => 'fsLockManager',
20 'backends' => array(
21 array(
22 'name' => 'localmutlitesting1',
23 'class' => 'FSFileBackend',
24 'lockManager' => 'nullLockManager',
25 'containerPaths' => array(
26 'cont1' => wfTempDir() . '/localtestingmulti1/cont1',
27 'cont2' => wfTempDir() . '/localtestingmulti1/cont2' ),
28 'isMultiMaster' => false
29 ),
30 array(
31 'name' => 'localmutlitesting2',
32 'class' => 'FSFileBackend',
33 'lockManager' => 'nullLockManager',
34 'containerPaths' => array(
35 'cont1' => wfTempDir() . '/localtestingmulti2/cont1',
36 'cont2' => wfTempDir() . '/localtestingmulti2/cont2' ),
37 'isMultiMaster' => true
38 )
39 )
40 ) );
41 $this->filesToPrune = $this->pathsToPrune = array();
42 }
43
44 private function singleBasePath() {
45 return 'mwstore://localtesting';
46 }
47
48 /**
49 * @dataProvider provider_testStore
50 */
51 public function testStore( $op, $source, $dest ) {
52 $this->filesToPrune[] = $source;
53 $this->pathsToPrune[] = $dest;
54
55 file_put_contents( $source, "Unit test file" );
56 $status = $this->backend->doOperation( $op );
57
58 $this->assertEquals( true, $status->isOK(),
59 "Store from $source to $dest succeeded." );
60 $this->assertEquals( true, $status->isGood(),
61 "Store from $source to $dest succeeded without warnings." );
62 $this->assertEquals( true, file_exists( $source ),
63 "Source file $source still exists." );
64 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
65 "Destination file $dest exists." );
66
67 $props1 = FSFile::getPropsFromPath( $source );
68 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
69 $this->assertEquals( $props1, $props2,
70 "Source and destination have the same props." );
71 }
72
73 public function provider_testStore() {
74 $cases = array();
75
76 $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath();
77 $toPath = $this->singleBasePath() . '/cont1/fun/obj1.txt';
78 $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath );
79 $cases[] = array(
80 $op, // operation
81 $tmpName, // source
82 $toPath, // dest
83 );
84
85 $op['overwriteDest'] = true;
86 $cases[] = array(
87 $op, // operation
88 $tmpName, // source
89 $toPath, // dest
90 );
91
92 return $cases;
93 }
94
95 /**
96 * @dataProvider provider_testCopy
97 */
98 public function testCopy( $op, $source, $dest ) {
99 $this->pathsToPrune[] = $source;
100 $this->pathsToPrune[] = $dest;
101
102 $status = $this->backend->doOperation(
103 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
104 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
105
106 $status = $this->backend->doOperation( $op );
107 $this->assertEquals( true, $status->isOK(),
108 "Copy from $source to $dest succeeded." );
109 $this->assertEquals( true, $status->isGood(),
110 "Copy from $source to $dest succeeded without warnings." );
111 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ),
112 "Source file $source still exists." );
113 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
114 "Destination file $dest exists after copy." );
115
116 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
117 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
118 $this->assertEquals( $props1, $props2,
119 "Source and destination have the same props." );
120 }
121
122 public function provider_testCopy() {
123 $cases = array();
124
125 $source = $this->singleBasePath() . '/cont1/file.txt';
126 $dest = $this->singleBasePath() . '/cont2/fileMoved.txt';
127
128 $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest );
129 $cases[] = array(
130 $op, // operation
131 $source, // source
132 $dest, // dest
133 );
134
135 $op['overwriteDest'] = true;
136 $cases[] = array(
137 $op, // operation
138 $source, // source
139 $dest, // dest
140 );
141
142 return $cases;
143 }
144
145 /**
146 * @dataProvider provider_testMove
147 */
148 public function testMove( $op, $source, $dest ) {
149 $this->pathsToPrune[] = $source;
150 $this->pathsToPrune[] = $dest;
151
152 $status = $this->backend->doOperation(
153 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
154 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
155
156 $status = $this->backend->doOperation( $op );
157 $this->assertEquals( true, $status->isOK(),
158 "Move from $source to $dest succeeded." );
159 $this->assertEquals( true, $status->isGood(),
160 "Move from $source to $dest succeeded without warnings." );
161 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
162 "Source file $source does not still exists." );
163 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
164 "Destination file $dest exists after move." );
165
166 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
167 $props2 = $this->backend->getFileProps( array( 'src' => $dest ) );
168 $this->assertEquals( false, $props1['fileExists'],
169 "Source file does not exist accourding to props." );
170 $this->assertEquals( true, $props2['fileExists'],
171 "Destination file exists accourding to props." );
172 }
173
174 public function provider_testMove() {
175 $cases = array();
176
177 $source = $this->singleBasePath() . '/cont1/file.txt';
178 $dest = $this->singleBasePath() . '/cont2/fileMoved.txt';
179
180 $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest );
181 $cases[] = array(
182 $op, // operation
183 $source, // source
184 $dest, // dest
185 );
186
187 $op['overwriteDest'] = true;
188 $cases[] = array(
189 $op, // operation
190 $source, // source
191 $dest, // dest
192 );
193
194 return $cases;
195 }
196
197 /**
198 * @dataProvider provider_testDelete
199 */
200 public function testDelete( $op, $source, $withSource, $okStatus ) {
201 $this->pathsToPrune[] = $source;
202
203 if ( $withSource ) {
204 $status = $this->backend->doOperation(
205 array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) );
206 $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." );
207 }
208
209 $status = $this->backend->doOperation( $op );
210 if ( $okStatus ) {
211 $this->assertEquals( true, $status->isOK(), "Deletion of file at $source succeeded." );
212 } else {
213 $this->assertEquals( false, $status->isOK(), "Deletion of file at $source failed." );
214 }
215
216 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ),
217 "Source file $source does not exist after move." );
218
219 $props1 = $this->backend->getFileProps( array( 'src' => $source ) );
220 $this->assertEquals( false, $props1['fileExists'],
221 "Source file $source does not exist according to props." );
222 }
223
224 public function provider_testDelete() {
225 $cases = array();
226
227 $source = $this->singleBasePath() . '/cont1/myfacefile.txt';
228
229 $op = array( 'op' => 'delete', 'src' => $source );
230 $cases[] = array(
231 $op, // operation
232 $source, // source
233 true, // with source
234 true // succeeds
235 );
236
237 $cases[] = array(
238 $op, // operation
239 $source, // source
240 false, // without source
241 false // fails
242 );
243
244 $op['ignoreMissingSource'] = true;
245 $cases[] = array(
246 $op, // operation
247 $source, // source
248 false, // without source
249 true // succeeds
250 );
251
252 return $cases;
253 }
254
255 /**
256 * @dataProvider provider_testCreate
257 */
258 public function testCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) {
259 $this->pathsToPrune[] = $dest;
260
261 $oldText = 'blah...blah...waahwaah';
262 if ( $alreadyExists ) {
263 $status = $this->backend->doOperation(
264 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
265 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
266 }
267
268 $status = $this->backend->doOperation( $op );
269 if ( $okStatus ) {
270 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
271 } else {
272 $this->assertEquals( false, $status->isOK(), "Creation of file at $dest failed." );
273 }
274
275 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
276 "Dest file $dest exists after creation." );
277
278 $props1 = $this->backend->getFileProps( array( 'src' => $dest ) );
279 $this->assertEquals( true, $props1['fileExists'],
280 "Dest file $dest exists according to props." );
281 if ( $okStatus ) { // file content is what we saved
282 $this->assertEquals( $newSize, $props1['size'],
283 "Dest file $dest has expected size according to props." );
284 } else { // file content is some other previous text
285 $this->assertEquals( strlen( $oldText ), $props1['size'],
286 "Dest file $dest has different size that given text according to props." );
287 }
288 }
289
290 /**
291 * @dataProvider provider_testCreate
292 */
293 public function provider_testCreate() {
294 $cases = array();
295
296 $source = $this->singleBasePath() . '/cont2/myspacefile.txt';
297
298 $dummyText = 'hey hey';
299 $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source );
300 $cases[] = array(
301 $op, // operation
302 $source, // source
303 false, // no dest already exists
304 true, // succeeds
305 strlen( $dummyText )
306 );
307
308 $cases[] = array(
309 $op, // operation
310 $source, // source
311 true, // dest already exists
312 false, // fails
313 strlen( $dummyText )
314 );
315
316 $op['overwriteDest'] = true;
317 $cases[] = array(
318 $op, // operation
319 $source, // source
320 true, // dest already exists
321 true, // succeeds
322 strlen( $dummyText )
323 );
324
325 return $cases;
326 }
327
328 /**
329 * @dataProvider provider_testConcatenate
330 */
331 public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) {
332 $this->pathsToPrune = array_merge( $this->pathsToPrune, $srcs );
333 $this->pathsToPrune[] = $op['dst'];
334
335 $expContent = '';
336 // Create sources
337 $ops = array();
338 foreach ( $srcs as $i => $source ) {
339 $ops[] = array(
340 'op' => 'create', // operation
341 'dst' => $source, // source
342 'content' => $srcsContent[$i]
343 );
344 $expContent .= $srcsContent[$i];
345 }
346 $status = $this->backend->doOperations( $ops );
347
348 $this->assertEquals( true, $status->isOK(), "Creation of source files succeeded." );
349
350 $dest = $op['dst'];
351 if ( $alreadyExists ) {
352 $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false;
353 $this->assertEquals( true, $ok, "Creation of file at $dest succeeded." );
354 } else {
355 $ok = file_put_contents( $dest, '' ) !== false;
356 $this->assertEquals( true, $ok, "Creation of 0-byte file at $dest succeeded." );
357 }
358
359 // Combine them
360 $status = $this->backend->doOperation( $op );
361 if ( $okStatus ) {
362 $this->assertEquals( true, $status->isOK(), "Creation of concat file at $dest succeeded." );
363 } else {
364 $this->assertEquals( false, $status->isOK(), "Creation of concat file at $dest failed." );
365 }
366
367 if ( $okStatus ) {
368 $this->assertEquals( true, is_file( $dest ),
369 "Dest concat file $dest exists after creation." );
370 } else {
371 $this->assertEquals( true, is_file( $dest ),
372 "Dest concat file $dest exists after failed creation." );
373 }
374
375 $contents = file_get_contents( $dest );
376 $this->assertNotEquals( false, $contents, "File at $dest exists." );
377
378 if ( $okStatus ) {
379 $this->assertEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
380 } else {
381 $this->assertNotEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
382 }
383 }
384
385 function provider_testConcatenate() {
386 $cases = array();
387
388 $rand = mt_rand( 0, 2000000000 ) . time();
389 $dest = wfTempDir() . "/randomfile!$rand.txt";
390 $srcs = array(
391 $this->singleBasePath() . '/cont1/file1.txt',
392 $this->singleBasePath() . '/cont1/file2.txt',
393 $this->singleBasePath() . '/cont1/file3.txt',
394 $this->singleBasePath() . '/cont1/file4.txt',
395 $this->singleBasePath() . '/cont1/file5.txt',
396 $this->singleBasePath() . '/cont1/file6.txt',
397 $this->singleBasePath() . '/cont1/file7.txt',
398 $this->singleBasePath() . '/cont1/file8.txt',
399 $this->singleBasePath() . '/cont1/file9.txt',
400 $this->singleBasePath() . '/cont1/file10.txt'
401 );
402 $content = array(
403 'egfage',
404 'ageageag',
405 'rhokohlr',
406 'shgmslkg',
407 'kenga',
408 'owagmal',
409 'kgmae',
410 'g eak;g',
411 'lkaem;a',
412 'legma'
413 );
414 $op = array( 'op' => 'concatenate', 'srcs' => $srcs, 'dst' => $dest );
415
416 $cases[] = array(
417 $op, // operation
418 $srcs, // sources
419 $content, // content for each source
420 false, // no dest already exists
421 true, // succeeds
422 );
423
424 $cases[] = array(
425 $op, // operation
426 $srcs, // sources
427 $content, // content for each source
428 true, // no dest already exists
429 false, // succeeds
430 );
431
432 return $cases;
433 }
434
435 /**
436 * @dataProvider provider_testGetFileContents
437 */
438 public function testGetFileContents( $src, $content ) {
439 $this->pathsToPrune[] = $src;
440
441 $status = $this->backend->doOperation(
442 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
443 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
444
445 $newContents = $this->backend->getFileContents( array( 'src' => $src ) );
446 $this->assertNotEquals( false, $newContents, "Read of file at $src succeeded." );
447
448 $this->assertEquals( $content, $newContents, "Contents read match data at $src." );
449 }
450
451 function provider_testGetFileContents() {
452 $cases = array();
453
454 $base = $this->singleBasePath();
455 $cases[] = array( "$base/cont1/b/z/some_file.txt", "some file contents" );
456 $cases[] = array( "$base/cont1/b/some-other_file.txt", "more file contents" );
457
458 return $cases;
459 }
460
461 /**
462 * @dataProvider provider_testGetLocalCopy
463 */
464 public function testGetLocalCopy( $src, $content ) {
465 $this->pathsToPrune[] = $src;
466
467 $status = $this->backend->doOperation(
468 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
469 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
470
471 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $src ) );
472 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
473
474 $contents = file_get_contents( $tmpFile->getPath() );
475 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
476 }
477
478 function provider_testGetLocalCopy() {
479 $cases = array();
480
481 $base = $this->singleBasePath();
482 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
483 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
484
485 return $cases;
486 }
487
488 /**
489 * @dataProvider provider_testGetLocalReference
490 */
491 public function testGetLocalReference( $src, $content ) {
492 $this->pathsToPrune[] = $src;
493
494 $status = $this->backend->doOperation(
495 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
496 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
497
498 $tmpFile = $this->backend->getLocalReference( array( 'src' => $src ) );
499 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
500
501 $contents = file_get_contents( $tmpFile->getPath() );
502 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
503 }
504
505 function provider_testGetLocalReference() {
506 $cases = array();
507
508 $base = $this->singleBasePath();
509 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
510 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
511
512 return $cases;
513 }
514
515 // @TODO: testPrepare
516
517 // @TODO: testSecure
518
519 // @TODO: testClean
520
521 // @TODO: testDoOperations
522
523 public function testGetFileList() {
524 $base = $this->singleBasePath();
525 $files = array(
526 "$base/cont1/test1.txt",
527 "$base/cont1/test2.txt",
528 "$base/cont1/test3.txt",
529 "$base/cont1/subdir1/test1.txt",
530 "$base/cont1/subdir1/test2.txt",
531 "$base/cont1/subdir2/test3.txt",
532 "$base/cont1/subdir2/test4.txt",
533 "$base/cont1/subdir2/subdir/test1.txt",
534 "$base/cont1/subdir2/subdir/test2.txt",
535 "$base/cont1/subdir2/subdir/test3.txt",
536 "$base/cont1/subdir2/subdir/test4.txt",
537 "$base/cont1/subdir2/subdir/test5.txt",
538 "$base/cont1/subdir2/subdir/sub/test0.txt",
539 "$base/cont1/subdir2/subdir/sub/120-px-file.txt",
540 );
541 $this->pathsToPrune = array_merge( $this->pathsToPrune, $files );
542
543 // Add the files
544 $ops = array();
545 foreach ( $files as $file ) {
546 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
547 }
548 $status = $this->backend->doOperations( $ops );
549 $this->assertEquals( true, $status->isOK(), "Creation of files succeeded." );
550
551 // Expected listing
552 $expected = array(
553 "test1.txt",
554 "test2.txt",
555 "test3.txt",
556 "subdir1/test1.txt",
557 "subdir1/test2.txt",
558 "subdir2/test3.txt",
559 "subdir2/test4.txt",
560 "subdir2/subdir/test1.txt",
561 "subdir2/subdir/test2.txt",
562 "subdir2/subdir/test3.txt",
563 "subdir2/subdir/test4.txt",
564 "subdir2/subdir/test5.txt",
565 "subdir2/subdir/sub/test0.txt",
566 "subdir2/subdir/sub/120-px-file.txt",
567 );
568 sort( $expected );
569
570 // Actual listing (no trailing slash)
571 $list = array();
572 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1" ) );
573 foreach ( $iter as $file ) {
574 $list[] = $file;
575 }
576 sort( $list );
577
578 $this->assertEquals( $expected, $list, "Correct file listing." );
579
580 // Actual listing (with trailing slash)
581 $list = array();
582 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1/" ) );
583 foreach ( $iter as $file ) {
584 $list[] = $file;
585 }
586 sort( $list );
587
588 $this->assertEquals( $expected, $list, "Correct file listing." );
589
590 foreach ( $files as $file ) {
591 $this->backend->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) );
592 }
593
594 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1/not/exists" ) );
595 foreach ( $iter as $iter ) {} // no errors
596 }
597
598 function tearDown() {
599 parent::tearDown();
600 foreach ( $this->filesToPrune as $file ) {
601 @unlink( $file );
602 }
603 foreach ( $this->pathsToPrune as $file ) {
604 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
605 $this->multiBackend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
606 }
607 $this->backend = $this->multiBackend = null;
608 $this->filesToPrune = $this->pathsToPrune = array();
609 }
610 }