3 class TitleTest
extends MediaWikiTestCase
{
5 protected function setUp() {
8 $this->setMwGlobals( array(
9 'wgLanguageCode' => 'en',
10 'wgContLang' => Language
::factory( 'en' ),
12 'wgLang' => Language
::factory( 'en' ),
13 'wgAllowUserJs' => false,
14 'wgDefaultLanguageVariant' => false,
18 function testLegalChars() {
19 $titlechars = Title
::legalChars();
21 foreach ( range( 1, 255 ) as $num ) {
23 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
24 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
26 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
32 * @dataProvider provideBug31100
34 function testBug31100FixSpecialName( $text, $expectedParam ) {
35 $title = Title
::newFromText( $text );
36 $fixed = $title->fixSpecialName();
37 $stuff = explode( '/', $fixed->getDbKey(), 2 );
38 if ( count( $stuff ) == 2 ) {
43 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
46 public static function provideBug31100() {
48 array( 'Special:Version', null ),
49 array( 'Special:Version/', '' ),
50 array( 'Special:Version/param', 'param' ),
55 * Auth-less test of Title::isValidMoveOperation
58 * @param string $source
59 * @param string $target
60 * @param array|string|true $expected Required error
61 * @dataProvider provideTestIsValidMoveOperation
63 function testIsValidMoveOperation( $source, $target, $expected ) {
64 $title = Title
::newFromText( $source );
65 $nt = Title
::newFromText( $target );
66 $errors = $title->isValidMoveOperation( $nt, false );
67 if ( $expected === true ) {
68 $this->assertTrue( $errors );
70 $errors = $this->flattenErrorsArray( $errors );
71 foreach ( (array)$expected as $error ) {
72 $this->assertContains( $error, $errors );
77 function flattenErrorsArray( $errors ) {
79 foreach ( $errors as $error ) {
80 $result[] = $error[0];
85 public static function provideTestIsValidMoveOperation() {
87 array( 'Test', 'Test', 'selfmove' ),
88 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
93 * @dataProvider provideCasesForGetpageviewlanguage
95 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
96 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
98 // Setup environnement for this test
99 $wgLanguageCode = $contLang;
100 $wgContLang = Language
::factory( $contLang );
101 $wgLang = Language
::factory( $lang );
102 $wgDefaultLanguageVariant = $variant;
103 $wgAllowUserJs = true;
105 $title = Title
::newFromText( $titleText );
106 $this->assertInstanceOf( 'Title', $title,
107 "Test must be passed a valid title text, you gave '$titleText'"
109 $this->assertEquals( $expected,
110 $title->getPageViewLanguage()->getCode(),
115 function provideCasesForGetpageviewlanguage() {
119 # - wgContLang (expected in most case)
120 # - wgLang (on some specific pages)
121 # - wgDefaultLanguageVariant
124 array( 'fr', 'Main_page', 'fr', 'fr', false ),
125 array( 'es', 'Main_page', 'es', 'zh-tw', false ),
126 array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
128 array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
129 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
130 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
131 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
132 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
133 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
134 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
135 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
137 array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
138 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
139 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
140 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
141 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
142 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
143 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
144 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
145 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
146 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
148 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
149 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
155 * @dataProvider provideBaseTitleCases
157 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
158 $title = Title
::newFromText( $title );
159 $this->assertEquals( $expected,
160 $title->getBaseText(),
165 function provideBaseTitleCases() {
167 # Title, expected base, optional message
168 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
169 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
174 * @dataProvider provideRootTitleCases
176 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
177 $title = Title
::newFromText( $title );
178 $this->assertEquals( $expected,
179 $title->getRootText(),
184 public static function provideRootTitleCases() {
186 # Title, expected base, optional message
187 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
188 array('User:Foo/Bar/Baz', 'Foo' ),
193 * @todo Handle $wgNamespacesWithSubpages cases
194 * @dataProvider provideSubpageTitleCases
196 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
197 $title = Title
::newFromText( $title );
198 $this->assertEquals( $expected,
199 $title->getSubpageText(),
204 function provideSubpageTitleCases() {
206 # Title, expected base, optional message
207 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
208 array('User:John_Doe/subOne', 'subOne' ),