summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/filerepo
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpunit/includes/filerepo')
-rw-r--r--tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php138
-rw-r--r--tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php114
2 files changed, 252 insertions, 0 deletions
diff --git a/tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php b/tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php
new file mode 100644
index 00000000..681e3681
--- /dev/null
+++ b/tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php
@@ -0,0 +1,138 @@
+<?php
+
+class FileBackendDBRepoWrapperTest extends MediaWikiTestCase {
+ protected $backendName = 'foo-backend';
+ protected $repoName = 'pureTestRepo';
+
+ /**
+ * @dataProvider getBackendPathsProvider
+ * @covers FileBackendDBRepoWrapper::getBackendPaths
+ */
+ public function testGetBackendPaths(
+ $mocks,
+ $latest,
+ $dbReadsExpected,
+ $dbReturnValue,
+ $originalPath,
+ $expectedBackendPath,
+ $message ) {
+ list( $dbMock, $backendMock, $wrapperMock ) = $mocks;
+
+ $dbMock->expects( $dbReadsExpected )
+ ->method( 'selectField' )
+ ->will( $this->returnValue( $dbReturnValue ) );
+
+ $newPaths = $wrapperMock->getBackendPaths( array( $originalPath ), $latest );
+
+ $this->assertEquals(
+ $expectedBackendPath,
+ $newPaths[0],
+ $message );
+ }
+
+ public function getBackendPathsProvider() {
+ $prefix = 'mwstore://' . $this->backendName . '/' . $this->repoName;
+ $mocksForCaching = $this->getMocks();
+
+ return array(
+ array(
+ $mocksForCaching,
+ false,
+ $this->once(),
+ '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ $prefix . '-public/f/o/foobar.jpg',
+ $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ 'Public path translated correctly',
+ ),
+ array(
+ $mocksForCaching,
+ false,
+ $this->never(),
+ '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ $prefix . '-public/f/o/foobar.jpg',
+ $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ 'LRU cache leveraged',
+ ),
+ array(
+ $this->getMocks(),
+ true,
+ $this->once(),
+ '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ $prefix . '-public/f/o/foobar.jpg',
+ $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ 'Latest obtained',
+ ),
+ array(
+ $this->getMocks(),
+ true,
+ $this->never(),
+ '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
+ $prefix . '-deleted/f/o/foobar.jpg',
+ $prefix . '-original/f/o/o/foobar',
+ 'Deleted path translated correctly',
+ ),
+ array(
+ $this->getMocks(),
+ true,
+ $this->once(),
+ null,
+ $prefix . '-public/b/a/baz.jpg',
+ $prefix . '-public/b/a/baz.jpg',
+ 'Path left untouched if no sha1 can be found',
+ ),
+ );
+ }
+
+ /**
+ * @covers FileBackendDBRepoWrapper::getFileContentsMulti
+ */
+ public function testGetFileContentsMulti() {
+ list( $dbMock, $backendMock, $wrapperMock ) = $this->getMocks();
+
+ $sha1Path = 'mwstore://' . $this->backendName . '/' . $this->repoName
+ . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9';
+ $filenamePath = 'mwstore://' . $this->backendName . '/' . $this->repoName
+ . '-public/f/o/foobar.jpg';
+
+ $dbMock->expects( $this->once() )
+ ->method( 'selectField' )
+ ->will( $this->returnValue( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' ) );
+
+ $backendMock->expects( $this->once() )
+ ->method( 'getFileContentsMulti')
+ ->will( $this->returnValue( array( $sha1Path => 'foo' ) ) );
+
+ $result = $wrapperMock->getFileContentsMulti( array( 'srcs' => array( $filenamePath ) ) );
+
+ $this->assertEquals(
+ array( $filenamePath => 'foo' ),
+ $result,
+ 'File contents paths translated properly'
+ );
+ }
+
+ protected function getMocks() {
+ $dbMock = $this->getMockBuilder( 'DatabaseMysql' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $backendMock = $this->getMock( 'FSFileBackend',
+ array(),
+ array( array(
+ 'name' => $this->backendName,
+ 'wikiId' => wfWikiId()
+ ) ) );
+
+ $wrapperMock = $this->getMock( 'FileBackendDBRepoWrapper',
+ array( 'getDB' ),
+ array( array(
+ 'backend' => $backendMock,
+ 'repoName' => $this->repoName,
+ 'dbHandleFactory' => null
+ ) ) );
+
+ $wrapperMock->expects( $this->any() )->method( 'getDB' )->will( $this->returnValue( $dbMock ) );
+
+ return array( $dbMock, $backendMock, $wrapperMock );
+ }
+}
diff --git a/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
new file mode 100644
index 00000000..551d3a76
--- /dev/null
+++ b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
@@ -0,0 +1,114 @@
+<?php
+
+class MigrateFileRepoLayoutTest extends MediaWikiTestCase {
+ protected $tmpPrefix;
+ protected $migratorMock;
+ protected $tmpFilepath;
+ protected $text = 'testing';
+
+ protected function setUp() {
+ parent::setUp();
+
+ $filename = 'Foo.png';
+
+ $this->tmpPrefix = wfTempDir() . '/migratefilelayout-test-' . time() . '-' . mt_rand();
+
+ $backend = new FSFileBackend( array(
+ 'name' => 'local-migratefilerepolayouttest',
+ 'wikiId' => wfWikiID(),
+ 'containerPaths' => array(
+ 'migratefilerepolayouttest-original' => "{$this->tmpPrefix}-original",
+ 'migratefilerepolayouttest-public' => "{$this->tmpPrefix}-public",
+ 'migratefilerepolayouttest-thumb' => "{$this->tmpPrefix}-thumb",
+ 'migratefilerepolayouttest-temp' => "{$this->tmpPrefix}-temp",
+ 'migratefilerepolayouttest-deleted' => "{$this->tmpPrefix}-deleted",
+ )
+ ) );
+
+ $dbMock = $this->getMockBuilder( 'DatabaseMysql' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $imageRow = new stdClass;
+ $imageRow->img_name = $filename;
+ $imageRow->img_sha1 = sha1( $this->text );
+
+ $dbMock->expects( $this->any() )
+ ->method( 'select' )
+ ->will( $this->onConsecutiveCalls(
+ new FakeResultWrapper( array( $imageRow ) ), // image
+ new FakeResultWrapper( array() ), // image
+ new FakeResultWrapper( array() ) // filearchive
+ ) );
+
+ $repoMock = $this->getMock( 'LocalRepo',
+ array( 'getMasterDB' ),
+ array( array(
+ 'name' => 'migratefilerepolayouttest',
+ 'backend' => $backend
+ ) ) );
+
+ $repoMock->expects( $this->any() )->method( 'getMasterDB' )->will( $this->returnValue( $dbMock ) );
+
+ $this->migratorMock = $this->getMock( 'MigrateFileRepoLayout', array( 'getRepo' ) );
+ $this->migratorMock->expects( $this->any() )->method( 'getRepo' )->will( $this->returnValue( $repoMock ) );
+
+ $this->tmpFilepath = TempFSFile::factory( 'migratefilelayout-test-', 'png' )->getPath();
+
+ file_put_contents( $this->tmpFilepath, $this->text );
+
+ $hashPath = $repoMock->getHashPath( $filename );
+
+ $status = $repoMock->store( $this->tmpFilepath, 'public', $hashPath . $filename, FileRepo::OVERWRITE );
+ }
+
+ protected function deleteFilesRecursively( $directory ) {
+ foreach ( glob( $directory . '/*' ) as $file ) {
+ if ( is_dir( $file ) ) {
+ $this->deleteFilesRecursively( $file );
+ } else {
+ unlink( $file );
+ }
+ }
+
+ rmdir( $directory );
+ }
+
+ protected function tearDown() {
+ foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
+ $this->deleteFilesRecursively( $directory );
+ }
+
+ unlink( $this->tmpFilepath );
+
+ parent::tearDown();
+ }
+
+ public function testMigration() {
+ $this->migratorMock->loadParamsAndArgs( null, array( 'oldlayout' => 'name', 'newlayout' => 'sha1' ) );
+
+ ob_start();
+
+ $this->migratorMock->execute();
+
+ ob_end_clean();
+
+ $sha1 = sha1( $this->text );
+
+ $expectedOriginalFilepath = $this->tmpPrefix
+ . '-original/'
+ . substr( $sha1, 0, 1 )
+ . '/'
+ . substr( $sha1, 1, 1 )
+ . '/'
+ . substr( $sha1, 2, 1 )
+ . '/'
+ . $sha1 ;
+
+ $this->assertEquals( file_get_contents( $expectedOriginalFilepath ), $this->text, 'New sha1 file should be exist and have the right contents' );
+
+ $expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
+
+ $this->assertEquals( file_get_contents( $expectedPublicFilepath ), $this->text, 'Existing name file should still and have the right contents' );
+ }
+}