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