MediaWiki REL1_33
StoreBatchTest.php
Go to the documentation of this file.
1<?php
2
8
9 protected $createdFiles;
10 protected $date;
12 protected $repo;
13
14 protected function setUp() {
15 global $wgFileBackends;
16 parent::setUp();
17
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;
26 }
27 }
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",
42 ]
43 ] );
44 }
45 $this->repo = new FileRepo( [
46 'name' => 'unittests',
47 'backend' => $backend
48 ] );
49
50 $this->date = gmdate( "YmdHis" );
51 $this->createdFiles = [];
52 }
53
54 protected function tearDown() {
55 // Delete files
56 $this->repo->cleanupBatch( $this->createdFiles );
57 parent::tearDown();
58 }
59
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 );
72
73 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
74 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
75 $this->createdFiles[] = $result->value;
76
77 return $result;
78 }
79
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;
96 }
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;
116 }
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}" );
121 }
122
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
133 );
134 $this->storecohort(
135 "Test2.png",
136 "$IP/tests/phpunit/data/filerepo/wiki.png",
137 "$IP/tests/phpunit/data/filerepo/video.png",
138 true
139 );
140 }
141}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgFileBackends
File backend structure configuration.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Class for a file system (FS) based file backend.
Base class for file repositories.
Definition FileRepo.php:39
const OVERWRITE_SAME
Definition FileRepo.php:42
const OVERWRITE
Definition FileRepo.php:41
static singleton( $domain=false)
getNewTempDirectory()
obtains a new temporary directory
FileRepo medium.
teststore()
FileRepo::store.
storecohort( $fn, $infn, $otherfn, $fromrepo)
Test storing a file using different flags.
storeit( $originalName, $srcPath, $flags)
Store a file or virtual URL source into a media file name.
namespace being checked & $result
Definition hooks.txt:2340
$IP
Definition update.php:3
$f
Definition router.php:79