Debian Codegraph @

skins/MinervaNeue/tests/qunit/skins.minerva.scripts/TitleUtil.test.js

raw symbols SCIP
1 ( function () {
2 const TitleUtil = require( 'skins.minerva.scripts/TitleUtil.js' );
3 const mwUriOrg = mw.Uri;
4
5 QUnit.module( 'Minerva TitleUtil', QUnit.newMwEnvironment( {
6 beforeEach: function () {
7 this.mwUriOrg = mw.Uri;
8 mw.Uri = mw.UriRelative( 'https://meta.wikimedia.org/w/index.php' );
9 mw.config.set( {
10 wgArticlePath: '/wiki/$1',
11 wgScriptPath: '/w'
12 } );
13 },
14 afterEach: function () {
15 mw.Uri = mwUriOrg;
16 },
17 // mw.Title relies on these three config vars
18 // Restore them after each test run
19 config: {
20 wgFormattedNamespaces: {
21 '-2': 'Media',
22 '-1': 'Special',
23 0: '',
24 1: 'Talk',
25 2: 'User',
26 3: 'User talk',
27 4: 'Wikipedia',
28 5: 'Wikipedia talk',
29 6: 'File',
30 7: 'File talk',
31 8: 'MediaWiki',
32 9: 'MediaWiki talk',
33 10: 'Template',
34 11: 'Template talk',
35 12: 'Help',
36 13: 'Help talk',
37 14: 'Category',
38 15: 'Category talk',
39 // testing custom / localized namespace
40 100: 'Penguins'
41 },
42 wgNamespaceIds: {
43 /* eslint-disable camelcase */
44 media: -2,
45 special: -1,
46 '': 0,
47 talk: 1,
48 user: 2,
49 user_talk: 3,
50 wikipedia: 4,
51 wikipedia_talk: 5,
52 file: 6,
53 file_talk: 7,
54 mediawiki: 8,
55 mediawiki_talk: 9,
56 template: 10,
57 template_talk: 11,
58 help: 12,
59 help_talk: 13,
60 category: 14,
61 category_talk: 15,
62 image: 6,
63 image_talk: 7,
64 project: 4,
65 project_talk: 5,
66 // Testing custom namespaces and aliases
67 penguins: 100,
68 antarctic_waterfowl: 100
69 /* eslint-enable camelcase */
70 },
71 wgCaseSensitiveNamespaces: []
72 }
73 } ) );
74
75 QUnit.test.each( '.newFromUri() authority', {
76 empty: '',
77 metawiki: 'https://meta.wikimedia.org'
78 }, ( assert, authority ) => {
79 const validateReadOnlyLink = { validateReadOnlyLink: true };
80 assert.strictEqual(
81 TitleUtil.newFromUri( authority + '/w/index.php?title=Title' ).getPrefixedDb(),
82 'Title',
83 'title is in query parameter'
84 );
85 assert.strictEqual(
86 TitleUtil.newFromUri( authority + '/wiki/Title' ).getPrefixedDb(),
87 'Title',
88 'title is in path'
89 );
90 assert.strictEqual(
91 TitleUtil.newFromUri( authority + '/foo/bar/wiki/Title' ),
92 null,
93 'title is not in nonmatching path'
94 );
95 assert.strictEqual(
96 TitleUtil.newFromUri( authority + '/wiki/%E6%B8%AC%E8%A9%A6' ).getPrefixedDb(),
97 '測試',
98 'title is decoded'
99 );
100 assert.strictEqual(
101 TitleUtil.newFromUri( authority + '/wiki/Foo bar' ).getPrefixedDb(),
102 'Foo_bar',
103 'title with space is decoded'
104 );
105 assert.strictEqual(
106 TitleUtil.newFromUri( authority + '/wiki/Foo%20bar' ).getPrefixedDb(),
107 'Foo_bar',
108 'title with encoded space is decoded'
109 );
110 assert.strictEqual(
111 TitleUtil.newFromUri( authority + '/w/index.php?title=Title#fragment' ).getPrefixedDb(),
112 'Title',
113 'fragment is omitted from query title'
114 );
115 assert.strictEqual(
116 TitleUtil.newFromUri( authority + '/wiki/Title#fragment' ).getPrefixedDb(),
117 'Title',
118 'fragment is omitted from path title'
119 );
120 assert.strictEqual(
121 TitleUtil.newFromUri( authority + '/w/index.php?title=Title#fragment' ).getFragment(),
122 'fragment',
123 'fragment is present after query parameter'
124 );
125 assert.strictEqual(
126 TitleUtil.newFromUri( authority + '/wiki/Title#fragment' ).getFragment(),
127 'fragment',
128 'fragment is present after path'
129 );
130 assert.strictEqual(
131 TitleUtil.newFromUri( authority + '/w/index.php?title=Title#foo%20bar' ).getFragment(),
132 'foo bar',
133 'fragment is decoded'
134 );
135
136 assert.strictEqual(
137 TitleUtil.newFromUri( authority + '/w/index.php?title=Title', validateReadOnlyLink ).getPrefixedDb(),
138 'Title',
139 'query title is read-only'
140 );
141 assert.strictEqual(
142 TitleUtil.newFromUri( authority + '/wiki/Title', validateReadOnlyLink ).getPrefixedDb(),
143 'Title',
144 'path title is read-only'
145 );
146 assert.strictEqual(
147 TitleUtil.newFromUri( authority + '/w/index.php?title=Title&oldid=123', validateReadOnlyLink ).getPrefixedDb(),
148 'Title',
149 'query title with revision is read-only'
150 );
151 assert.strictEqual(
152 TitleUtil.newFromUri( authority + '/w/index.php?title=Title&param', validateReadOnlyLink ),
153 null,
154 'query title with unknown parameter is not read-only'
155 );
156 } );
157
158 QUnit.test.each( '.newFromUri() bad input', [
159 '%', null, undefined, '', ' ', '/', {}, '\\', '/wiki/%', '/w/index.php?title=%'
160 ], ( assert, input ) => {
161 assert.strictEqual(
162 TitleUtil.newFromUri( input ),
163 null,
164 'no Title in bad input input="' + input + '"'
165 );
166 } );
167
168 QUnit.test( '.newFromUri() misc', ( assert ) => {
169 // Parameters are passed to Uri's constructor.
170 assert.strictEqual(
171 TitleUtil.newFromUri( { protocol: 'https',
172 host: 'meta.wikimedia.org',
173 path: '/wiki/Title' } ).getPrefixedDb(),
174 'Title',
175 'title is in Uri parameters'
176 );
177
178 // A Uri itself can be passed.
179 assert.strictEqual(
180 TitleUtil.newFromUri( new mw.Uri( 'https://meta.wikimedia.org/wiki/Title' ) ).getPrefixedDb(),
181 'Title',
182 'title is in Uri'
183 );
184
185 // JSDoc examples.
186 // https://meta.wikimedia.org/wiki/Foo → Foo (path title)
187 assert.strictEqual(
188 TitleUtil.newFromUri( 'https://meta.wikimedia.org/wiki/Foo' ).getPrefixedDb(),
189 'Foo',
190 'path title'
191 );
192
193 // http://meta.wikimedia.org/wiki/Foo → Foo (mismatching protocol)
194 assert.strictEqual(
195 TitleUtil.newFromUri( 'http://meta.wikimedia.org/wiki/Foo' ).getPrefixedDb(),
196 'Foo',
197 'mismatching protocol'
198 );
199
200 // /wiki/Foo → Foo (relative URI)
201 assert.strictEqual(
202 TitleUtil.newFromUri( '/wiki/Foo' ).getPrefixedDb(),
203 'Foo',
204 'relative URI'
205 );
206
207 // /w/index.php?title=Foo → Foo (title query parameter)
208 assert.strictEqual(
209 TitleUtil.newFromUri( '/w/index.php?title=Foo' ).getPrefixedDb(),
210 'Foo',
211 'title query parameter'
212 );
213
214 // /wiki/Talk:Foo → Talk:Foo (non-main namespace URI)
215 assert.strictEqual(
216 TitleUtil.newFromUri( '/wiki/Talk:Foo' ).getPrefixedDb(),
217 'Talk:Foo',
218 'non-main namespace URI'
219 );
220
221 // /wiki/Foo bar → Foo_bar (name with spaces)
222 assert.strictEqual(
223 TitleUtil.newFromUri( '/wiki/Foo bar' ).getPrefixedDb(),
224 'Foo_bar',
225 'name with spaces'
226 );
227
228 // /wiki/Foo%20bar → Foo_bar (name with percent encoded spaces)
229 assert.strictEqual(
230 TitleUtil.newFromUri( '/wiki/Foo%20bar' ).getPrefixedDb(),
231 'Foo_bar',
232 'name with percent encoded spaces'
233 );
234
235 // /wiki/Foo+bar → Foo+bar (name with +)
236 assert.strictEqual(
237 TitleUtil.newFromUri( '/wiki/Foo+bar' ).getPrefixedDb(),
238 'Foo+bar',
239 'name with +'
240 );
241
242 // /w/index.php?title=Foo%2bbar → Foo+bar (query parameter with +)
243 assert.strictEqual(
244 TitleUtil.newFromUri( '/w/index.php?title=Foo%2bbar' ).getPrefixedDb(),
245 'Foo+bar',
246 'query parameter with +'
247 );
248
249 // / → null (mismatching article path)
250 assert.strictEqual(
251 TitleUtil.newFromUri( '/' ),
252 null,
253 'mismatching article path'
254 );
255
256 // /wiki/index.php?title=Foo → null (mismatching script path)
257 assert.strictEqual(
258 TitleUtil.newFromUri( '/wiki/index.php?title=Foo' ),
259 null,
260 'mismatching script path'
261 );
262
263 // https://archive.org/ → null (mismatching host)
264 assert.strictEqual(
265 TitleUtil.newFromUri( 'https://archive.org/' ),
266 null,
267 'mismatching host (0)'
268 );
269
270 // https://foo.wikimedia.org/ → null (mismatching host)
271 assert.strictEqual(
272 TitleUtil.newFromUri( 'https://foo.wikimedia.org/' ),
273 null,
274 'mismatching host (1)'
275 );
276
277 // https://en.wikipedia.org/wiki/Bar → null (mismatching host)
278 assert.strictEqual(
279 TitleUtil.newFromUri( 'https://en.wikipedia.org/wiki/Bar' ),
280 null,
281 'mismatching host (2)'
282 );
283 } );
284 }() );