summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/TitleTest.php
blob: f61652df19593e769acace92f30af3bf861b4353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

class TitleTest extends MediaWikiTestCase {

	function testLegalChars() {
		$titlechars = Title::legalChars();

		foreach ( range( 1, 255 ) as $num ) {
			$chr = chr( $num );
			if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
				$this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
			} else {
				$this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
			}
		}
	}

	/**
	 * @dataProvider dataBug31100
	 */
	function testBug31100FixSpecialName( $text, $expectedParam ) {
		$title = Title::newFromText( $text );
		$fixed = $title->fixSpecialName();
		$stuff = explode( '/', $fixed->getDbKey(), 2 );
		if ( count( $stuff ) == 2 ) {
			$par = $stuff[1];
		} else {
			$par = null;
		}
		$this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
	}

	function dataBug31100() {
		return array(
			array( 'Special:Version', null ),
			array( 'Special:Version/', '' ),
			array( 'Special:Version/param', 'param' ),
		);
	}
	
	/**
	 * Auth-less test of Title::isValidMoveOperation
	 * 
	 * @group Database
	 * @param string $source
	 * @param string $target
	 * @param array|string|true $expected Required error
	 * @dataProvider dataTestIsValidMoveOperation
	 */
	function testIsValidMoveOperation( $source, $target, $expected ) {
		$title = Title::newFromText( $source );
		$nt = Title::newFromText( $target );
		$errors = $title->isValidMoveOperation( $nt, false );
		if ( $expected === true ) {
			$this->assertTrue( $errors );
		} else {
			$errors = $this->flattenErrorsArray( $errors );
			foreach ( (array)$expected as $error ) {
				$this->assertContains( $error, $errors );
			}
		}
	}
	
	function flattenErrorsArray( $errors ) {
		$result = array();
		foreach ( $errors as $error ) {
			$result[] = $error[0];
		}
		return $result;
	}
	
	function dataTestIsValidMoveOperation() {
		return array( 
			array( 'Test', 'Test', 'selfmove' ),
			array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
		);
	}
	
	
	/**
	 * @dataProvider provideCasesForGetpageviewlanguage
	 */
	function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
		// Save globals
		global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
		$save['wgContLang']               = $wgContLang;
		$save['wgLang']                   = $wgLang;
		$save['wgAllowUserJs']            = $wgAllowUserJs;
		$save['wgLanguageCode']           = $wgLanguageCode;
		$save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;

		// Setup test environnement:
		$wgContLang = Language::factory( $contLang );
		$wgLang     = Language::factory( $lang );
		# To test out .js titles:
		$wgAllowUserJs = true;
		$wgLanguageCode = $contLang;
		$wgDefaultLanguageVariant = $variant;

		$title = Title::newFromText( $titleText );
		$this->assertInstanceOf( 'Title', $title,
			"Test must be passed a valid title text, you gave '$titleText'"
		);
		$this->assertEquals( $expected,
			$title->getPageViewLanguage()->getCode(),
			$msg
		);

		// Restore globals
		$wgContLang               = $save['wgContLang'];
		$wgLang                   = $save['wgLang'];
		$wgAllowUserJs            = $save['wgAllowUserJs'];
		$wgLanguageCode           = $save['wgLanguageCode'];
		$wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
	}

	function provideCasesForGetpageviewlanguage() {
		# Format:
		# - expected
		# - Title name
		# - wgContLang (expected in most case)
		# - wgLang (on some specific pages)
		# - wgDefaultLanguageVariant
		# - Optional message
		return array(
			array( 'fr', 'Main_page', 'fr', 'fr', false ),
			array( 'es', 'Main_page', 'es', 'zh-tw', false ),
			array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),

			array( 'es',    'Main_page',                 'es', 'zh-tw', 'zh-cn' ),
			array( 'es',    'MediaWiki:About',           'es', 'zh-tw', 'zh-cn' ),
			array( 'es',    'MediaWiki:About/',          'es', 'zh-tw', 'zh-cn' ),
			array( 'de',    'MediaWiki:About/de',        'es', 'zh-tw', 'zh-cn' ),
			array( 'en',    'MediaWiki:Common.js',       'es', 'zh-tw', 'zh-cn' ),
			array( 'en',    'MediaWiki:Common.css',      'es', 'zh-tw', 'zh-cn' ),
			array( 'en',    'User:JohnDoe/Common.js',    'es', 'zh-tw', 'zh-cn' ),
			array( 'en',    'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),

			array( 'zh-cn', 'Main_page',                 'zh', 'zh-tw', 'zh-cn' ),
			array( 'zh',    'MediaWiki:About',           'zh', 'zh-tw', 'zh-cn' ),
			array( 'zh',    'MediaWiki:About/',          'zh', 'zh-tw', 'zh-cn' ),
			array( 'de',    'MediaWiki:About/de',        'zh', 'zh-tw', 'zh-cn' ),
			array( 'zh-cn', 'MediaWiki:About/zh-cn',     'zh', 'zh-tw', 'zh-cn' ),
			array( 'zh-tw', 'MediaWiki:About/zh-tw',     'zh', 'zh-tw', 'zh-cn' ),
			array( 'en',    'MediaWiki:Common.js',       'zh', 'zh-tw', 'zh-cn' ),
			array( 'en',    'MediaWiki:Common.css',      'zh', 'zh-tw', 'zh-cn' ),
			array( 'en',    'User:JohnDoe/Common.js',    'zh', 'zh-tw', 'zh-cn' ),
			array( 'en',    'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),

			array( 'zh-tw', 'Special:NewPages',       'es', 'zh-tw', 'zh-cn' ),
			array( 'zh-tw', 'Special:NewPages',       'zh', 'zh-tw', 'zh-cn' ),

		);
	}
}