summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/site/SiteTest.php
blob: 63d90d2e926c602a58d9b5194a8bd7d8515ada1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php

/**
 * Tests for the Site class.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @since 1.21
 *
 * @ingroup Site
 * @ingroup Test
 *
 * @group Site
 *
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class SiteTest extends MediaWikiTestCase {

	public function instanceProvider() {
		return $this->arrayWrap( TestSites::getSites() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getInterwikiIds
	 */
	public function testGetInterwikiIds( Site $site ) {
		$this->assertInternalType( 'array', $site->getInterwikiIds() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getNavigationIds
	 */
	public function testGetNavigationIds( Site $site ) {
		$this->assertInternalType( 'array', $site->getNavigationIds() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::addNavigationId
	 */
	public function testAddNavigationId( Site $site ) {
		$site->addNavigationId( 'foobar' );
		$this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::addInterwikiId
	 */
	public function testAddInterwikiId( Site $site ) {
		$site->addInterwikiId( 'foobar' );
		$this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getLanguageCode
	 */
	public function testGetLanguageCode( Site $site ) {
		$this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::setLanguageCode
	 */
	public function testSetLanguageCode( Site $site ) {
		$site->setLanguageCode( 'en' );
		$this->assertEquals( 'en', $site->getLanguageCode() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::normalizePageName
	 */
	public function testNormalizePageName( Site $site ) {
		$this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getGlobalId
	 */
	public function testGetGlobalId( Site $site ) {
		$this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::setGlobalId
	 */
	public function testSetGlobalId( Site $site ) {
		$site->setGlobalId( 'foobar' );
		$this->assertEquals( 'foobar', $site->getGlobalId() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getType
	 */
	public function testGetType( Site $site ) {
		$this->assertInternalType( 'string', $site->getType() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getPath
	 */
	public function testGetPath( Site $site ) {
		$this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
		$this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
		$this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::getAllPaths
	 */
	public function testGetAllPaths( Site $site ) {
		$this->assertInternalType( 'array', $site->getAllPaths() );
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::setPath
	 * @covers Site::removePath
	 */
	public function testSetAndRemovePath( Site $site ) {
		$count = count( $site->getAllPaths() );

		$site->setPath( 'spam', 'http://www.wikidata.org/$1' );
		$site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
		$site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );

		$this->assertEquals( $count + 2, count( $site->getAllPaths() ) );

		$this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
		$this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );

		$site->removePath( 'spam' );
		$site->removePath( 'foobar' );

		$this->assertEquals( $count, count( $site->getAllPaths() ) );

		$this->assertNull( $site->getPath( 'foobar' ) );
		$this->assertNull( $site->getPath( 'spam' ) );
	}

	/**
	 * @covers Site::setLinkPath
	 */
	public function testSetLinkPath() {
		$site = new Site();
		$path = "TestPath/$1";

		$site->setLinkPath( $path );
		$this->assertEquals( $path, $site->getLinkPath() );
	}

	/**
	 * @covers Site::getLinkPathType
	 */
	public function testGetLinkPathType() {
		$site = new Site();

		$path = 'TestPath/$1';
		$site->setLinkPath( $path );
		$this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );

		$path = 'AnotherPath/$1';
		$site->setPath( $site->getLinkPathType(), $path );
		$this->assertEquals( $path, $site->getLinkPath() );
	}

	/**
	 * @covers Site::setPath
	 */
	public function testSetPath() {
		$site = new Site();

		$path = 'TestPath/$1';
		$site->setPath( 'foo', $path );

		$this->assertEquals( $path, $site->getPath( 'foo' ) );
	}

	/**
	 * @covers Site::setPath
	 * @covers Site::getProtocol
	 */
	public function testProtocolRelativePath() {
		$site = new Site();

		$type = $site->getLinkPathType();
		$path = '//acme.com/'; // protocol-relative URL
		$site->setPath( $type, $path );

		$this->assertEquals( '', $site->getProtocol() );
	}

	public static function provideGetPageUrl() {
		//NOTE: the assumption that the URL is built by replacing $1
		//      with the urlencoded version of $page
		//      is true for Site but not guaranteed for subclasses.
		//      Subclasses need to override this provider appropriately.

		return array(
			array( #0
				'http://acme.test/TestPath/$1',
				'Foo',
				'/TestPath/Foo',
			),
			array( #1
				'http://acme.test/TestScript?x=$1&y=bla',
				'Foo',
				'TestScript?x=Foo&y=bla',
			),
			array( #2
				'http://acme.test/TestPath/$1',
				'foo & bar/xyzzy (quux-shmoox?)',
				'/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
			),
		);
	}

	/**
	 * @dataProvider provideGetPageUrl
	 * @covers Site::getPageUrl
	 */
	public function testGetPageUrl( $path, $page, $expected ) {
		$site = new Site();

		//NOTE: the assumption that getPageUrl is based on getLinkPath
		//      is true for Site but not guaranteed for subclasses.
		//      Subclasses need to override this test case appropriately.
		$site->setLinkPath( $path );
		$this->assertContains( $path, $site->getPageUrl() );

		$this->assertContains( $expected, $site->getPageUrl( $page ) );
	}

	protected function assertTypeOrFalse( $type, $value ) {
		if ( $value === false ) {
			$this->assertTrue( true );
		} else {
			$this->assertInternalType( $type, $value );
		}
	}

	/**
	 * @dataProvider instanceProvider
	 * @param Site $site
	 * @covers Site::serialize
	 * @covers Site::unserialize
	 */
	public function testSerialization( Site $site ) {
		$this->assertInstanceOf( 'Serializable', $site );

		$serialization = serialize( $site );
		$newInstance = unserialize( $serialization );

		$this->assertInstanceOf( 'Site', $newInstance );

		$this->assertEquals( $serialization, serialize( $newInstance ) );
	}
}