| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Status\Status; |
| 4 |
use MediaWiki\WikiMap\WikiMap; |
| 5 |
use Wikimedia\FileBackend\FSFileBackend; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class StoreBatchTest extends MediaWikiIntegrationTestCase { |
| 12 |
|
| 13 |
|
| 14 |
protected $createdFiles; |
| 15 |
|
| 16 |
protected $date; |
| 17 |
|
| 18 |
protected $repo; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
global $wgFileBackends; |
| 22 |
parent::setUp(); |
| 23 |
|
| 24 |
|
| 25 |
$tmpPrefix = $this->getNewTempDirectory(); |
| 26 |
if ( $this->getCliArg( 'use-filebackend' ) ) { |
| 27 |
$name = $this->getCliArg( 'use-filebackend' ); |
| 28 |
$useConfig = []; |
| 29 |
foreach ( $wgFileBackends as $conf ) { |
| 30 |
if ( $conf['name'] == $name ) { |
| 31 |
$useConfig = $conf; |
| 32 |
} |
| 33 |
} |
| 34 |
$useConfig['lockManager'] = $this->getServiceContainer()->getLockManagerGroupFactory() |
| 35 |
->getLockManagerGroup()->get( $useConfig['lockManager'] ); |
| 36 |
$useConfig['name'] = 'local-testing'; |
| 37 |
$class = $useConfig['class']; |
| 38 |
$backend = new $class( $useConfig ); |
| 39 |
} else { |
| 40 |
$backend = new FSFileBackend( [ |
| 41 |
'name' => 'local-testing', |
| 42 |
'wikiId' => WikiMap::getCurrentWikiId(), |
| 43 |
'containerPaths' => [ |
| 44 |
'unittests-public' => "{$tmpPrefix}/public", |
| 45 |
'unittests-thumb' => "{$tmpPrefix}/thumb", |
| 46 |
'unittests-temp' => "{$tmpPrefix}/temp", |
| 47 |
'unittests-deleted' => "{$tmpPrefix}/deleted", |
| 48 |
] |
| 49 |
] ); |
| 50 |
} |
| 51 |
$this->repo = new FileRepo( [ |
| 52 |
'name' => 'unittests', |
| 53 |
'backend' => $backend |
| 54 |
] ); |
| 55 |
|
| 56 |
$this->date = gmdate( "YmdHis" ); |
| 57 |
$this->createdFiles = []; |
| 58 |
} |
| 59 |
|
| 60 |
protected function tearDown(): void { |
| 61 |
|
| 62 |
$this->repo->cleanupBatch( $this->createdFiles ); |
| 63 |
parent::tearDown(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private function storeit( $originalName, $srcPath, $flags ) { |
| 75 |
$hashPath = $this->repo->getHashPath( $originalName ); |
| 76 |
$dstRel = "$hashPath{$this->date}!$originalName"; |
| 77 |
$dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName ); |
| 78 |
|
| 79 |
$result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags ); |
| 80 |
$result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel; |
| 81 |
$this->createdFiles[] = $result->value; |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private function storecohort( $fn, $infn, $otherfn, $fromrepo ) { |
| 95 |
$f = $this->storeit( $fn, $infn, 0 ); |
| 96 |
$this->assertStatusGood( $f, 'failed to store a new file' ); |
| 97 |
$this->assertSame( 0, $f->failCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 98 |
$this->assertSame( 1, $f->successCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 99 |
if ( $fromrepo ) { |
| 100 |
$f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE ); |
| 101 |
$infn = $f->value; |
| 102 |
} |
| 103 |
|
| 104 |
$f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE ); |
| 105 |
$this->assertStatusGood( $f, 'We should be allowed to overwrite' ); |
| 106 |
$this->assertSame( 0, $f->failCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 107 |
$this->assertSame( 1, $f->successCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 108 |
|
| 109 |
$f = $this->storeit( $fn, $infn, 0 ); |
| 110 |
$this->assertStatusError( 'backend-fail-alreadyexists', $f, 'We should not be allowed to overwrite' ); |
| 111 |
$this->assertSame( 1, $f->failCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 112 |
$this->assertSame( 0, $f->successCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 113 |
|
| 114 |
$f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME ); |
| 115 |
$this->assertStatusGood( $f, 'We should be able to overwrite the same content' ); |
| 116 |
$this->assertSame( 0, $f->failCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 117 |
$this->assertSame( 1, $f->successCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 118 |
|
| 119 |
if ( $fromrepo ) { |
| 120 |
$f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE ); |
| 121 |
$otherfn = $f->value; |
| 122 |
} |
| 123 |
$f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME ); |
| 124 |
$this->assertStatusError( 'backend-fail-notsame', $f, 'We should not be allowed to overwrite different content' ); |
| 125 |
$this->assertSame( 1, $f->failCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 126 |
$this->assertSame( 0, $f->successCount, "counts wrong {$f->successCount} {$f->failCount}" ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function teststore() { |
| 133 |
global $IP; |
| 134 |
$this->storecohort( |
| 135 |
"Test1.png", |
| 136 |
"$IP/tests/phpunit/data/filerepo/wiki.png", |
| 137 |
"$IP/tests/phpunit/data/filerepo/video.png", |
| 138 |
false |
| 139 |
); |
| 140 |
$this->storecohort( |
| 141 |
"Test2.png", |
| 142 |
"$IP/tests/phpunit/data/filerepo/wiki.png", |
| 143 |
"$IP/tests/phpunit/data/filerepo/video.png", |
| 144 |
true |
| 145 |
); |
| 146 |
} |
| 147 |
} |