| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Site; |
| 4 |
|
| 5 |
use MediaWiki\Site\Site; |
| 6 |
use MediaWikiIntegrationTestCase; |
| 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 |
class SiteTest extends MediaWikiIntegrationTestCase { |
| 35 |
|
| 36 |
public function instanceProvider() { |
| 37 |
return $this->arrayWrap( TestSites::getSites() ); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testGetInterwikiIds( Site $site ) { |
| 46 |
$this->assertIsArray( $site->getInterwikiIds() ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testGetNavigationIds( Site $site ) { |
| 55 |
$this->assertIsArray( $site->getNavigationIds() ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function testAddNavigationId( Site $site ) { |
| 64 |
$site->addNavigationId( 'foobar' ); |
| 65 |
$this->assertContains( 'foobar', $site->getNavigationIds() ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testAddInterwikiId( Site $site ) { |
| 74 |
$site->addInterwikiId( 'foobar' ); |
| 75 |
$this->assertContains( 'foobar', $site->getInterwikiIds() ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function testGetLanguageCode( Site $site ) { |
| 84 |
$this->assertThat( |
| 85 |
$site->getLanguageCode(), |
| 86 |
$this->logicalOr( $this->isNull(), $this->isType( 'string' ) ) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function testSetLanguageCode( Site $site ) { |
| 96 |
$site->setLanguageCode( 'en' ); |
| 97 |
$this->assertEquals( 'en', $site->getLanguageCode() ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testNormalizePageName( Site $site ) { |
| 106 |
$this->assertIsString( $site->normalizePageName( 'Foobar' ) ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function testGetGlobalId( Site $site ) { |
| 115 |
$this->assertThat( |
| 116 |
$site->getGlobalId(), |
| 117 |
$this->logicalOr( $this->isNull(), $this->isType( 'string' ) ) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function testSetGlobalId( Site $site ) { |
| 127 |
$site->setGlobalId( 'foobar' ); |
| 128 |
$this->assertEquals( 'foobar', $site->getGlobalId() ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function testGetType( Site $site ) { |
| 137 |
$this->assertIsString( $site->getType() ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function testGetPath( Site $site ) { |
| 146 |
$this->assertThat( |
| 147 |
$site->getPath( 'page_path' ), |
| 148 |
$this->logicalOr( $this->isNull(), $this->isType( 'string' ) ) |
| 149 |
); |
| 150 |
$this->assertThat( |
| 151 |
$site->getPath( 'file_path' ), |
| 152 |
$this->logicalOr( $this->isNull(), $this->isType( 'string' ) ) |
| 153 |
); |
| 154 |
$this->assertThat( |
| 155 |
$site->getPath( 'foobar' ), |
| 156 |
$this->logicalOr( $this->isNull(), $this->isType( 'string' ) ) |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testGetAllPaths( Site $site ) { |
| 166 |
$this->assertIsArray( $site->getAllPaths() ); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function testSetAndRemovePath( Site $site ) { |
| 176 |
$count = count( $site->getAllPaths() ); |
| 177 |
|
| 178 |
$site->setPath( 'spam', 'http://www.wikidata.org/$1' ); |
| 179 |
$site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' ); |
| 180 |
$site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' ); |
| 181 |
|
| 182 |
$this->assertCount( $count + 2, $site->getAllPaths() ); |
| 183 |
|
| 184 |
$this->assertIsString( $site->getPath( 'foobar' ) ); |
| 185 |
$this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) ); |
| 186 |
|
| 187 |
$site->removePath( 'spam' ); |
| 188 |
$site->removePath( 'foobar' ); |
| 189 |
|
| 190 |
$this->assertCount( $count, $site->getAllPaths() ); |
| 191 |
|
| 192 |
$this->assertNull( $site->getPath( 'foobar' ) ); |
| 193 |
$this->assertNull( $site->getPath( 'spam' ) ); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function testSetLinkPath() { |
| 200 |
$site = new Site(); |
| 201 |
$path = "TestPath/$1"; |
| 202 |
|
| 203 |
$site->setLinkPath( $path ); |
| 204 |
$this->assertEquals( $path, $site->getLinkPath() ); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function testGetLinkPathType() { |
| 211 |
$site = new Site(); |
| 212 |
|
| 213 |
$path = 'TestPath/$1'; |
| 214 |
$site->setLinkPath( $path ); |
| 215 |
$this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) ); |
| 216 |
|
| 217 |
$path = 'AnotherPath/$1'; |
| 218 |
$site->setPath( $site->getLinkPathType(), $path ); |
| 219 |
$this->assertEquals( $path, $site->getLinkPath() ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function testSetPath() { |
| 226 |
$site = new Site(); |
| 227 |
|
| 228 |
$path = 'TestPath/$1'; |
| 229 |
$site->setPath( 'foo', $path ); |
| 230 |
|
| 231 |
$this->assertEquals( $path, $site->getPath( 'foo' ) ); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function testProtocolRelativePath() { |
| 239 |
$site = new Site(); |
| 240 |
|
| 241 |
$type = $site->getLinkPathType(); |
| 242 |
$path = '//acme.com/'; |
| 243 |
$site->setPath( $type, $path ); |
| 244 |
|
| 245 |
$this->assertSame( '', $site->getProtocol() ); |
| 246 |
} |
| 247 |
|
| 248 |
public static function provideGetPageUrl() { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
return [ |
| 255 |
[ |
| 256 |
'http://acme.test/TestPath/$1', |
| 257 |
'Foo', |
| 258 |
'/TestPath/Foo', |
| 259 |
], |
| 260 |
[ |
| 261 |
'http://acme.test/TestScript?x=$1&y=bla', |
| 262 |
'Foo', |
| 263 |
'TestScript?x=Foo&y=bla', |
| 264 |
], |
| 265 |
[ |
| 266 |
'http://acme.test/TestPath/$1', |
| 267 |
'foo & bar/xyzzy (quux-shmoox?)', |
| 268 |
'/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29', |
| 269 |
], |
| 270 |
]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function testGetPageUrl( $path, $page, $expected ) { |
| 278 |
$site = new Site(); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
$site->setLinkPath( $path ); |
| 284 |
$this->assertStringContainsString( $path, $site->getPageUrl() ); |
| 285 |
|
| 286 |
$this->assertStringContainsString( $expected, $site->getPageUrl( $page ) ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function testSerialization( Site $site ) { |
| 296 |
$serialization = serialize( $site ); |
| 297 |
$newInstance = unserialize( $serialization ); |
| 298 |
|
| 299 |
$this->assertInstanceOf( Site::class, $newInstance ); |
| 300 |
|
| 301 |
$this->assertEquals( $serialization, serialize( $newInstance ) ); |
| 302 |
} |
| 303 |
} |