Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / filerepo / FileBackendDBRepoWrapperTest.php
blob7a77ac294e28b046d212bfa250bcca95ad87412d
1 <?php
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';
12 /**
13 * @dataProvider getBackendPathsProvider
14 * @covers \FileBackendDBRepoWrapper::getBackendPaths
16 public function testGetBackendPaths(
17 $mocks,
18 $latest,
19 $dbReadsExpected,
20 $dbReturnValue,
21 $originalPath,
22 $expectedBackendPath,
23 $message ) {
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 );
33 $this->assertEquals(
34 $expectedBackendPath,
35 $newPaths[0],
36 $message );
39 public function getBackendPathsProvider() {
40 $prefix = 'mwstore://' . self::BACKEND_NAME . '/' . self::REPO_NAME;
41 $mocksForCaching = $this->getMocks();
43 return [
45 $mocksForCaching,
46 false,
47 $this->once(),
48 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
49 $prefix . '-public/f/o/foobar.jpg',
50 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
51 'Public path translated correctly',
54 $mocksForCaching,
55 false,
56 $this->never(),
57 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
58 $prefix . '-public/f/o/foobar.jpg',
59 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
60 'LRU cache leveraged',
63 $this->getMocks(),
64 true,
65 $this->once(),
66 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
67 $prefix . '-public/f/o/foobar.jpg',
68 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
69 'Latest obtained',
72 $this->getMocks(),
73 true,
74 $this->never(),
75 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
76 $prefix . '-deleted/f/o/foobar.jpg',
77 $prefix . '-original/f/o/o/foobar',
78 'Deleted path translated correctly',
81 $this->getMocks(),
82 true,
83 $this->once(),
84 null,
85 $prefix . '-public/b/a/baz.jpg',
86 $prefix . '-public/b/a/baz.jpg',
87 'Path left untouched if no sha1 can be found',
92 /**
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 ] ] );
114 $this->assertEquals(
115 [ $filenamePath => 'foo' ],
116 $result,
117 'File contents paths translated properly'
121 protected function getMocks() {
122 $dbMock = $this->getMockBuilder( IDatabase::class )
123 ->disableOriginalClone()
124 ->disableOriginalConstructor()
125 ->getMock();
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()
132 ] ] )
133 ->getMock();
135 $wrapperMock = $this->getMockBuilder( FileBackendDBRepoWrapper::class )
136 ->onlyMethods( [ 'getDB' ] )
137 ->setConstructorArgs( [ [
138 'backend' => $backendMock,
139 'repoName' => self::REPO_NAME,
140 'dbHandleFactory' => null
141 ] ] )
142 ->getMock();
144 $wrapperMock->method( 'getDB' )->willReturn( $dbMock );
146 return [ $dbMock, $backendMock, $wrapperMock ];