summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/GlobalFunctions/wfExpandUrlTest.php
blob: 41230a1ea362027e8f846a3277a3edee4f649c59 (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
<?php
/**
 * @covers ::wfExpandUrl
 */
class WfExpandUrlTest extends MediaWikiTestCase {
	/**
	 * @dataProvider provideExpandableUrls
	 */
	public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto, $server, $canServer, $httpsMode, $message ) {
		// Fake $wgServer and $wgCanonicalServer
		$this->setMwGlobals( array(
			'wgServer' => $server,
			'wgCanonicalServer' => $canServer,
		) );

		// Fake $_SERVER['HTTPS'] if needed
		if ( $httpsMode ) {
			$_SERVER['HTTPS'] = 'on';
		} else {
			unset( $_SERVER['HTTPS'] );
		}

		$this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
	}

	/**
	 * Provider of URL examples for testing wfExpandUrl()
	 *
	 * @return array
	 */
	public static function provideExpandableUrls() {
		$modes = array( 'http', 'https' );
		$servers = array(
			'http' => 'http://example.com',
			'https' => 'https://example.com',
			'protocol-relative' => '//example.com'
		);
		$defaultProtos = array(
			'http' => PROTO_HTTP,
			'https' => PROTO_HTTPS,
			'protocol-relative' => PROTO_RELATIVE,
			'current' => PROTO_CURRENT,
			'canonical' => PROTO_CANONICAL
		);

		$retval = array();
		foreach ( $modes as $mode ) {
			$httpsMode = $mode == 'https';
			foreach ( $servers as $serverDesc => $server ) {
				foreach ( $modes as $canServerMode ) {
					$canServer = "$canServerMode://example2.com";
					foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
						$retval[] = array(
							'http://example.com', 'http://example.com',
							$defaultProto, $server, $canServer, $httpsMode,
							"Testing fully qualified http URLs (no need to expand) ' .
							'(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
						);
						$retval[] = array(
							'https://example.com', 'https://example.com',
							$defaultProto, $server, $canServer, $httpsMode,
							"Testing fully qualified https URLs (no need to expand) ' .
							'(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
						);
						# Would be nice to support this, see fixme on wfExpandUrl()
						$retval[] = array(
							"wiki/FooBar", 'wiki/FooBar',
							$defaultProto, $server, $canServer, $httpsMode,
							"Test non-expandable relative URLs ' .
							'(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
						);

						// Determine expected protocol
						if ( $protoDesc == 'protocol-relative' ) {
							$p = '';
						} elseif ( $protoDesc == 'current' ) {
							$p = "$mode:";
						} elseif ( $protoDesc == 'canonical' ) {
							$p = "$canServerMode:";
						} else {
							$p = $protoDesc . ':';
						}
						// Determine expected server name
						if ( $protoDesc == 'canonical' ) {
							$srv = $canServer;
						} elseif ( $serverDesc == 'protocol-relative' ) {
							$srv = $p . $server;
						} else {
							$srv = $server;
						}

						$retval[] = array(
							"$p//wikipedia.org", '//wikipedia.org',
							$defaultProto, $server, $canServer, $httpsMode,
							"Test protocol-relative URL ' .
							'(defaultProto: $protoDesc, wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
						);
						$retval[] = array(
							"$srv/wiki/FooBar", '/wiki/FooBar',
							$defaultProto, $server, $canServer, $httpsMode,
							"Testing expanding URL beginning with / ' .
							'(defaultProto: $protoDesc , wgServer: $server, wgCanonicalServer: $canServer, current request protocol: $mode )"
						);
					}
				}
			}
		}

		return $retval;
	}
}