summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/MediaWikiTest.php
blob: e1962436ecd9fc10f1732a81813d8dfabc64defc (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
156
157
<?php

class MediaWikiTest extends MediaWikiTestCase {
	protected function setUp() {
		parent::setUp();

		$this->setMwGlobals( array(
			'wgServer' => 'http://example.org',
			'wgScriptPath' => '/w',
			'wgScript' => '/w/index.php',
			'wgArticlePath' => '/wiki/$1',
			'wgActionPaths' => array(),
		) );
	}

	public static function provideTryNormaliseRedirect() {
		return array(
			array(
				// View: Canonical
				'url' => 'http://example.org/wiki/Foo_Bar',
				'query' => array(),
				'title' => 'Foo_Bar',
				'redirect' => false,
			),
			array(
				// View: Escaped title
				'url' => 'http://example.org/wiki/Foo%20Bar',
				'query' => array(),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// View: Script path
				'url' => 'http://example.org/w/index.php?title=Foo_Bar',
				'query' => array( 'title' => 'Foo_Bar' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// View: Script path with implicit title from page id
				'url' => 'http://example.org/w/index.php?curid=123',
				'query' => array( 'curid' => '123' ),
				'title' => 'Foo_Bar',
				'redirect' => false,
			),
			array(
				// View: Script path with implicit title from revision id
				'url' => 'http://example.org/w/index.php?oldid=123',
				'query' => array( 'oldid' => '123' ),
				'title' => 'Foo_Bar',
				'redirect' => false,
			),
			array(
				// View: Script path without title
				'url' => 'http://example.org/w/index.php',
				'query' => array(),
				'title' => 'Main_Page',
				'redirect' => 'http://example.org/wiki/Main_Page',
			),
			array(
				// View: Script path with empty title
				'url' => 'http://example.org/w/index.php?title=',
				'query' => array( 'title' => '' ),
				'title' => 'Main_Page',
				'redirect' => 'http://example.org/wiki/Main_Page',
			),
			array(
				// View: Index with escaped title
				'url' => 'http://example.org/w/index.php?title=Foo%20Bar',
				'query' => array( 'title' => 'Foo Bar' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// View: Script path with escaped title
				'url' => 'http://example.org/w/?title=Foo_Bar',
				'query' => array( 'title' => 'Foo_Bar' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// View: Root path with escaped title
				'url' => 'http://example.org/?title=Foo_Bar',
				'query' => array( 'title' => 'Foo_Bar' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// View: Canonical with redundant query
				'url' => 'http://example.org/wiki/Foo_Bar?action=view',
				'query' => array( 'action' => 'view' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// Edit: Canonical view url with action query
				'url' => 'http://example.org/wiki/Foo_Bar?action=edit',
				'query' => array( 'action' => 'edit' ),
				'title' => 'Foo_Bar',
				'redirect' => false,
			),
			array(
				// View: Index with action query
				'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=view',
				'query' => array( 'title' => 'Foo_Bar', 'action' => 'view' ),
				'title' => 'Foo_Bar',
				'redirect' => 'http://example.org/wiki/Foo_Bar',
			),
			array(
				// Edit: Index with action query
				'url' => 'http://example.org/w/index.php?title=Foo_Bar&action=edit',
				'query' => array( 'title' => 'Foo_Bar', 'action' => 'edit' ),
				'title' => 'Foo_Bar',
				'redirect' => false,
			),
		);
	}

	/**
	 * @dataProvider provideTryNormaliseRedirect
	 * @covers MediaWiki::tryNormaliseRedirect
	 */
	public function testTryNormaliseRedirect( $url, $query, $title, $expectedRedirect = false ) {
		// Set SERVER because interpolateTitle() doesn't use getRequestURL(),
		// whereas tryNormaliseRedirect does().
		$_SERVER['REQUEST_URI'] = $url;

		$req = new FauxRequest( $query );
		$req->setRequestURL( $url );
		// This adds a virtual 'title' query parameter. Normally called from Setup.php
		$req->interpolateTitle();

		$titleObj = Title::newFromText( $title );

		// Set global context since some involved code paths don't yet have context
		$context = RequestContext::getMain();
		$context->setRequest( $req );
		$context->setTitle( $titleObj );

		$mw = new MediaWiki( $context );

		$method = new ReflectionMethod( $mw, 'tryNormaliseRedirect' );
		$method->setAccessible( true );
		$ret = $method->invoke( $mw, $titleObj );

		$this->assertEquals(
			$expectedRedirect !== false,
			$ret,
			'Return true only when redirecting'
		);

		$this->assertEquals(
			$expectedRedirect ?: '',
			$context->getOutput()->getRedirect()
		);
	}
}