MediaWiki
REL1_21
|
00001 <?php 00006 class StoreBatchTest extends MediaWikiTestCase { 00007 00008 protected 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 00046 protected function tearDown() { 00047 $this->repo->cleanupBatch( $this->createdFiles ); // delete files 00048 foreach ( $this->createdFiles as $tmp ) { // delete dirs 00049 $tmp = $this->repo->resolveVirtualUrl( $tmp ); 00050 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) { 00051 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) ); 00052 } 00053 } 00054 parent::tearDown(); 00055 } 00056 00064 private function storeit( $originalName, $srcPath, $flags ) { 00065 $hashPath = $this->repo->getHashPath( $originalName ); 00066 $dstRel = "$hashPath{$this->date}!$originalName"; 00067 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName ); 00068 00069 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags ); 00070 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel; 00071 $this->createdFiles[] = $result->value; 00072 return $result; 00073 } 00074 00083 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) { 00084 $f = $this->storeit( $fn, $infn, 0 ); 00085 $this->assertTrue( $f->isOK(), 'failed to store a new file' ); 00086 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" ); 00087 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" ); 00088 if ( $fromrepo ) { 00089 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE ); 00090 $infn = $f->value; 00091 } 00092 // This should work because we're allowed to overwrite 00093 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE ); 00094 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' ); 00095 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" ); 00096 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" ); 00097 // This should fail because we're overwriting. 00098 $f = $this->storeit( $fn, $infn, 0 ); 00099 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' ); 00100 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" ); 00101 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" ); 00102 // This should succeed because we're overwriting the same content. 00103 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME ); 00104 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' ); 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 different content. 00108 if ( $fromrepo ) { 00109 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE ); 00110 $otherfn = $f->value; 00111 } 00112 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME ); 00113 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' ); 00114 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" ); 00115 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" ); 00116 } 00117 00118 public function teststore() { 00119 global $IP; 00120 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false ); 00121 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true ); 00122 } 00123 }