From: Russ Nelson Date: Tue, 11 Oct 2011 19:49:29 +0000 (+0000) Subject: Some docs and a test for FileRepo::storeBatch() X-Git-Tag: 1.31.0-rc.0~27140 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=b3881a28c18b228d1f9e34c98dfd1d5a91fc8e98;p=lhc%2Fweb%2Fwiklou.git Some docs and a test for FileRepo::storeBatch() --- diff --git a/includes/filerepo/README b/includes/filerepo/README index 415b8d1d94..7323af09fb 100644 --- a/includes/filerepo/README +++ b/includes/filerepo/README @@ -42,18 +42,21 @@ Tim Starling, June 2007 Structure: -File.php defines an abstract class File. - ForeignAPIFile.php extends File. - LocalFile.php extends File. - ForeignDBFile.php extends LocalFile - Image.php extends LocalFile - UnregisteredLocalFile.php extends File. -FileRepo.php defines an abstract class FileRepo. - ForeignAPIRepo.php extends FileRepo +File defines an abstract class File. + ForeignAPIFile extends File. + LocalFile extends File. + ForeignDBFile extends LocalFile + Image extends LocalFile + SwiftFile extends LocalFile + UnregisteredLocalFile extends File. + UploadStashFile extends UnregisteredLocalFile. +FileRepo defines an abstract class FileRepo. + ForeignAPIRepo extends FileRepo FSRepo extends FileRepo - LocalRepo.php extends FSRepo - ForeignDBRepo.php extends LocalRepo - ForeignDBViaLBRepo.php extends LocalRepo + LocalRepo extends FSRepo + ForeignDBRepo extends LocalRepo + ForeignDBViaLBRepo extends LocalRepo + SwiftRepo extends LocalRepo NullRepo extends FileRepo Russ Nelson, March 2011 diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 2d83074296..4fe7df3adb 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -56,6 +56,9 @@ class RepoGroup { /** * Set the singleton instance to a given object + * Used by extensions which hook into the Repo chain. + * It's not enough to just create a superclass ... you have + * to get people to call into it even though all they know is RepoGroup::singleton() * * @param $instance RepoGroup */ diff --git a/tests/phpunit/includes/filerepo/StoreBatchTest.php b/tests/phpunit/includes/filerepo/StoreBatchTest.php new file mode 100644 index 0000000000..4924a4f4b0 --- /dev/null +++ b/tests/phpunit/includes/filerepo/StoreBatchTest.php @@ -0,0 +1,99 @@ +repo = RepoGroup::singleton()->getLocalRepo(); + $this->date = gmdate( "YmdHis" ); + $this->users = array( + 'sysop' => new ApiTestUser( + 'Uploadstashtestsysop', + 'Upload Stash Test Sysop', + 'upload_stash_test_sysop@sample.com', + array( 'sysop' ) + ), + 'uploader' => new ApiTestUser( + 'Uploadstashtestuser', + 'Upload Stash Test User', + 'upload_stash_test_user@sample.com', + array() + ) + ); + } + + /** + * Store a file or virtual URL source into a media file name. + * + * @param $originalName string The title of the image + * @param $srcPath string The filepath or virtual URL + * @param $flags integer Flags to pass into repo::store(). + */ + private function storeit($originalName, $srcPath, $flags) { + $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; + return $result; + } + + + /** + * Test storing a file using different flags. + * + * @param $fn string The title of the image + * @param $infn string The name of the file (in the filesystem) + * @param $otherfn string The name of the different file (in the filesystem) + * @param $fromrepo logical 'true' if we want to copy from a virtual URL out of the Repo. + */ + private function storecohort($fn, $infn, $otherfn, $fromrepo) { + $f = $this->storeit( $fn, $infn, 0 ); + $this->assertTrue( $f->isOK(), 'failed to store a new file' ); + $this->assertTrue( $f->failCount == 0, "counts wrong {$f->successCount} {$f->failCount}" ); + $this->assertTrue( $f->successCount == 1 , "counts wrong {$f->successCount} {$f->failCount}" ); + if ( $fromrepo ) { + $f = $this->storeit( "Other-$fn", $infn, FileRepo::OVERWRITE); + $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' ); + $this->assertTrue( $f->failCount == 0, "counts wrong {$f->successCount} {$f->failCount}" ); + $this->assertTrue( $f->successCount == 1 , "counts wrong {$f->successCount} {$f->failCount}" ); + // This should fail because we're overwriting. + $f = $this->storeit( $fn, $infn, 0 ); + $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite' ); + #$this->assertTrue( $f->failCount == 0, "counts wrong {$f->successCount} {$f->failCount}" ); + #$this->assertTrue( $f->successCount == 0 , "counts wrong {$f->successCount} {$f->failCount}" ); + // 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' ); + $this->assertTrue( $f->failCount == 0, "counts wrong {$f->successCount} {$f->failCount}" ); + $this->assertTrue( $f->successCount == 1 , "counts wrong {$f->successCount} {$f->failCount}" ); + // This should fail because we're overwriting different content. + if ( $fromrepo ) { + $f = $this->storeit( "Other-$fn", $otherfn, FileRepo::OVERWRITE); + $otherfn = $f->value; + } + $f = $this->storeit( $fn, $otherfn, FileRepo::OVERWRITE_SAME ); + $this->assertFalse( $f->isOK(), 'We should not be allowed to overwrite different content' ); + #$this->assertTrue( $f->failCount == 0, "counts wrong {$f->successCount} {$f->failCount}" ); + #$this->assertTrue( $f->successCount == 0 , "counts wrong {$f->successCount} {$f->failCount}" ); + } + + public function teststore() { + global $IP; + $this->storecohort( "Test1.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", false ); + $this->storecohort( "Test2.png", "$IP/skins/monobook/wiki.png", "$IP/skins/monobook/video.png", true ); + } + + public function tearDown() { + parent::tearDown(); + + } +}