1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
<?php
use MediaWiki\WikiMap\WikiMap;
use PHPUnit\Framework\MockObject\MockObject;
use Wikimedia\FileBackend\FSFile\TempFSFile;
use Wikimedia\FileBackend\FSFileBackend;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\SelectQueryBuilder;
/**
* @covers \MigrateFileRepoLayout
*/
class MigrateFileRepoLayoutTest extends MediaWikiIntegrationTestCase {
/** @var string */
protected $tmpPrefix;
/** @var MigrateFileRepoLayout&MockObject */
protected $migratorMock;
/** @var string */
protected $tmpFilepath;
private const TEXT = 'testing';
protected function setUp(): void {
parent::setUp();
$filename = 'Foo.png';
$this->tmpPrefix = $this->getNewTempDirectory();
$backend = new FSFileBackend( [
'name' => 'local-migratefilerepolayouttest',
'wikiId' => WikiMap::getCurrentWikiId(),
'containerPaths' => [
'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->createMock( IDatabase::class );
$imageRow = (object)[
'img_name' => $filename,
'img_sha1' => sha1( self::TEXT ),
];
$dbMock->method( 'select' )
->willReturnOnConsecutiveCalls(
new FakeResultWrapper( [ $imageRow ] ), // image
new FakeResultWrapper( [] ), // image
new FakeResultWrapper( [] ) // filearchive
);
$dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( fn () => new SelectQueryBuilder( $dbMock ) );
$repoMock = $this->getMockBuilder( LocalRepo::class )
->onlyMethods( [ 'getPrimaryDB', 'getReplicaDB' ] )
->setConstructorArgs( [ [
'name' => 'migratefilerepolayouttest',
'backend' => $backend
] ] )
->getMock();
$repoMock
->method( 'getPrimaryDB' )
->willReturn( $dbMock );
$replicaDB = $this->createMock( IDatabase::class );
$replicaDB->method( 'getSessionLagStatus' )->willReturn( [ 'lag' => 0, 'since' => time() ] );
$repoMock->method( 'getReplicaDB' )->willReturn( $replicaDB );
$this->migratorMock = $this->getMockBuilder( MigrateFileRepoLayout::class )
->onlyMethods( [ 'getRepo' ] )->getMock();
$this->migratorMock
->method( 'getRepo' )
->willReturn( $repoMock );
$this->tmpFilepath = TempFSFile::factory(
'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
file_put_contents( $this->tmpFilepath, self::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(): void {
foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
$this->deleteFilesRecursively( $directory );
}
unlink( $this->tmpFilepath );
parent::tearDown();
}
public function testMigration() {
$this->migratorMock->loadParamsAndArgs(
null,
[ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
);
ob_start();
$this->migratorMock->execute();
ob_end_clean();
$sha1 = sha1( self::TEXT );
$expectedOriginalFilepath = $this->tmpPrefix
. '-original/'
. substr( $sha1, 0, 1 )
. '/'
. substr( $sha1, 1, 1 )
. '/'
. substr( $sha1, 2, 1 )
. '/'
. $sha1;
$this->assertEquals(
self::TEXT,
file_get_contents( $expectedOriginalFilepath ),
'New sha1 file should be exist and have the right contents'
);
$expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
$this->assertEquals(
self::TEXT,
file_get_contents( $expectedPublicFilepath ),
'Existing name file should still and have the right contents'
);
}
}
|