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