Merge "Expand core post edit functionality to match VE"
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfExpandUrlTest.php
1 <?php
2 /**
3 * @covers ::wfExpandUrl
4 */
5 class WfExpandUrlTest extends MediaWikiTestCase {
6 /**
7 * @dataProvider provideExpandableUrls
8 */
9 public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
10 $server, $canServer, $httpsMode, $message
11 ) {
12 // Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
13 $this->setMwGlobals( array(
14 'wgServer' => $server,
15 'wgCanonicalServer' => $canServer,
16 'wgRequest' => new FauxRequest( array(), false, null, $httpsMode ? 'https' : 'http' )
17 ) );
18
19 $this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
20 }
21
22 /**
23 * Provider of URL examples for testing wfExpandUrl()
24 *
25 * @return array
26 */
27 public static function provideExpandableUrls() {
28 $modes = array( 'http', 'https' );
29 $servers = array(
30 'http' => 'http://example.com',
31 'https' => 'https://example.com',
32 'protocol-relative' => '//example.com'
33 );
34 $defaultProtos = array(
35 'http' => PROTO_HTTP,
36 'https' => PROTO_HTTPS,
37 'protocol-relative' => PROTO_RELATIVE,
38 'current' => PROTO_CURRENT,
39 'canonical' => PROTO_CANONICAL
40 );
41
42 $retval = array();
43 foreach ( $modes as $mode ) {
44 $httpsMode = $mode == 'https';
45 foreach ( $servers as $serverDesc => $server ) {
46 foreach ( $modes as $canServerMode ) {
47 $canServer = "$canServerMode://example2.com";
48 foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
49 $retval[] = array(
50 'http://example.com', 'http://example.com',
51 $defaultProto, $server, $canServer, $httpsMode,
52 "Testing fully qualified http URLs (no need to expand) "
53 . "(defaultProto: $protoDesc , wgServer: $server, "
54 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
55 );
56 $retval[] = array(
57 'https://example.com', 'https://example.com',
58 $defaultProto, $server, $canServer, $httpsMode,
59 "Testing fully qualified https URLs (no need to expand) "
60 . "(defaultProto: $protoDesc , wgServer: $server, "
61 . "wgCanonicalServer: $canServer, current request protocol: $mode )"
62 );
63 # Would be nice to support this, see fixme on wfExpandUrl()
64 $retval[] = array(
65 "wiki/FooBar", 'wiki/FooBar',
66 $defaultProto, $server, $canServer, $httpsMode,
67 "Test non-expandable relative URLs (defaultProto: $protoDesc, "
68 . "wgServer: $server, wgCanonicalServer: $canServer, "
69 . "current request protocol: $mode )"
70 );
71
72 // Determine expected protocol
73 if ( $protoDesc == 'protocol-relative' ) {
74 $p = '';
75 } elseif ( $protoDesc == 'current' ) {
76 $p = "$mode:";
77 } elseif ( $protoDesc == 'canonical' ) {
78 $p = "$canServerMode:";
79 } else {
80 $p = $protoDesc . ':';
81 }
82 // Determine expected server name
83 if ( $protoDesc == 'canonical' ) {
84 $srv = $canServer;
85 } elseif ( $serverDesc == 'protocol-relative' ) {
86 $srv = $p . $server;
87 } else {
88 $srv = $server;
89 }
90
91 $retval[] = array(
92 "$p//wikipedia.org", '//wikipedia.org',
93 $defaultProto, $server, $canServer, $httpsMode,
94 "Test protocol-relative URL (defaultProto: $protoDesc, "
95 . "wgServer: $server, wgCanonicalServer: $canServer, "
96 . "current request protocol: $mode )"
97 );
98 $retval[] = array(
99 "$srv/wiki/FooBar",
100 '/wiki/FooBar',
101 $defaultProto,
102 $server,
103 $canServer,
104 $httpsMode,
105 "Testing expanding URL beginning with / (defaultProto: $protoDesc, "
106 . "wgServer: $server, wgCanonicalServer: $canServer, "
107 . "current request protocol: $mode )"
108 );
109 }
110 }
111 }
112 }
113
114 return $retval;
115 }
116 }