6 class StoreBatchTest
extends MediaWikiTestCase
{
8 protected 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();
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 ) );
58 * Store a file or virtual URL source into a media file name.
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().
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 );
69 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
70 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
71 $this->createdFiles
[] = $result->value
;
77 * Test storing a file using different flags.
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.
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}" );
90 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
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.
110 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
111 $otherfn = $f->value
;
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}" );
119 public function teststore() {
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 );