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