7 class StoreBatchTest
extends MediaWikiTestCase
{
9 protected $createdFiles;
14 protected function setUp() {
15 global $wgFileBackends;
18 # Forge a FSRepo object to not have to rely on local wiki settings
19 $tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
20 if ( $this->getCliArg( 'use-filebackend=' ) ) {
21 $name = $this->getCliArg( 'use-filebackend=' );
23 foreach ( $wgFileBackends as $conf ) {
24 if ( $conf['name'] == $name ) {
28 $useConfig['name'] = 'local-testing'; // swap name
29 $class = $useConfig['class'];
30 $backend = new $class( $useConfig );
32 $backend = new FSFileBackend( array(
33 'name' => 'local-testing',
34 'lockManager' => 'nullLockManager',
35 'containerPaths' => array(
36 'unittests-public' => "{$tmpPrefix}-public",
37 'unittests-thumb' => "{$tmpPrefix}-thumb",
38 'unittests-temp' => "{$tmpPrefix}-temp",
39 'unittests-deleted' => "{$tmpPrefix}-deleted",
43 $this->repo
= new FileRepo( array(
44 'name' => 'unittests',
48 $this->date
= gmdate( "YmdHis" );
49 $this->createdFiles
= array();
52 protected function tearDown() {
53 $this->repo
->cleanupBatch( $this->createdFiles
); // delete files
54 foreach ( $this->createdFiles
as $tmp ) { // delete dirs
55 $tmp = $this->repo
->resolveVirtualUrl( $tmp );
56 while ( $tmp = FileBackend
::parentStoragePath( $tmp ) ) {
57 $this->repo
->getBackend()->clean( array( 'dir' => $tmp ) );
64 * Store a file or virtual URL source into a media file name.
66 * @param $originalName string The title of the image
67 * @param $srcPath string The filepath or virtual URL
68 * @param $flags integer Flags to pass into repo::store().
69 * @return FileRepoStatus
71 private function storeit( $originalName, $srcPath, $flags ) {
72 $hashPath = $this->repo
->getHashPath( $originalName );
73 $dstRel = "$hashPath{$this->date}!$originalName";
74 $dstUrlRel = $hashPath . $this->date
. '!' . rawurlencode( $originalName );
76 $result = $this->repo
->store( $srcPath, 'temp', $dstRel, $flags );
77 $result->value
= $this->repo
->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
78 $this->createdFiles
[] = $result->value
;
84 * Test storing a file using different flags.
86 * @param $fn string The title of the image
87 * @param $infn string The name of the file (in the filesystem)
88 * @param $otherfn string The name of the different file (in the filesystem)
89 * @param $fromrepo bool 'true' if we want to copy from a virtual URL out of the Repo.
91 private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
92 $f = $this->storeit( $fn, $infn, 0 );
93 $this->assertTrue( $f->isOK(), 'failed to store a new file' );
94 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
95 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
97 $f = $this->storeit( "Other-$fn", $infn, FileRepo
::OVERWRITE
);
100 // This should work because we're allowed to overwrite
101 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE
);
102 $this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
103 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
104 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
105 // This should fail because we're overwriting.
106 $f = $this->storeit( $fn, $infn, 0 );
107 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
108 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
109 $this->assertEquals( $f->successCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
110 // This should succeed because we're overwriting the same content.
111 $f = $this->storeit( $fn, $infn, FileRepo
::OVERWRITE_SAME
);
112 $this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
113 $this->assertEquals( $f->failCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
114 $this->assertEquals( $f->successCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
115 // This should fail because we're overwriting different content.
117 $f = $this->storeit( "Other-$fn", $otherfn, FileRepo
::OVERWRITE
);
118 $otherfn = $f->value
;
120 $f = $this->storeit( $fn, $otherfn, FileRepo
::OVERWRITE_SAME
);
121 $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
122 $this->assertEquals( $f->failCount
, 1, "counts wrong {$f->successCount} {$f->failCount}" );
123 $this->assertEquals( $f->successCount
, 0, "counts wrong {$f->successCount} {$f->failCount}" );
127 * @covers FileRepo::store
129 public function teststore() {
131 $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false );
132 $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true );