| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Page\PageIdentity; |
| 23 |
use MediaWiki\Page\PageReference; |
| 24 |
use MediaWiki\Page\PageReferenceValue; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\Title\TitleValue; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class TitleValueTest extends \MediaWikiUnitTestCase { |
| 34 |
|
| 35 |
public static function goodConstructorProvider() { |
| 36 |
return [ |
| 37 |
[ NS_MAIN, '', 'fragment', '', true, false ], |
| 38 |
[ NS_MAIN, '', '', 'interwiki', false, true ], |
| 39 |
[ NS_MAIN, '', 'fragment', 'interwiki', true, true ], |
| 40 |
[ NS_USER, 'TestThis', 'stuff', '', true, false ], |
| 41 |
[ NS_USER, 'TestThis', '', 'baz', false, true ], |
| 42 |
[ NS_MAIN, 'foo bar', '', '', false, false ], |
| 43 |
[ NS_MAIN, 'foo_bar', '', '', false, false ], |
| 44 |
[ NS_MAIN, '', '', '', false, false ], |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testConstruction( $ns, $text, $fragment, $interwiki, $hasFragment, |
| 52 |
$hasInterwiki |
| 53 |
) { |
| 54 |
$title = new TitleValue( $ns, $text, $fragment, $interwiki ); |
| 55 |
|
| 56 |
$this->assertEquals( $ns, $title->getNamespace() ); |
| 57 |
$this->assertTrue( $title->inNamespace( $ns ) ); |
| 58 |
$this->assertEquals( strtr( $text, ' ', '_' ), $title->getDBkey() ); |
| 59 |
$this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() ); |
| 60 |
$this->assertEquals( $fragment, $title->getFragment() ); |
| 61 |
$this->assertEquals( $hasFragment, $title->hasFragment() ); |
| 62 |
$this->assertEquals( $interwiki, $title->getInterwiki() ); |
| 63 |
$this->assertEquals( $hasInterwiki, $title->isExternal() ); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function testTryNew( $ns, $text, $fragment, $interwiki, $hasFragment, |
| 70 |
$hasInterwiki |
| 71 |
) { |
| 72 |
$title = TitleValue::tryNew( $ns, $text, $fragment, $interwiki ); |
| 73 |
|
| 74 |
$this->assertEquals( $ns, $title->getNamespace() ); |
| 75 |
$this->assertTrue( $title->inNamespace( $ns ) ); |
| 76 |
$this->assertEquals( strtr( $text, ' ', '_' ), $title->getDBkey() ); |
| 77 |
$this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() ); |
| 78 |
$this->assertEquals( $fragment, $title->getFragment() ); |
| 79 |
$this->assertEquals( $hasFragment, $title->hasFragment() ); |
| 80 |
$this->assertEquals( $interwiki, $title->getInterwiki() ); |
| 81 |
$this->assertEquals( $hasInterwiki, $title->isExternal() ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function testAssertValidSpec( $ns, $text, $fragment, $interwiki ) { |
| 88 |
TitleValue::assertValidSpec( $ns, $text, $fragment, $interwiki ); |
| 89 |
$this->assertTrue( true ); |
| 90 |
} |
| 91 |
|
| 92 |
public static function badConstructorNamespaceTypeProvider() { |
| 93 |
return [ |
| 94 |
[ 'foo', 'title', 'fragment', '' ], |
| 95 |
[ null, 'title', 'fragment', '' ], |
| 96 |
[ 2.3, 'title', 'fragment', '' ], |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
public static function badConstructorProvider() { |
| 101 |
return [ |
| 102 |
[ NS_MAIN, 5, 'fragment', '' ], |
| 103 |
[ NS_MAIN, null, 'fragment', '' ], |
| 104 |
[ NS_USER, '', 'fragment', '' ], |
| 105 |
[ NS_USER, '', '', 'interwiki' ], |
| 106 |
[ NS_MAIN, 'bar_', '', '' ], |
| 107 |
[ NS_MAIN, '_foo', '', '' ], |
| 108 |
[ NS_MAIN, ' eek ', '', '' ], |
| 109 |
|
| 110 |
[ NS_MAIN, 'title', 5, '' ], |
| 111 |
[ NS_MAIN, 'title', null, '' ], |
| 112 |
[ NS_MAIN, 'title', [], '' ], |
| 113 |
|
| 114 |
[ NS_MAIN, 'title', '', 5 ], |
| 115 |
[ NS_MAIN, 'title', null, 5 ], |
| 116 |
[ NS_MAIN, 'title', [], 5 ], |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function testConstructionErrors( $ns, $text, $fragment, $interwiki ) { |
| 125 |
$this->expectException( InvalidArgumentException::class ); |
| 126 |
new TitleValue( $ns, $text, $fragment, $interwiki ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function testTryNewErrors( $ns, $text, $fragment, $interwiki ) { |
| 133 |
$this->expectException( InvalidArgumentException::class ); |
| 134 |
TitleValue::tryNew( $ns, $text, $fragment, $interwiki ); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function testTryNewFailure( $ns, $text, $fragment, $interwiki ) { |
| 141 |
$this->assertNull( TitleValue::tryNew( $ns, $text, $fragment, $interwiki ) ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testAssertValidSpecErrors( $ns, $text, $fragment, $interwiki ) { |
| 148 |
$this->expectException( InvalidArgumentException::class ); |
| 149 |
TitleValue::assertValidSpec( $ns, $text, $fragment, $interwiki ); |
| 150 |
} |
| 151 |
|
| 152 |
public static function fragmentTitleProvider() { |
| 153 |
return [ |
| 154 |
[ new TitleValue( NS_MAIN, 'Test' ), 'foo' ], |
| 155 |
[ new TitleValue( NS_TALK, 'Test', 'foo' ), '' ], |
| 156 |
[ new TitleValue( NS_CATEGORY, 'Test', 'foo' ), 'bar' ], |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function testCreateFragmentTitle( TitleValue $title, $fragment ) { |
| 164 |
$fragmentTitle = $title->createFragmentTarget( $fragment ); |
| 165 |
|
| 166 |
$this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() ); |
| 167 |
$this->assertEquals( $title->getText(), $fragmentTitle->getText() ); |
| 168 |
$this->assertEquals( $fragment, $fragmentTitle->getFragment() ); |
| 169 |
} |
| 170 |
|
| 171 |
public static function provideNewFromPage() { |
| 172 |
yield [ new PageReferenceValue( NS_USER, 'Test', PageIdentity::LOCAL ) ]; |
| 173 |
yield [ new PageReferenceValue( NS_USER, 'Test', 'acme' ) ]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function testNewFromPage( PageReference $page ) { |
| 182 |
$title = TitleValue::newFromPage( $page ); |
| 183 |
|
| 184 |
$this->assertSame( $page->getNamespace(), $title->getNamespace() ); |
| 185 |
$this->assertSame( $page->getDBkey(), $title->getDBkey() ); |
| 186 |
$this->assertSame( $page->getDBkey(), $title->getText() ); |
| 187 |
$this->assertSame( '', $title->getFragment() ); |
| 188 |
$this->assertSame( '', $title->getInterwiki() ); |
| 189 |
$this->assertFalse( $title->isExternal() ); |
| 190 |
$this->assertFalse( $title->hasFragment() ); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function testCastPageToLinkTarget( PageReference $page ) { |
| 199 |
$title = TitleValue::castPageToLinkTarget( $page ); |
| 200 |
|
| 201 |
$this->assertSame( $page->getNamespace(), $title->getNamespace() ); |
| 202 |
$this->assertSame( $page->getDBkey(), $title->getDBkey() ); |
| 203 |
$this->assertSame( $page->getDBkey(), $title->getText() ); |
| 204 |
$this->assertSame( '', $title->getFragment() ); |
| 205 |
$this->assertSame( '', $title->getInterwiki() ); |
| 206 |
$this->assertFalse( $title->isExternal() ); |
| 207 |
$this->assertFalse( $title->hasFragment() ); |
| 208 |
} |
| 209 |
|
| 210 |
public function testCastTitleToLinkTarget() { |
| 211 |
$page = Title::makeTitle( NS_MAIN, 'Test' ); |
| 212 |
$this->assertSame( $page, TitleValue::castPageToLinkTarget( $page ) ); |
| 213 |
} |
| 214 |
|
| 215 |
public function testCastNullToLinkTarget() { |
| 216 |
$this->assertNull( TitleValue::castPageToLinkTarget( null ) ); |
| 217 |
} |
| 218 |
|
| 219 |
public static function getTextProvider() { |
| 220 |
return [ |
| 221 |
[ 'Foo', 'Foo' ], |
| 222 |
[ 'Foo_Bar', 'Foo Bar' ], |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function testGetText( $dbkey, $text ) { |
| 230 |
$title = new TitleValue( NS_MAIN, $dbkey ); |
| 231 |
|
| 232 |
$this->assertEquals( $text, $title->getText() ); |
| 233 |
} |
| 234 |
|
| 235 |
public static function provideTestToString() { |
| 236 |
yield [ |
| 237 |
new TitleValue( 0, 'Foo' ), |
| 238 |
'0:Foo' |
| 239 |
]; |
| 240 |
yield [ |
| 241 |
new TitleValue( 1, 'Bar_Baz' ), |
| 242 |
'1:Bar_Baz' |
| 243 |
]; |
| 244 |
yield [ |
| 245 |
new TitleValue( 9, 'JoJo', 'Frag' ), |
| 246 |
'9:JoJo#Frag' |
| 247 |
]; |
| 248 |
yield [ |
| 249 |
new TitleValue( 200, 'tea', 'Fragment', 'wikicode' ), |
| 250 |
'wikicode:200:tea#Fragment' |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function testToString( TitleValue $value, $expected ) { |
| 258 |
$this->assertSame( |
| 259 |
$expected, |
| 260 |
$value->__toString() |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
public static function provideIsSameLinkAs() { |
| 265 |
yield [ |
| 266 |
new TitleValue( 0, 'Foo' ), |
| 267 |
new TitleValue( 0, 'Foo' ), |
| 268 |
true |
| 269 |
]; |
| 270 |
yield [ |
| 271 |
new TitleValue( 1, 'Bar_Baz' ), |
| 272 |
new TitleValue( 1, 'Bar_Baz' ), |
| 273 |
true |
| 274 |
]; |
| 275 |
yield [ |
| 276 |
new TitleValue( 0, 'Foo' ), |
| 277 |
new TitleValue( 1, 'Foo' ), |
| 278 |
false |
| 279 |
]; |
| 280 |
yield [ |
| 281 |
new TitleValue( 0, 'Foo' ), |
| 282 |
new TitleValue( 0, 'Foozz' ), |
| 283 |
false |
| 284 |
]; |
| 285 |
yield [ |
| 286 |
new TitleValue( 0, 'Foo', '' ), |
| 287 |
new TitleValue( 0, 'Foo', 'Bar' ), |
| 288 |
false |
| 289 |
]; |
| 290 |
yield [ |
| 291 |
new TitleValue( 0, 'Foo', '', 'bar' ), |
| 292 |
new TitleValue( 0, 'Foo', '', '' ), |
| 293 |
false |
| 294 |
]; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function testIsSameLinkAs( TitleValue $a, TitleValue $b, $expected ) { |
| 301 |
$this->assertSame( $expected, $a->isSameLinkAs( $b ) ); |
| 302 |
$this->assertSame( $expected, $b->isSameLinkAs( $a ) ); |
| 303 |
} |
| 304 |
} |