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