3 class TitleTest
extends MediaWikiTestCase
{
5 function testLegalChars() {
6 $titlechars = Title
::legalChars();
8 foreach ( range( 1, 255 ) as $num ) {
10 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
11 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
13 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
19 * @dataProvider dataBug31100
21 function testBug31100FixSpecialName( $text, $expectedParam ) {
22 $title = Title
::newFromText( $text );
23 $fixed = $title->fixSpecialName();
24 $stuff = explode( '/', $fixed->getDbKey(), 2 );
25 if ( count( $stuff ) == 2 ) {
30 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
33 function dataBug31100() {
35 array( 'Special:Version', null ),
36 array( 'Special:Version/', '' ),
37 array( 'Special:Version/param', 'param' ),
42 * Auth-less test of Title::isValidMoveOperation
45 * @param string $source
46 * @param string $target
47 * @param array|string|true $expected Required error
48 * @dataProvider dataTestIsValidMoveOperation
50 function testIsValidMoveOperation( $source, $target, $expected ) {
51 $title = Title
::newFromText( $source );
52 $nt = Title
::newFromText( $target );
53 $errors = $title->isValidMoveOperation( $nt, false );
54 if ( $expected === true ) {
55 $this->assertTrue( $errors );
57 $errors = $this->flattenErrorsArray( $errors );
58 foreach ( (array)$expected as $error ) {
59 $this->assertContains( $error, $errors );
64 function flattenErrorsArray( $errors ) {
66 foreach ( $errors as $error ) {
67 $result[] = $error[0];
72 function dataTestIsValidMoveOperation() {
74 array( 'Test', 'Test', 'selfmove' ),
75 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
81 * @dataProvider provideCasesForGetpageviewlanguage
83 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
85 global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
86 $save['wgContLang'] = $wgContLang;
87 $save['wgLang'] = $wgLang;
88 $save['wgAllowUserJs'] = $wgAllowUserJs;
89 $save['wgLanguageCode'] = $wgLanguageCode;
90 $save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;
92 // Setup test environnement:
93 $wgContLang = Language
::factory( $contLang );
94 $wgLang = Language
::factory( $lang );
95 # To test out .js titles:
96 $wgAllowUserJs = true;
97 $wgLanguageCode = $contLang;
98 $wgDefaultLanguageVariant = $variant;
100 $title = Title
::newFromText( $titleText );
101 $this->assertInstanceOf( 'Title', $title,
102 "Test must be passed a valid title text, you gave '$titleText'"
104 $this->assertEquals( $expected,
105 $title->getPageViewLanguage()->getCode(),
110 $wgContLang = $save['wgContLang'];
111 $wgLang = $save['wgLang'];
112 $wgAllowUserJs = $save['wgAllowUserJs'];
113 $wgLanguageCode = $save['wgLanguageCode'];
114 $wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
117 function provideCasesForGetpageviewlanguage() {
121 # - wgContLang (expected in most case)
122 # - wgLang (on some specific pages)
123 # - wgDefaultLanguageVariant
126 array( 'fr', 'Main_page', 'fr', 'fr', false ),
127 array( 'es', 'Main_page', 'es', 'zh-tw', false ),
128 array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
130 array( 'es', 'Main_page', 'es', 'zh-tw', 'zh-cn' ),
131 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
132 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
133 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
134 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
135 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
136 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
137 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
139 array( 'zh-cn', 'Main_page', 'zh', 'zh-tw', 'zh-cn' ),
140 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
141 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
142 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
143 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
144 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
145 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
146 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
147 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
148 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
150 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
151 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),