Merge "Chinese Conversion Table Update 2017-6"
[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 use MediaWikiCoversValidator;
33
34 protected function setUp() {
35 $this->cacheFile = $this->getCacheFile();
36 }
37
38 protected function tearDown() {
39 unlink( $this->cacheFile );
40 }
41
42 public function testBuild() {
43 $cacheBuilder = $this->newSitesCacheFileBuilder( $this->getSites() );
44 $cacheBuilder->build();
45
46 $contents = file_get_contents( $this->cacheFile );
47 $this->assertEquals( json_encode( $this->getExpectedData() ), $contents );
48 }
49
50 private function getExpectedData() {
51 return [
52 'sites' => [
53 'foobar' => [
54 'globalid' => 'foobar',
55 'type' => 'unknown',
56 'group' => 'none',
57 'source' => 'local',
58 'language' => null,
59 'localids' => [],
60 'config' => [],
61 'data' => [],
62 'forward' => false,
63 'internalid' => null,
64 'identifiers' => []
65 ],
66 'enwiktionary' => [
67 'globalid' => 'enwiktionary',
68 'type' => 'mediawiki',
69 'group' => 'wiktionary',
70 'source' => 'local',
71 'language' => 'en',
72 'localids' => [
73 'equivalent' => [ 'enwiktionary' ]
74 ],
75 'config' => [],
76 'data' => [
77 'paths' => [
78 'page_path' => 'https://en.wiktionary.org/wiki/$1',
79 'file_path' => 'https://en.wiktionary.org/w/$1'
80 ]
81 ],
82 'forward' => false,
83 'internalid' => null,
84 'identifiers' => [
85 [
86 'type' => 'equivalent',
87 'key' => 'enwiktionary'
88 ]
89 ]
90 ]
91 ]
92 ];
93 }
94
95 private function newSitesCacheFileBuilder( SiteList $sites ) {
96 return new SitesCacheFileBuilder(
97 $this->getSiteLookup( $sites ),
98 $this->cacheFile
99 );
100 }
101
102 private function getSiteLookup( SiteList $sites ) {
103 $siteLookup = $this->getMockBuilder( 'SiteLookup' )
104 ->disableOriginalConstructor()
105 ->getMock();
106
107 $siteLookup->expects( $this->any() )
108 ->method( 'getSites' )
109 ->will( $this->returnValue( $sites ) );
110
111 return $siteLookup;
112 }
113
114 private function getSites() {
115 $sites = [];
116
117 $site = new Site();
118 $site->setGlobalId( 'foobar' );
119 $sites[] = $site;
120
121 $site = new MediaWikiSite();
122 $site->setGlobalId( 'enwiktionary' );
123 $site->setGroup( 'wiktionary' );
124 $site->setLanguageCode( 'en' );
125 $site->addNavigationId( 'enwiktionary' );
126 $site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" );
127 $site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" );
128 $sites[] = $site;
129
130 return new SiteList( $sites );
131 }
132
133 private function getCacheFile() {
134 return tempnam( sys_get_temp_dir(), 'mw-test-sitelist' );
135 }
136
137 }