]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / filerepo / MigrateFileRepoLayoutTest.php
1 <?php
2
3 class MigrateFileRepoLayoutTest extends MediaWikiTestCase {
4         protected $tmpPrefix;
5         protected $migratorMock;
6         protected $tmpFilepath;
7         protected $text = 'testing';
8
9         protected function setUp() {
10                 parent::setUp();
11
12                 $filename = 'Foo.png';
13
14                 $this->tmpPrefix = $this->getNewTempDirectory();
15
16                 $backend = new FSFileBackend( [
17                         'name' => 'local-migratefilerepolayouttest',
18                         'wikiId' => wfWikiID(),
19                         'containerPaths' => [
20                                 'migratefilerepolayouttest-original' => "{$this->tmpPrefix}-original",
21                                 'migratefilerepolayouttest-public' => "{$this->tmpPrefix}-public",
22                                 'migratefilerepolayouttest-thumb' => "{$this->tmpPrefix}-thumb",
23                                 'migratefilerepolayouttest-temp' => "{$this->tmpPrefix}-temp",
24                                 'migratefilerepolayouttest-deleted' => "{$this->tmpPrefix}-deleted",
25                         ]
26                 ] );
27
28                 $dbMock = $this->getMockBuilder( 'DatabaseMysqli' )
29                         ->disableOriginalConstructor()
30                         ->getMock();
31
32                 $imageRow = new stdClass;
33                 $imageRow->img_name = $filename;
34                 $imageRow->img_sha1 = sha1( $this->text );
35
36                 $dbMock->expects( $this->any() )
37                         ->method( 'select' )
38                         ->will( $this->onConsecutiveCalls(
39                                 new FakeResultWrapper( [ $imageRow ] ), // image
40                                 new FakeResultWrapper( [] ), // image
41                                 new FakeResultWrapper( [] ) // filearchive
42                         ) );
43
44                 $repoMock = $this->getMockBuilder( 'LocalRepo' )
45                         ->setMethods( [ 'getMasterDB' ] )
46                         ->setConstructorArgs( [ [
47                                         'name' => 'migratefilerepolayouttest',
48                                         'backend' => $backend
49                                 ] ] )
50                         ->getMock();
51
52                 $repoMock
53                         ->expects( $this->any() )
54                         ->method( 'getMasterDB' )
55                         ->will( $this->returnValue( $dbMock ) );
56
57                 $this->migratorMock = $this->getMockBuilder( 'MigrateFileRepoLayout' )
58                         ->setMethods( [ 'getRepo' ] )->getMock();
59                 $this->migratorMock
60                         ->expects( $this->any() )
61                         ->method( 'getRepo' )
62                         ->will( $this->returnValue( $repoMock ) );
63
64                 $this->tmpFilepath = TempFSFile::factory(
65                         'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
66
67                 file_put_contents( $this->tmpFilepath, $this->text );
68
69                 $hashPath = $repoMock->getHashPath( $filename );
70
71                 $status = $repoMock->store(
72                         $this->tmpFilepath,
73                         'public',
74                         $hashPath . $filename,
75                         FileRepo::OVERWRITE
76                 );
77         }
78
79         protected function deleteFilesRecursively( $directory ) {
80                 foreach ( glob( $directory . '/*' ) as $file ) {
81                         if ( is_dir( $file ) ) {
82                                 $this->deleteFilesRecursively( $file );
83                         } else {
84                                 unlink( $file );
85                         }
86                 }
87
88                 rmdir( $directory );
89         }
90
91         protected function tearDown() {
92                 foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
93                         $this->deleteFilesRecursively( $directory );
94                 }
95
96                 unlink( $this->tmpFilepath );
97
98                 parent::tearDown();
99         }
100
101         public function testMigration() {
102                 $this->migratorMock->loadParamsAndArgs(
103                         null,
104                         [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
105                 );
106
107                 ob_start();
108
109                 $this->migratorMock->execute();
110
111                 ob_end_clean();
112
113                 $sha1 = sha1( $this->text );
114
115                 $expectedOriginalFilepath = $this->tmpPrefix
116                         . '-original/'
117                         . substr( $sha1, 0, 1 )
118                         . '/'
119                         . substr( $sha1, 1, 1 )
120                         . '/'
121                         . substr( $sha1, 2, 1 )
122                         . '/'
123                         . $sha1;
124
125                 $this->assertEquals(
126                         file_get_contents( $expectedOriginalFilepath ),
127                         $this->text,
128                         'New sha1 file should be exist and have the right contents'
129                 );
130
131                 $expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
132
133                 $this->assertEquals(
134                         file_get_contents( $expectedPublicFilepath ),
135                         $this->text,
136                         'Existing name file should still and have the right contents'
137                 );
138         }
139 }