4 * @covers MigrateFileRepoLayout
6 class MigrateFileRepoLayoutTest
extends MediaWikiTestCase
{
8 protected $migratorMock;
9 protected $tmpFilepath;
10 protected $text = 'testing';
12 protected function setUp() {
15 $filename = 'Foo.png';
17 $this->tmpPrefix
= $this->getNewTempDirectory();
19 $backend = new FSFileBackend( [
20 'name' => 'local-migratefilerepolayouttest',
21 'wikiId' => wfWikiID(),
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",
31 $dbMock = $this->getMockBuilder( Wikimedia\Rdbms\DatabaseMysqli
::class )
32 ->disableOriginalConstructor()
35 $imageRow = new stdClass
;
36 $imageRow->img_name
= $filename;
37 $imageRow->img_sha1
= sha1( $this->text
);
39 $dbMock->expects( $this->any() )
41 ->will( $this->onConsecutiveCalls(
42 new FakeResultWrapper( [ $imageRow ] ), // image
43 new FakeResultWrapper( [] ), // image
44 new FakeResultWrapper( [] ) // filearchive
47 $repoMock = $this->getMockBuilder( LocalRepo
::class )
48 ->setMethods( [ 'getMasterDB' ] )
49 ->setConstructorArgs( [ [
50 'name' => 'migratefilerepolayouttest',
56 ->expects( $this->any() )
57 ->method( 'getMasterDB' )
58 ->will( $this->returnValue( $dbMock ) );
60 $this->migratorMock
= $this->getMockBuilder( MigrateFileRepoLayout
::class )
61 ->setMethods( [ 'getRepo' ] )->getMock();
63 ->expects( $this->any() )
65 ->will( $this->returnValue( $repoMock ) );
67 $this->tmpFilepath
= TempFSFile
::factory(
68 'migratefilelayout-test-', 'png', wfTempDir() )->getPath();
70 file_put_contents( $this->tmpFilepath
, $this->text
);
72 $hashPath = $repoMock->getHashPath( $filename );
74 $status = $repoMock->store(
77 $hashPath . $filename,
82 protected function deleteFilesRecursively( $directory ) {
83 foreach ( glob( $directory . '/*' ) as $file ) {
84 if ( is_dir( $file ) ) {
85 $this->deleteFilesRecursively( $file );
94 protected function tearDown() {
95 foreach ( glob( $this->tmpPrefix
. '*' ) as $directory ) {
96 $this->deleteFilesRecursively( $directory );
99 unlink( $this->tmpFilepath
);
104 public function testMigration() {
105 $this->migratorMock
->loadParamsAndArgs(
107 [ 'oldlayout' => 'name', 'newlayout' => 'sha1' ]
112 $this->migratorMock
->execute();
116 $sha1 = sha1( $this->text
);
118 $expectedOriginalFilepath = $this->tmpPrefix
120 . substr( $sha1, 0, 1 )
122 . substr( $sha1, 1, 1 )
124 . substr( $sha1, 2, 1 )
129 file_get_contents( $expectedOriginalFilepath ),
131 'New sha1 file should be exist and have the right contents'
134 $expectedPublicFilepath = $this->tmpPrefix
. '-public/f/f8/Foo.png';
137 file_get_contents( $expectedPublicFilepath ),
139 'Existing name file should still and have the right contents'