Merge "DumpUploads: output local path instead of mwstore path"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SitesCacheFileBuilderTest.php
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.25
21 *
22 * @ingroup Site
23 * @ingroup Test
24 *
25 * @covers SitesCacheFileBuilder
26 * @group Site
27 *
28 * @author Katie Filbert < aude.wiki@gmail.com >
29 */
30 class SitesCacheFileBuilderTest extends PHPUnit_Framework_TestCase {
31
32 protected function setUp() {
33 $this->cacheFile = $this->getCacheFile();
34 }
35
36 protected function tearDown() {
37 unlink( $this->cacheFile );
38 }
39
40 public function testBuild() {
41 $cacheBuilder = $this->newSitesCacheFileBuilder( $this->getSites() );
42 $cacheBuilder->build();
43
44 $contents = file_get_contents( $this->cacheFile );
45 $this->assertEquals( json_encode( $this->getExpectedData() ), $contents );
46 }
47
48 private function getExpectedData() {
49 return [
50 'sites' => [
51 'foobar' => [
52 'globalid' => 'foobar',
53 'type' => 'unknown',
54 'group' => 'none',
55 'source' => 'local',
56 'language' => null,
57 'localids' => [],
58 'config' => [],
59 'data' => [],
60 'forward' => false,
61 'internalid' => null,
62 'identifiers' => []
63 ],
64 'enwiktionary' => [
65 'globalid' => 'enwiktionary',
66 'type' => 'mediawiki',
67 'group' => 'wiktionary',
68 'source' => 'local',
69 'language' => 'en',
70 'localids' => [
71 'equivalent' => [ 'enwiktionary' ]
72 ],
73 'config' => [],
74 'data' => [
75 'paths' => [
76 'page_path' => 'https://en.wiktionary.org/wiki/$1',
77 'file_path' => 'https://en.wiktionary.org/w/$1'
78 ]
79 ],
80 'forward' => false,
81 'internalid' => null,
82 'identifiers' => [
83 [
84 'type' => 'equivalent',
85 'key' => 'enwiktionary'
86 ]
87 ]
88 ]
89 ]
90 ];
91 }
92
93 private function newSitesCacheFileBuilder( SiteList $sites ) {
94 return new SitesCacheFileBuilder(
95 $this->getSiteLookup( $sites ),
96 $this->cacheFile
97 );
98 }
99
100 private function getSiteLookup( SiteList $sites ) {
101 $siteLookup = $this->getMockBuilder( 'SiteLookup' )
102 ->disableOriginalConstructor()
103 ->getMock();
104
105 $siteLookup->expects( $this->any() )
106 ->method( 'getSites' )
107 ->will( $this->returnValue( $sites ) );
108
109 return $siteLookup;
110 }
111
112 private function getSites() {
113 $sites = [];
114
115 $site = new Site();
116 $site->setGlobalId( 'foobar' );
117 $sites[] = $site;
118
119 $site = new MediaWikiSite();
120 $site->setGlobalId( 'enwiktionary' );
121 $site->setGroup( 'wiktionary' );
122 $site->setLanguageCode( 'en' );
123 $site->addNavigationId( 'enwiktionary' );
124 $site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" );
125 $site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" );
126 $sites[] = $site;
127
128 return new SiteList( $sites );
129 }
130
131 private function getCacheFile() {
132 return tempnam( sys_get_temp_dir(), 'mw-test-sitelist' );
133 }
134
135 }