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