3 use MediaWiki\Status\Status
;
4 use MediaWiki\WikiMap\WikiMap
;
5 use Wikimedia\FileBackend\FSFileBackend
;
11 class StoreBatchTest
extends MediaWikiIntegrationTestCase
{
14 protected $createdFiles;
20 protected function setUp(): void
{
21 global $wgFileBackends;
24 # Forge a FileRepo object to not have to rely on local wiki settings
25 $tmpPrefix = $this->getNewTempDirectory();
26 if ( $this->getCliArg( 'use-filebackend' ) ) {
27 $name = $this->getCliArg( 'use-filebackend' );
29 foreach ( $wgFileBackends as $conf ) {
30 if ( $conf['name'] == $name ) {
34 $useConfig['lockManager'] = $this->getServiceContainer()->getLockManagerGroupFactory()
35 ->getLockManagerGroup()->get( $useConfig['lockManager'] );
36 $useConfig['name'] = 'local-testing'; // swap name
37 $class = $useConfig['class'];
38 $backend = new $class( $useConfig );
40 $backend = new FSFileBackend( [
41 'name' => 'local-testing',
42 'wikiId' => WikiMap
::getCurrentWikiId(),
44 'unittests-public' => "{$tmpPrefix}/public",
45 'unittests-thumb' => "{$tmpPrefix}/thumb",
46 'unittests-temp' => "{$tmpPrefix}/temp",
47 'unittests-deleted' => "{$tmpPrefix}/deleted",
51 $this->repo
= new FileRepo( [
52 'name' => 'unittests',
56 $this->date
= gmdate( "YmdHis" );
57 $this->createdFiles
= [];
60 protected function tearDown(): void
{
62 $this->repo
->cleanupBatch( $this->createdFiles
);
67 * Store a file or virtual URL source into a media file name.
69 * @param string $originalName The title of the image
70 * @param string $srcPath The filepath or virtual URL
71 * @param int $flags Flags to pass into repo::store().
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 );
79 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
80 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
81 $this->createdFiles
[] = $result->value
;
87 * Test storing a file using different flags.
89 * @param string $fn The title of the image
90 * @param string $infn The name of the file (in the filesystem)
91 * @param string $otherfn The name of the different file (in the filesystem)
92 * @param bool $fromrepo 'true' if we want to copy from a virtual URL out of the Repo.
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}" );
100 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
103 // This should work because we're allowed to overwrite
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 // This should fail because we're overwriting.
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 // This should succeed because we're overwriting the same content.
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 // This should fail because we're overwriting different content.
120 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
121 $otherfn = $f->value
;
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}" );
130 * @covers \FileRepo::store
132 public function teststore() {
136 "$IP/tests/phpunit/data/filerepo/wiki.png",
137 "$IP/tests/phpunit/data/filerepo/video.png",
142 "$IP/tests/phpunit/data/filerepo/wiki.png",
143 "$IP/tests/phpunit/data/filerepo/video.png",