File: ve.dm.MWInternalLinkAnnotation.test.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (240 lines) | stat: -rw-r--r-- 6,399 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*!
 * VisualEditor DataModel MWInternalLinkAnnotation tests.
 *
 * @copyright See AUTHORS.txt
 */

QUnit.module( 've.dm.MWInternalLinkAnnotation', ve.test.utils.newMwEnvironment() );

QUnit.test( 'toDataElement', ( assert ) => {
	// The expected data depends on site configuration, so we need to generate the cases several times.
	const getCases = () => {
		const
			externalLink = ( href ) => {
				const link = document.createElement( 'a' );
				link.setAttribute( 'href', href );
				return link;
			},
			internalLink = ( pageTitle, params ) => {
				const link = document.createElement( 'a' );
				link.setAttribute( 'href', mw.Title.newFromText( pageTitle ).getUrl( params ) );
				return link;
			},
			parsoidLink = ( href ) => {
				const link = document.createElement( 'a' );
				if ( mw.config.get( 'wgArticlePath' ).includes( '?' ) ) {
					href = href.replace( './', './index.php?title=' );
				}
				link.setAttribute( 'href', href );
				return link;
			};
		return [
			{
				msg: 'Not an internal link',
				element: externalLink( 'http://example.com/' ),
				expected: {
					type: 'link/mwExternal',
					attributes: {
						href: 'http://example.com/'
					}
				}
			},
			{
				msg: 'Simple',
				element: internalLink( 'Foo' ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo',
						normalizedTitle: 'Foo',
						title: 'Foo'
					}
				}
			},
			{
				msg: 'Relative path',
				element: parsoidLink( './Foo' ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo',
						normalizedTitle: 'Foo',
						title: 'Foo'
					}
				}
			},
			{
				msg: 'History link',
				element: internalLink( 'Foo', { action: 'history' } ),
				expected: {
					type: 'link/mwExternal',
					attributes: {
						href: mw.Title.newFromText( 'Foo' ).getUrl( { action: 'history' } )
					}
				}
			},
			{
				msg: 'Diff link',
				element: internalLink( 'Foo', { diff: '3', oldid: '2' } ),
				expected: {
					type: 'link/mwExternal',
					attributes: {
						href: mw.Title.newFromText( 'Foo' ).getUrl( { diff: '3', oldid: '2' } )
					}
				}
			},
			{
				msg: 'Red link',
				element: internalLink( 'Foo', { action: 'edit', redlink: '1' } ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo',
						normalizedTitle: 'Foo',
						title: 'Foo'
					}
				}
			},
			{
				// Because percent-encoded URLs aren't valid titles, but what they decode to might be
				msg: 'Percent encoded characters',
				element: internalLink( 'Foo?' ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo?',
						normalizedTitle: 'Foo?',
						title: 'Foo?'
					}
				}
			},
			{
				// The fragment should make it into some parts of this, and not others
				msg: 'Fragments',
				element: internalLink( 'Foo#bar' ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo',
						normalizedTitle: 'Foo#bar',
						title: 'Foo#bar'
					}
				}
			},
			{
				// Question marks in the fragment shouldn't confuse this
				msg: 'Question marks in fragments',
				element: internalLink( 'Foo#bar?' ),
				expected: {
					type: 'link/mwInternal',
					attributes: {
						lookupTitle: 'Foo',
						normalizedTitle: 'Foo#' + mw.util.escapeIdForLink( 'bar?' ),
						title: 'Foo#' + mw.util.escapeIdForLink( 'bar?' )
					}
				}
			}
		];
	};

	const converter = new ve.dm.Converter( ve.dm.modelRegistry, ve.dm.nodeFactory, ve.dm.annotationFactory, ve.dm.metaItemFactory );

	const articlePaths = [
		{
			// MediaWiki config settings:
			// $wgScriptPath = '/w';
			// $wgUsePathInfo = false;
			msg: 'query string URL',
			config: {
				wgServer: 'http://example.org',
				wgScript: '/w/index.php',
				wgArticlePath: '/w/index.php?title=$1'
			},
			// Parsoid-generated <base href> given these MediaWiki config settings:
			base: 'http://example.org/w/'
		},
		{
			// MediaWiki config settings:
			// $wgScriptPath = '/w';
			// $wgUsePathInfo = true;
			msg: 'short URL using pathinfo',
			config: {
				wgServer: 'http://example.org',
				wgScript: '/w/index.php',
				wgArticlePath: '/w/index.php/$1'
			},
			// Parsoid-generated <base href> given these MediaWiki config settings:
			base: 'http://example.org/w/index.php/'
		},
		{
			// MediaWiki config settings:
			// $wgScriptPath = '/w';
			// $wgArticlePath = '/wiki/$1';
			msg: 'proper short URL',
			config: {
				wgServer: 'http://example.org',
				wgScript: '/w/index.php',
				wgArticlePath: '/wiki/$1'
			},
			// Parsoid-generated <base href> given these MediaWiki config settings:
			base: 'http://example.org/wiki/'
		}
	];

	for ( const pathData of articlePaths ) {
		// Set up global state (site configuration)
		mw.config.set( pathData.config );

		const doc = ve.dm.mwExample.createExampleDocumentFromData( [], null, pathData.base );
		// toDataElement is called during a converter run, so we need to fake up a bit of state to test it.
		// This would normally be done by ve.dm.converter.getModelFromDom.
		converter.doc = doc.getHtmlDocument();
		converter.targetDoc = doc.getHtmlDocument();
		converter.store = doc.getStore();
		converter.internalList = doc.getInternalList();
		converter.contextStack = [];
		converter.fromClipboard = true;

		// Generate test cases for this site configuration
		for ( const caseItem of getCases() ) {
			assert.deepEqual(
				ve.dm.MWInternalLinkAnnotation.static.toDataElement( [ caseItem.element ], converter ),
				caseItem.expected,
				caseItem.msg + ': ' + pathData.msg
			);
		}
	}
} );

QUnit.test.each( 'getFragment', {
	'No fragment returns null': {
		original: 'Foo',
		expected: null
	},
	'Invalid title returns null': {
		original: 'A%20B',
		expected: null
	},
	'Blank fragment returns empty string': {
		original: 'Foo#',
		expected: ''
	},
	'Extant fragment returns same string': {
		original: 'Foo#bar',
		expected: 'bar'
	},
	'Hash-bang works returns full string': {
		original: 'Foo#!bar',
		expected: '!bar'
	},
	'Double-hash returns everything after the first hash': {
		original: 'Foo##bar',
		expected: '#bar'
	},
	'Multi-fragment returns everything after the first hash': {
		original: 'Foo#bar#baz#bat',
		expected: 'bar#baz#bat'
	}
}, ( assert, { original, expected } ) => {
	assert.strictEqual( ve.dm.MWInternalLinkAnnotation.static.getFragment( original ), expected );
} );