| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Extension\TemplateData\Api\ApiTemplateData; |
| 4 |
use MediaWiki\Extension\TemplateData\TemplateDataBlob; |
| 5 |
use MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter; |
| 6 |
use MediaWiki\Extension\TemplateData\TemplateDataValidator; |
| 7 |
use MediaWiki\Json\FormatJson; |
| 8 |
use MediaWiki\Language\RawMessage; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Status\Status; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use Wikimedia\TestingAccessWrapper; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class TemplateDataBlobTest extends MediaWikiIntegrationTestCase { |
| 23 |
|
| 24 |
protected function setUp(): void { |
| 25 |
parent::setUp(); |
| 26 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'qqx' ); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private function generatePseudorandomString( int $minLength, int $seed ): string { |
| 38 |
srand( $seed ); |
| 39 |
$string = ''; |
| 40 |
while ( strlen( $string ) < $minLength ) { |
| 41 |
$string .= str_shuffle( '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ); |
| 42 |
} |
| 43 |
return $string; |
| 44 |
} |
| 45 |
|
| 46 |
public static function provideParse() { |
| 47 |
$cases = [ |
| 48 |
[ |
| 49 |
'input' => '{', |
| 50 |
'status' => 'templatedata-invalid-parse' |
| 51 |
], |
| 52 |
[ |
| 53 |
'input' => '[] |
| 54 |
', |
| 55 |
'status' => '(templatedata-invalid-type: templatedata, object)' |
| 56 |
], |
| 57 |
[ |
| 58 |
'input' => '{ |
| 59 |
"params": {} |
| 60 |
} |
| 61 |
', |
| 62 |
'output' => '{ |
| 63 |
"description": null, |
| 64 |
"params": {}, |
| 65 |
"sets": [], |
| 66 |
"format": null, |
| 67 |
"maps" : {} |
| 68 |
} |
| 69 |
', |
| 70 |
'status' => true, |
| 71 |
'msg' => 'Minimal valid blob' |
| 72 |
], |
| 73 |
[ |
| 74 |
'input' => '{ |
| 75 |
"params": {}, |
| 76 |
"foo": "bar" |
| 77 |
} |
| 78 |
', |
| 79 |
'status' => '(templatedata-invalid-unknown: foo)', |
| 80 |
'msg' => 'Unknown properties' |
| 81 |
], |
| 82 |
[ |
| 83 |
'input' => '{ "description": [], "params": {} }', |
| 84 |
'status' => '(templatedata-invalid-type: description, string|object)', |
| 85 |
], |
| 86 |
[ |
| 87 |
'input' => '{}', |
| 88 |
'status' => '(templatedata-invalid-missing: params, object)', |
| 89 |
'msg' => 'Empty object' |
| 90 |
], |
| 91 |
[ |
| 92 |
'input' => '{ "params": [] }', |
| 93 |
'status' => '(templatedata-invalid-type: params, object)', |
| 94 |
], |
| 95 |
[ |
| 96 |
'input' => '{ "params": { "": {} } }', |
| 97 |
'status' => 'templatedata-invalid-unnamed-parameter', |
| 98 |
], |
| 99 |
[ |
| 100 |
'input' => '{ "params": { "a": [] } }', |
| 101 |
'status' => '(templatedata-invalid-type: params.a, object)', |
| 102 |
], |
| 103 |
[ |
| 104 |
'input' => '{ "params": { "a": { "foo": "" } } }', |
| 105 |
'status' => '(templatedata-invalid-unknown: params.a.foo)', |
| 106 |
], |
| 107 |
[ |
| 108 |
'input' => '{ "params": { "a": { "label": [] } } }', |
| 109 |
'status' => '(templatedata-invalid-type: params.a.label, string|object)', |
| 110 |
], |
| 111 |
[ |
| 112 |
'input' => '{ "params": { "a": { "required": "" } } }', |
| 113 |
'status' => '(templatedata-invalid-type: params.a.required, boolean)', |
| 114 |
], |
| 115 |
[ |
| 116 |
'input' => '{ "params": { "a": { "suggested": "" } } }', |
| 117 |
'status' => '(templatedata-invalid-type: params.a.suggested, boolean)', |
| 118 |
], |
| 119 |
[ |
| 120 |
'input' => '{ "params": { "a": { "description": [] } } }', |
| 121 |
'status' => '(templatedata-invalid-type: params.a.description, string|object)', |
| 122 |
], |
| 123 |
[ |
| 124 |
'input' => '{ "params": { "a": { "example": [] } } }', |
| 125 |
'status' => '(templatedata-invalid-type: params.a.example, string|object)', |
| 126 |
], |
| 127 |
[ |
| 128 |
'input' => '{ "params": { "a": { "deprecated": [] } } }', |
| 129 |
'status' => '(templatedata-invalid-type: params.a.deprecated, boolean|string)', |
| 130 |
], |
| 131 |
[ |
| 132 |
'input' => '{ "params": { "a": { "aliases": "" } } }', |
| 133 |
'status' => '(templatedata-invalid-type: params.a.aliases, array)', |
| 134 |
], |
| 135 |
[ |
| 136 |
'input' => '{ "params": { "a": { "aliases": [ "1", 2, {} ] } } }', |
| 137 |
'status' => '(templatedata-invalid-type: params.a.aliases[2], int|string)', |
| 138 |
], |
| 139 |
[ |
| 140 |
'input' => '{ "params": { "a": { "autovalue": [] } } }', |
| 141 |
'status' => '(templatedata-invalid-type: params.a.autovalue, string)', |
| 142 |
], |
| 143 |
[ |
| 144 |
'input' => '{ "params": { "a": { "default": [] } } }', |
| 145 |
'status' => '(templatedata-invalid-type: params.a.default, string|object)', |
| 146 |
], |
| 147 |
[ |
| 148 |
'input' => '{ "params": { "a": { "type": [] } } }', |
| 149 |
'status' => '(templatedata-invalid-type: params.a.type, string)', |
| 150 |
], |
| 151 |
[ |
| 152 |
'input' => '{ "params": { "a": { "type": "" } } }', |
| 153 |
'status' => '(templatedata-invalid-value: params.a.type)', |
| 154 |
], |
| 155 |
[ |
| 156 |
'input' => '{ "params": { "a": { "suggestedvalues": "" } } }', |
| 157 |
'status' => '(templatedata-invalid-type: params.a.suggestedvalues, array)', |
| 158 |
], |
| 159 |
[ |
| 160 |
'input' => '{ "params": { "a": { "suggestedvalues": [ {} ] } } }', |
| 161 |
'status' => '(templatedata-invalid-type: params.a.suggestedvalues[0], string)', |
| 162 |
], |
| 163 |
[ |
| 164 |
'input' => '{ |
| 165 |
"params": { |
| 166 |
"foo": {} |
| 167 |
} |
| 168 |
} |
| 169 |
', |
| 170 |
'output' => '{ |
| 171 |
"description": null, |
| 172 |
"params": { |
| 173 |
"foo": { |
| 174 |
"label": null, |
| 175 |
"description": null, |
| 176 |
"default": null, |
| 177 |
"example": null, |
| 178 |
"required": false, |
| 179 |
"suggested": false, |
| 180 |
"suggestedvalues": [], |
| 181 |
"deprecated": false, |
| 182 |
"aliases": [], |
| 183 |
"type": "unknown", |
| 184 |
"autovalue": null |
| 185 |
} |
| 186 |
}, |
| 187 |
"sets": [], |
| 188 |
"format": null, |
| 189 |
"maps": {} |
| 190 |
} |
| 191 |
', |
| 192 |
'msg' => 'Optional properties are added if missing' |
| 193 |
], |
| 194 |
[ |
| 195 |
'input' => '{ |
| 196 |
"params": { |
| 197 |
"comment": { |
| 198 |
"type": "string/line" |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
', |
| 203 |
'output' => '{ |
| 204 |
"description": null, |
| 205 |
"params": { |
| 206 |
"comment": { |
| 207 |
"label": null, |
| 208 |
"description": null, |
| 209 |
"default": null, |
| 210 |
"example": null, |
| 211 |
"autovalue": null, |
| 212 |
"required": false, |
| 213 |
"suggested": false, |
| 214 |
"suggestedvalues": [], |
| 215 |
"deprecated": false, |
| 216 |
"aliases": [], |
| 217 |
"type": "line" |
| 218 |
} |
| 219 |
}, |
| 220 |
"sets": [], |
| 221 |
"format": null, |
| 222 |
"maps": {} |
| 223 |
} |
| 224 |
', |
| 225 |
'msg' => 'Old string/* types are mapped to the unprefixed versions' |
| 226 |
], |
| 227 |
[ |
| 228 |
'input' => '{ |
| 229 |
"description": "User badge MediaWiki developers.", |
| 230 |
"params": { |
| 231 |
"nickname": { |
| 232 |
"label": null, |
| 233 |
"description": "User name of user who owns the badge", |
| 234 |
"default": "Base page name of the host page", |
| 235 |
"example": null, |
| 236 |
"required": false, |
| 237 |
"suggested": true, |
| 238 |
"aliases": [ 1 ] |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
', |
| 243 |
'output' => '{ |
| 244 |
"description": { |
| 245 |
"qqx": "User badge MediaWiki developers." |
| 246 |
}, |
| 247 |
"params": { |
| 248 |
"nickname": { |
| 249 |
"label": null, |
| 250 |
"description": { |
| 251 |
"qqx": "User name of user who owns the badge" |
| 252 |
}, |
| 253 |
"default": { |
| 254 |
"qqx": "Base page name of the host page" |
| 255 |
}, |
| 256 |
"example": null, |
| 257 |
"required": false, |
| 258 |
"suggested": true, |
| 259 |
"suggestedvalues": [], |
| 260 |
"deprecated": false, |
| 261 |
"aliases": [ |
| 262 |
"1" |
| 263 |
], |
| 264 |
"type": "unknown", |
| 265 |
"autovalue": null |
| 266 |
} |
| 267 |
}, |
| 268 |
"sets": [], |
| 269 |
"format": null, |
| 270 |
"maps": {} |
| 271 |
} |
| 272 |
', |
| 273 |
'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language' |
| 274 |
], |
| 275 |
[ |
| 276 |
'input' => '{ "params": { "b": { "inherits": "a" } } }', |
| 277 |
'status' => '(templatedata-invalid-missing: params.a)', |
| 278 |
], |
| 279 |
[ |
| 280 |
'input' => '{ |
| 281 |
"description": "Document the documenter.", |
| 282 |
"params": { |
| 283 |
"1d": { |
| 284 |
"description": "Description of the template parameter", |
| 285 |
"required": true, |
| 286 |
"default": "example" |
| 287 |
}, |
| 288 |
"2d": { |
| 289 |
"inherits": "1d", |
| 290 |
"default": "overridden" |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
', |
| 295 |
'output' => '{ |
| 296 |
"description": { |
| 297 |
"qqx": "Document the documenter." |
| 298 |
}, |
| 299 |
"params": { |
| 300 |
"1d": { |
| 301 |
"label": null, |
| 302 |
"description": { |
| 303 |
"qqx": "Description of the template parameter" |
| 304 |
}, |
| 305 |
"example": null, |
| 306 |
"required": true, |
| 307 |
"suggested": false, |
| 308 |
"suggestedvalues": [], |
| 309 |
"default": { |
| 310 |
"qqx": "example" |
| 311 |
}, |
| 312 |
"deprecated": false, |
| 313 |
"aliases": [], |
| 314 |
"type": "unknown", |
| 315 |
"autovalue": null |
| 316 |
}, |
| 317 |
"2d": { |
| 318 |
"label": null, |
| 319 |
"description": { |
| 320 |
"qqx": "Description of the template parameter" |
| 321 |
}, |
| 322 |
"example": null, |
| 323 |
"required": true, |
| 324 |
"suggested": false, |
| 325 |
"suggestedvalues": [], |
| 326 |
"default": { |
| 327 |
"qqx": "overridden" |
| 328 |
}, |
| 329 |
"deprecated": false, |
| 330 |
"aliases": [], |
| 331 |
"type": "unknown", |
| 332 |
"autovalue": null |
| 333 |
} |
| 334 |
}, |
| 335 |
"sets": [], |
| 336 |
"format": null, |
| 337 |
"maps" : {} |
| 338 |
} |
| 339 |
', |
| 340 |
'msg' => 'The inherits property copies over properties from another parameter ' |
| 341 |
. '(preserving overides)' |
| 342 |
], |
| 343 |
[ |
| 344 |
'input' => '{ "params": {}, "sets": {} }', |
| 345 |
'status' => '(templatedata-invalid-type: sets, array)' |
| 346 |
], |
| 347 |
[ |
| 348 |
'input' => '{ "params": {}, "sets": [ [] ] }', |
| 349 |
'status' => '(templatedata-invalid-value: sets.0)' |
| 350 |
], |
| 351 |
[ |
| 352 |
'input' => '{ |
| 353 |
"params": {}, |
| 354 |
"sets": [ |
| 355 |
{ |
| 356 |
"label": "Example" |
| 357 |
} |
| 358 |
] |
| 359 |
}', |
| 360 |
'status' => '(templatedata-invalid-missing: sets.0.params, array)' |
| 361 |
], |
| 362 |
[ |
| 363 |
'input' => '{ "params": {}, "sets": [ { "label": "", "params": {} } ] }', |
| 364 |
'status' => '(templatedata-invalid-type: sets.0.params, array)' |
| 365 |
], |
| 366 |
[ |
| 367 |
'input' => '{ "params": {}, "sets": [ { "label": "", "params": [] } ] }', |
| 368 |
'status' => '(templatedata-invalid-empty-array: sets.0.params)' |
| 369 |
], |
| 370 |
[ |
| 371 |
'input' => '{ |
| 372 |
"params": { |
| 373 |
"foo": { |
| 374 |
} |
| 375 |
}, |
| 376 |
"sets": [ |
| 377 |
{ |
| 378 |
"params": ["foo"] |
| 379 |
} |
| 380 |
] |
| 381 |
}', |
| 382 |
'status' => '(templatedata-invalid-missing: sets.0.label, string|object)' |
| 383 |
], |
| 384 |
[ |
| 385 |
'input' => '{ "params": {}, "sets": [ { "label": [] } ] }', |
| 386 |
'status' => '(templatedata-invalid-type: sets.0.label, string|object)' |
| 387 |
], |
| 388 |
[ |
| 389 |
'input' => '{ |
| 390 |
"params": { |
| 391 |
"foo": { |
| 392 |
}, |
| 393 |
"bar": { |
| 394 |
} |
| 395 |
}, |
| 396 |
"sets": [ |
| 397 |
{ |
| 398 |
"label": "Foo with Quux", |
| 399 |
"params": ["foo", "quux"] |
| 400 |
} |
| 401 |
] |
| 402 |
}', |
| 403 |
'status' => '(templatedata-invalid-value: sets.0.params[1])' |
| 404 |
], |
| 405 |
[ |
| 406 |
'input' => '{ |
| 407 |
"params": { |
| 408 |
"foo": { |
| 409 |
}, |
| 410 |
"bar": { |
| 411 |
}, |
| 412 |
"quux": { |
| 413 |
} |
| 414 |
}, |
| 415 |
"sets": [ |
| 416 |
{ |
| 417 |
"label": "Foo with Quux", |
| 418 |
"params": ["foo", "quux"] |
| 419 |
}, |
| 420 |
{ |
| 421 |
"label": "Bar with Quux", |
| 422 |
"params": ["bar", "quux"] |
| 423 |
} |
| 424 |
] |
| 425 |
}', |
| 426 |
'output' => '{ |
| 427 |
"description": null, |
| 428 |
"params": { |
| 429 |
"foo": { |
| 430 |
"label": null, |
| 431 |
"required": false, |
| 432 |
"example": null, |
| 433 |
"suggested": false, |
| 434 |
"suggestedvalues": [], |
| 435 |
"description": null, |
| 436 |
"deprecated": false, |
| 437 |
"aliases": [], |
| 438 |
"default": null, |
| 439 |
"type": "unknown", |
| 440 |
"autovalue": null |
| 441 |
}, |
| 442 |
"bar": { |
| 443 |
"label": null, |
| 444 |
"required": false, |
| 445 |
"suggested": false, |
| 446 |
"suggestedvalues": [], |
| 447 |
"description": null, |
| 448 |
"example": null, |
| 449 |
"deprecated": false, |
| 450 |
"aliases": [], |
| 451 |
"default": null, |
| 452 |
"type": "unknown", |
| 453 |
"autovalue": null |
| 454 |
}, |
| 455 |
"quux": { |
| 456 |
"label": null, |
| 457 |
"required": false, |
| 458 |
"suggested": false, |
| 459 |
"suggestedvalues": [], |
| 460 |
"description": null, |
| 461 |
"example": null, |
| 462 |
"deprecated": false, |
| 463 |
"aliases": [], |
| 464 |
"default": null, |
| 465 |
"type": "unknown", |
| 466 |
"autovalue": null |
| 467 |
} |
| 468 |
}, |
| 469 |
"sets": [ |
| 470 |
{ |
| 471 |
"label": { |
| 472 |
"qqx": "Foo with Quux" |
| 473 |
}, |
| 474 |
"params": ["foo", "quux"] |
| 475 |
}, |
| 476 |
{ |
| 477 |
"label": { |
| 478 |
"qqx": "Bar with Quux" |
| 479 |
}, |
| 480 |
"params": ["bar", "quux"] |
| 481 |
} |
| 482 |
], |
| 483 |
"format": null, |
| 484 |
"maps": {} |
| 485 |
}', |
| 486 |
'status' => true |
| 487 |
], |
| 488 |
[ |
| 489 |
'input' => '{ |
| 490 |
"description": "Testing some template description.", |
| 491 |
"params": { |
| 492 |
"bar": { |
| 493 |
"label": "Bar label", |
| 494 |
"description": "Bar description", |
| 495 |
"default": "Baz", |
| 496 |
"example": "Foo bar baz", |
| 497 |
"autovalue": "{{SomeTemplate}}", |
| 498 |
"required": true, |
| 499 |
"suggested": false, |
| 500 |
"suggestedvalues": [ "baz", "boo" ], |
| 501 |
"deprecated": false, |
| 502 |
"aliases": [ "foo", "baz" ], |
| 503 |
"type": "line" |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
', |
| 508 |
'output' => '{ |
| 509 |
"description": { |
| 510 |
"qqx": "Testing some template description." |
| 511 |
}, |
| 512 |
"params": { |
| 513 |
"bar": { |
| 514 |
"label": { |
| 515 |
"qqx": "Bar label" |
| 516 |
}, |
| 517 |
"description": { |
| 518 |
"qqx": "Bar description" |
| 519 |
}, |
| 520 |
"default": { |
| 521 |
"qqx": "Baz" |
| 522 |
}, |
| 523 |
"example": { |
| 524 |
"qqx": "Foo bar baz" |
| 525 |
}, |
| 526 |
"autovalue": "{{SomeTemplate}}", |
| 527 |
"required": true, |
| 528 |
"suggested": false, |
| 529 |
"suggestedvalues": [ "baz", "boo" ], |
| 530 |
"deprecated": false, |
| 531 |
"aliases": [ "foo", "baz" ], |
| 532 |
"type": "line" |
| 533 |
} |
| 534 |
}, |
| 535 |
"sets": [], |
| 536 |
"format": null, |
| 537 |
"maps" : {} |
| 538 |
} |
| 539 |
', |
| 540 |
'msg' => 'Parameter attributes preserve information.' |
| 541 |
], |
| 542 |
[ |
| 543 |
'input' => '{ "params": {}, "maps": [] }', |
| 544 |
'status' => '(templatedata-invalid-type: maps, object)' |
| 545 |
], |
| 546 |
[ |
| 547 |
'input' => '{ "params": {}, "maps": { "a": [] } }', |
| 548 |
'status' => '(templatedata-invalid-type: maps.a, object)' |
| 549 |
], |
| 550 |
[ |
| 551 |
'input' => '{ "params": {}, "maps": { "a": { "b": "c" } } }', |
| 552 |
'status' => '(templatedata-invalid-param: c, maps.a.b)' |
| 553 |
], |
| 554 |
[ |
| 555 |
'input' => '{ "params": {}, "maps": { "a": { "b": [ {} ] } } }', |
| 556 |
'status' => '(templatedata-invalid-type: maps.a.b[0], string|array)' |
| 557 |
], |
| 558 |
[ |
| 559 |
'input' => '{ "params": {}, "maps": { "a": { "b": [ "c" ] } } }', |
| 560 |
'status' => '(templatedata-invalid-param: c, maps.a.b[0])' |
| 561 |
], |
| 562 |
[ |
| 563 |
'input' => '{ |
| 564 |
"params": { |
| 565 |
"foo": { |
| 566 |
}, |
| 567 |
"bar": { |
| 568 |
} |
| 569 |
}, |
| 570 |
"sets": [], |
| 571 |
"maps": { |
| 572 |
"application": { |
| 573 |
"things": [ |
| 574 |
"foo", |
| 575 |
["bar", "quux"] |
| 576 |
] |
| 577 |
} |
| 578 |
} |
| 579 |
}', |
| 580 |
'status' => '(templatedata-invalid-param: quux, maps.application.things[1][1])' |
| 581 |
], |
| 582 |
[ |
| 583 |
'input' => '{ |
| 584 |
"params": { |
| 585 |
"foo": { |
| 586 |
}, |
| 587 |
"bar": { |
| 588 |
} |
| 589 |
}, |
| 590 |
"sets": [], |
| 591 |
"maps": { |
| 592 |
"application": { |
| 593 |
"things": { |
| 594 |
"appbar": "bar", |
| 595 |
"appfoo": "foo" |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
}', |
| 600 |
'status' => '(templatedata-invalid-type: maps.application.things, string|array)' |
| 601 |
], |
| 602 |
[ |
| 603 |
'input' => '{ |
| 604 |
"params": { |
| 605 |
"foo": { |
| 606 |
}, |
| 607 |
"bar": { |
| 608 |
} |
| 609 |
}, |
| 610 |
"sets": [], |
| 611 |
"maps": { |
| 612 |
"application": { |
| 613 |
"things": [ |
| 614 |
[ true ] |
| 615 |
] |
| 616 |
} |
| 617 |
} |
| 618 |
}', |
| 619 |
'status' => '(templatedata-invalid-type: maps.application.things[0][0], string)' |
| 620 |
], |
| 621 |
[ |
| 622 |
'input' => '{ "params": {}, "format": "" }', |
| 623 |
'status' => '(templatedata-invalid-format: format)' |
| 624 |
], |
| 625 |
[ |
| 626 |
'input' => '{ |
| 627 |
"params": { |
| 628 |
"foo": {} |
| 629 |
}, |
| 630 |
"format": "meshuggah format" |
| 631 |
}', |
| 632 |
'status' => '(templatedata-invalid-format: format)' |
| 633 |
], |
| 634 |
[ |
| 635 |
'input' => '{ |
| 636 |
"params": {}, |
| 637 |
"format": "inline" |
| 638 |
}', |
| 639 |
'output' => '{ |
| 640 |
"description": null, |
| 641 |
"params": {}, |
| 642 |
"sets": [], |
| 643 |
"format": "inline", |
| 644 |
"maps": {} |
| 645 |
} |
| 646 |
', |
| 647 |
'msg' => '"inline" is a valid format string', |
| 648 |
'status' => true |
| 649 |
], |
| 650 |
[ |
| 651 |
'input' => '{ |
| 652 |
"params": {}, |
| 653 |
"format": "block" |
| 654 |
}', |
| 655 |
'output' => '{ |
| 656 |
"description": null, |
| 657 |
"params": {}, |
| 658 |
"sets": [], |
| 659 |
"format": "block", |
| 660 |
"maps": {} |
| 661 |
} |
| 662 |
', |
| 663 |
'msg' => '"block" is a valid format string', |
| 664 |
'status' => true |
| 665 |
], |
| 666 |
[ |
| 667 |
'input' => '{ |
| 668 |
"params": {}, |
| 669 |
"format": "{{_ |\n ___ = _}}" |
| 670 |
}', |
| 671 |
'output' => '{ |
| 672 |
"description": null, |
| 673 |
"params": {}, |
| 674 |
"sets": [], |
| 675 |
"format": "{{_ |\n ___ = _}}", |
| 676 |
"maps": {} |
| 677 |
} |
| 678 |
', |
| 679 |
'msg' => 'Custom parameter format string (1)', |
| 680 |
'status' => true |
| 681 |
], |
| 682 |
[ |
| 683 |
'input' => '{ |
| 684 |
"params": {}, |
| 685 |
"format": "{{_|_=_\n}}\n" |
| 686 |
}', |
| 687 |
'output' => '{ |
| 688 |
"description": null, |
| 689 |
"params": {}, |
| 690 |
"sets": [], |
| 691 |
"format": "{{_|_=_\n}}\n", |
| 692 |
"maps": {} |
| 693 |
} |
| 694 |
', |
| 695 |
'msg' => 'Custom parameter format string (2)', |
| 696 |
'status' => true |
| 697 |
], |
| 698 |
]; |
| 699 |
|
| 700 |
foreach ( $cases as $case ) { |
| 701 |
yield [ $case ]; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
private function getStatusText( Status $status ): string { |
| 706 |
|
| 707 |
|
| 708 |
return html_entity_decode( $status->getMessage()->plain() ); |
| 709 |
} |
| 710 |
|
| 711 |
private function ksort( array &$input ): void { |
| 712 |
ksort( $input ); |
| 713 |
foreach ( $input as &$value ) { |
| 714 |
if ( is_array( $value ) ) { |
| 715 |
$this->ksort( $value ); |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
private function assertStrictJsonEquals( string $expected, stdClass $actual, ?string $message = null ): void { |
| 734 |
|
| 735 |
|
| 736 |
$expected = json_decode( $expected, true ); |
| 737 |
$actual = json_decode( json_encode( $actual ), true ); |
| 738 |
$this->ksort( $expected ); |
| 739 |
$this->ksort( $actual ); |
| 740 |
|
| 741 |
$this->assertSame( |
| 742 |
FormatJson::encode( $expected, true ), |
| 743 |
FormatJson::encode( $actual, true ), |
| 744 |
$message |
| 745 |
); |
| 746 |
} |
| 747 |
|
| 748 |
private function assertTemplateData( array $case ): void { |
| 749 |
|
| 750 |
$case['status'] ??= true; |
| 751 |
$case['msg'] ??= $case['status']; |
| 752 |
|
| 753 |
$t = TemplateDataBlob::newFromJSON( $this->db, $case['input'] ); |
| 754 |
$actual = $t->getData(); |
| 755 |
$status = $t->getStatus(); |
| 756 |
|
| 757 |
if ( $case['status'] === true ) { |
| 758 |
$this->assertStatusGood( $status ); |
| 759 |
} elseif ( !str_starts_with( $case['status'], '(' ) ) { |
| 760 |
$this->assertStatusError( $case['status'], $status ); |
| 761 |
} else { |
| 762 |
$this->assertSame( $case['status'], $this->getStatusText( $status ), $case['msg'] ); |
| 763 |
} |
| 764 |
|
| 765 |
if ( !isset( $case['output'] ) ) { |
| 766 |
$expected = is_string( $case['status'] ) |
| 767 |
? '{ "description": null, "params": {}, "sets": [], "maps": {}, "format": null }' |
| 768 |
: $case['input']; |
| 769 |
$this->assertStrictJsonEquals( $expected, $actual, $case['msg'] ); |
| 770 |
} else { |
| 771 |
$this->assertStrictJsonEquals( $case['output'], $actual, $case['msg'] ); |
| 772 |
|
| 773 |
|
| 774 |
$t = TemplateDataBlob::newFromJSON( $this->db, $case['output'] ); |
| 775 |
$status = $t->getStatus(); |
| 776 |
|
| 777 |
if ( !$status->isGood() ) { |
| 778 |
$this->assertSame( $case['status'], $this->getStatusText( $status ), |
| 779 |
'Roundtrip status: ' . $case['msg'] |
| 780 |
); |
| 781 |
} |
| 782 |
$this->assertStrictJsonEquals( $case['output'], $t->getData(), |
| 783 |
'Roundtrip: ' . $case['msg'] |
| 784 |
); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
public function testParse( array $case ) { |
| 792 |
$this->assertTemplateData( $case ); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
public function testParseLongString() { |
| 799 |
if ( $this->getDb()->getType() !== 'mysql' ) { |
| 800 |
$this->markTestSkipped( 'long compressed strings break on MySQL only' ); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
$json = '{ |
| 805 |
"description": "' . $this->generatePseudorandomString( 100000, 42 ) . '", |
| 806 |
"params": {} |
| 807 |
}'; |
| 808 |
$templateData = TemplateDataBlob::newFromJSON( $this->db, $json ); |
| 809 |
|
| 810 |
$this->assertStatusError( 'templatedata-invalid-length', |
| 811 |
$templateData->getStatus() ); |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
public function testIsValidInterfaceText( $text, bool $expected ) { |
| 818 |
|
| 819 |
$validator = TestingAccessWrapper::newFromObject( |
| 820 |
new TemplateDataValidator( [] ) |
| 821 |
); |
| 822 |
$this->assertSame( $expected, $validator->isValidInterfaceText( $text ) ); |
| 823 |
} |
| 824 |
|
| 825 |
public static function provideInterfaceTexts() { |
| 826 |
return [ |
| 827 |
|
| 828 |
[ null, false ], |
| 829 |
[ [], false ], |
| 830 |
[ [ 'en' => 'example' ], false ], |
| 831 |
[ (object)[], false ], |
| 832 |
[ (object)[ null ], false ], |
| 833 |
[ (object)[ 'en' => null ], false ], |
| 834 |
|
| 835 |
[ 'example', true ], |
| 836 |
[ (object)[ 'de' => 'Beispiel', 'en' => 'example' ], true ], |
| 837 |
|
| 838 |
|
| 839 |
[ '', true ], |
| 840 |
[ (object)[ 'en' => '' ], true ], |
| 841 |
|
| 842 |
|
| 843 |
[ (object)[ '' => 'example' ], false ], |
| 844 |
[ (object)[ ' ' => 'example' ], false ], |
| 845 |
]; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
public function testGetJsonForDatabase() { |
| 863 |
|
| 864 |
|
| 865 |
$gzJson = gzencode( '{}' ); |
| 866 |
$templateData = TemplateDataBlob::newFromDatabase( $this->db, $gzJson ); |
| 867 |
$this->assertInstanceOf( TemplateDataBlob::class, $templateData ); |
| 868 |
} |
| 869 |
|
| 870 |
public static function provideGetDataInLanguage() { |
| 871 |
$cases = [ |
| 872 |
[ |
| 873 |
'input' => '{ |
| 874 |
"description": { |
| 875 |
"de": "German", |
| 876 |
"nl": "Dutch", |
| 877 |
"en": "English", |
| 878 |
"de-formal": "German (formal address)" |
| 879 |
}, |
| 880 |
"params": {} |
| 881 |
} |
| 882 |
', |
| 883 |
'output' => '{ |
| 884 |
"description": "German", |
| 885 |
"params": {}, |
| 886 |
"sets": [], |
| 887 |
"format": null, |
| 888 |
"maps" : {} |
| 889 |
} |
| 890 |
', |
| 891 |
'lang' => 'de', |
| 892 |
'msg' => 'Simple description' |
| 893 |
], |
| 894 |
[ |
| 895 |
'input' => '{ |
| 896 |
"description": "Hi", |
| 897 |
"params": {} |
| 898 |
} |
| 899 |
', |
| 900 |
'output' => '{ |
| 901 |
"description": "Hi", |
| 902 |
"params": {}, |
| 903 |
"sets": [], |
| 904 |
"format": null, |
| 905 |
"maps" : {} |
| 906 |
} |
| 907 |
', |
| 908 |
'lang' => 'fr', |
| 909 |
'msg' => 'Non multi-language value returned as is (expands to { "en": value } for' . |
| 910 |
' content-lang, "fr" falls back to "en")' |
| 911 |
], |
| 912 |
[ |
| 913 |
'input' => '{ |
| 914 |
"description": { |
| 915 |
"nl": "Dutch", |
| 916 |
"de": "German" |
| 917 |
}, |
| 918 |
"params": {} |
| 919 |
} |
| 920 |
', |
| 921 |
'output' => '{ |
| 922 |
"description": "Dutch", |
| 923 |
"params": {}, |
| 924 |
"sets": [], |
| 925 |
"format": null, |
| 926 |
"maps" : {} |
| 927 |
} |
| 928 |
', |
| 929 |
'lang' => 'fr', |
| 930 |
'msg' => 'Try content language before giving up on user language and fallbacks' |
| 931 |
], |
| 932 |
[ |
| 933 |
'input' => '{ |
| 934 |
"description": { |
| 935 |
"es": "Spanish", |
| 936 |
"de": "German" |
| 937 |
}, |
| 938 |
"params": {} |
| 939 |
} |
| 940 |
', |
| 941 |
'output' => '{ |
| 942 |
"description": null, |
| 943 |
"params": {}, |
| 944 |
"sets": [], |
| 945 |
"format": null, |
| 946 |
"maps" : {} |
| 947 |
} |
| 948 |
', |
| 949 |
'lang' => 'fr', |
| 950 |
'msg' => 'Description is optional, use null if no suitable fallback' |
| 951 |
], |
| 952 |
[ |
| 953 |
'input' => '{ |
| 954 |
"description": { |
| 955 |
"de": "German", |
| 956 |
"nl": "Dutch", |
| 957 |
"en": "English" |
| 958 |
}, |
| 959 |
"params": {} |
| 960 |
} |
| 961 |
', |
| 962 |
'output' => '{ |
| 963 |
"description": "German", |
| 964 |
"params": {}, |
| 965 |
"sets": [], |
| 966 |
"format": null, |
| 967 |
"maps" : {} |
| 968 |
} |
| 969 |
', |
| 970 |
'lang' => 'de-formal', |
| 971 |
'msg' => '"de-formal" falls back to "de"' |
| 972 |
], |
| 973 |
[ |
| 974 |
'input' => '{ |
| 975 |
"params": { |
| 976 |
"foo": { |
| 977 |
"label": { |
| 978 |
"fr": "French", |
| 979 |
"en": "English" |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
} |
| 984 |
', |
| 985 |
'output' => '{ |
| 986 |
"description": null, |
| 987 |
"params": { |
| 988 |
"foo": { |
| 989 |
"label": "French", |
| 990 |
"required": false, |
| 991 |
"example": null, |
| 992 |
"suggested": false, |
| 993 |
"suggestedvalues": [], |
| 994 |
"description": null, |
| 995 |
"deprecated": false, |
| 996 |
"aliases": [], |
| 997 |
"default": null, |
| 998 |
"type": "unknown", |
| 999 |
"autovalue": null |
| 1000 |
} |
| 1001 |
}, |
| 1002 |
"sets": [], |
| 1003 |
"format": null, |
| 1004 |
"maps" : {} |
| 1005 |
} |
| 1006 |
', |
| 1007 |
'lang' => 'fr', |
| 1008 |
'msg' => 'Simple parameter label' |
| 1009 |
], |
| 1010 |
[ |
| 1011 |
'input' => '{ |
| 1012 |
"params": { |
| 1013 |
"foo": { |
| 1014 |
"default": { |
| 1015 |
"fr": "French", |
| 1016 |
"en": "English" |
| 1017 |
} |
| 1018 |
} |
| 1019 |
} |
| 1020 |
} |
| 1021 |
', |
| 1022 |
'output' => '{ |
| 1023 |
"description": null, |
| 1024 |
"params": { |
| 1025 |
"foo": { |
| 1026 |
"default": "French", |
| 1027 |
"required": false, |
| 1028 |
"suggested": false, |
| 1029 |
"suggestedvalues": [], |
| 1030 |
"description": null, |
| 1031 |
"deprecated": false, |
| 1032 |
"aliases": [], |
| 1033 |
"label": null, |
| 1034 |
"type": "unknown", |
| 1035 |
"autovalue": null, |
| 1036 |
"example": null |
| 1037 |
} |
| 1038 |
}, |
| 1039 |
"sets": [], |
| 1040 |
"format": null, |
| 1041 |
"maps" : {} |
| 1042 |
} |
| 1043 |
', |
| 1044 |
'lang' => 'fr', |
| 1045 |
'msg' => 'Simple parameter default value' |
| 1046 |
], |
| 1047 |
[ |
| 1048 |
'input' => '{ |
| 1049 |
"params": { |
| 1050 |
"foo": { |
| 1051 |
"label": { |
| 1052 |
"es": "Spanish", |
| 1053 |
"de": "German" |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} |
| 1058 |
', |
| 1059 |
'output' => '{ |
| 1060 |
"description": null, |
| 1061 |
"params": { |
| 1062 |
"foo": { |
| 1063 |
"label": null, |
| 1064 |
"required": false, |
| 1065 |
"suggested": false, |
| 1066 |
"suggestedvalues": [], |
| 1067 |
"description": null, |
| 1068 |
"example": null, |
| 1069 |
"deprecated": false, |
| 1070 |
"aliases": [], |
| 1071 |
"default": null, |
| 1072 |
"type": "unknown", |
| 1073 |
"autovalue": null |
| 1074 |
} |
| 1075 |
}, |
| 1076 |
"sets": [], |
| 1077 |
"format": null, |
| 1078 |
"maps" : {} |
| 1079 |
} |
| 1080 |
', |
| 1081 |
'lang' => 'fr', |
| 1082 |
'msg' => 'Parameter label is optional, use null if no matching fallback' |
| 1083 |
], |
| 1084 |
[ |
| 1085 |
'input' => '{ |
| 1086 |
"params": { |
| 1087 |
"foo": {} |
| 1088 |
}, |
| 1089 |
"sets": [ |
| 1090 |
{ |
| 1091 |
"label": { |
| 1092 |
"es": "Spanish", |
| 1093 |
"de": "German" |
| 1094 |
}, |
| 1095 |
"params": ["foo"] |
| 1096 |
} |
| 1097 |
] |
| 1098 |
} |
| 1099 |
', |
| 1100 |
'output' => '{ |
| 1101 |
"description": null, |
| 1102 |
"params": { |
| 1103 |
"foo": { |
| 1104 |
"label": null, |
| 1105 |
"required": false, |
| 1106 |
"suggested": false, |
| 1107 |
"suggestedvalues": [], |
| 1108 |
"description": null, |
| 1109 |
"example": null, |
| 1110 |
"deprecated": false, |
| 1111 |
"aliases": [], |
| 1112 |
"default": null, |
| 1113 |
"type": "unknown", |
| 1114 |
"autovalue": null |
| 1115 |
} |
| 1116 |
}, |
| 1117 |
"sets": [ |
| 1118 |
{ |
| 1119 |
"label": "Spanish", |
| 1120 |
"params": ["foo"] |
| 1121 |
} |
| 1122 |
], |
| 1123 |
"format": null, |
| 1124 |
"maps": {} |
| 1125 |
} |
| 1126 |
', |
| 1127 |
'lang' => 'fr', |
| 1128 |
'msg' => 'Set label is not optional, choose first available key as final fallback' |
| 1129 |
], |
| 1130 |
]; |
| 1131 |
|
| 1132 |
foreach ( $cases as $case ) { |
| 1133 |
yield [ $case ]; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
public function testGetDataInLanguage( array $case ) { |
| 1141 |
|
| 1142 |
|
| 1143 |
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'nl' ); |
| 1144 |
|
| 1145 |
$t = TemplateDataBlob::newFromJSON( $this->db, $case['input'] ); |
| 1146 |
$status = $t->getStatus(); |
| 1147 |
|
| 1148 |
$this->assertStatusGood( $status, $case['msg'] ); |
| 1149 |
|
| 1150 |
$actual = $t->getDataInLanguage( $case['lang'] ); |
| 1151 |
$this->assertJsonStringEqualsJsonString( |
| 1152 |
$case['output'], |
| 1153 |
json_encode( $actual ), |
| 1154 |
$case['msg'] |
| 1155 |
); |
| 1156 |
} |
| 1157 |
|
| 1158 |
public static function provideParamOrder() { |
| 1159 |
$cases = [ |
| 1160 |
[ |
| 1161 |
'input' => '{ |
| 1162 |
"params": { |
| 1163 |
"foo": {}, |
| 1164 |
"bar": {}, |
| 1165 |
"baz": {} |
| 1166 |
} |
| 1167 |
} |
| 1168 |
', |
| 1169 |
'output' => '{ |
| 1170 |
"description": null, |
| 1171 |
"params": { |
| 1172 |
"foo": { |
| 1173 |
"label": null, |
| 1174 |
"required": false, |
| 1175 |
"suggested": false, |
| 1176 |
"suggestedvalues": [], |
| 1177 |
"description": null, |
| 1178 |
"example": null, |
| 1179 |
"deprecated": false, |
| 1180 |
"aliases": [], |
| 1181 |
"default": null, |
| 1182 |
"type": "unknown", |
| 1183 |
"autovalue": null |
| 1184 |
}, |
| 1185 |
"bar": { |
| 1186 |
"label": null, |
| 1187 |
"required": false, |
| 1188 |
"suggested": false, |
| 1189 |
"suggestedvalues": [], |
| 1190 |
"description": null, |
| 1191 |
"example": null, |
| 1192 |
"deprecated": false, |
| 1193 |
"aliases": [], |
| 1194 |
"default": null, |
| 1195 |
"type": "unknown", |
| 1196 |
"autovalue": null |
| 1197 |
}, |
| 1198 |
"baz": { |
| 1199 |
"label": null, |
| 1200 |
"required": false, |
| 1201 |
"suggested": false, |
| 1202 |
"suggestedvalues": [], |
| 1203 |
"description": null, |
| 1204 |
"example": null, |
| 1205 |
"deprecated": false, |
| 1206 |
"aliases": [], |
| 1207 |
"default": null, |
| 1208 |
"type": "unknown", |
| 1209 |
"autovalue": null |
| 1210 |
} |
| 1211 |
}, |
| 1212 |
"sets": [], |
| 1213 |
"format": null, |
| 1214 |
"maps" : {} |
| 1215 |
} |
| 1216 |
', |
| 1217 |
'msg' => 'Normalisation adds paramOrder' |
| 1218 |
], |
| 1219 |
[ |
| 1220 |
'input' => '{ |
| 1221 |
"params": { |
| 1222 |
"foo": {}, |
| 1223 |
"bar": {}, |
| 1224 |
"baz": {} |
| 1225 |
}, |
| 1226 |
"paramOrder": ["baz", "foo", "bar"] |
| 1227 |
} |
| 1228 |
', |
| 1229 |
'output' => '{ |
| 1230 |
"description": null, |
| 1231 |
"params": { |
| 1232 |
"foo": { |
| 1233 |
"label": null, |
| 1234 |
"required": false, |
| 1235 |
"suggested": false, |
| 1236 |
"suggestedvalues": [], |
| 1237 |
"description": null, |
| 1238 |
"example": null, |
| 1239 |
"deprecated": false, |
| 1240 |
"aliases": [], |
| 1241 |
"default": null, |
| 1242 |
"type": "unknown", |
| 1243 |
"autovalue": null |
| 1244 |
}, |
| 1245 |
"bar": { |
| 1246 |
"label": null, |
| 1247 |
"required": false, |
| 1248 |
"suggested": false, |
| 1249 |
"suggestedvalues": [], |
| 1250 |
"description": null, |
| 1251 |
"example": null, |
| 1252 |
"deprecated": false, |
| 1253 |
"aliases": [], |
| 1254 |
"default": null, |
| 1255 |
"type": "unknown", |
| 1256 |
"autovalue": null |
| 1257 |
}, |
| 1258 |
"baz": { |
| 1259 |
"label": null, |
| 1260 |
"required": false, |
| 1261 |
"suggested": false, |
| 1262 |
"suggestedvalues": [], |
| 1263 |
"description": null, |
| 1264 |
"example": null, |
| 1265 |
"deprecated": false, |
| 1266 |
"aliases": [], |
| 1267 |
"default": null, |
| 1268 |
"type": "unknown", |
| 1269 |
"autovalue": null |
| 1270 |
} |
| 1271 |
}, |
| 1272 |
"paramOrder": ["baz", "foo", "bar"], |
| 1273 |
"sets": [], |
| 1274 |
"format": null, |
| 1275 |
"maps" : {} |
| 1276 |
} |
| 1277 |
', |
| 1278 |
'msg' => 'Custom paramOrder' |
| 1279 |
], |
| 1280 |
[ |
| 1281 |
'input' => '{ "params": {}, "paramOrder": {} }', |
| 1282 |
'status' => '(templatedata-invalid-type: paramOrder, array)', |
| 1283 |
], |
| 1284 |
[ |
| 1285 |
'input' => '{ |
| 1286 |
"params": { |
| 1287 |
"foo": {}, |
| 1288 |
"bar": {}, |
| 1289 |
"baz": {} |
| 1290 |
}, |
| 1291 |
"paramOrder": ["foo", "bar"] |
| 1292 |
} |
| 1293 |
', |
| 1294 |
'status' => '(templatedata-invalid-missing: paramOrder[ "baz" ])', |
| 1295 |
'msg' => 'Incomplete paramOrder' |
| 1296 |
], |
| 1297 |
[ |
| 1298 |
'input' => '{ |
| 1299 |
"params": {} |
| 1300 |
} |
| 1301 |
', |
| 1302 |
'output' => '{ |
| 1303 |
"description": null, |
| 1304 |
"params": {}, |
| 1305 |
"sets": [], |
| 1306 |
"format": null, |
| 1307 |
"maps" : {} |
| 1308 |
} |
| 1309 |
', |
| 1310 |
'msg' => 'Empty parameter object produces empty array paramOrder' |
| 1311 |
], |
| 1312 |
[ |
| 1313 |
'input' => '{ |
| 1314 |
"params": { |
| 1315 |
"foo": {}, |
| 1316 |
"bar": {}, |
| 1317 |
"baz": {} |
| 1318 |
}, |
| 1319 |
"paramOrder": ["foo", "bar", "baz", "quux"] |
| 1320 |
} |
| 1321 |
', |
| 1322 |
'status' => '(templatedata-invalid-value: paramOrder[ "quux" ])', |
| 1323 |
'msg' => 'Unknown params in paramOrder' |
| 1324 |
], |
| 1325 |
[ |
| 1326 |
'input' => '{ |
| 1327 |
"params": { |
| 1328 |
"foo": {}, |
| 1329 |
"bar": {}, |
| 1330 |
"baz": {} |
| 1331 |
}, |
| 1332 |
"paramOrder": ["foo", "bar", "baz", "bar"] |
| 1333 |
} |
| 1334 |
', |
| 1335 |
'status' => '(templatedata-invalid-duplicate-value: paramOrder[3], paramOrder[1], bar)', |
| 1336 |
'msg' => 'Duplicate params in paramOrder' |
| 1337 |
], |
| 1338 |
]; |
| 1339 |
|
| 1340 |
foreach ( $cases as $case ) { |
| 1341 |
yield [ $case ]; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
public function testParamOrder( array $case ) { |
| 1349 |
$this->assertTemplateData( $case ); |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
public function testGetRawParams( string $inputWikitext, array $expectedParams ) { |
| 1357 |
|
| 1358 |
$api = TestingAccessWrapper::newFromObject( $this->createMock( ApiTemplateData::class ) ); |
| 1359 |
$params = $api->getRawParams( $inputWikitext ); |
| 1360 |
$this->assertArrayEquals( $expectedParams, $params, true, true ); |
| 1361 |
} |
| 1362 |
|
| 1363 |
public static function provideGetRawParams() { |
| 1364 |
return [ |
| 1365 |
'No params' => [ |
| 1366 |
'Lorem ipsum {{tpl}}.', |
| 1367 |
[] |
| 1368 |
], |
| 1369 |
'Two plain params' => [ |
| 1370 |
'Lorem {{{name}}} ipsum {{{surname}}}', |
| 1371 |
[ 'name' => [], 'surname' => [] ] |
| 1372 |
], |
| 1373 |
'Param with multiple casing and default value' => [ |
| 1374 |
'Lorem {{{name|{{{Name|Default name}}}}}} ipsum', |
| 1375 |
[ 'name' => [] ] |
| 1376 |
], |
| 1377 |
'Param name contains comment' => [ |
| 1378 |
'Lorem {{{name<!-- comment -->}}} ipsum', |
| 1379 |
[ 'name' => [] ] |
| 1380 |
], |
| 1381 |
'Letter-case and underscore-space normalization' => [ |
| 1382 |
'Lorem {{{First name|{{{first_name}}}}}} ipsum {{{first-Name}}}', |
| 1383 |
[ 'First name' => [] ] |
| 1384 |
], |
| 1385 |
'Dynamic param name' => [ |
| 1386 |
'{{{{{#if:{{{nominee|}}}|nominee|candidate}}|}}}', |
| 1387 |
[ 'nominee' => [] ] |
| 1388 |
], |
| 1389 |
'More complicated dynamic param name' => [ |
| 1390 |
'{{{party{{#if:{{{party_election||}}}|_election||}}|}}}', |
| 1391 |
[ 'party_election' => [] ] |
| 1392 |
], |
| 1393 |
'Bang in a param name' => [ |
| 1394 |
'{{{!}}} {{{foo!}}}', |
| 1395 |
[ '!' => [], 'foo!' => [] ] |
| 1396 |
], |
| 1397 |
'Bang as a magic word in a table construct' => [ |
| 1398 |
'{{{!}} class=""', |
| 1399 |
[] |
| 1400 |
], |
| 1401 |
'Params within comments and nowiki tags' => [ |
| 1402 |
'Lorem <!-- {{{name}}} --> ipsum <nowiki > {{{middlename}}}' . |
| 1403 |
'</nowiki> <pre>{{{pre}}}</pre> {{{surname}}}', |
| 1404 |
[ 'surname' => [] ] |
| 1405 |
], |
| 1406 |
'Param within comments and param name outside with comment' => [ |
| 1407 |
'Lorem {{{name<!--comment-->}}} ipsum <!--{{{surname}}}-->', |
| 1408 |
[ 'name' => [] ] |
| 1409 |
], |
| 1410 |
'safesubst: hack with an unnamed parameter' => [ |
| 1411 |
'{{ {{{|safesubst:}}}#invoke:…|{{{1}}}|{{{ 1 }}}}}', |
| 1412 |
[ '1' => [] ] |
| 1413 |
], |
| 1414 |
'Characters impossible in parameter names' => [ |
| 1415 |
'{{test|a|b=c|d=e=f}} {{{a|b}}} {{{d=e}}}', |
| 1416 |
[ 'a' => [] ] |
| 1417 |
], |
| 1418 |
'Characters that are, while technically possible, almost certainly a mistake' => [ |
| 1419 |
"{{{a{a}}} {{{b}b}}} {{{c\nc}}}", |
| 1420 |
[] |
| 1421 |
], |
| 1422 |
'Table syntax escaped with {{!}}' => [ |
| 1423 |
'{{{!}}\n! This is table syntax\n{{!}}}', |
| 1424 |
[] |
| 1425 |
], |
| 1426 |
]; |
| 1427 |
} |
| 1428 |
|
| 1429 |
public static function provideGetHtml() { |
| 1430 |
|
| 1431 |
yield 'No params' => [ |
| 1432 |
[ 'params' => [ (object)[] ] ], |
| 1433 |
<<<HTML |
| 1434 |
<section class="mw-templatedata-doc-wrap"> |
| 1435 |
<header><p class="mw-templatedata-doc-desc mw-templatedata-doc-muted">(templatedata-doc-desc-empty)</p></header> |
| 1436 |
<table class="wikitable mw-templatedata-doc-params"> |
| 1437 |
<caption> |
| 1438 |
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p> |
| 1439 |
</caption> |
| 1440 |
<thead><tr><th colspan="2">(templatedata-doc-param-name)</th><th>(templatedata-doc-param-desc)</th><th>(templatedata-doc-param-type)</th><th>(templatedata-doc-param-status)</th></tr></thead> |
| 1441 |
<tbody> |
| 1442 |
<tr> |
| 1443 |
<td class="mw-templatedata-doc-muted" colspan="7">(templatedata-doc-no-params-set)</td> |
| 1444 |
</tr> |
| 1445 |
</tbody> |
| 1446 |
</table> |
| 1447 |
</section> |
| 1448 |
HTML |
| 1449 |
]; |
| 1450 |
yield 'Basic params' => [ |
| 1451 |
[ 'params' => [ 'foo' => (object)[], 'bar' => [ 'required' => true ] ] ], |
| 1452 |
<<<HTML |
| 1453 |
<section class="mw-templatedata-doc-wrap"> |
| 1454 |
<header><p class="mw-templatedata-doc-desc mw-templatedata-doc-muted">(templatedata-doc-desc-empty)</p></header> |
| 1455 |
<table class="wikitable mw-templatedata-doc-params sortable"> |
| 1456 |
<caption> |
| 1457 |
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p> |
| 1458 |
</caption> |
| 1459 |
<thead><tr><th colspan="2">(templatedata-doc-param-name)</th><th>(templatedata-doc-param-desc)</th><th>(templatedata-doc-param-type)</th><th>(templatedata-doc-param-status)</th></tr></thead> |
| 1460 |
<tbody> |
| 1461 |
<tr> |
| 1462 |
<th>foo</th> |
| 1463 |
<td class="mw-templatedata-doc-param-name"><code>foo</code></td> |
| 1464 |
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td> |
| 1465 |
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td> |
| 1466 |
<td class="mw-templatedata-doc-param-status-optional" data-sort-value="0">(templatedata-doc-param-status-optional)</td> |
| 1467 |
</tr> |
| 1468 |
<tr> |
| 1469 |
<th>bar</th> |
| 1470 |
<td class="mw-templatedata-doc-param-name"><code>bar</code></td> |
| 1471 |
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td> |
| 1472 |
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td> |
| 1473 |
<td class="mw-templatedata-doc-param-status-required" data-sort-value="2">(templatedata-doc-param-status-required)</td> |
| 1474 |
</tr> |
| 1475 |
</tbody> |
| 1476 |
</table> |
| 1477 |
</section> |
| 1478 |
HTML |
| 1479 |
]; |
| 1480 |
yield [ |
| 1481 |
[ 'description' => 'Template docs', 'params' => [ |
| 1482 |
'suggestedParam' => [ |
| 1483 |
'label' => 'Label', |
| 1484 |
'description' => 'Param docs', |
| 1485 |
'aliases' => [ 'Alias1', 'Alias2' ], |
| 1486 |
'suggestedvalues' => [ 'Suggested1', 'Suggested2' ], |
| 1487 |
'suggested' => true, |
| 1488 |
'default' => 'Default docs', |
| 1489 |
'example' => 'Example docs', |
| 1490 |
'autovalue' => 'Auto value', |
| 1491 |
], |
| 1492 |
'deprecatedParam' => [ 'type' => 'date', 'deprecated' => true ], |
| 1493 |
] ], |
| 1494 |
<<<HTML |
| 1495 |
<section class="mw-templatedata-doc-wrap"> |
| 1496 |
<header><p class="mw-templatedata-doc-desc">Template docs</p></header> |
| 1497 |
<table class="wikitable mw-templatedata-doc-params sortable"> |
| 1498 |
<caption> |
| 1499 |
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p> |
| 1500 |
</caption> |
| 1501 |
<thead><tr><th colspan="2">(templatedata-doc-param-name)</th><th>(templatedata-doc-param-desc)</th><th>(templatedata-doc-param-type)</th><th>(templatedata-doc-param-status)</th></tr></thead> |
| 1502 |
<tbody> |
| 1503 |
<tr> |
| 1504 |
<th>Label</th> |
| 1505 |
<td class="mw-templatedata-doc-param-name"><code>suggestedParam</code> <code class="mw-templatedata-doc-param-alias">Alias1</code> <code class="mw-templatedata-doc-param-alias">Alias2</code></td> |
| 1506 |
<td> |
| 1507 |
<p>Param docs</p> |
| 1508 |
<dl> |
| 1509 |
<dt>(templatedata-doc-param-suggestedvalues)</dt><dd><code>Suggested1</code> <code>Suggested2</code></dd> |
| 1510 |
<dt>(templatedata-doc-param-default)</dt><dd>Default docs</dd> |
| 1511 |
<dt>(templatedata-doc-param-example)</dt><dd>Example docs</dd> |
| 1512 |
<dt>(templatedata-doc-param-autovalue)</dt><dd><code>Auto value</code></dd> |
| 1513 |
</dl> |
| 1514 |
</td> |
| 1515 |
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td> |
| 1516 |
<td class="mw-templatedata-doc-param-status-suggested" data-sort-value="1">(templatedata-doc-param-status-suggested)</td> |
| 1517 |
</tr> |
| 1518 |
<tr> |
| 1519 |
<th>deprecatedParam</th> |
| 1520 |
<td class="mw-templatedata-doc-param-name"><code>deprecatedParam</code></td> |
| 1521 |
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td> |
| 1522 |
<td class="mw-templatedata-doc-param-type">(templatedata-doc-param-type-date)</td> |
| 1523 |
<td class="mw-templatedata-doc-param-status-deprecated" data-sort-value="-1">(templatedata-doc-param-status-deprecated)</td> |
| 1524 |
</tr> |
| 1525 |
</tbody> |
| 1526 |
</table> |
| 1527 |
</section> |
| 1528 |
HTML |
| 1529 |
]; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
public function testGetHtml( array $data, string $expected ) { |
| 1537 |
$t = TemplateDataBlob::newFromJSON( $this->db, json_encode( $data ) ); |
| 1538 |
$localizer = new class implements MessageLocalizer { |
| 1539 |
|
| 1540 |
public function msg( $key, ...$params ) { |
| 1541 |
return new RawMessage( "($key)" ); |
| 1542 |
} |
| 1543 |
|
| 1544 |
}; |
| 1545 |
$title = Title::newFromText( 'Template:Test/doc' ); |
| 1546 |
$formatter = new TemplateDataHtmlFormatter( $localizer ); |
| 1547 |
$actual = $formatter->getHtml( $t, $title ); |
| 1548 |
$linedActual = preg_replace( '/>\s*</', ">\n<", $actual ); |
| 1549 |
|
| 1550 |
$linedExpected = preg_replace( '/>\s*</', ">\n<", trim( $expected ) ); |
| 1551 |
|
| 1552 |
$this->assertSame( $linedExpected, $linedActual ); |
| 1553 |
} |
| 1554 |
|
| 1555 |
} |