Tests: Make phpunit providers "public static".
[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 return $this->arrayWrap( TestSites::getSites() );
36 }
37
38 /**
39 * @dataProvider instanceProvider
40 * @param Site $site
41 */
42 public function testGetInterwikiIds( Site $site ) {
43 $this->assertInternalType( 'array', $site->getInterwikiIds() );
44 }
45
46 /**
47 * @dataProvider instanceProvider
48 * @param Site $site
49 */
50 public function testGetNavigationIds( Site $site ) {
51 $this->assertInternalType( 'array', $site->getNavigationIds() );
52 }
53
54 /**
55 * @dataProvider instanceProvider
56 * @param Site $site
57 */
58 public function testAddNavigationId( Site $site ) {
59 $site->addNavigationId( 'foobar' );
60 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
61 }
62
63 /**
64 * @dataProvider instanceProvider
65 * @param Site $site
66 */
67 public function testAddInterwikiId( Site $site ) {
68 $site->addInterwikiId( 'foobar' );
69 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
70 }
71
72 /**
73 * @dataProvider instanceProvider
74 * @param Site $site
75 */
76 public function testGetLanguageCode( Site $site ) {
77 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
78 }
79
80 /**
81 * @dataProvider instanceProvider
82 * @param Site $site
83 */
84 public function testSetLanguageCode( Site $site ) {
85 $site->setLanguageCode( 'en' );
86 $this->assertEquals( 'en', $site->getLanguageCode() );
87 }
88
89 /**
90 * @dataProvider instanceProvider
91 * @param Site $site
92 */
93 public function testNormalizePageName( Site $site ) {
94 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
95 }
96
97 /**
98 * @dataProvider instanceProvider
99 * @param Site $site
100 */
101 public function testGetGlobalId( Site $site ) {
102 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
103 }
104
105 /**
106 * @dataProvider instanceProvider
107 * @param Site $site
108 */
109 public function testSetGlobalId( Site $site ) {
110 $site->setGlobalId( 'foobar' );
111 $this->assertEquals( 'foobar', $site->getGlobalId() );
112 }
113
114 /**
115 * @dataProvider instanceProvider
116 * @param Site $site
117 */
118 public function testGetType( Site $site ) {
119 $this->assertInternalType( 'string', $site->getType() );
120 }
121
122 /**
123 * @dataProvider instanceProvider
124 * @param Site $site
125 */
126 public function testGetPath( Site $site ) {
127 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
128 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
129 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
130 }
131
132 /**
133 * @dataProvider instanceProvider
134 * @param Site $site
135 */
136 public function testGetAllPaths( Site $site ) {
137 $this->assertInternalType( 'array', $site->getAllPaths() );
138 }
139
140 /**
141 * @dataProvider instanceProvider
142 * @param Site $site
143 */
144 public function testSetAndRemovePath( Site $site ) {
145 $count = count( $site->getAllPaths() );
146
147 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
148 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
149 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
150
151 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
152
153 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
154 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
155
156 $site->removePath( 'spam' );
157 $site->removePath( 'foobar' );
158
159 $this->assertEquals( $count, count( $site->getAllPaths() ) );
160
161 $this->assertNull( $site->getPath( 'foobar' ) );
162 $this->assertNull( $site->getPath( 'spam' ) );
163 }
164
165 public function testSetLinkPath() {
166 $site = new Site();
167 $path = "TestPath/$1";
168
169 $site->setLinkPath( $path );
170 $this->assertEquals( $path, $site->getLinkPath() );
171 }
172
173 public function testGetLinkPathType() {
174 $site = new Site();
175
176 $path = 'TestPath/$1';
177 $site->setLinkPath( $path );
178 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
179
180 $path = 'AnotherPath/$1';
181 $site->setPath( $site->getLinkPathType(), $path );
182 $this->assertEquals( $path, $site->getLinkPath() );
183 }
184
185 public function testSetPath() {
186 $site = new Site();
187
188 $path = 'TestPath/$1';
189 $site->setPath( 'foo', $path );
190
191 $this->assertEquals( $path, $site->getPath( 'foo' ) );
192 }
193
194 public function testProtocolRelativePath() {
195 $site = new Site();
196
197 $type = $site->getLinkPathType();
198 $path = '//acme.com/'; // protocol-relative URL
199 $site->setPath( $type, $path );
200
201 $this->assertEquals( '', $site->getProtocol() );
202 }
203
204 public static function provideGetPageUrl() {
205 //NOTE: the assumption that the URL is built by replacing $1
206 // with the urlencoded version of $page
207 // is true for Site but not guaranteed for subclasses.
208 // Subclasses need to override this provider appropriately.
209
210 return array(
211 array( #0
212 'http://acme.test/TestPath/$1',
213 'Foo',
214 '/TestPath/Foo',
215 ),
216 array( #1
217 'http://acme.test/TestScript?x=$1&y=bla',
218 'Foo',
219 'TestScript?x=Foo&y=bla',
220 ),
221 array( #2
222 'http://acme.test/TestPath/$1',
223 'foo & bar/xyzzy (quux-shmoox?)',
224 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
225 ),
226 );
227 }
228
229 /**
230 * @dataProvider provideGetPageUrl
231 */
232 public function testGetPageUrl( $path, $page, $expected ) {
233 $site = new Site();
234
235 //NOTE: the assumption that getPageUrl is based on getLinkPath
236 // is true for Site but not guaranteed for subclasses.
237 // Subclasses need to override this test case appropriately.
238 $site->setLinkPath( $path );
239 $this->assertContains( $path, $site->getPageUrl() );
240
241 $this->assertContains( $expected, $site->getPageUrl( $page ) );
242 }
243
244 protected function assertTypeOrFalse( $type, $value ) {
245 if ( $value === false ) {
246 $this->assertTrue( true );
247 } else {
248 $this->assertInternalType( $type, $value );
249 }
250 }
251
252 /**
253 * @dataProvider instanceProvider
254 * @param Site $site
255 */
256 public function testSerialization( Site $site ) {
257 $this->assertInstanceOf( 'Serializable', $site );
258
259 $serialization = serialize( $site );
260 $newInstance = unserialize( $serialization );
261
262 $this->assertInstanceOf( 'Site', $newInstance );
263
264 $this->assertEquals( $serialization, serialize( $newInstance ) );
265 }
266
267 }