Debian Codegraph @

extensions/TemplateData/tests/qunit/ext.templateData.tests.js

raw symbols SCIP
1 /**
2 * TemplateData Generator GUI Unit Tests
3 */
4 'use strict';
5
6 const DataModule = require( 'ext.templateDataGenerator.data' ),
7 Model = DataModule.Model,
8 SourceHandler = DataModule.SourceHandler;
9
10 QUnit.module( 'ext.templateData', QUnit.newMwEnvironment() );
11
12 const resultDescCurrLang = {};
13 const resultDescMockLang = {};
14 const currLanguage = mw.config.get( 'wgContentLanguage' ) || 'en';
15 const originalWikitext = 'Some text here that is not templatedata information.' +
16 '<templatedata>' +
17 '{' +
18 '"description": {\n' +
19 '"' + currLanguage + '": "Label unsigned comments in a conversation.",\n' +
20 '"blah": "Template description in some blah language."\n' +
21 '},' +
22 '"params": {' +
23 '"user": {' +
24 '"label": "Username",' +
25 '"type": "wiki-user-name",' +
26 '"required": true,' +
27 '"description": "User name of person who forgot to sign their comment.",' +
28 '"aliases": ["1"]' +
29 '},' +
30 '"date": {' +
31 '"label": "Date",' +
32 '"description": {' +
33 '"' + currLanguage + '": "Timestamp of when the comment was posted, in YYYY-MM-DD format."' +
34 '},' +
35 '"aliases": ["2"],' +
36 '"autovalue": "{{subst:CURRENTMONTHNAME}}",' +
37 '"suggested": true' +
38 '},' +
39 '"year": {' +
40 '"label": "Year",' +
41 '"type": "number"' +
42 '},' +
43 '"month": {' +
44 '"label": "Month",' +
45 '"inherits": "year"' +
46 '},' +
47 '"day": {' +
48 '"label": "Day",' +
49 '"inherits": "year"' +
50 '},' +
51 '"comment": {' +
52 '"required": false' +
53 '}' +
54 '},' +
55 '"sets": [' +
56 '{' +
57 '"label": "Date",' +
58 '"params": ["year", "month", "day"]' +
59 '}' +
60 ']' +
61 '}' +
62 '</templatedata>' +
63 'Trailing text at the end.';
64
65 // Prepare description language objects
66 resultDescCurrLang[ currLanguage ] = 'Some string here in ' + currLanguage + ' language.';
67 resultDescMockLang.blah = 'Some string here in blah language.';
68 const resultDescBothLang = Object.assign( {}, resultDescCurrLang, resultDescMockLang );
69 const finalJsonParams = {
70 user: {
71 label: 'Username',
72 type: 'wiki-user-name',
73 required: true,
74 description: 'User name of person who forgot to sign their comment.',
75 aliases: [ '1' ]
76 },
77 date: {
78 label: 'Date',
79 description: {
80 // currLanguage goes here
81 },
82 aliases: [ '2' ],
83 autovalue: '{{subst:CURRENTMONTHNAME}}',
84 suggested: true,
85 type: undefined
86 },
87 year: {
88 label: 'Year',
89 type: 'number'
90 },
91 month: {
92 label: 'Month',
93 inherits: 'year',
94 type: undefined
95 },
96 comment: {
97 required: false,
98 type: 'wiki-page-name'
99 },
100 newParam1: {
101 description: {
102 blah: 'Some string here in blah language.'
103 },
104 required: true,
105 type: undefined
106 },
107 newParam2: {
108 description: undefined,
109 type: undefined
110 },
111 newParam3: {
112 description: 'Some string here in ' + currLanguage + ' language.',
113 deprecated: 'This is deprecated.',
114 type: undefined
115 },
116 newParam4: {
117 description: {
118 // currLanguage goes here
119 blah: resultDescBothLang.blah
120 },
121 type: undefined
122 },
123 newParam5: {
124 type: 'wiki-page-name'
125 }
126 };
127 finalJsonParams.date.description[ currLanguage ] = 'Timestamp of when the comment was posted, in YYYY-MM-DD format.';
128 finalJsonParams.newParam1.description[ currLanguage ] = 'Some string here in ' + currLanguage + ' language.';
129 finalJsonParams.newParam4.description[ currLanguage ] = resultDescBothLang[ currLanguage ];
130
131 const finalJson = {
132 description: {
133 blah: 'Template description in some blah language.'
134 },
135 params: finalJsonParams,
136 sets: [
137 {
138 label: 'Date',
139 params: [
140 'year',
141 'month',
142 'day'
143 ]
144 }
145 ],
146 format: 'inline'
147 };
148 finalJson.description[ currLanguage ] = 'Label unsigned comments in a conversation.';
149
150 // Test validation tools
151 QUnit.test( 'Validation tools', ( assert ) => {
152 const tests = {
153 compare: [
154 {
155 obj1: null,
156 obj2: undefined,
157 result: false,
158 msg: 'Compare: null vs undefined'
159 },
160 {
161 obj1: 'string',
162 obj2: undefined,
163 result: false,
164 msg: 'Compare: string vs undefined'
165 },
166 {
167 obj1: undefined,
168 obj2: undefined,
169 result: true,
170 msg: 'Compare: undefined vs undefined'
171 },
172 {
173 obj1: null,
174 obj2: null,
175 result: true,
176 msg: 'Compare: null vs null'
177 },
178 {
179 obj1: 'A proper string.',
180 obj2: 'A proper string.',
181 result: true,
182 msg: 'Compare: strings'
183 },
184 {
185 obj1: true,
186 obj2: true,
187 result: true,
188 msg: 'Compare: booleans'
189 },
190 {
191 obj1: { 1: 'string', 2: 'another', 4: 'and another' },
192 obj2: { 1: 'string', 2: 'another', 4: 'and another' },
193 result: true,
194 allowSubset: true,
195 msg: 'Compare: plain object full equality'
196 },
197 {
198 obj1: { 1: 'string', 2: 'another', 4: 'and another' },
199 obj2: { 1: 'another', 2: 'and another', 4: 'string' },
200 result: false,
201 allowSubset: true,
202 msg: 'Compare: plain object full inequality'
203 },
204 {
205 obj1: { 1: 'string', 2: 'another', 4: 'and another' },
206 obj2: { 4: 'and another' },
207 result: true,
208 allowSubset: true,
209 msg: 'Compare: plain object subset equality'
210 },
211 {
212 obj1: [ 'val1', 'val2', 'val3' ],
213 obj2: [ 'val1', 'val2', 'val3' ],
214 result: true,
215 msg: 'Compare: arrays'
216 },
217 {
218 obj1: [ 'val1', 'val2', 'val3' ],
219 obj2: [ 'val1' ],
220 result: true,
221 allowSubset: true,
222 msg: 'Compare: array subset: true'
223 },
224 {
225 obj1: [ 'val1', 'val2', 'val3' ],
226 obj2: [ 'val1' ],
227 result: false,
228 allowSubset: false,
229 msg: 'Compare: array subset: false'
230 },
231 {
232 obj1: {
233 param1: {
234 type: 'unknown',
235 aliases: [ 'alias2', 'alias1', 'alias3' ],
236 description: 'Some description',
237 required: true,
238 suggested: false
239 },
240 param2: {
241 required: true
242 }
243 },
244 obj2: {
245 param1: {
246 type: 'unknown',
247 aliases: [ 'alias2', 'alias1', 'alias3' ],
248 description: 'Some description',
249 required: true,
250 suggested: false
251 },
252 param2: {
253 required: true
254 }
255 },
256 result: true,
257 allowSubset: true,
258 msg: 'Compare: complex objects'
259 },
260 {
261 obj1: {
262 param1: {
263 type: 'unknown',
264 aliases: [ 'alias1', 'alias2', 'alias3' ],
265 description: 'Some description',
266 required: true,
267 suggested: false
268 },
269 param2: {
270 required: true
271 }
272 },
273 obj2: {
274 param1: {
275 aliases: [ 'alias1', 'alias2', 'alias3' ],
276 suggested: false
277 }
278 },
279 result: true,
280 allowSubset: true,
281 msg: 'Compare: complex objects subset'
282 }
283 ],
284 splitAndTrimArray: [
285 {
286 string: 'str1 , str2 ',
287 delim: ',',
288 result: [ 'str1', 'str2' ],
289 msg: 'splitAndTrimArray: split and trim'
290 },
291 {
292 string: 'str1, str2, , , , str3',
293 delim: ',',
294 result: [ 'str1', 'str2', 'str3' ],
295 msg: 'splitAndTrimArray: remove empty values'
296 },
297 {
298 string: 'str1|str2|str3',
299 delim: '|',
300 result: [ 'str1', 'str2', 'str3' ],
301 msg: 'splitAndTrimArray: different delimeter'
302 }
303 ],
304 arrayUnionWithoutEmpty: [
305 {
306 arrays: [ [ 'en', 'he', '' ], [ 'he', 'de', '' ], [ 'en', 'de' ] ],
307 result: [ 'en', 'he', 'de' ],
308 msg: 'arrayUnionWithoutEmpty: Remove duplications'
309 },
310 {
311 arrays: [ [ 'en', '', '' ], [ 'he', '', '' ], [ 'de', '' ] ],
312 result: [ 'en', 'he', 'de' ],
313 msg: 'arrayUnionWithoutEmpty: Remove empty values'
314 }
315 ],
316 props: {
317 all: [
318 'name',
319 'aliases',
320 'label',
321 'description',
322 'example',
323 'type',
324 'suggestedvalues',
325 'default',
326 'autovalue',
327 'status',
328 'deprecated',
329 'deprecatedValue',
330 'required',
331 'suggested'
332 ],
333 language: [
334 'label',
335 'description',
336 'example',
337 'default'
338 ]
339 }
340 };
341
342 // Compare
343 for ( let i = 0; i < tests.compare.length; i++ ) {
344 const testVars = tests.compare[ i ];
345 assert.strictEqual(
346 Model.static.compare( testVars.obj1, testVars.obj2, testVars.allowSubset ),
347 testVars.result,
348 testVars.msg
349 );
350 }
351
352 // Split and trim
353 for ( let i = 0; i < tests.splitAndTrimArray.length; i++ ) {
354 const testVars = tests.splitAndTrimArray[ i ];
355 assert.deepEqual(
356 Model.static.splitAndTrimArray( testVars.string, testVars.delim ),
357 testVars.result,
358 testVars.msg
359 );
360 }
361
362 // arrayUnionWithoutEmpty
363 for ( let i = 0; i < tests.arrayUnionWithoutEmpty.length; i++ ) {
364 const testVars = tests.arrayUnionWithoutEmpty[ i ];
365 assert.deepEqual(
366 Model.static.arrayUnionWithoutEmpty.apply( testVars, testVars.arrays ),
367 testVars.result,
368 testVars.msg
369 );
370 }
371
372 // Props
373 assert.deepEqual(
374 Model.static.getAllProperties( false ),
375 tests.props.all,
376 'All properties'
377 );
378 assert.deepEqual(
379 Model.static.getPropertiesWithLanguage(),
380 tests.props.language,
381 'Language properties'
382 );
383 } );
384
385 // Test model load
386 QUnit.test( 'TemplateData model', ( assert ) => {
387 const sourceHandler = new SourceHandler(),
388 paramAddTest = [
389 {
390 key: 'newParam1',
391 data: { required: true },
392 result: { name: 'newParam1', required: true },
393 description: '',
394 msg: 'Adding a simple parameter.'
395 },
396 {
397 key: 'newParam2',
398 data: null,
399 result: { name: 'newParam2' },
400 description: '',
401 msg: 'Adding new parameter without data.'
402 },
403 {
404 key: 'newParam3',
405 data: { description: 'Some string here in ' + currLanguage + ' language.' },
406 result: { name: 'newParam3', description: resultDescCurrLang },
407 description: 'Some string here in ' + currLanguage + ' language.',
408 msg: 'Adding parameter with language prop: original without language.'
409 },
410 {
411 key: 'newParam4',
412 data: {
413 description: resultDescBothLang
414 },
415 result: { name: 'newParam4', description: resultDescBothLang },
416 description: 'Some string here in ' + currLanguage + ' language.',
417 msg: 'Adding parameter with language prop: original with multiple languages.'
418 },
419 {
420 key: 'newParam5',
421 data: { type: 'string/wiki-page-name' },
422 result: { name: 'newParam5', type: 'wiki-page-name' },
423 description: '',
424 msg: 'Adding parameter with obsolete type'
425 }
426 ],
427 paramChangeTest = [
428 {
429 key: 'newParam1',
430 property: 'description',
431 language: currLanguage,
432 value: resultDescCurrLang[ currLanguage ],
433 result: Object.assign( {}, paramAddTest[ 0 ].result, {
434 description: resultDescCurrLang
435 } ),
436 msg: 'Adding description in current language.'
437 },
438 {
439 key: 'newParam1',
440 property: 'description',
441 language: 'blah',
442 value: resultDescMockLang.blah,
443 result: Object.assign( {}, paramAddTest[ 0 ].result, {
444 description: Object.assign( {}, resultDescCurrLang, resultDescMockLang )
445 } ),
446 msg: 'Adding description in mock language.'
447 },
448 {
449 key: 'comment',
450 property: 'type',
451 value: 'wiki-page-name',
452 result: {
453 name: 'comment',
454 type: 'wiki-page-name',
455 required: false
456 },
457 msg: 'Adding type to comment.'
458 },
459 {
460 key: 'newParam2',
461 property: 'description',
462 language: 'blah',
463 value: '',
464 result: Object.assign( {}, paramAddTest[ 1 ].result, {
465 description: { blah: '' }
466 } ),
467 msg: 'Adding empty description in mock language.'
468 },
469 {
470 key: 'newParam3',
471 property: 'deprecated',
472 value: true,
473 result: Object.assign( {}, paramAddTest[ 2 ].result, {
474 deprecated: true
475 } ),
476 msg: 'Adding deprecated property (boolean).'
477 },
478 {
479 key: 'newParam3',
480 property: 'deprecatedValue',
481 value: 'This is deprecated.',
482 result: Object.assign( {}, paramAddTest[ 2 ].result, {
483 deprecated: true,
484 deprecatedValue: 'This is deprecated.'
485 } ),
486 msg: 'Adding deprecated property (string).'
487 }
488 ];
489
490 return sourceHandler.buildModel( originalWikitext )
491 .done( ( model ) => {
492 // Check description
493 assert.strictEqual(
494 model.getTemplateDescription(),
495 'Label unsigned comments in a conversation.',
496 'Description in default language.'
497 );
498 assert.strictEqual(
499 model.getTemplateDescription( 'blah' ),
500 'Template description in some blah language.',
501 'Description in mock language.'
502 );
503
504 // Check parameters
505 assert.deepEqual(
506 Object.keys( model.getParams() ),
507 [ 'user', 'date', 'year', 'month', 'day', 'comment' ],
508 'Parameters retention.'
509 );
510
511 for ( let i = 0; i < paramAddTest.length; i++ ) {
512 // Add parameter
513 model.addParam( paramAddTest[ i ].key, paramAddTest[ i ].data );
514
515 // Test new param data
516 assert.deepEqual(
517 model.getParamData( paramAddTest[ i ].key ),
518 paramAddTest[ i ].result,
519 paramAddTest[ i ].msg + ' (parameter data)'
520 );
521
522 // Check description in current language
523 assert.strictEqual(
524 model.getParamValue( paramAddTest[ i ].key, 'description', currLanguage ),
525 paramAddTest[ i ].description,
526 paramAddTest[ i ].msg + ' (description in current language)'
527 );
528 }
529
530 // Change parameter properties
531 for ( let i = 0; i < paramChangeTest.length; i++ ) {
532 const testVars = paramChangeTest[ i ];
533 model.setParamProperty( testVars.key, testVars.property, testVars.value, testVars.language );
534 assert.deepEqual(
535 model.getParamData( testVars.key ),
536 paramChangeTest[ i ].result,
537 paramChangeTest[ i ].msg
538 );
539 }
540
541 // Delete parameter
542 model.deleteParam( 'day' );
543
544 // Format checks
545 assert.deepEqual(
546 model.getTemplateFormat(),
547 null,
548 'Initial template format (unspecified)'
549 );
550 [
551 '\n{{_ |\n__ = __}}',
552 '{{_|_=_\n}}\n',
553 '{{_|_=_}}', // No newlines
554 '\n{{ __ \n|\n __ = __\n }}\n', // Max newlines/ws
555 null,
556 'block',
557 'inline'
558 ].forEach( ( f ) => {
559 model.setTemplateFormat( f );
560 assert.deepEqual(
561 model.getTemplateFormat(),
562 f,
563 'Set template format to ' + JSON.stringify( f )
564 );
565 } );
566
567 // Ouput a final templatedata
568 assert.deepEqual(
569 model.outputTemplateData(),
570 finalJson,
571 'Final templatedata output'
572 );
573
574 // Move 'user' to offset 3 (in original array), i.e. after 'year'
575 model.reorderParamOrderKey( 'user', 3 );
576 assert.deepEqual(
577 model.paramOrder,
578 [ 'date', 'year', 'user', 'month', 'comment', 'newParam1', 'newParam2', 'newParam3', 'newParam4', 'newParam5' ],
579 'Final templatedata output with paramOrder'
580 );
581
582 // Move 'month' to offset 2, i.e. after 'year'
583 model.reorderParamOrderKey( 'month', 2 );
584 assert.deepEqual(
585 model.paramOrder,
586 [ 'date', 'year', 'month', 'user', 'comment', 'newParam1', 'newParam2', 'newParam3', 'newParam4', 'newParam5' ],
587 'Final templatedata output with paramOrder'
588 );
589 } );
590 } );
591
592 // Test model with maps in wikitext
593 QUnit.test( 'TemplateData sourceHandler with maps', ( assert ) => {
594 const sourceHandler = new SourceHandler(),
595 wikitextWithMaps = 'Some text here that is not templatedata information.' +
596 '<templatedata>' +
597 '{' +
598 '"description": {\n' +
599 '"en": "Template description in English."\n' +
600 '},' +
601 '"params": {' +
602 '"user": {' +
603 '"label": "Username",' +
604 '"type": "wiki-user-name",' +
605 '"required": true,' +
606 '"description": "User name of person who forgot to sign their comment.",' +
607 '"aliases": ["1"]' +
608 '},' +
609 '"date": {' +
610 '"label": "Date",' +
611 '"aliases": ["2"],' +
612 '"autovalue": "{{subst:CURRENTMONTHNAME}}",' +
613 '"suggested": true' +
614 '},' +
615 '"year": {' +
616 '"label": "Year",' +
617 '"type": "number"' +
618 '},' +
619 '"month": {' +
620 '"label": "Month",' +
621 '"inherits": "year"' +
622 '},' +
623 '"day": {' +
624 '"label": "Day",' +
625 '"inherits": "year"' +
626 '},' +
627 '"comment": {' +
628 '"required": false' +
629 '}' +
630 '},' +
631 '"sets": [' +
632 '{' +
633 '"label": "Date",' +
634 '"params": ["year", "month", "day"]' +
635 '}' +
636 '],' +
637 '"maps": {' +
638 '"exampleconsumer": {' +
639 '"cuser": "user",' +
640 '"cdate": ["year", "month", "day"],' +
641 '"ccomment": [ ["comment"] ]' +
642 '}' +
643 '}' +
644 '}' +
645 '</templatedata>' +
646 'Trailing text at the end.',
647 params = {
648 user: {
649 label: 'Username',
650 type: 'wiki-user-name',
651 required: true,
652 description: 'User name of person who forgot to sign their comment.',
653 aliases: [ '1' ]
654 },
655 date: {
656 label: 'Date',
657 aliases: [ '2' ],
658 autovalue: '{{subst:CURRENTMONTHNAME}}',
659 suggested: true,
660 type: undefined
661 },
662 year: {
663 label: 'Year',
664 type: 'number'
665 },
666 month: {
667 label: 'Month',
668 inherits: 'year',
669 type: undefined
670 },
671 day: {
672 label: 'Day',
673 inherits: 'year',
674 type: undefined
675 },
676 comment: {
677 required: false,
678 type: undefined
679 }
680 },
681 jsonWithMaps = {
682 description: {
683 en: 'Template description in English.'
684 },
685 params: params,
686 sets: [
687 {
688 label: 'Date',
689 params: [
690 'year',
691 'month',
692 'day'
693 ]
694 }
695 ],
696 maps: {
697 exampleconsumer: {
698 cuser: 'user',
699 cdate: [ 'year', 'month', 'day' ],
700 ccomment: [ [ 'comment' ] ]
701 }
702 }
703 };
704
705 return sourceHandler.buildModel( wikitextWithMaps )
706 .done( ( model ) => {
707 assert.deepEqual(
708 model.outputTemplateData(),
709 jsonWithMaps,
710 'Final templatedata output'
711 );
712 } );
713 } );
714
715 // Test model fail
716 QUnit.test( 'TemplateData sourceHandler failure', ( assert ) => {
717 const sourceHandler = new SourceHandler(),
718 erronousString = '<templatedata>{\n' +
719 '"params": {\n' +
720 // Open quote
721 '"user: {\n' +
722 '"label": "Username",\n' +
723 '"type": "wiki-user-name",\n' +
724 '"required": true,\n' +
725 '"description": "User name of person who forgot to sign their comment.",\n' +
726 '"aliases": [\n' +
727 '"1"\n' +
728 ']\n' +
729 '},\n' +
730 '"date": {\n' +
731 '"label": "Date",\n' +
732 '"description": {\n' +
733 // Forgotten quotes
734 currLanguage + ': "Timestamp of when the comment was posted, in YYYY-MM-DD format."\n' +
735 '}\n' +
736 '"suggested": true\n' +
737 '}\n' +
738 '}\n' +
739 '}</templatedata>',
740 done = assert.async();
741
742 const promise = sourceHandler.buildModel( erronousString );
743 promise.always( () => {
744 assert.strictEqual( promise.state(), 'rejected', 'Promise rejected on erronous json string.' );
745 done();
746 } );
747 } );
748
749 // Test model gets default format
750 QUnit.test( 'TemplateData sourceHandler adding default format', ( assert ) => {
751 const sourceHandler = new SourceHandler(),
752 simpleTemplateDataNoFormat = '<templatedata>{\n' +
753 '"params": {}\n' +
754 '}</templatedata>',
755 simpleTemplateDataDefaultFormat = {
756 params: {}
757 };
758
759 return sourceHandler.buildModel( simpleTemplateDataNoFormat )
760 .done( ( model ) => {
761 assert.deepEqual(
762 model.outputTemplateData(),
763 simpleTemplateDataDefaultFormat,
764 'Final templatedata output'
765 );
766 } );
767 } );
768
769 QUnit.test( 'Duplicate parameter names', ( assert ) => {
770 const model = new Model();
771 model.addParam( 'color' );
772 assert.deepEqual( model.getParams(), {
773 color: { name: 'color' }
774 } );
775 assert.deepEqual( model.getTemplateParamOrder(), [ 'color' ] );
776 model.addParam( 'color' );
777 assert.deepEqual( model.getParams(), {
778 color: { name: 'color' },
779 color2: { name: 'color' }
780 } );
781 assert.deepEqual( model.getTemplateParamOrder(), [ 'color', 'color2' ] );
782
783 model.setParamProperty( 'color2', 'name', '1' );
784 assert.deepEqual( model.getParams(), {
785 color: { name: 'color' },
786 color2: { deleted: true },
787 1: { name: '1' }
788 } );
789 assert.deepEqual( model.getTemplateParamOrder(), [ 'color', '1' ] );
790 model.setParamProperty( 'color', 'name', '1' );
791 assert.deepEqual( model.getParams(), {
792 color: { deleted: true },
793 color2: { deleted: true },
794 1: { name: '1' },
795 '1-3': { name: '1' }
796 } );
797 assert.deepEqual( model.getTemplateParamOrder(), [ '1-3', '1' ] );
798 } );
799
800 QUnit.test( 'safesubst: hack with an unnamed parameter', ( assert ) => {
801 const handler = new SourceHandler(),
802 wikitext = '{{ {{{|safesubst:}}}#invoke:…|{{{1}}}|{{{ 1 }}}}}';
803
804 assert.deepEqual(
805 handler.extractParametersFromTemplateCode( wikitext ),
806 [ '1' ]
807 );
808 } );