Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / WikiReferenceTest.php
1 <?php
2
3 /**
4 * @covers WikiReference
5 */
6 class WikiReferenceTest extends MediaWikiUnitTestCase {
7
8 public function provideGetDisplayName() {
9 return [
10 'http' => [ 'foo.bar', 'http://foo.bar' ],
11 'https' => [ 'foo.bar', 'http://foo.bar' ],
12
13 // apparently, this is the expected behavior
14 'invalid' => [ 'purple kittens', 'purple kittens' ],
15 ];
16 }
17
18 /**
19 * @dataProvider provideGetDisplayName
20 */
21 public function testGetDisplayName( $expected, $canonicalServer ) {
22 $reference = new WikiReference( $canonicalServer, '/wiki/$1' );
23 $this->assertEquals( $expected, $reference->getDisplayName() );
24 }
25
26 public function testGetCanonicalServer() {
27 $reference = new WikiReference( 'https://acme.com', '/wiki/$1', '//acme.com' );
28 $this->assertEquals( 'https://acme.com', $reference->getCanonicalServer() );
29 }
30
31 public function provideGetCanonicalUrl() {
32 return [
33 'no fragment' => [
34 'https://acme.com/wiki/Foo',
35 'https://acme.com',
36 '//acme.com',
37 '/wiki/$1',
38 'Foo',
39 null
40 ],
41 'empty fragment' => [
42 'https://acme.com/wiki/Foo',
43 'https://acme.com',
44 '//acme.com',
45 '/wiki/$1',
46 'Foo',
47 ''
48 ],
49 'fragment' => [
50 'https://acme.com/wiki/Foo#Bar',
51 'https://acme.com',
52 '//acme.com',
53 '/wiki/$1',
54 'Foo',
55 'Bar'
56 ],
57 'double fragment' => [
58 'https://acme.com/wiki/Foo#Bar%23Xus',
59 'https://acme.com',
60 '//acme.com',
61 '/wiki/$1',
62 'Foo',
63 'Bar#Xus'
64 ],
65 'escaped fragment' => [
66 'https://acme.com/wiki/Foo%23Bar',
67 'https://acme.com',
68 '//acme.com',
69 '/wiki/$1',
70 'Foo#Bar',
71 null
72 ],
73 'empty path' => [
74 'https://acme.com/Foo',
75 'https://acme.com',
76 '//acme.com',
77 '/$1',
78 'Foo',
79 null
80 ],
81 ];
82 }
83
84 /**
85 * @dataProvider provideGetCanonicalUrl
86 */
87 public function testGetCanonicalUrl(
88 $expected, $canonicalServer, $server, $path, $page, $fragmentId
89 ) {
90 $reference = new WikiReference( $canonicalServer, $path, $server );
91 $this->assertEquals( $expected, $reference->getCanonicalUrl( $page, $fragmentId ) );
92 }
93
94 /**
95 * @dataProvider provideGetCanonicalUrl
96 * @note getUrl is an alias for getCanonicalUrl
97 */
98 public function testGetUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
99 $reference = new WikiReference( $canonicalServer, $path, $server );
100 $this->assertEquals( $expected, $reference->getUrl( $page, $fragmentId ) );
101 }
102
103 public function provideGetFullUrl() {
104 return [
105 'no fragment' => [
106 '//acme.com/wiki/Foo',
107 'https://acme.com',
108 '//acme.com',
109 '/wiki/$1',
110 'Foo',
111 null
112 ],
113 'empty fragment' => [
114 '//acme.com/wiki/Foo',
115 'https://acme.com',
116 '//acme.com',
117 '/wiki/$1',
118 'Foo',
119 ''
120 ],
121 'fragment' => [
122 '//acme.com/wiki/Foo#Bar',
123 'https://acme.com',
124 '//acme.com',
125 '/wiki/$1',
126 'Foo',
127 'Bar'
128 ],
129 'double fragment' => [
130 '//acme.com/wiki/Foo#Bar%23Xus',
131 'https://acme.com',
132 '//acme.com',
133 '/wiki/$1',
134 'Foo',
135 'Bar#Xus'
136 ],
137 'escaped fragment' => [
138 '//acme.com/wiki/Foo%23Bar',
139 'https://acme.com',
140 '//acme.com',
141 '/wiki/$1',
142 'Foo#Bar',
143 null
144 ],
145 'empty path' => [
146 '//acme.com/Foo',
147 'https://acme.com',
148 '//acme.com',
149 '/$1',
150 'Foo',
151 null
152 ],
153 ];
154 }
155
156 /**
157 * @dataProvider provideGetFullUrl
158 */
159 public function testGetFullUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
160 $reference = new WikiReference( $canonicalServer, $path, $server );
161 $this->assertEquals( $expected, $reference->getFullUrl( $page, $fragmentId ) );
162 }
163
164 }