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