3 use MediaWiki\WikiMap\WikiMap
;
4 use PHPUnit\Framework\MockObject\MockObject
;
5 use Wikimedia\FileBackend\FSFile\TempFSFile
;
6 use Wikimedia\FileBackend\FSFileBackend
;
7 use Wikimedia\Rdbms\FakeResultWrapper
;
8 use Wikimedia\Rdbms\IDatabase
;
9 use Wikimedia\Rdbms\SelectQueryBuilder
;
12 * @covers \MigrateFileRepoLayout
14 class MigrateFileRepoLayoutTest
extends MediaWikiIntegrationTestCase
{
17 /** @var MigrateFileRepoLayout&MockObject */
18 protected $migratorMock;
20 protected $tmpFilepath;
21 private const TEXT
= 'testing';
23 protected function setUp(): void
{
26 $filename = 'Foo.png';
28 $this->tmpPrefix
= $this->getNewTempDirectory();
30 $backend = new FSFileBackend( [
31 'name' => 'local-migratefilerepolayouttest',
32 'wikiId' => WikiMap
::getCurrentWikiId(),
34 'migratefilerepolayouttest-original' => "{$this->tmpPrefix}-original",
35 'migratefilerepolayouttest-public' => "{$this->tmpPrefix}-public",
36 'migratefilerepolayouttest-thumb' => "{$this->tmpPrefix}-thumb",
37 'migratefilerepolayouttest-temp' => "{$this->tmpPrefix}-temp",
38 'migratefilerepolayouttest-deleted' => "{$this->tmpPrefix}-deleted",
42 $dbMock = $this->createMock( IDatabase
::class );
45 'img_name' => $filename,
46 'img_sha1' => sha1( self
::TEXT
),
49 $dbMock->method( 'select' )
50 ->willReturnOnConsecutiveCalls(
51 new FakeResultWrapper( [ $imageRow ] ), // image
52 new FakeResultWrapper( [] ), // image
53 new FakeResultWrapper( [] ) // filearchive
55 $dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( fn () => new SelectQueryBuilder( $dbMock ) );
57 $repoMock = $this->getMockBuilder( LocalRepo
::class )
58 ->onlyMethods( [ 'getPrimaryDB', 'getReplicaDB' ] )
59 ->setConstructorArgs( [ [
60 'name' => 'migratefilerepolayouttest',
66 ->method( 'getPrimaryDB' )
67 ->willReturn( $dbMock );
68 $replicaDB = $this->createMock( IDatabase
::class );
69 $replicaDB->method( 'getSessionLagStatus' )->willReturn( [ 'lag' => 0, 'since' => time() ] );
70 $repoMock->method( 'getReplicaDB' )->willReturn( $replicaDB );
72 $this->migratorMock
= $this->getMockBuilder( MigrateFileRepoLayout
::class )
73 ->onlyMethods( [ 'getRepo' ] )->getMock();
76 ->willReturn( $repoMock );
78 $this->tmpFilepath
= TempFSFile
::factory(
79 'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
81 file_put_contents( $this->tmpFilepath
, self
::TEXT
);
83 $hashPath = $repoMock->getHashPath( $filename );
85 $status = $repoMock->store(
88 $hashPath . $filename,
93 protected function deleteFilesRecursively( $directory ) {
94 foreach ( glob( $directory . '/*' ) as $file ) {
95 if ( is_dir( $file ) ) {
96 $this->deleteFilesRecursively( $file );
105 protected function tearDown(): void
{
106 foreach ( glob( $this->tmpPrefix
. '*' ) as $directory ) {
107 $this->deleteFilesRecursively( $directory );
110 unlink( $this->tmpFilepath
);
115 public function testMigration() {
116 $this->migratorMock
->loadParamsAndArgs(
118 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
123 $this->migratorMock
->execute();
127 $sha1 = sha1( self
::TEXT
);
129 $expectedOriginalFilepath = $this->tmpPrefix
131 . substr( $sha1, 0, 1 )
133 . substr( $sha1, 1, 1 )
135 . substr( $sha1, 2, 1 )
141 file_get_contents( $expectedOriginalFilepath ),
142 'New sha1 file should be exist and have the right contents'
145 $expectedPublicFilepath = $this->tmpPrefix
. '-public/f/f8/Foo.png';
149 file_get_contents( $expectedPublicFilepath ),
150 'Existing name file should still and have the right contents'