MediaWiki  REL1_20
StoreBatchTest.php
Go to the documentation of this file.
00001 <?php
00006 class StoreBatchTest extends MediaWikiTestCase {
00007 
00008         public function setUp() {
00009                 global $wgFileBackends;
00010                 parent::setUp();
00011 
00012                 # Forge a FSRepo object to not have to rely on local wiki settings
00013                 $tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
00014                 if ( $this->getCliArg( 'use-filebackend=' ) ) {
00015                         $name = $this->getCliArg( 'use-filebackend=' );
00016                         $useConfig = array();
00017                         foreach ( $wgFileBackends as $conf ) {
00018                                 if ( $conf['name'] == $name ) {
00019                                         $useConfig = $conf;
00020                                 }
00021                         }
00022                         $useConfig['name'] = 'local-testing'; // swap name
00023                         $class = $useConfig['class'];
00024                         $backend = new $class( $useConfig );
00025                 } else {
00026                         $backend = new FSFileBackend( array(
00027                                 'name'        => 'local-testing',
00028                                 'lockManager' => 'nullLockManager',
00029                                 'containerPaths' => array(
00030                                         'unittests-public'  => "{$tmpPrefix}-public",
00031                                         'unittests-thumb'   => "{$tmpPrefix}-thumb",
00032                                         'unittests-temp'    => "{$tmpPrefix}-temp",
00033                                         'unittests-deleted' => "{$tmpPrefix}-deleted",
00034                                 )
00035                         ) );
00036                 }
00037                 $this->repo = new FileRepo( array(
00038                         'name'    => 'unittests',
00039                         'backend' => $backend
00040                 ) );
00041 
00042                 $this->date = gmdate( "YmdHis" );
00043                 $this->createdFiles = array();
00044         }
00045 
00053         private function storeit($originalName, $srcPath, $flags) {
00054                 $hashPath = $this->repo->getHashPath( $originalName );
00055                 $dstRel = "$hashPath{$this->date}!$originalName";
00056                 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
00057 
00058                 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
00059                 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
00060                 $this->createdFiles[] = $result->value;
00061                 return $result;
00062         }
00063 
00072         private function storecohort($fn, $infn, $otherfn, $fromrepo) {
00073                 $f = $this->storeit( $fn, $infn, 0 );
00074                 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
00075                 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00076                 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
00077                 if ( $fromrepo ) {
00078                         $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
00079                         $infn = $f->value;
00080                 }
00081                 // This should work because we're allowed to overwrite
00082                 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
00083                 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
00084                 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00085                 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
00086                 // This should fail because we're overwriting.
00087                 $f = $this->storeit( $fn, $infn, 0 );
00088                 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
00089                 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00090                 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
00091                 // This should succeed because we're overwriting the same content.
00092                 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
00093                 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
00094                 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00095                 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
00096                 // This should fail because we're overwriting different content.
00097                 if ( $fromrepo ) {
00098                         $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
00099                         $otherfn = $f->value;
00100                 }
00101                 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
00102                 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
00103                 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00104                 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
00105         }
00106 
00107         public function teststore() {
00108                 global $IP;
00109                 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
00110                 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
00111         }
00112 
00113         public function tearDown() {
00114                 $this->repo->cleanupBatch( $this->createdFiles ); // delete files
00115                 foreach ( $this->createdFiles as $tmp ) { // delete dirs
00116                         $tmp = $this->repo->resolveVirtualUrl( $tmp );
00117                         while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
00118                                 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
00119                         }
00120                 }
00121                 parent::tearDown();
00122         }
00123 }