Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
blob2c1e35dca0bad45e0e78225702f3044eed795df5
1 <?php
3 use MediaWiki\Status\Status;
4 use MediaWiki\WikiMap\WikiMap;
5 use Wikimedia\FileBackend\FSFileBackend;
7 /**
8 * @group FileRepo
9 * @group medium
11 class StoreBatchTest extends MediaWikiIntegrationTestCase {
13 /** @var string[] */
14 protected $createdFiles;
15 /** @var string */
16 protected $date;
17 /** @var FileRepo */
18 protected $repo;
20 protected function setUp(): void {
21 global $wgFileBackends;
22 parent::setUp();
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' );
28 $useConfig = [];
29 foreach ( $wgFileBackends as $conf ) {
30 if ( $conf['name'] == $name ) {
31 $useConfig = $conf;
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 );
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",
49 ] );
51 $this->repo = new FileRepo( [
52 'name' => 'unittests',
53 'backend' => $backend
54 ] );
56 $this->date = gmdate( "YmdHis" );
57 $this->createdFiles = [];
60 protected function tearDown(): void {
61 // Delete files
62 $this->repo->cleanupBatch( $this->createdFiles );
63 parent::tearDown();
66 /**
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().
72 * @return Status
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;
83 return $result;
86 /**
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}" );
99 if ( $fromrepo ) {
100 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
101 $infn = $f->value;
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.
119 if ( $fromrepo ) {
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() {
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
140 $this->storecohort(
141 "Test2.png",
142 "$IP/tests/phpunit/data/filerepo/wiki.png",
143 "$IP/tests/phpunit/data/filerepo/video.png",
144 true