Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / filerepo / MigrateFileRepoLayoutTest.php
bloba4510d4d35fe11e26262e84d66745d3bbafd2690
1 <?php
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;
11 /**
12 * @covers \MigrateFileRepoLayout
14 class MigrateFileRepoLayoutTest extends MediaWikiIntegrationTestCase {
15 /** @var string */
16 protected $tmpPrefix;
17 /** @var MigrateFileRepoLayout&MockObject */
18 protected $migratorMock;
19 /** @var string */
20 protected $tmpFilepath;
21 private const TEXT = 'testing';
23 protected function setUp(): void {
24 parent::setUp();
26 $filename = 'Foo.png';
28 $this->tmpPrefix = $this->getNewTempDirectory();
30 $backend = new FSFileBackend( [
31 'name' => 'local-migratefilerepolayouttest',
32 'wikiId' => WikiMap::getCurrentWikiId(),
33 'containerPaths' => [
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",
40 ] );
42 $dbMock = $this->createMock( IDatabase::class );
44 $imageRow = (object)[
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',
61 'backend' => $backend
62 ] ] )
63 ->getMock();
65 $repoMock
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();
74 $this->migratorMock
75 ->method( 'getRepo' )
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(
86 $this->tmpFilepath,
87 'public',
88 $hashPath . $filename,
89 FileRepo::OVERWRITE
93 protected function deleteFilesRecursively( $directory ) {
94 foreach ( glob( $directory . '/*' ) as $file ) {
95 if ( is_dir( $file ) ) {
96 $this->deleteFilesRecursively( $file );
97 } else {
98 unlink( $file );
102 rmdir( $directory );
105 protected function tearDown(): void {
106 foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
107 $this->deleteFilesRecursively( $directory );
110 unlink( $this->tmpFilepath );
112 parent::tearDown();
115 public function testMigration() {
116 $this->migratorMock->loadParamsAndArgs(
117 null,
118 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
121 ob_start();
123 $this->migratorMock->execute();
125 ob_end_clean();
127 $sha1 = sha1( self::TEXT );
129 $expectedOriginalFilepath = $this->tmpPrefix
130 . '-original/'
131 . substr( $sha1, 0, 1 )
132 . '/'
133 . substr( $sha1, 1, 1 )
134 . '/'
135 . substr( $sha1, 2, 1 )
136 . '/'
137 . $sha1;
139 $this->assertEquals(
140 self::TEXT,
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';
147 $this->assertEquals(
148 self::TEXT,
149 file_get_contents( $expectedPublicFilepath ),
150 'Existing name file should still and have the right contents'