Merge "Add documentation for wfClientAcceptsGzip()"
[lhc/web/wiklou.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( 'DatabaseMysql' )
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->getMock( 'LocalRepo',
45 [ 'getMasterDB' ],
46 [ [
47 'name' => 'migratefilerepolayouttest',
48 'backend' => $backend
49 ] ] );
50
51 $repoMock
52 ->expects( $this->any() )
53 ->method( 'getMasterDB' )
54 ->will( $this->returnValue( $dbMock ) );
55
56 $this->migratorMock = $this->getMock( 'MigrateFileRepoLayout', [ 'getRepo' ] );
57 $this->migratorMock
58 ->expects( $this->any() )
59 ->method( 'getRepo' )
60 ->will( $this->returnValue( $repoMock ) );
61
62 $this->tmpFilepath = TempFSFile::factory(
63 'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
64
65 file_put_contents( $this->tmpFilepath, $this->text );
66
67 $hashPath = $repoMock->getHashPath( $filename );
68
69 $status = $repoMock->store(
70 $this->tmpFilepath,
71 'public',
72 $hashPath . $filename,
73 FileRepo::OVERWRITE
74 );
75 }
76
77 protected function deleteFilesRecursively( $directory ) {
78 foreach ( glob( $directory . '/*' ) as $file ) {
79 if ( is_dir( $file ) ) {
80 $this->deleteFilesRecursively( $file );
81 } else {
82 unlink( $file );
83 }
84 }
85
86 rmdir( $directory );
87 }
88
89 protected function tearDown() {
90 foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
91 $this->deleteFilesRecursively( $directory );
92 }
93
94 unlink( $this->tmpFilepath );
95
96 parent::tearDown();
97 }
98
99 public function testMigration() {
100 $this->migratorMock->loadParamsAndArgs(
101 null,
102 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
103 );
104
105 ob_start();
106
107 $this->migratorMock->execute();
108
109 ob_end_clean();
110
111 $sha1 = sha1( $this->text );
112
113 $expectedOriginalFilepath = $this->tmpPrefix
114 . '-original/'
115 . substr( $sha1, 0, 1 )
116 . '/'
117 . substr( $sha1, 1, 1 )
118 . '/'
119 . substr( $sha1, 2, 1 )
120 . '/'
121 . $sha1;
122
123 $this->assertEquals(
124 file_get_contents( $expectedOriginalFilepath ),
125 $this->text,
126 'New sha1 file should be exist and have the right contents'
127 );
128
129 $expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
130
131 $this->assertEquals(
132 file_get_contents( $expectedPublicFilepath ),
133 $this->text,
134 'Existing name file should still and have the right contents'
135 );
136 }
137 }