d94f10bfb4a3d2c0478858ad1bb3169b4e7786e3
[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 $oldText = 'blah...blah...waahwaah';
353 $status = $this->backend->doOperation(
354 array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) );
355 $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." );
356 }
357
358 // Combine them
359 $status = $this->backend->doOperation( $op );
360 if ( $okStatus ) {
361 $this->assertEquals( true, $status->isOK(), "Creation of concat file at $dest succeeded." );
362 } else {
363 $this->assertEquals( false, $status->isOK(), "Creation of concat file at $dest failed." );
364 }
365
366 if ( $okStatus ) {
367 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
368 "Dest concat file $dest exists after creation." );
369 } else {
370 $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ),
371 "Dest concat file $dest exists after failed creation." );
372 }
373
374 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $dest ) );
375 $this->assertNotNull( $tmpFile, "Creation of local copy of $dest succeeded." );
376
377 $contents = file_get_contents( $tmpFile->getPath() );
378 $this->assertNotEquals( false, $contents, "Local copy of $dest exists." );
379
380 if ( $okStatus ) {
381 $this->assertEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
382 } else {
383 $this->assertNotEquals( $expContent, $contents, "Concat file at $dest has correct contents." );
384 }
385 }
386
387 function provider_testConcatenate() {
388 $cases = array();
389
390 $dest = $this->singleBasePath() . '/cont1/full_file.txt';
391 $srcs = array(
392 $this->singleBasePath() . '/cont1/file1.txt',
393 $this->singleBasePath() . '/cont1/file2.txt',
394 $this->singleBasePath() . '/cont1/file3.txt',
395 $this->singleBasePath() . '/cont1/file4.txt',
396 $this->singleBasePath() . '/cont1/file5.txt',
397 $this->singleBasePath() . '/cont1/file6.txt',
398 $this->singleBasePath() . '/cont1/file7.txt',
399 $this->singleBasePath() . '/cont1/file8.txt',
400 $this->singleBasePath() . '/cont1/file9.txt',
401 $this->singleBasePath() . '/cont1/file10.txt'
402 );
403 $content = array(
404 'egfage',
405 'ageageag',
406 'rhokohlr',
407 'shgmslkg',
408 'kenga',
409 'owagmal',
410 'kgmae',
411 'g eak;g',
412 'lkaem;a',
413 'legma'
414 );
415 $op = array( 'op' => 'concatenate', 'srcs' => $srcs, 'dst' => $dest );
416
417 $cases[] = array(
418 $op, // operation
419 $srcs, // sources
420 $content, // content for each source
421 false, // no dest already exists
422 true, // succeeds
423 );
424
425 $cases[] = array(
426 $op, // operation
427 $srcs, // sources
428 $content, // content for each source
429 true, // no dest already exists
430 false, // succeeds
431 );
432
433 $op['overwriteDest'] = true;
434 $cases[] = array(
435 $op, // operation
436 $srcs, // sources
437 $content, // content for each source
438 true, // no dest already exists
439 true, // succeeds
440 );
441
442 return $cases;
443 }
444
445 /**
446 * @dataProvider provider_testGetLocalCopy
447 */
448 public function testGetLocalCopy( $src, $content ) {
449 $this->pathsToPrune[] = $src;
450
451 $status = $this->backend->doOperation(
452 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
453 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
454
455 $tmpFile = $this->backend->getLocalCopy( array( 'src' => $src ) );
456 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
457
458 $contents = file_get_contents( $tmpFile->getPath() );
459 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
460 }
461
462 function provider_testGetLocalCopy() {
463 $cases = array();
464
465 $base = $this->singleBasePath();
466 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
467 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
468
469 return $cases;
470 }
471
472 /**
473 * @dataProvider provider_testGetReference
474 */
475 public function testGetLocalReference( $src, $content ) {
476 $this->pathsToPrune[] = $src;
477
478 $status = $this->backend->doOperation(
479 array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
480 $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
481
482 $tmpFile = $this->backend->getLocalReference( array( 'src' => $src ) );
483 $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." );
484
485 $contents = file_get_contents( $tmpFile->getPath() );
486 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
487 }
488
489 function provider_testGetReference() {
490 $cases = array();
491
492 $base = $this->singleBasePath();
493 $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" );
494 $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" );
495
496 return $cases;
497 }
498
499 // @TODO: testPrepare
500
501 // @TODO: testSecure
502
503 // @TODO: testClean
504
505 // @TODO: testDoOperations
506
507 public function testGetFileList() {
508 $base = $this->singleBasePath();
509 $files = array(
510 "$base/cont1/test1.txt",
511 "$base/cont1/test2.txt",
512 "$base/cont1/test3.txt",
513 "$base/cont1/subdir1/test1.txt",
514 "$base/cont1/subdir1/test2.txt",
515 "$base/cont1/subdir2/test3.txt",
516 "$base/cont1/subdir2/test4.txt",
517 "$base/cont1/subdir2/subdir/test1.txt",
518 "$base/cont1/subdir2/subdir/test2.txt",
519 "$base/cont1/subdir2/subdir/test3.txt",
520 "$base/cont1/subdir2/subdir/test4.txt",
521 "$base/cont1/subdir2/subdir/test5.txt",
522 "$base/cont1/subdir2/subdir/sub/test0.txt",
523 "$base/cont1/subdir2/subdir/sub/120-px-file.txt",
524 );
525 $this->pathsToPrune = array_merge( $this->pathsToPrune, $files );
526
527 // Add the files
528 $ops = array();
529 foreach ( $files as $file ) {
530 $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file );
531 }
532 $status = $this->backend->doOperations( $ops );
533 $this->assertEquals( true, $status->isOK(), "Creation of files succeeded." );
534
535 // Expected listing
536 $expected = array(
537 "test1.txt",
538 "test2.txt",
539 "test3.txt",
540 "subdir1/test1.txt",
541 "subdir1/test2.txt",
542 "subdir2/test3.txt",
543 "subdir2/test1.txt",
544 "subdir2/subdir/test1.txt",
545 "subdir2/subdir/test2.txt",
546 "subdir2/subdir/test3.txt",
547 "subdir2/subdir/test4.txt",
548 "subdir2/subdir/test5.txt",
549 "subdir2/subdir/sub/test0.txt",
550 "subdir2/subdir/sub/120-px-file.txt",
551 );
552 $expected = sort( $expected );
553
554 // Actual listing (no trailing slash)
555 $list = array();
556 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1" ) );
557 foreach ( $iter as $file ) {
558 $list[] = $file;
559 }
560
561 $this->assertEquals( $expected, sort( $list ), "Correct file listing." );
562
563 // Actual listing (with trailing slash)
564 $list = array();
565 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1/" ) );
566 foreach ( $iter as $file ) {
567 $list[] = $file;
568 }
569
570 $this->assertEquals( $expected, sort( $list ), "Correct file listing." );
571
572 foreach ( $files as $file ) {
573 $this->backend->delete( array( 'src' => "$base/$files" ) );
574 }
575
576 $iter = $this->backend->getFileList( array( 'dir' => "$base/cont1/not/exists" ) );
577 foreach ( $iter as $iter ) {} // no errors
578 }
579
580 function tearDown() {
581 parent::tearDown();
582 foreach ( $this->filesToPrune as $file ) {
583 @unlink( $file );
584 }
585 foreach ( $this->pathsToPrune as $file ) {
586 $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
587 $this->multiBackend->doOperation( array( 'op' => 'delete', 'src' => $file ) );
588 }
589 $this->backend = $this->multiBackend = null;
590 $this->filesToPrune = $this->pathsToPrune = array();
591 }
592 }