7 class StoreBatchTest
extends MediaWikiTestCase
{
9 protected $createdFiles;
14 protected function setUp() {
15 global $wgFileBackends;
18 # Forge a FileRepo object to not have to rely on local wiki settings
19 $tmpPrefix = $this->getNewTempDirectory();
20 if ( $this->getCliArg( 'use-filebackend' ) ) {
21 $name = $this->getCliArg( 'use-filebackend' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
28 $useConfig['lockManager'] = LockManagerGroup
::singleton()->get( $useConfig['lockManager'] );
29 unset( $useConfig['fileJournal'] );
30 $useConfig['name'] = 'local-testing'; // swap name
31 $class = $useConfig['class'];
32 $backend = new $class( $useConfig );
34 $backend = new FSFileBackend( [
35 'name' => 'local-testing',
36 'wikiId' => wfWikiID(),
38 'unittests-public' => "{$tmpPrefix}/public",
39 'unittests-thumb' => "{$tmpPrefix}/thumb",
40 'unittests-temp' => "{$tmpPrefix}/temp",
41 'unittests-deleted' => "{$tmpPrefix}/deleted",
45 $this->repo
= new FileRepo( [
46 'name' => 'unittests',
50 $this->date
= gmdate( "YmdHis" );
51 $this->createdFiles
= [];
54 protected function tearDown() {
56 $this->repo
->cleanupBatch( $this->createdFiles
);
61 * Store a file or virtual URL source into a media file name.
63 * @param string $originalName The title of the image
64 * @param string $srcPath The filepath or virtual URL
65 * @param int $flags Flags to pass into repo::store().
68 private function storeit( $originalName, $srcPath, $flags ) {
69 $hashPath = $this->repo
->getHashPath( $originalName );
70 $dstRel = "$hashPath{$this->date}!$originalName";
71 $dstUrlRel = $hashPath . $this->date
. '!' . rawurlencode( $originalName );
73 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
74 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
75 $this->createdFiles
[] = $result->value
;
81 * Test storing a file using different flags.
83 * @param string $fn The title of the image
84 * @param string $infn The name of the file (in the filesystem)
85 * @param string $otherfn The name of the different file (in the filesystem)
86 * @param bool $fromrepo 'true' if we want to copy from a virtual URL out of the Repo.
88 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
89 $f = $this->storeit( $fn, $infn, 0 );
90 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
91 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
92 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
94 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
97 // This should work because we're allowed to overwrite
98 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE
);
99 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
100 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
101 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
102 // This should fail because we're overwriting.
103 $f = $this->storeit( $fn, $infn, 0 );
104 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
105 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
106 $this->assertEquals( $f->successCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
107 // This should succeed because we're overwriting the same content.
108 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE_SAME
);
109 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
110 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
111 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
112 // This should fail because we're overwriting different content.
114 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
115 $otherfn = $f->value
;
117 $f = $this->storeit( $fn, $otherfn, FileRepo
::OVERWRITE_SAME
);
118 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
119 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
120 $this->assertEquals( $f->successCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
124 * @covers FileRepo::store
126 public function teststore() {
130 "$IP/tests/phpunit/data/filerepo/wiki.png",
131 "$IP/tests/phpunit/data/filerepo/video.png",
136 "$IP/tests/phpunit/data/filerepo/wiki.png",
137 "$IP/tests/phpunit/data/filerepo/video.png",