Merge "API: Correct 'from_namespace' logic in ApiQueryBacklinksprop"
[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 = wfTempDir() . '/migratefilelayout-test-' . time() . '-' . mt_rand();
15
16 $backend = new FSFileBackend( array(
17 'name' => 'local-migratefilerepolayouttest',
18 'wikiId' => wfWikiID(),
19 'containerPaths' => array(
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( array( $imageRow ) ), // image
40 new FakeResultWrapper( array() ), // image
41 new FakeResultWrapper( array() ) // filearchive
42 ) );
43
44 $repoMock = $this->getMock( 'LocalRepo',
45 array( 'getMasterDB' ),
46 array( array(
47 'name' => 'migratefilerepolayouttest',
48 'backend' => $backend
49 ) ) );
50
51 $repoMock->expects( $this->any() )->method( 'getMasterDB' )->will( $this->returnValue( $dbMock ) );
52
53 $this->migratorMock = $this->getMock( 'MigrateFileRepoLayout', array( 'getRepo' ) );
54 $this->migratorMock->expects( $this->any() )->method( 'getRepo' )->will( $this->returnValue( $repoMock ) );
55
56 $this->tmpFilepath = TempFSFile::factory( 'migratefilelayout-test-', 'png' )->getPath();
57
58 file_put_contents( $this->tmpFilepath, $this->text );
59
60 $hashPath = $repoMock->getHashPath( $filename );
61
62 $status = $repoMock->store( $this->tmpFilepath, 'public', $hashPath . $filename, FileRepo::OVERWRITE );
63 }
64
65 protected function deleteFilesRecursively( $directory ) {
66 foreach ( glob( $directory . '/*' ) as $file ) {
67 if ( is_dir( $file ) ) {
68 $this->deleteFilesRecursively( $file );
69 } else {
70 unlink( $file );
71 }
72 }
73
74 rmdir( $directory );
75 }
76
77 protected function tearDown() {
78 foreach ( glob( $this->tmpPrefix . '*' ) as $directory ) {
79 $this->deleteFilesRecursively( $directory );
80 }
81
82 unlink( $this->tmpFilepath );
83
84 parent::tearDown();
85 }
86
87 public function testMigration() {
88 $this->migratorMock->loadParamsAndArgs( null, array( 'oldlayout' => 'name', 'newlayout' => 'sha1' ) );
89
90 ob_start();
91
92 $this->migratorMock->execute();
93
94 ob_end_clean();
95
96 $sha1 = sha1( $this->text );
97
98 $expectedOriginalFilepath = $this->tmpPrefix
99 . '-original/'
100 . substr( $sha1, 0, 1 )
101 . '/'
102 . substr( $sha1, 1, 1 )
103 . '/'
104 . substr( $sha1, 2, 1 )
105 . '/'
106 . $sha1;
107
108 $this->assertEquals( file_get_contents( $expectedOriginalFilepath ), $this->text, 'New sha1 file should be exist and have the right contents' );
109
110 $expectedPublicFilepath = $this->tmpPrefix . '-public/f/f8/Foo.png';
111
112 $this->assertEquals( file_get_contents( $expectedPublicFilepath ), $this->text, 'Existing name file should still and have the right contents' );
113 }
114 }