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