]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/filerepo/FileBackendDBRepoWrapperTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / filerepo / FileBackendDBRepoWrapperTest.php
1 <?php
2
3 class FileBackendDBRepoWrapperTest extends MediaWikiTestCase {
4         protected $backendName = 'foo-backend';
5         protected $repoName = 'pureTestRepo';
6
7         /**
8          * @dataProvider getBackendPathsProvider
9          * @covers FileBackendDBRepoWrapper::getBackendPaths
10          */
11         public function testGetBackendPaths(
12                 $mocks,
13                 $latest,
14                 $dbReadsExpected,
15                 $dbReturnValue,
16                 $originalPath,
17                 $expectedBackendPath,
18                 $message ) {
19                 list( $dbMock, $backendMock, $wrapperMock ) = $mocks;
20
21                 $dbMock->expects( $dbReadsExpected )
22                         ->method( 'selectField' )
23                         ->will( $this->returnValue( $dbReturnValue ) );
24
25                 $newPaths = $wrapperMock->getBackendPaths( [ $originalPath ], $latest );
26
27                 $this->assertEquals(
28                         $expectedBackendPath,
29                         $newPaths[0],
30                         $message );
31         }
32
33         public function getBackendPathsProvider() {
34                 $prefix = 'mwstore://' . $this->backendName . '/' . $this->repoName;
35                 $mocksForCaching = $this->getMocks();
36
37                 return [
38                         [
39                                 $mocksForCaching,
40                                 false,
41                                 $this->once(),
42                                 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
43                                 $prefix . '-public/f/o/foobar.jpg',
44                                 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
45                                 'Public path translated correctly',
46                         ],
47                         [
48                                 $mocksForCaching,
49                                 false,
50                                 $this->never(),
51                                 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
52                                 $prefix . '-public/f/o/foobar.jpg',
53                                 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
54                                 'LRU cache leveraged',
55                         ],
56                         [
57                                 $this->getMocks(),
58                                 true,
59                                 $this->once(),
60                                 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
61                                 $prefix . '-public/f/o/foobar.jpg',
62                                 $prefix . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9',
63                                 'Latest obtained',
64                         ],
65                         [
66                                 $this->getMocks(),
67                                 true,
68                                 $this->never(),
69                                 '96246614d75ba1703bdfd5d7660bb57407aaf5d9',
70                                 $prefix . '-deleted/f/o/foobar.jpg',
71                                 $prefix . '-original/f/o/o/foobar',
72                                 'Deleted path translated correctly',
73                         ],
74                         [
75                                 $this->getMocks(),
76                                 true,
77                                 $this->once(),
78                                 null,
79                                 $prefix . '-public/b/a/baz.jpg',
80                                 $prefix . '-public/b/a/baz.jpg',
81                                 'Path left untouched if no sha1 can be found',
82                         ],
83                 ];
84         }
85
86         /**
87          * @covers FileBackendDBRepoWrapper::getFileContentsMulti
88          */
89         public function testGetFileContentsMulti() {
90                 list( $dbMock, $backendMock, $wrapperMock ) = $this->getMocks();
91
92                 $sha1Path = 'mwstore://' . $this->backendName . '/' . $this->repoName
93                         . '-original/9/6/2/96246614d75ba1703bdfd5d7660bb57407aaf5d9';
94                 $filenamePath = 'mwstore://' . $this->backendName . '/' . $this->repoName
95                         . '-public/f/o/foobar.jpg';
96
97                 $dbMock->expects( $this->once() )
98                         ->method( 'selectField' )
99                         ->will( $this->returnValue( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' ) );
100
101                 $backendMock->expects( $this->once() )
102                         ->method( 'getFileContentsMulti' )
103                         ->will( $this->returnValue( [ $sha1Path => 'foo' ] ) );
104
105                 $result = $wrapperMock->getFileContentsMulti( [ 'srcs' => [ $filenamePath ] ] );
106
107                 $this->assertEquals(
108                         [ $filenamePath => 'foo' ],
109                         $result,
110                         'File contents paths translated properly'
111                 );
112         }
113
114         protected function getMocks() {
115                 $dbMock = $this->getMockBuilder( 'DatabaseMysqli' )
116                         ->disableOriginalClone()
117                         ->disableOriginalConstructor()
118                         ->getMock();
119
120                 $backendMock = $this->getMockBuilder( 'FSFileBackend' )
121                         ->setConstructorArgs( [ [
122                                         'name' => $this->backendName,
123                                         'wikiId' => wfWikiID()
124                                 ] ] )
125                         ->getMock();
126
127                 $wrapperMock = $this->getMockBuilder( 'FileBackendDBRepoWrapper' )
128                         ->setMethods( [ 'getDB' ] )
129                         ->setConstructorArgs( [ [
130                                         'backend' => $backendMock,
131                                         'repoName' => $this->repoName,
132                                         'dbHandleFactory' => null
133                                 ] ] )
134                         ->getMock();
135
136                 $wrapperMock->expects( $this->any() )->method( 'getDB' )->will( $this->returnValue( $dbMock ) );
137
138                 return [ $dbMock, $backendMock, $wrapperMock ];
139         }
140 }