Merge "Make Job::toString handle non-primitive parameters."
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteTest.php
1 <?php
2
3 /**
4 * Tests for the Site class.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.21
23 *
24 * @ingroup Site
25 * @ingroup Test
26 *
27 * @group Site
28 *
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 */
32 class SiteTest extends MediaWikiTestCase {
33
34 public function instanceProvider() {
35 $instances = array();
36
37 $instances[] = new Site();
38
39 $site = new Site();
40 $site->setGlobalId( 'enwiki' );
41 $site->setInternalId( 42 );
42 $instances[] = $site;
43
44 $site = new MediaWikiSite();
45 $site->setGlobalId( 'nlwiki' );
46 $site->setLanguageCode( 'nl' );
47 $instances[] = $site;
48
49 return $this->arrayWrap( $instances );
50 }
51
52 /**
53 * @dataProvider instanceProvider
54 * @param Site $site
55 */
56 public function testGetInterwikiIds( Site $site ) {
57 $this->assertInternalType( 'array', $site->getInterwikiIds() );
58 }
59
60 /**
61 * @dataProvider instanceProvider
62 * @param Site $site
63 */
64 public function testGetNavigationIds( Site $site ) {
65 $this->assertInternalType( 'array', $site->getNavigationIds() );
66 }
67
68 /**
69 * @dataProvider instanceProvider
70 * @param Site $site
71 */
72 public function testAddNavigationId( Site $site ) {
73 $site->addNavigationId( 'foobar' );
74 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
75 }
76
77 /**
78 * @dataProvider instanceProvider
79 * @param Site $site
80 */
81 public function testAddInterwikiId( Site $site ) {
82 $site->addInterwikiId( 'foobar' );
83 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
84 }
85
86 /**
87 * @dataProvider instanceProvider
88 * @param Site $site
89 */
90 public function testGetLanguageCode( Site $site ) {
91 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
92 }
93
94 /**
95 * @dataProvider instanceProvider
96 * @param Site $site
97 */
98 public function testSetLanguageCode( Site $site ) {
99 $site->setLanguageCode( 'en' );
100 $this->assertEquals( 'en', $site->getLanguageCode() );
101 }
102
103 /**
104 * @dataProvider instanceProvider
105 * @param Site $site
106 */
107 public function testNormalizePageName( Site $site ) {
108 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
109 }
110
111 /**
112 * @dataProvider instanceProvider
113 * @param Site $site
114 */
115 public function testGetGlobalId( Site $site ) {
116 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
117 }
118
119 /**
120 * @dataProvider instanceProvider
121 * @param Site $site
122 */
123 public function testSetGlobalId( Site $site ) {
124 $site->setGlobalId( 'foobar' );
125 $this->assertEquals( 'foobar', $site->getGlobalId() );
126 }
127
128 /**
129 * @dataProvider instanceProvider
130 * @param Site $site
131 */
132 public function testGetType( Site $site ) {
133 $this->assertInternalType( 'string', $site->getType() );
134 }
135
136 /**
137 * @dataProvider instanceProvider
138 * @param Site $site
139 */
140 public function testGetPath( Site $site ) {
141 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
142 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
143 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
144 }
145
146 /**
147 * @dataProvider instanceProvider
148 * @param Site $site
149 */
150 public function testGetAllPaths( Site $site ) {
151 $this->assertInternalType( 'array', $site->getAllPaths() );
152 }
153
154 /**
155 * @dataProvider instanceProvider
156 * @param Site $site
157 */
158 public function testSetAndRemovePath( Site $site ) {
159 $count = count( $site->getAllPaths() );
160
161 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
162 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
163 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
164
165 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
166
167 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
168 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
169
170 $site->removePath( 'spam' );
171 $site->removePath( 'foobar' );
172
173 $this->assertEquals( $count, count( $site->getAllPaths() ) );
174
175 $this->assertNull( $site->getPath( 'foobar' ) );
176 $this->assertNull( $site->getPath( 'spam' ) );
177 }
178
179 public function testSetLinkPath() {
180 $site = new Site();
181 $path = "TestPath/$1";
182
183 $site->setLinkPath( $path );
184 $this->assertEquals( $path, $site->getLinkPath() );
185 }
186
187 public function testGetLinkPathType() {
188 $site = new Site();
189
190 $path = 'TestPath/$1';
191 $site->setLinkPath( $path );
192 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
193
194 $path = 'AnotherPath/$1';
195 $site->setPath( $site->getLinkPathType(), $path );
196 $this->assertEquals( $path, $site->getLinkPath() );
197 }
198
199 public function testSetPath() {
200 $site = new Site();
201
202 $path = 'TestPath/$1';
203 $site->setPath( 'foo', $path );
204
205 $this->assertEquals( $path, $site->getPath( 'foo' ) );
206 }
207
208 public function testProtocolRelativePath() {
209 $site = new Site();
210
211 $type = $site->getLinkPathType();
212 $path = '//acme.com/'; // protocol-relative URL
213 $site->setPath( $type, $path );
214
215 $this->assertEquals( '', $site->getProtocol() );
216 }
217
218 public function provideGetPageUrl() {
219 //NOTE: the assumption that the URL is built by replacing $1
220 // with the urlencoded version of $page
221 // is true for Site but not guaranteed for subclasses.
222 // Subclasses need to override this provider appropriately.
223
224 return array(
225 array( #0
226 'http://acme.test/TestPath/$1',
227 'Foo',
228 '/TestPath/Foo',
229 ),
230 array( #1
231 'http://acme.test/TestScript?x=$1&y=bla',
232 'Foo',
233 'TestScript?x=Foo&y=bla',
234 ),
235 array( #2
236 'http://acme.test/TestPath/$1',
237 'foo & bar/xyzzy (quux-shmoox?)',
238 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
239 ),
240 );
241 }
242
243 /**
244 * @dataProvider provideGetPageUrl
245 */
246 public function testGetPageUrl( $path, $page, $expected ) {
247 $site = new Site();
248
249 //NOTE: the assumption that getPageUrl is based on getLinkPath
250 // is true for Site but not guaranteed for subclasses.
251 // Subclasses need to override this test case appropriately.
252 $site->setLinkPath( $path );
253 $this->assertContains( $path, $site->getPageUrl() );
254
255 $this->assertContains( $expected, $site->getPageUrl( $page ) );
256 }
257
258 protected function assertTypeOrFalse( $type, $value ) {
259 if ( $value === false ) {
260 $this->assertTrue( true );
261 }
262 else {
263 $this->assertInternalType( $type, $value );
264 }
265 }
266
267 }