5 class StoreBatchTest
extends MediaWikiTestCase
{
7 public function setUp() {
10 # Forge a FSRepo object to not have to rely on local wiki settings
11 $this->tmpDir
= wfTempDir() . '/store-batch-test-' . time() . '-' . mt_rand();
12 $this->repo
= new FSRepo( array(
14 'backend' => new FSFileBackend( array(
15 'name' => 'local-backend',
16 'lockManager' => 'nullLockManager',
17 'containerPaths' => array(
18 'test-public' => $this->tmpDir
. "/public",
19 'test-thumb' => $this->tmpDir
. "/thumb",
20 'test-temp' => $this->tmpDir
. "/temp",
21 'test-deleted' => $this->tmpDir
. "/deleted",
26 $this->date
= gmdate( "YmdHis" );
27 $this->createdFiles
= array();
31 * Store a file or virtual URL source into a media file name.
33 * @param $originalName string The title of the image
34 * @param $srcPath string The filepath or virtual URL
35 * @param $flags integer Flags to pass into repo::store().
37 private function storeit($originalName, $srcPath, $flags) {
38 $hashPath = $this->repo
->getHashPath( $originalName );
39 $dstRel = "$hashPath{$this->date}!$originalName";
40 $dstUrlRel = $hashPath . $this->date
. '!' . rawurlencode( $originalName );
42 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
43 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
44 $this->createdFiles
[] = $result->value
;
49 * Test storing a file using different flags.
51 * @param $fn string The title of the image
52 * @param $infn string The name of the file (in the filesystem)
53 * @param $otherfn string The name of the different file (in the filesystem)
54 * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo.
56 private function storecohort($fn, $infn, $otherfn, $fromrepo) {
57 $f = $this->storeit( $fn, $infn, 0 );
58 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
59 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
60 $this->assertEquals( $f->successCount
, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
62 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
65 // This should work because we're allowed to overwrite
66 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE
);
67 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
68 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
69 $this->assertEquals( $f->successCount
, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
70 // This should fail because we're overwriting.
71 $f = $this->storeit( $fn, $infn, 0 );
72 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
73 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
74 $this->assertEquals( $f->successCount
, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
75 // This should succeed because we're overwriting the same content.
76 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE_SAME
);
77 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
78 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
79 $this->assertEquals( $f->successCount
, 1 , "counts wrong {$f->successCount} {$f->failCount}" );
80 // This should fail because we're overwriting different content.
82 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
85 $f = $this->storeit( $fn, $otherfn, FileRepo
::OVERWRITE_SAME
);
86 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
87 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
88 $this->assertEquals( $f->successCount
, 0 , "counts wrong {$f->successCount} {$f->failCount}" );
91 public function teststore() {
93 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
94 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );
97 public function tearDown() {
98 $this->repo
->cleanupBatch( $this->createdFiles
);
99 foreach ( $this->createdFiles
as $tmp ) {
100 $tmp = $this->repo
->resolveVirtualUrl( $tmp );
101 while ( $tmp = FileBackend
::parentStoragePath( $tmp ) ) {
102 $this->repo
->getBackend()->clean( array( 'dir' => $tmp ) );