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