Merge "Add version number to deprecated setting"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 class TitleTest extends MediaWikiTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( array(
9 'wgLanguageCode' => 'en',
10 'wgContLang' => Language::factory( 'en' ),
11 // User language
12 'wgLang' => Language::factory( 'en' ),
13 'wgAllowUserJs' => false,
14 'wgDefaultLanguageVariant' => false,
15 ) );
16 }
17
18 function testLegalChars() {
19 $titlechars = Title::legalChars();
20
21 foreach ( range( 1, 255 ) as $num ) {
22 $chr = chr( $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" );
25 } else {
26 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
27 }
28 }
29 }
30
31 /**
32 * @dataProvider provideBug31100
33 */
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 ) {
39 $par = $stuff[1];
40 } else {
41 $par = null;
42 }
43 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
44 }
45
46 public static function provideBug31100() {
47 return array(
48 array( 'Special:Version', null ),
49 array( 'Special:Version/', '' ),
50 array( 'Special:Version/param', 'param' ),
51 );
52 }
53
54 /**
55 * Auth-less test of Title::isValidMoveOperation
56 *
57 * @group Database
58 * @param string $source
59 * @param string $target
60 * @param array|string|true $expected Required error
61 * @dataProvider provideTestIsValidMoveOperation
62 */
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 );
69 } else {
70 $errors = $this->flattenErrorsArray( $errors );
71 foreach ( (array)$expected as $error ) {
72 $this->assertContains( $error, $errors );
73 }
74 }
75 }
76
77 function flattenErrorsArray( $errors ) {
78 $result = array();
79 foreach ( $errors as $error ) {
80 $result[] = $error[0];
81 }
82 return $result;
83 }
84
85 public static function provideTestIsValidMoveOperation() {
86 return array(
87 array( 'Test', 'Test', 'selfmove' ),
88 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
89 );
90 }
91
92 /**
93 * @dataProvider provideCasesForGetpageviewlanguage
94 */
95 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
96 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
97
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;
104
105 $title = Title::newFromText( $titleText );
106 $this->assertInstanceOf( 'Title', $title,
107 "Test must be passed a valid title text, you gave '$titleText'"
108 );
109 $this->assertEquals( $expected,
110 $title->getPageViewLanguage()->getCode(),
111 $msg
112 );
113 }
114
115 function provideCasesForGetpageviewlanguage() {
116 # Format:
117 # - expected
118 # - Title name
119 # - wgContLang (expected in most case)
120 # - wgLang (on some specific pages)
121 # - wgDefaultLanguageVariant
122 # - Optional message
123 return array(
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 ),
127
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' ),
136
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' ),
147
148 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
149 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
150
151 );
152 }
153
154 /**
155 * @dataProvider provideBaseTitleCases
156 */
157 function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
158 $title = Title::newFromText( $title );
159 $this->assertEquals( $expected,
160 $title->getBaseText(),
161 $msg
162 );
163 }
164
165 function provideBaseTitleCases() {
166 return array(
167 # Title, expected base, optional message
168 array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
169 array('User:Foo/Bar/Baz', 'Foo/Bar' ),
170 );
171 }
172
173 /**
174 * @dataProvider provideRootTitleCases
175 */
176 function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
177 $title = Title::newFromText( $title );
178 $this->assertEquals( $expected,
179 $title->getRootText(),
180 $msg
181 );
182 }
183
184 public static function provideRootTitleCases() {
185 return array(
186 # Title, expected base, optional message
187 array('User:John_Doe/subOne/subTwo', 'John Doe' ),
188 array('User:Foo/Bar/Baz', 'Foo' ),
189 );
190 }
191
192 /**
193 * @todo Handle $wgNamespacesWithSubpages cases
194 * @dataProvider provideSubpageTitleCases
195 */
196 function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
197 $title = Title::newFromText( $title );
198 $this->assertEquals( $expected,
199 $title->getSubpageText(),
200 $msg
201 );
202 }
203
204 function provideSubpageTitleCases() {
205 return array(
206 # Title, expected base, optional message
207 array('User:John_Doe/subOne/subTwo', 'subTwo' ),
208 array('User:John_Doe/subOne', 'subOne' ),
209 );
210 }
211
212 }