r110180: fixed copy-paste cruft for custom backend code. Also made backend names...
[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 global $wgFileBackends;
9 parent::setUp();
10
11 # Forge a FSRepo object to not have to rely on local wiki settings
12 $tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
13 if ( $this->getCliArg( 'use-filebackend=' ) ) {
14 $name = $this->getCliArg( 'use-filebackend=' );
15 $useConfig = array();
16 foreach ( $wgFileBackends as $conf ) {
17 if ( $conf['name'] == $name ) {
18 $useConfig = $conf;
19 }
20 }
21 $useConfig['name'] = 'local-testing'; // swap name
22 $class = $useConfig['class'];
23 $backend = new $class( $useConfig );
24 } else {
25 $backend = new FSFileBackend( array(
26 'name' => 'local-testing',
27 'lockManager' => 'nullLockManager',
28 'containerPaths' => array(
29 'unittests-public' => "{$tmpPrefix}-public",
30 'unittests-thumb' => "{$tmpPrefix}-thumb",
31 'unittests-temp' => "{$tmpPrefix}-temp",
32 'unittests-deleted' => "{$tmpPrefix}-deleted",
33 )
34 ) );
35 }
36 $this->repo = new FileRepo( array(
37 'name' => 'unittests',
38 'backend' => $backend
39 ) );
40
41 $this->date = gmdate( "YmdHis" );
42 $this->createdFiles = array();
43 }
44
45 /**
46 * Store a file or virtual URL source into a media file name.
47 *
48 * @param $originalName string The title of the image
49 * @param $srcPath string The filepath or virtual URL
50 * @param $flags integer Flags to pass into repo::store().
51 */
52 private function storeit($originalName, $srcPath, $flags) {
53 $hashPath = $this->repo->getHashPath( $originalName );
54 $dstRel = "$hashPath{$this->date}!$originalName";
55 $dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
56
57 $result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
58 $result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
59 $this->createdFiles[] = $result->value;
60 return $result;
61 }
62
63 /**
64 * Test storing a file using different flags.
65 *
66 * @param $fn string The title of the image
67 * @param $infn string The name of the file (in the filesystem)
68 * @param $otherfn string The name of the different file (in the filesystem)
69 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
70 */
71 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
72 $f = $this->storeit( $fn, $infn, 0 );
73 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
74 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
75 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
76 if ( $fromrepo ) {
77 $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE);
78 $infn = $f->value;
79 }
80 // This should work because we're allowed to overwrite
81 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
82 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
83 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
84 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
85 // This should fail because we're overwriting.
86 $f = $this->storeit( $fn, $infn, 0 );
87 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
88 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
89 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
90 // This should succeed because we're overwriting the same content.
91 $f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
92 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
93 $this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
94 $this->assertEquals( $f->successCount, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
95 // This should fail because we're overwriting different content.
96 if ( $fromrepo ) {
97 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE);
98 $otherfn = $f->value;
99 }
100 $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
101 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
102 $this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
103 $this->assertEquals( $f->successCount, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
104 }
105
106 public function teststore() {
107 global $IP;
108 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
109 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
110 }
111
112 public function tearDown() {
113 $this->repo->cleanupBatch( $this->createdFiles ); // delete files
114 foreach ( $this->createdFiles as $tmp ) { // delete dirs
115 $tmp = $this->repo->resolveVirtualUrl( $tmp );
116 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
117 $this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
118 }
119 }
120 parent::tearDown();
121 }
122 }