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( [
17 'name' => 'local-migratefilerepolayouttest',
18 'wikiId' => wfWikiID(),
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( 'DatabaseMysqli' )
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( [ $imageRow ] ), // image
40 new FakeResultWrapper( [] ), // image
41 new FakeResultWrapper( [] ) // filearchive
44 $repoMock = $this->getMockBuilder( 'LocalRepo' )
45 ->setMethods( [ 'getMasterDB' ] )
46 ->setConstructorArgs( [ [
47 'name' => 'migratefilerepolayouttest',
53 ->expects( $this->any() )
54 ->method( 'getMasterDB' )
55 ->will( $this->returnValue( $dbMock ) );
57 $this->migratorMock
= $this->getMockBuilder( 'MigrateFileRepoLayout' )
58 ->setMethods( [ 'getRepo' ] )->getMock();
60 ->expects( $this->any() )
62 ->will( $this->returnValue( $repoMock ) );
64 $this->tmpFilepath
= TempFSFile
::factory(
65 'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
67 file_put_contents( $this->tmpFilepath
, $this->text
);
69 $hashPath = $repoMock->getHashPath( $filename );
71 $status = $repoMock->store(
74 $hashPath . $filename,
79 protected function deleteFilesRecursively( $directory ) {
80 foreach ( glob( $directory . '/*' ) as $file ) {
81 if ( is_dir( $file ) ) {
82 $this->deleteFilesRecursively( $file );
91 protected function tearDown() {
92 foreach ( glob( $this->tmpPrefix
. '*' ) as $directory ) {
93 $this->deleteFilesRecursively( $directory );
96 unlink( $this->tmpFilepath
);
101 public function testMigration() {
102 $this->migratorMock
->loadParamsAndArgs(
104 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
109 $this->migratorMock
->execute();
113 $sha1 = sha1( $this->text
);
115 $expectedOriginalFilepath = $this->tmpPrefix
117 . substr( $sha1, 0, 1 )
119 . substr( $sha1, 1, 1 )
121 . substr( $sha1, 2, 1 )
126 file_get_contents( $expectedOriginalFilepath ),
128 'New sha1 file should be exist and have the right contents'
131 $expectedPublicFilepath = $this->tmpPrefix
. '-public/f/f8/Foo.png';
134 file_get_contents( $expectedPublicFilepath ),
136 'Existing name file should still and have the right contents'