Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
blob337c65c4b763295ebfd96ae5935fca3d7491b6e9
1 <?php
3 /**
4 * @group FileRepo
5 * @group medium
6 */
7 class StoreBatchTest extends MediaWikiTestCase {
9 protected $createdFiles;
10 protected $date;
11 /** @var FileRepo */
12 protected $repo;
14 protected function setUp() {
15 global $wgFileBackends;
16 parent::setUp();
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' );
22 $useConfig = [];
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
25 $useConfig = $conf;
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 );
33 } else {
34 $backend = new FSFileBackend( [
35 'name' => 'local-testing',
36 'wikiId' => wfWikiID(),
37 'containerPaths' => [
38 'unittests-public' => "{$tmpPrefix}/public",
39 'unittests-thumb' => "{$tmpPrefix}/thumb",
40 'unittests-temp' => "{$tmpPrefix}/temp",
41 'unittests-deleted' => "{$tmpPrefix}/deleted",
43 ] );
45 $this->repo = new FileRepo( [
46 'name' => 'unittests',
47 'backend' => $backend
48 ] );
50 $this->date = gmdate( "YmdHis" );
51 $this->createdFiles = [];
54 protected function tearDown() {
55 // Delete files
56 $this->repo->cleanupBatch( $this->createdFiles );
57 parent::tearDown();
60 /**
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().
66 * @return Status
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;
77 return $result;
80 /**
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}" );
93 if ( $fromrepo ) {
94 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
95 $infn = $f->value;
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.
113 if ( $fromrepo ) {
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() {
127 global $IP;
128 $this->storecohort(
129 "Test1.png",
130 "$IP/tests/phpunit/data/filerepo/wiki.png",
131 "$IP/tests/phpunit/data/filerepo/video.png",
132 false
134 $this->storecohort(
135 "Test2.png",
136 "$IP/tests/phpunit/data/filerepo/wiki.png",
137 "$IP/tests/phpunit/data/filerepo/video.png",
138 true