60eba7731000fa701f80e37375d47eb5a2187a02
[lhc/web/wiklou.git] / tests / phpunit / includes / filerepo / StoreBatchTest.php
1 <?php
2 /**
3 * @group Filerepo
4 */
5 class StoreBatchTest extends MediaWikiTestCase {
6
7 public function setUp() {
8 parent::setUp();
9
10 $this->repo = RepoGroup::singleton()->getLocalRepo();
11 $this->date = gmdate( "YmdHis" );
12 $this->users = array(
13 'sysop' => new ApiTestUser(
14 'Uploadstashtestsysop',
15 'Upload Stash Test Sysop',
16 'upload_stash_test_sysop@sample.com',
17 array( 'sysop' )
18 ),
19 'uploader' => new ApiTestUser(
20 'Uploadstashtestuser',
21 'Upload Stash Test User',
22 'upload_stash_test_user@sample.com',
23 array()
24 )
25 );
26 }
27
28 /**
29 * Store a file or virtual URL source into a media file name.
30 *
31 * @param $originalName string The title of the image
32 * @param $srcPath string The filepath or virtual URL
33 * @param $flags integer Flags to pass into repo::store().
34 */
35 private function storeit($originalName, $srcPath, $flags) {
36 $hashPath = $this->repo->getHashPath( $originalName );
37 $dstRel = "$hashPath{$this->date}!$originalName";
38 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
39
40 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
41 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
42 return $result;
43 }
44
45
46 /**
47 * Test storing a file using different flags.
48 *
49 * @param $fn string The title of the image
50 * @param $infn string The name of the file (in the filesystem)
51 * @param $otherfn string The name of the different file (in the filesystem)
52 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
53 */
54 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
55 $f = $this->storeit( $fn, $infn, 0 );
56 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
57 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
58 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
59 if ( $fromrepo ) {
60 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
61 $infn = $f->value;
62 }
63 // This should work because we're allowed to overwrite
64 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
65 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
66 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
67 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
68 // This should fail because we're overwriting.
69 $f = $this->storeit( $fn, $infn, 0 );
70 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
71 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
72 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
73 // This should succeed because we're overwriting the same content.
74 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
75 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
76 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
77 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
78 // This should fail because we're overwriting different content.
79 if ( $fromrepo ) {
80 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
81 $otherfn = $f->value;
82 }
83 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
84 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
85 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
86 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
87 }
88
89 public function teststore() {
90 global $IP;
91 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
92 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
93 }
94
95 public function tearDown() {
96 parent::tearDown();
97
98 }
99 }