bc14f48762168d19e5b7cc517cd2c059c025b065
[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 $tmpDir = wfTempDir() . '/' . time() . '-' . mt_rand();
10 $this->repo = new FSRepo( array(
11 'name' => 'test',
12 'backend' => new FSFileBackend( array(
13 'name' => 'local-backend',
14 'lockManager' => 'nullLockManager',
15 'containerPaths' => array(
16 'test-public' => "$tmpDir/public",
17 'test-thumb' => "$tmpDir/thumb",
18 'test-temp' => "$tmpDir/temp",
19 'test-deleted' => "$tmpDir/deleted",
20 )
21 ) )
22 ) );
23 $this->date = gmdate( "YmdHis" );
24 $this->createdFiles = array();
25 $this->users = array(
26 'sysop' => new ApiTestUser(
27 'Uploadstashtestsysop',
28 'Upload Stash Test Sysop',
29 'upload_stash_test_sysop@example.com',
30 array( 'sysop' )
31 ),
32 'uploader' => new ApiTestUser(
33 'Uploadstashtestuser',
34 'Upload Stash Test User',
35 'upload_stash_test_user@example.com',
36 array()
37 )
38 );
39 }
40
41 /**
42 * Store a file or virtual URL source into a media file name.
43 *
44 * @param $originalName string The title of the image
45 * @param $srcPath string The filepath or virtual URL
46 * @param $flags integer Flags to pass into repo::store().
47 */
48 private function storeit($originalName, $srcPath, $flags) {
49 $hashPath = $this->repo->getHashPath( $originalName );
50 $dstRel = "$hashPath{$this->date}!$originalName";
51 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
52
53 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
54 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
55 $this->createdFiles[] = $result->value;
56 return $result;
57 }
58
59 /**
60 * Test storing a file using different flags.
61 *
62 * @param $fn string The title of the image
63 * @param $infn string The name of the file (in the filesystem)
64 * @param $otherfn string The name of the different file (in the filesystem)
65 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
66 */
67 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
68 $f = $this->storeit( $fn, $infn, 0 );
69 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
70 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
71 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
72 if ( $fromrepo ) {
73 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
74 $infn = $f->value;
75 }
76 // This should work because we're allowed to overwrite
77 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
78 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
79 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
80 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
81 // This should fail because we're overwriting.
82 $f = $this->storeit( $fn, $infn, 0 );
83 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
84 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
85 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
86 // This should succeed because we're overwriting the same content.
87 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
88 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
89 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
90 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
91 // This should fail because we're overwriting different content.
92 if ( $fromrepo ) {
93 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
94 $otherfn = $f->value;
95 }
96 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
97 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
98 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
99 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
100 }
101
102 public function teststore() {
103 global $IP;
104 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
105 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
106 }
107
108 public function tearDown() {
109 $this->repo->cleanupBatch( $this->createdFiles );
110 parent::tearDown();
111 }
112 }