Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / filerepo / FileBackendDBRepoWrapperTest.php
blobea3f862a0602556c5cd2a64e52894826867ce948
1 <?php
3 class FileBackendDBRepoWrapperTest extends MediaWikiTestCase {
4 protected $backendName = 'foo-backend';
5 protected $repoName = 'pureTestRepo';
7 /**
8 * @dataProvider getBackendPathsProvider
9 * @covers FileBackendDBRepoWrapper::getBackendPaths
11 public function testGetBackendPaths(
12 $mocks,
13 $latest,
14 $dbReadsExpected,
15 $dbReturnValue,
16 $originalPath,
17 $expectedBackendPath,
18 $message ) {
19 list( $dbMock, $backendMock, $wrapperMock ) = $mocks;
21 $dbMock->expects( $dbReadsExpected )
22 ->method( 'selectField' )
23 ->will( $this->returnValue( $dbReturnValue ) );
25 $newPaths = $wrapperMock->getBackendPaths( array( $originalPath ), $latest );
27 $this->assertEquals(
28 $expectedBackendPath,
29 $newPaths[0],
30 $message );
33 public function getBackendPathsProvider() {
34 $prefix = 'mwstore://' . $this->backendName . '/' . $this->repoName;
35 $mocksForCaching = $this->getMocks();
37 return array(
38 array(
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',
47 array(
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',
56 array(
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',
65 array(
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',
74 array(
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',
86 /**
87 * @covers FileBackendDBRepoWrapper::getFileContentsMulti
89 public function testGetFileContentsMulti() {
90 list( $dbMock, $backendMock, $wrapperMock ) = $this->getMocks();
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';
97 $dbMock->expects( $this->once() )
98 ->method( 'selectField' )
99 ->will( $this->returnValue( '96246614d75ba1703bdfd5d7660bb57407aaf5d9' ) );
101 $backendMock->expects( $this->once() )
102 ->method( 'getFileContentsMulti' )
103 ->will( $this->returnValue( array( $sha1Path => 'foo' ) ) );
105 $result = $wrapperMock->getFileContentsMulti( array( 'srcs' => array( $filenamePath ) ) );
107 $this->assertEquals(
108 array( $filenamePath => 'foo' ),
109 $result,
110 'File contents paths translated properly'
114 protected function getMocks() {
115 $dbMock = $this->getMockBuilder( 'DatabaseMysql' )
116 ->disableOriginalConstructor()
117 ->getMock();
119 $backendMock = $this->getMock( 'FSFileBackend',
120 array(),
121 array( array(
122 'name' => $this->backendName,
123 'wikiId' => wfWikiId()
124 ) ) );
126 $wrapperMock = $this->getMock( 'FileBackendDBRepoWrapper',
127 array( 'getDB' ),
128 array( array(
129 'backend' => $backendMock,
130 'repoName' => $this->repoName,
131 'dbHandleFactory' => null
132 ) ) );
134 $wrapperMock->expects( $this->any() )->method( 'getDB' )->will( $this->returnValue( $dbMock ) );
136 return array( $dbMock, $backendMock, $wrapperMock );