Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / http / HttpAcceptNegotiatorTest.php
1 <?php
2
3 use Wikimedia\Http\HttpAcceptNegotiator;
4
5 /**
6 * @covers Wikimedia\Http\HttpAcceptNegotiator
7 *
8 * @author Daniel Kinzler
9 */
10 class HttpAcceptNegotiatorTest extends \PHPUnit\Framework\TestCase {
11
12 public function provideGetFirstSupportedValue() {
13 return [
14 [ // #0: empty
15 [], // supported
16 [], // accepted
17 null, // default
18 null, // expected
19 ],
20 [ // #1: simple
21 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
22 [ 'text/xzy', 'text/bar' ], // accepted
23 null, // default
24 'text/BAR', // expected
25 ],
26 [ // #2: default
27 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
28 [ 'text/xzy', 'text/xoo' ], // accepted
29 'X', // default
30 'X', // expected
31 ],
32 [ // #3: preference
33 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
34 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
35 null, // default
36 'text/bar', // expected
37 ],
38 [ // #4: * wildcard
39 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
40 [ 'text/xoo', '*' ], // accepted
41 null, // default
42 'text/foo', // expected
43 ],
44 [ // #5: */* wildcard
45 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
46 [ 'text/xoo', '*/*' ], // accepted
47 null, // default
48 'text/foo', // expected
49 ],
50 [ // #6: text/* wildcard
51 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
52 [ 'application/*', 'text/foo' ], // accepted
53 null, // default
54 'application/zuul', // expected
55 ],
56 ];
57 }
58
59 /**
60 * @dataProvider provideGetFirstSupportedValue
61 */
62 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
63 $negotiator = new HttpAcceptNegotiator( $supported );
64 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
65
66 $this->assertEquals( $expected, $actual );
67 }
68
69 public function provideGetBestSupportedKey() {
70 return [
71 [ // #0: empty
72 [], // supported
73 [], // accepted
74 null, // default
75 null, // expected
76 ],
77 [ // #1: simple
78 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
79 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
80 null, // default
81 'text/BAR', // expected
82 ],
83 [ // #2: default
84 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
85 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
86 'X', // default
87 'X', // expected
88 ],
89 [ // #3: weighted
90 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
91 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
92 null, // default
93 'text/BAR', // expected
94 ],
95 [ // #4: zero weight
96 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
97 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
98 null, // default
99 null, // expected
100 ],
101 [ // #5: * wildcard
102 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
103 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
104 null, // default
105 'text/foo', // expected
106 ],
107 [ // #6: */* wildcard
108 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
109 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
110 null, // default
111 'text/foo', // expected
112 ],
113 [ // #7: text/* wildcard
114 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
115 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
116 null, // default
117 'application/zuul', // expected
118 ],
119 [ // #8: Test specific format preferred over wildcard (T133314)
120 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
121 [ '*/*' => 1, 'text/html' => 1 ], // accepted
122 null, // default
123 'text/html', // expected
124 ],
125 [ // #9: Test specific format preferred over range (T133314)
126 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
127 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
128 null, // default
129 'text/html', // expected
130 ],
131 [ // #10: Test range preferred over wildcard (T133314)
132 [ 'application/rdf+xml', 'text/html' ], // supported
133 [ '*/*' => 1, 'text/*' => 1 ], // accepted
134 null, // default
135 'text/html', // expected
136 ],
137 ];
138 }
139
140 /**
141 * @dataProvider provideGetBestSupportedKey
142 */
143 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
144 $negotiator = new HttpAcceptNegotiator( $supported );
145 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
146
147 $this->assertEquals( $expected, $actual );
148 }
149
150 }