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