* Added FileBackend::parentStoragePath() convenience function for getting parent...
[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
30 /**
31 * Store a file or virtual URL source into a media file name.
32 *
33 * @param $originalName string The title of the image
34 * @param $srcPath string The filepath or virtual URL
35 * @param $flags integer Flags to pass into repo::store().
36 */
37 private function storeit($originalName, $srcPath, $flags) {
38 $hashPath = $this->repo->getHashPath( $originalName );
39 $dstRel = "$hashPath{$this->date}!$originalName";
40 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
41
42 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
43 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
44 $this->createdFiles[] = $result->value;
45 return $result;
46 }
47
48 /**
49 * Test storing a file using different flags.
50 *
51 * @param $fn string The title of the image
52 * @param $infn string The name of the file (in the filesystem)
53 * @param $otherfn string The name of the different file (in the filesystem)
54 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
55 */
56 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
57 $f = $this->storeit( $fn, $infn, 0 );
58 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
59 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
60 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
61 if ( $fromrepo ) {
62 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
63 $infn = $f->value;
64 }
65 // This should work because we're allowed to overwrite
66 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
67 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
68 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
69 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
70 // This should fail because we're overwriting.
71 $f = $this->storeit( $fn, $infn, 0 );
72 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
73 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
74 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
75 // This should succeed because we're overwriting the same content.
76 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
77 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
78 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
79 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
80 // This should fail because we're overwriting different content.
81 if ( $fromrepo ) {
82 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
83 $otherfn = $f->value;
84 }
85 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
86 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
87 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
88 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
89 }
90
91 public function teststore() {
92 global $IP;
93 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
94 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
95 }
96
97 public function tearDown() {
98 $this->repo->cleanupBatch( $this->createdFiles );
99 foreach ( $this->createdFiles as $tmp ) {
100 $tmp = $this->repo->resolveVirtualUrl( $tmp );
101 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
102 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
103 }
104 }
105 parent::tearDown();
106 }
107 }