6 class StoreBatchTest
extends MediaWikiTestCase
{
8 public function setUp() {
9 global $wgFileBackends;
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=' );
17 foreach ( $wgFileBackends as $conf ) {
18 if ( $conf['name'] == $name ) {
22 $useConfig['name'] = 'local-testing'; // swap name
23 $class = $useConfig['class'];
24 $backend = new $class( $useConfig );
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",
37 $this->repo
= new FileRepo( array(
38 'name' => 'unittests',
42 $this->date
= gmdate( "YmdHis" );
43 $this->createdFiles
= array();
47 * Store a file or virtual URL source into a media file name.
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().
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 );
58 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
59 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
60 $this->createdFiles
[] = $result->value
;
65 * Test storing a file using different flags.
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.
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}" );
78 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
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.
98 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
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}" );
107 public function teststore() {
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 );
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 ) );