Debian Codegraph @

tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php

raw symbols SCIP
1 <?php
2
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;
10
11 /**
12 * @covers \MigrateFileRepoLayout
13 */
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';
22
23 protected function setUp(): void {
24 parent::setUp();
25
26 $filename = 'Foo.png';
27
28 $this->tmpPrefix = $this->getNewTempDirectory();
29
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",
39 ]
40 ] );
41
42 $dbMock = $this->createMock( IDatabase::class );
43
44 $imageRow = (object)[
45 'img_name' => $filename,
46 'img_sha1' => sha1( self::TEXT ),
47 ];
48
49 $dbMock->method( 'select' )
50 ->willReturnOnConsecutiveCalls(
51 new FakeResultWrapper( [ $imageRow ] ), // image
52 new FakeResultWrapper( [] ), // image
53 new FakeResultWrapper( [] ) // filearchive
54 );
55 $dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( fn () => new SelectQueryBuilder( $dbMock ) );
56
57 $repoMock = $this->getMockBuilder( LocalRepo::class )
58 ->onlyMethods( [ 'getPrimaryDB', 'getReplicaDB' ] )
59 ->setConstructorArgs( [ [
60 'name' => 'migratefilerepolayouttest',
61 'backend' => $backend
62 ] ] )
63 ->getMock();
64
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 );
71
72 $this->migratorMock = $this->getMockBuilder( MigrateFileRepoLayout::class )
73 ->onlyMethods( [ 'getRepo' ] )->getMock();
74 $this->migratorMock
75 ->method( 'getRepo' )
76 ->willReturn( $repoMock );
77
78 $this->tmpFilepath = TempFSFile::factory(
79 'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
80
81 file_put_contents( $this->tmpFilepath, self::TEXT );
82
83 $hashPath = $repoMock->getHashPath( $filename );
84
85 $status = $repoMock->store(
86 $this->tmpFilepath,
87 'public',
88 $hashPath . $filename,
89 FileRepo::OVERWRITE
90 );
91 }
92
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 );
99 }
100 }
101
102 rmdir( $directory );
103 }
104
105 protected function tearDown(): void {
106 foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
107 $this->deleteFilesRecursively( $directory );
108 }
109
110 unlink( $this->tmpFilepath );
111
112 parent::tearDown();
113 }
114
115 public function testMigration() {
116 $this->migratorMock->loadParamsAndArgs(
117 null,
118 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
119 );
120
121 ob_start();
122
123 $this->migratorMock->execute();
124
125 ob_end_clean();
126
127 $sha1 = sha1( self::TEXT );
128
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;
138
139 $this->assertEquals(
140 self::TEXT,
141 file_get_contents( $expectedOriginalFilepath ),
142 'New sha1 file should be exist and have the right contents'
143 );
144
145 $expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
146
147 $this->assertEquals(
148 self::TEXT,
149 file_get_contents( $expectedPublicFilepath ),
150 'Existing name file should still and have the right contents'
151 );
152 }
153 }