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