3 class MigrateFileRepoLayoutTest
extends MediaWikiTestCase
{
5 protected $migratorMock;
6 protected $tmpFilepath;
7 protected $text = 'testing';
9 protected function setUp() {
12 $filename = 'Foo.png';
14 $this->tmpPrefix
= $this->getNewTempDirectory();
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",
28 $dbMock = $this->getMockBuilder( 'DatabaseMysql' )
29 ->disableOriginalConstructor()
32 $imageRow = new stdClass
;
33 $imageRow->img_name
= $filename;
34 $imageRow->img_sha1
= sha1( $this->text
);
36 $dbMock->expects( $this->any() )
38 ->will( $this->onConsecutiveCalls(
39 new FakeResultWrapper( array( $imageRow ) ), // image
40 new FakeResultWrapper( array() ), // image
41 new FakeResultWrapper( array() ) // filearchive
44 $repoMock = $this->getMock( 'LocalRepo',
45 array( 'getMasterDB' ),
47 'name' => 'migratefilerepolayouttest',
52 ->expects( $this->any() )
53 ->method( 'getMasterDB' )
54 ->will( $this->returnValue( $dbMock ) );
56 $this->migratorMock
= $this->getMock( 'MigrateFileRepoLayout', array( 'getRepo' ) );
58 ->expects( $this->any() )
60 ->will( $this->returnValue( $repoMock ) );
62 $this->tmpFilepath
= TempFSFile
::factory( 'migratefilelayout-test-', 'png' )->getPath();
64 file_put_contents( $this->tmpFilepath
, $this->text
);
66 $hashPath = $repoMock->getHashPath( $filename );
68 $status = $repoMock->store(
71 $hashPath . $filename,
76 protected function deleteFilesRecursively( $directory ) {
77 foreach ( glob( $directory . '/*' ) as $file ) {
78 if ( is_dir( $file ) ) {
79 $this->deleteFilesRecursively( $file );
88 protected function tearDown() {
89 foreach ( glob( $this->tmpPrefix
. '*' ) as $directory ) {
90 $this->deleteFilesRecursively( $directory );
93 unlink( $this->tmpFilepath
);
98 public function testMigration() {
99 $this->migratorMock
->loadParamsAndArgs(
101 array( 'oldlayout' => 'name', 'newlayout' => 'sha1' )
106 $this->migratorMock
->execute();
110 $sha1 = sha1( $this->text
);
112 $expectedOriginalFilepath = $this->tmpPrefix
114 . substr( $sha1, 0, 1 )
116 . substr( $sha1, 1, 1 )
118 . substr( $sha1, 2, 1 )
123 file_get_contents( $expectedOriginalFilepath ),
125 'New sha1 file should be exist and have the right contents'
128 $expectedPublicFilepath = $this->tmpPrefix
. '-public/f/f8/Foo.png';
131 file_get_contents( $expectedPublicFilepath ),
133 'Existing name file should still and have the right contents'