3 use MediaWiki\WikiMap\WikiMap
;
4 use Wikimedia\FileBackend\FSFileBackend
;
5 use Wikimedia\Rdbms\IDatabase
;
6 use Wikimedia\Rdbms\SelectQueryBuilder
;
8 class FileBackendDBRepoWrapperTest
extends MediaWikiIntegrationTestCase
{
9 private const BACKEND_NAME
= 'foo-backend';
10 private const REPO_NAME
= 'pureTestRepo';
13 * @dataProvider getBackendPathsProvider
14 * @covers \FileBackendDBRepoWrapper::getBackendPaths
16 public function testGetBackendPaths(
24 [ $dbMock, $backendMock, $wrapperMock ] = $mocks;
26 $dbMock->expects( $dbReadsExpected )
27 ->method( 'selectField' )
28 ->willReturn( $dbReturnValue );
29 $dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $dbMock ) );
31 $newPaths = $wrapperMock->getBackendPaths( [ $originalPath ], $latest );
39 public function getBackendPathsProvider() {
40 $prefix = 'mwstore://' . self
::BACKEND_NAME
. '/' . self
::REPO_NAME
;
41 $mocksForCaching = $this->getMocks();
48 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
49 $prefix . '-public/f/o/foobar.jpg',
50 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
51 'Public path translated correctly',
57 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
58 $prefix . '-public/f/o/foobar.jpg',
59 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
60 'LRU cache leveraged',
66 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
67 $prefix . '-public/f/o/foobar.jpg',
68 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
75 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
76 $prefix . '-deleted/f/o/foobar.jpg',
77 $prefix . '-original/f/o/o/foobar',
78 'Deleted path translated correctly',
85 $prefix . '-public/b/a/baz.jpg',
86 $prefix . '-public/b/a/baz.jpg',
87 'Path left untouched if no sha1 can be found',
93 * @covers \FileBackendDBRepoWrapper::getFileContentsMulti
95 public function testGetFileContentsMulti() {
96 [ $dbMock, $backendMock, $wrapperMock ] = $this->getMocks();
98 $sha1Path = 'mwstore://' . self
::BACKEND_NAME
. '/' . self
::REPO_NAME
99 . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9';
100 $filenamePath = 'mwstore://' . self
::BACKEND_NAME
. '/' . self
::REPO_NAME
101 . '-public/f/o/foobar.jpg';
103 $dbMock->expects( $this->once() )
104 ->method( 'selectField' )
105 ->willReturn( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' );
106 $dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $dbMock ) );
108 $backendMock->expects( $this->once() )
109 ->method( 'getFileContentsMulti' )
110 ->willReturn( [ $sha1Path => 'foo' ] );
112 $result = $wrapperMock->getFileContentsMulti( [ 'srcs' => [ $filenamePath ] ] );
115 [ $filenamePath => 'foo' ],
117 'File contents paths translated properly'
121 protected function getMocks() {
122 $dbMock = $this->getMockBuilder( IDatabase
::class )
123 ->disableOriginalClone()
124 ->disableOriginalConstructor()
126 $dbMock->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $dbMock ) );
128 $backendMock = $this->getMockBuilder( FSFileBackend
::class )
129 ->setConstructorArgs( [ [
130 'name' => self
::BACKEND_NAME
,
131 'wikiId' => WikiMap
::getCurrentWikiId()
135 $wrapperMock = $this->getMockBuilder( FileBackendDBRepoWrapper
::class )
136 ->onlyMethods( [ 'getDB' ] )
137 ->setConstructorArgs( [ [
138 'backend' => $backendMock,
139 'repoName' => self
::REPO_NAME
,
140 'dbHandleFactory' => null
144 $wrapperMock->method( 'getDB' )->willReturn( $dbMock );
146 return [ $dbMock, $backendMock, $wrapperMock ];