2011-10-11 19:49:29 +00:00
|
|
|
<?php
|
2013-10-21 08:46:11 +00:00
|
|
|
|
2011-10-11 19:49:29 +00:00
|
|
|
/**
|
2012-01-12 18:44:00 +00:00
|
|
|
* @group FileRepo
|
2012-08-29 15:01:02 +00:00
|
|
|
* @group medium
|
2011-10-11 19:49:29 +00:00
|
|
|
*/
|
|
|
|
|
class StoreBatchTest extends MediaWikiTestCase {
|
|
|
|
|
|
2013-10-21 08:46:11 +00:00
|
|
|
protected $createdFiles;
|
|
|
|
|
protected $date;
|
|
|
|
|
/** @var FileRepo */
|
|
|
|
|
protected $repo;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2012-01-22 00:34:04 +00:00
|
|
|
global $wgFileBackends;
|
2011-10-11 19:49:29 +00:00
|
|
|
parent::setUp();
|
2012-01-10 14:26:17 +00:00
|
|
|
|
|
|
|
|
# Forge a FSRepo object to not have to rely on local wiki settings
|
2012-01-27 22:46:55 +00:00
|
|
|
$tmpPrefix = wfTempDir() . '/storebatch-test-' . time() . '-' . mt_rand();
|
2014-05-05 16:10:07 +00:00
|
|
|
if ( $this->getCliArg( 'use-filebackend' ) ) {
|
|
|
|
|
$name = $this->getCliArg( 'use-filebackend' );
|
2012-01-22 00:34:04 +00:00
|
|
|
$useConfig = array();
|
|
|
|
|
foreach ( $wgFileBackends as $conf ) {
|
|
|
|
|
if ( $conf['name'] == $name ) {
|
|
|
|
|
$useConfig = $conf;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-03 20:55:44 +00:00
|
|
|
$useConfig['lockManager'] = LockManagerGroup::singleton()->get( $useConfig['lockManager'] );
|
|
|
|
|
unset( $useConfig['fileJournal'] );
|
2012-01-28 00:43:55 +00:00
|
|
|
$useConfig['name'] = 'local-testing'; // swap name
|
|
|
|
|
$class = $useConfig['class'];
|
|
|
|
|
$backend = new $class( $useConfig );
|
2012-01-22 00:34:04 +00:00
|
|
|
} else {
|
|
|
|
|
$backend = new FSFileBackend( array(
|
2013-02-15 10:17:52 +00:00
|
|
|
'name' => 'local-testing',
|
2013-12-03 20:55:44 +00:00
|
|
|
'wikiId' => wfWikiID(),
|
2012-01-07 06:13:01 +00:00
|
|
|
'containerPaths' => array(
|
2013-02-15 10:17:52 +00:00
|
|
|
'unittests-public' => "{$tmpPrefix}-public",
|
|
|
|
|
'unittests-thumb' => "{$tmpPrefix}-thumb",
|
|
|
|
|
'unittests-temp' => "{$tmpPrefix}-temp",
|
2012-01-27 22:46:55 +00:00
|
|
|
'unittests-deleted' => "{$tmpPrefix}-deleted",
|
2012-01-07 06:13:01 +00:00
|
|
|
)
|
2012-01-22 00:34:04 +00:00
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
$this->repo = new FileRepo( array(
|
2013-02-15 10:17:52 +00:00
|
|
|
'name' => 'unittests',
|
2012-01-22 00:34:04 +00:00
|
|
|
'backend' => $backend
|
2012-01-07 06:13:01 +00:00
|
|
|
) );
|
2012-01-10 14:26:17 +00:00
|
|
|
|
2011-10-11 19:49:29 +00:00
|
|
|
$this->date = gmdate( "YmdHis" );
|
2011-12-16 19:59:58 +00:00
|
|
|
$this->createdFiles = array();
|
2011-10-11 19:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function tearDown() {
|
|
|
|
|
$this->repo->cleanupBatch( $this->createdFiles ); // delete files
|
|
|
|
|
foreach ( $this->createdFiles as $tmp ) { // delete dirs
|
|
|
|
|
$tmp = $this->repo->resolveVirtualUrl( $tmp );
|
|
|
|
|
while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
|
|
|
|
|
$this->repo->getBackend()->clean( array( 'dir' => $tmp ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 19:49:29 +00:00
|
|
|
/**
|
|
|
|
|
* Store a file or virtual URL source into a media file name.
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $originalName The title of the image
|
|
|
|
|
* @param string $srcPath The filepath or virtual URL
|
|
|
|
|
* @param int $flags Flags to pass into repo::store().
|
2013-10-21 08:46:11 +00:00
|
|
|
* @return FileRepoStatus
|
2011-10-11 19:49:29 +00:00
|
|
|
*/
|
2013-02-15 10:17:52 +00:00
|
|
|
private function storeit( $originalName, $srcPath, $flags ) {
|
2011-10-11 19:49:29 +00:00
|
|
|
$hashPath = $this->repo->getHashPath( $originalName );
|
|
|
|
|
$dstRel = "$hashPath{$this->date}!$originalName";
|
|
|
|
|
$dstUrlRel = $hashPath . $this->date . '!' . rawurlencode( $originalName );
|
|
|
|
|
|
|
|
|
|
$result = $this->repo->store( $srcPath, 'temp', $dstRel, $flags );
|
|
|
|
|
$result->value = $this->repo->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
|
2011-12-16 19:59:58 +00:00
|
|
|
$this->createdFiles[] = $result->value;
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-10-11 19:49:29 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
2012-01-07 06:13:01 +00:00
|
|
|
|
2011-10-11 19:49:29 +00:00
|
|
|
/**
|
|
|
|
|
* Test storing a file using different flags.
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $fn The title of the image
|
|
|
|
|
* @param string $infn The name of the file (in the filesystem)
|
|
|
|
|
* @param string $otherfn The name of the different file (in the filesystem)
|
|
|
|
|
* @param bool $fromrepo 'true' if we want to copy from a virtual URL out of the Repo.
|
2011-10-11 19:49:29 +00:00
|
|
|
*/
|
2013-02-15 10:17:52 +00:00
|
|
|
private function storecohort( $fn, $infn, $otherfn, $fromrepo ) {
|
2011-10-11 19:49:29 +00:00
|
|
|
$f = $this->storeit( $fn, $infn, 0 );
|
|
|
|
|
$this->assertTrue( $f->isOK(), 'failed to store a new file' );
|
2011-10-16 22:18:18 +00:00
|
|
|
$this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
|
2011-10-11 19:49:29 +00:00
|
|
|
if ( $fromrepo ) {
|
2013-02-15 10:17:52 +00:00
|
|
|
$f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE );
|
2011-10-11 19:49:29 +00:00
|
|
|
$infn = $f->value;
|
|
|
|
|
}
|
|
|
|
|
// This should work because we're allowed to overwrite
|
|
|
|
|
$f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE );
|
|
|
|
|
$this->assertTrue( $f->isOK(), 'We should be allowed to overwrite' );
|
2011-10-16 22:18:18 +00:00
|
|
|
$this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
|
2011-10-11 19:49:29 +00:00
|
|
|
// This should fail because we're overwriting.
|
|
|
|
|
$f = $this->storeit( $fn, $infn, 0 );
|
|
|
|
|
$this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' );
|
2011-10-16 22:18:18 +00:00
|
|
|
$this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
|
2011-10-11 19:49:29 +00:00
|
|
|
// This should succeed because we're overwriting the same content.
|
|
|
|
|
$f = $this->storeit( $fn, $infn, FileRepo::OVERWRITE_SAME );
|
|
|
|
|
$this->assertTrue( $f->isOK(), 'We should be able to overwrite the same content' );
|
2011-10-16 22:18:18 +00:00
|
|
|
$this->assertEquals( $f->failCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertEquals( $f->successCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
|
2011-10-11 19:49:29 +00:00
|
|
|
// This should fail because we're overwriting different content.
|
|
|
|
|
if ( $fromrepo ) {
|
2013-02-15 10:17:52 +00:00
|
|
|
$f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE );
|
2011-10-11 19:49:29 +00:00
|
|
|
$otherfn = $f->value;
|
|
|
|
|
}
|
|
|
|
|
$f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME );
|
|
|
|
|
$this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' );
|
2011-10-16 22:18:18 +00:00
|
|
|
$this->assertEquals( $f->failCount, 1, "counts wrong {$f->successCount} {$f->failCount}" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertEquals( $f->successCount, 0, "counts wrong {$f->successCount} {$f->failCount}" );
|
2011-10-11 19:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-21 08:46:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers FileRepo::store
|
|
|
|
|
*/
|
2011-10-11 19:49:29 +00:00
|
|
|
public function teststore() {
|
|
|
|
|
global $IP;
|
2014-04-24 16:06:46 +00:00
|
|
|
$this->storecohort(
|
|
|
|
|
"Test1.png",
|
2014-05-26 09:55:07 +00:00
|
|
|
"$IP/tests/phpunit/data/filerepo/wiki.png",
|
|
|
|
|
"$IP/tests/phpunit/data/filerepo/video.png",
|
2014-04-24 16:06:46 +00:00
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
$this->storecohort(
|
|
|
|
|
"Test2.png",
|
2014-05-26 09:55:07 +00:00
|
|
|
"$IP/tests/phpunit/data/filerepo/wiki.png",
|
|
|
|
|
"$IP/tests/phpunit/data/filerepo/video.png",
|
2014-04-24 16:06:46 +00:00
|
|
|
true
|
|
|
|
|
);
|
2011-10-11 19:49:29 +00:00
|
|
|
}
|
|
|
|
|
}
|