merged master (2012-09-11)
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 /**
4 *
5 * @group Database
6 * ^--- needed for language cache stuff
7 */
8 class TitleTest extends MediaWikiTestCase {
9
10 function testLegalChars() {
11 $titlechars = Title::legalChars();
12
13 foreach ( range( 1, 255 ) as $num ) {
14 $chr = chr( $num );
15 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
16 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
17 } else {
18 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
19 }
20 }
21 }
22
23 /**
24 * @dataProvider dataBug31100
25 */
26 function testBug31100FixSpecialName( $text, $expectedParam ) {
27 $title = Title::newFromText( $text );
28 $fixed = $title->fixSpecialName();
29 $stuff = explode( '/', $fixed->getDbKey(), 2 );
30 if ( count( $stuff ) == 2 ) {
31 $par = $stuff[1];
32 } else {
33 $par = null;
34 }
35 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
36 }
37
38 function dataBug31100() {
39 return array(
40 array( 'Special:Version', null ),
41 array( 'Special:Version/', '' ),
42 array( 'Special:Version/param', 'param' ),
43 );
44 }
45
46 /**
47 * Auth-less test of Title::isValidMoveOperation
48 *
49 * @group Database
50 * @param string $source
51 * @param string $target
52 * @param array|string|true $expected Required error
53 * @dataProvider dataTestIsValidMoveOperation
54 */
55 function testIsValidMoveOperation( $source, $target, $expected ) {
56 $title = Title::newFromText( $source );
57 $nt = Title::newFromText( $target );
58 $errors = $title->isValidMoveOperation( $nt, false );
59 if ( $expected === true ) {
60 $this->assertTrue( $errors );
61 } else {
62 $errors = $this->flattenErrorsArray( $errors );
63 foreach ( (array)$expected as $error ) {
64 $this->assertContains( $error, $errors );
65 }
66 }
67 }
68
69 function flattenErrorsArray( $errors ) {
70 $result = array();
71 foreach ( $errors as $error ) {
72 $result[] = $error[0];
73 }
74 return $result;
75 }
76
77 function dataTestIsValidMoveOperation() {
78 return array(
79 array( 'Test', 'Test', 'selfmove' ),
80 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
81 );
82 }
83
84
85 /**
86 * @dataProvider provideCasesForGetpageviewlanguage
87 */
88 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
89 // Save globals
90 global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
91 $save['wgContLang'] = $wgContLang;
92 $save['wgLang'] = $wgLang;
93 $save['wgAllowUserJs'] = $wgAllowUserJs;
94 $save['wgLanguageCode'] = $wgLanguageCode;
95 $save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;
96
97 // Setup test environnement:
98 $wgContLang = Language::factory( $contLang );
99 $wgLang = Language::factory( $lang );
100 # To test out .js titles:
101 $wgAllowUserJs = true;
102 $wgLanguageCode = $contLang;
103 $wgDefaultLanguageVariant = $variant;
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 // Restore globals
115 $wgContLang = $save['wgContLang'];
116 $wgLang = $save['wgLang'];
117 $wgAllowUserJs = $save['wgAllowUserJs'];
118 $wgLanguageCode = $save['wgLanguageCode'];
119 $wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
120 }
121
122 function provideCasesForGetpageviewlanguage() {
123 # Format:
124 # - expected
125 # - Title name
126 # - wgContLang (expected in most case)
127 # - wgLang (on some specific pages)
128 # - wgDefaultLanguageVariant
129 # - Optional message
130 return array(
131 array( 'fr', 'Main_page', 'fr', 'fr', false ),
132 array( 'es', 'Main_page', 'es', 'zh-tw', false ),
133 array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
134
135 array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
136 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
137 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
138 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
139 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
140 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
141 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
142 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
143
144 array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
145 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
146 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
147 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
148 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
149 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
150 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
151 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
152 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
153 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
154
155 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
156 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
157
158 );
159 }
160 }