MediaWiki  REL1_24
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['lockManager'] = LockManagerGroup::singleton()->get( $useConfig['lockManager'] );
00029             unset( $useConfig['fileJournal'] );
00030             $useConfig['name'] = 'local-testing'; // swap name
00031             $class = $useConfig['class'];
00032             $backend = new $class( $useConfig );
00033         } else {
00034             $backend = new FSFileBackend( array(
00035                 'name' => 'local-testing',
00036                 'wikiId' => wfWikiID(),
00037                 'containerPaths' => array(
00038                     'unittests-public' => "{$tmpPrefix}-public",
00039                     'unittests-thumb' => "{$tmpPrefix}-thumb",
00040                     'unittests-temp' => "{$tmpPrefix}-temp",
00041                     'unittests-deleted' => "{$tmpPrefix}-deleted",
00042                 )
00043             ) );
00044         }
00045         $this->repo = new FileRepo( array(
00046             'name' => 'unittests',
00047             'backend' => $backend
00048         ) );
00049 
00050         $this->date = gmdate( "YmdHis" );
00051         $this->createdFiles = array();
00052     }
00053 
00054     protected function tearDown() {
00055         $this->repo->cleanupBatch( $this->createdFiles ); // delete files
00056         foreach ( $this->createdFiles as $tmp ) { // delete dirs
00057             $tmp = $this->repo->resolveVirtualUrl( $tmp );
00058             while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
00059                 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
00060             }
00061         }
00062         parent::tearDown();
00063     }
00064 
00073     private function storeit( $originalName, $srcPath, $flags ) {
00074         $hashPath = $this->repo->getHashPath( $originalName );
00075         $dstRel = "$hashPath{$this->date}!$originalName";
00076         $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
00077 
00078         $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
00079         $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
00080         $this->createdFiles[] = $result->value;
00081 
00082         return $result;
00083     }
00084 
00093     private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
00094         $f = $this->storeit( $fn, $infn, 0 );
00095         $this->assertTrue( $f->isOK(), 'failed to store a new file' );
00096         $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00097         $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00098         if ( $fromrepo ) {
00099             $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
00100             $infn = $f->value;
00101         }
00102         // This should work because we're allowed to overwrite
00103         $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
00104         $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
00105         $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00106         $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00107         // This should fail because we're overwriting.
00108         $f = $this->storeit( $fn, $infn, 0 );
00109         $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
00110         $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00111         $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00112         // This should succeed because we're overwriting the same content.
00113         $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
00114         $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
00115         $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00116         $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00117         // This should fail because we're overwriting different content.
00118         if ( $fromrepo ) {
00119             $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE );
00120             $otherfn = $f->value;
00121         }
00122         $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
00123         $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
00124         $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
00125         $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
00126     }
00127 
00131     public function teststore() {
00132         global $IP;
00133         $this->storecohort(
00134             "Test1.png",
00135             "$IP/tests/phpunit/data/filerepo/wiki.png",
00136             "$IP/tests/phpunit/data/filerepo/video.png",
00137             false
00138         );
00139         $this->storecohort(
00140             "Test2.png",
00141             "$IP/tests/phpunit/data/filerepo/wiki.png",
00142             "$IP/tests/phpunit/data/filerepo/video.png",
00143             true
00144         );
00145     }
00146 }