865003dff24110e7a4ef240c86efce1cb8b4c70e
5 class StoreBatchTest
extends MediaWikiTestCase
{
7 public function setUp() {
8 global $wgFileBackends;
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=' );
16 foreach ( $wgFileBackends as $conf ) {
17 if ( $conf['name'] == $name ) {
21 $useConfig['name'] = 'localtesting'; // swap name
22 $backend = new $conf['class']( $useConfig );
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",
35 $this->repo
= new FileRepo( array(
36 'name' => 'unittests',
40 $this->date
= gmdate( "YmdHis" );
41 $this->createdFiles
= array();
45 * Store a file or virtual URL source into a media file name.
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().
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 );
56 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
57 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
58 $this->createdFiles
[] = $result->value
;
63 * Test storing a file using different flags.
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.
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}" );
76 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
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.
96 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
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}" );
105 public function teststore() {
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 );
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 ) );