Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
blob3276cf98d4b082521b7142e59d54feb49f2fd3a4
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 FSRepo object to not have to rely on local wiki settings
19 $tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
20 if ( $this->getCliArg( 'use-filebackend=' ) ) {
21 $name = $this->getCliArg( 'use-filebackend=' );
22 $useConfig = array();
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( array(
35 'name' => 'local-testing',
36 'wikiId' => wfWikiID(),
37 'containerPaths' => array(
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( array(
46 'name' => 'unittests',
47 'backend' => $backend
48 ) );
50 $this->date = gmdate( "YmdHis" );
51 $this->createdFiles = array();
54 protected function tearDown() {
55 $this->repo->cleanupBatch( $this->createdFiles ); // delete files
56 foreach ( $this->createdFiles as $tmp ) { // delete dirs
57 $tmp = $this->repo->resolveVirtualUrl( $tmp );
58 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
59 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
62 parent::tearDown();
65 /**
66 * Store a file or virtual URL source into a media file name.
68 * @param string $originalName The title of the image
69 * @param string $srcPath The filepath or virtual URL
70 * @param int $flags Flags to pass into repo::store().
71 * @return FileRepoStatus
73 private function storeit( $originalName, $srcPath, $flags ) {
74 $hashPath = $this->repo->getHashPath( $originalName );
75 $dstRel = "$hashPath{$this->date}!$originalName";
76 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
78 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
79 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
80 $this->createdFiles[] = $result->value;
82 return $result;
85 /**
86 * Test storing a file using different flags.
88 * @param string $fn The title of the image
89 * @param string $infn The name of the file (in the filesystem)
90 * @param string $otherfn The name of the different file (in the filesystem)
91 * @param bool $fromrepo 'true' if we want to copy from a virtual URL out of the Repo.
93 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
94 $f = $this->storeit( $fn, $infn, 0 );
95 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
96 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
97 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
98 if ( $fromrepo ) {
99 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
100 $infn = $f->value;
102 // This should work because we're allowed to overwrite
103 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
104 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
105 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
106 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
107 // This should fail because we're overwriting.
108 $f = $this->storeit( $fn, $infn, 0 );
109 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
110 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
111 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
112 // This should succeed because we're overwriting the same content.
113 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
114 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
115 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
116 $this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
117 // This should fail because we're overwriting different content.
118 if ( $fromrepo ) {
119 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE );
120 $otherfn = $f->value;
122 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
123 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
124 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
125 $this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
129 * @covers FileRepo::store
131 public function teststore() {
132 global $IP;
133 $this->storecohort(
134 "Test1.png",
135 "$IP/skins/monobook/wiki.png",
136 "$IP/skins/monobook/video.png",
137 false
139 $this->storecohort(
140 "Test2.png",
141 "$IP/skins/monobook/wiki.png",
142 "$IP/skins/monobook/video.png",
143 true