2013-04-16 14:18:10 +00:00
|
|
|
<?php
|
2014-07-07 22:54:25 +00:00
|
|
|
|
2021-12-23 09:45:11 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\Api\ApiTemplateData;
|
2021-11-25 21:35:46 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\TemplateDataBlob;
|
2022-02-03 08:31:50 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter;
|
2021-12-23 09:10:28 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\TemplateDataValidator;
|
2024-01-04 20:53:10 +00:00
|
|
|
use MediaWiki\Language\RawMessage;
|
2024-04-08 11:27:18 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-01-04 20:53:10 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-08-19 04:19:39 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-08-19 13:29:55 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
2014-07-07 22:54:25 +00:00
|
|
|
/**
|
2019-07-08 01:46:33 +00:00
|
|
|
* @group Database
|
2021-11-25 21:35:46 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataBlob
|
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataCompressedBlob
|
2021-12-23 15:08:01 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataNormalizer
|
2021-12-23 09:10:28 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataValidator
|
2023-08-23 07:29:25 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2014-07-07 22:54:25 +00:00
|
|
|
*/
|
2021-08-28 10:48:23 +00:00
|
|
|
class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
|
2013-04-16 14:18:10 +00:00
|
|
|
|
2021-07-23 22:51:45 +00:00
|
|
|
protected function setUp(): void {
|
2013-04-16 14:18:10 +00:00
|
|
|
parent::setUp();
|
2021-08-28 10:48:23 +00:00
|
|
|
$this->setMwGlobals( 'wgLanguageCode', 'qqx' );
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
/**
|
|
|
|
* Helper method to generate a string that gzip can't compress.
|
|
|
|
*
|
|
|
|
* Output is consistent when given the same seed.
|
2021-08-28 09:37:48 +00:00
|
|
|
* @param int $minLength
|
2023-06-26 16:08:42 +00:00
|
|
|
* @param int $seed
|
2021-01-17 19:17:57 +00:00
|
|
|
* @return string
|
2013-07-23 13:33:45 +00:00
|
|
|
*/
|
2023-06-26 16:08:42 +00:00
|
|
|
private function generatePseudorandomString( int $minLength, int $seed ): string {
|
2021-08-28 09:37:48 +00:00
|
|
|
srand( $seed );
|
2013-07-23 13:33:45 +00:00
|
|
|
$string = '';
|
2021-08-28 09:37:48 +00:00
|
|
|
while ( strlen( $string ) < $minLength ) {
|
|
|
|
$string .= str_shuffle( '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' );
|
2013-07-23 13:33:45 +00:00
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2013-10-06 16:31:20 +00:00
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideParse() {
|
2016-05-10 00:00:14 +00:00
|
|
|
$cases = [
|
2022-02-03 11:45:27 +00:00
|
|
|
[
|
|
|
|
'input' => '{',
|
2024-04-08 10:51:10 +00:00
|
|
|
'status' => 'templatedata-invalid-parse'
|
2022-02-03 11:45:27 +00:00
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '[]
|
2013-04-16 14:18:10 +00:00
|
|
|
',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-type: templatedata, object)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
2013-05-15 22:15:52 +00:00
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-05-15 23:05:06 +00:00
|
|
|
}
|
2013-04-16 14:18:10 +00:00
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'status' => true,
|
|
|
|
'msg' => 'Minimal valid blob'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
2013-04-16 14:18:10 +00:00
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-unknown: foo)',
|
2013-05-15 23:05:06 +00:00
|
|
|
'msg' => 'Unknown properties'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "description": [], "params": {} }',
|
|
|
|
'status' => '(templatedata-invalid-type: description, string|object)',
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-missing: params, object)',
|
2013-05-15 23:05:06 +00:00
|
|
|
'msg' => 'Empty object'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2021-08-28 11:27:15 +00:00
|
|
|
'input' => '{ "params": [] }',
|
|
|
|
'status' => '(templatedata-invalid-type: params, object)',
|
|
|
|
],
|
2023-04-11 16:20:31 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "": {} } }',
|
2024-04-08 10:51:10 +00:00
|
|
|
'status' => 'templatedata-invalid-unnamed-parameter',
|
2023-04-11 16:20:31 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": [] } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a, object)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "foo": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-unknown: params.a.foo)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "label": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.label, string|object)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "required": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.required, boolean)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "suggested": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.suggested, boolean)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "description": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.description, string|object)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "example": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.example, string|object)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "deprecated": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.deprecated, boolean|string)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "aliases": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.aliases, array)',
|
|
|
|
],
|
2022-01-10 11:13:05 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "aliases": [ "1", 2, {} ] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.aliases[2], int|string)',
|
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "autovalue": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.autovalue, string)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "default": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.default, string|object)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "type": [] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.type, string)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "type": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-value: params.a.type)',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "suggestedvalues": "" } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.suggestedvalues, array)',
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2022-01-10 11:13:05 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "a": { "suggestedvalues": [ {} ] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: params.a.suggestedvalues[0], string)',
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{
|
2013-04-16 14:18:10 +00:00
|
|
|
"params": {
|
|
|
|
"foo": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'output' => '{
|
2013-05-08 23:56:57 +00:00
|
|
|
"description": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"params": {
|
|
|
|
"foo": {
|
2013-05-08 23:56:57 +00:00
|
|
|
"label": null,
|
|
|
|
"description": null,
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-04-16 14:18:10 +00:00
|
|
|
"deprecated": false,
|
2013-05-08 23:56:57 +00:00
|
|
|
"aliases": [],
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps": {}
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'msg' => 'Optional properties are added if missing'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-24 16:39:04 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"comment": {
|
|
|
|
"type": "string/line"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"comment": {
|
|
|
|
"label": null,
|
|
|
|
"description": null,
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"autovalue": null,
|
2013-10-24 16:39:04 +00:00
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-24 16:39:04 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
|
|
|
"type": "line"
|
|
|
|
}
|
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps": {}
|
2013-10-24 16:39:04 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Old string/* types are mapped to the unprefixed versions'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{
|
2013-04-16 14:18:10 +00:00
|
|
|
"description": "User badge MediaWiki developers.",
|
|
|
|
"params": {
|
|
|
|
"nickname": {
|
2013-05-08 23:56:57 +00:00
|
|
|
"label": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"description": "User name of user who owns the badge",
|
|
|
|
"default": "Base page name of the host page",
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": true,
|
2022-01-10 11:13:05 +00:00
|
|
|
"aliases": [ 1 ]
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'output' => '{
|
2013-04-16 14:18:10 +00:00
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "User badge MediaWiki developers."
|
2013-04-16 14:18:10 +00:00
|
|
|
},
|
|
|
|
"params": {
|
|
|
|
"nickname": {
|
2013-05-08 23:56:57 +00:00
|
|
|
"label": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "User name of user who owns the badge"
|
2013-04-16 14:18:10 +00:00
|
|
|
},
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Base page name of the host page"
|
2015-03-09 21:45:04 +00:00
|
|
|
},
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-04-16 14:18:10 +00:00
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": true,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-04-16 14:18:10 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [
|
|
|
|
"1"
|
2013-05-08 23:56:57 +00:00
|
|
|
],
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps": {}
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": { "b": { "inherits": "a" } } }',
|
|
|
|
'status' => '(templatedata-invalid-missing: params.a)',
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 23:05:06 +00:00
|
|
|
'input' => '{
|
2013-04-30 22:32:08 +00:00
|
|
|
"description": "Document the documenter.",
|
|
|
|
"params": {
|
|
|
|
"1d": {
|
|
|
|
"description": "Description of the template parameter",
|
|
|
|
"required": true,
|
|
|
|
"default": "example"
|
|
|
|
},
|
|
|
|
"2d": {
|
|
|
|
"inherits": "1d",
|
|
|
|
"default": "overridden"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
2013-05-15 23:05:06 +00:00
|
|
|
'output' => '{
|
2013-04-30 22:32:08 +00:00
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Document the documenter."
|
2013-04-30 22:32:08 +00:00
|
|
|
},
|
|
|
|
"params": {
|
|
|
|
"1d": {
|
2013-05-08 23:56:57 +00:00
|
|
|
"label": null,
|
2013-04-30 22:32:08 +00:00
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Description of the template parameter"
|
2013-04-30 22:32:08 +00:00
|
|
|
},
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-04-30 22:32:08 +00:00
|
|
|
"required": true,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "example"
|
2015-03-09 21:45:04 +00:00
|
|
|
},
|
2013-04-30 22:32:08 +00:00
|
|
|
"deprecated": false,
|
2013-05-08 23:56:57 +00:00
|
|
|
"aliases": [],
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-04-30 22:32:08 +00:00
|
|
|
},
|
|
|
|
"2d": {
|
2013-05-08 23:56:57 +00:00
|
|
|
"label": null,
|
2013-04-30 22:32:08 +00:00
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Description of the template parameter"
|
2013-04-30 22:32:08 +00:00
|
|
|
},
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-04-30 22:32:08 +00:00
|
|
|
"required": true,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "overridden"
|
2015-03-09 21:45:04 +00:00
|
|
|
},
|
2013-04-30 22:32:08 +00:00
|
|
|
"deprecated": false,
|
2013-05-08 23:56:57 +00:00
|
|
|
"aliases": [],
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-04-30 22:32:08 +00:00
|
|
|
}
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-04-30 22:32:08 +00:00
|
|
|
}
|
|
|
|
',
|
2013-05-16 15:01:00 +00:00
|
|
|
'msg' => 'The inherits property copies over properties from another parameter '
|
|
|
|
. '(preserving overides)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "sets": {} }',
|
|
|
|
'status' => '(templatedata-invalid-type: sets, array)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "sets": [ [] ] }',
|
|
|
|
'status' => '(templatedata-invalid-value: sets.0)'
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 22:15:52 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": "Example"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-missing: sets.0.params, array)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "sets": [ { "label": "", "params": {} } ] }',
|
|
|
|
'status' => '(templatedata-invalid-type: sets.0.params, array)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "sets": [ { "label": "", "params": [] } ] }',
|
|
|
|
'status' => '(templatedata-invalid-empty-array: sets.0.params)'
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 22:15:52 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"params": ["foo"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-missing: sets.0.label, string|object)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "sets": [ { "label": [] } ] }',
|
|
|
|
'status' => '(templatedata-invalid-type: sets.0.label, string|object)'
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-05-15 22:15:52 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": "Foo with Quux",
|
|
|
|
"params": ["foo", "quux"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-value: sets.0.params[1])'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-05-15 22:15:52 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
},
|
|
|
|
"quux": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": "Foo with Quux",
|
|
|
|
"params": ["foo", "quux"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": "Bar with Quux",
|
|
|
|
"params": ["bar", "quux"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-05-15 22:15:52 +00:00
|
|
|
"description": null,
|
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-05-15 22:15:52 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-05-15 22:15:52 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
|
|
|
"quux": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-05-15 22:15:52 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-05-15 22:15:52 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Foo with Quux"
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
|
|
|
"params": ["foo", "quux"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Bar with Quux"
|
2013-05-15 22:15:52 +00:00
|
|
|
},
|
|
|
|
"params": ["bar", "quux"]
|
|
|
|
}
|
2014-10-18 15:28:26 +00:00
|
|
|
],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps": {}
|
2013-05-15 22:15:52 +00:00
|
|
|
}',
|
|
|
|
'status' => true
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2014-09-02 15:29:33 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": "Testing some template description.",
|
|
|
|
"params": {
|
|
|
|
"bar": {
|
|
|
|
"label": "Bar label",
|
|
|
|
"description": "Bar description",
|
|
|
|
"default": "Baz",
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": "Foo bar baz",
|
2014-09-02 15:29:33 +00:00
|
|
|
"autovalue": "{{SomeTemplate}}",
|
|
|
|
"required": true,
|
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [ "baz", "boo" ],
|
2014-09-02 15:29:33 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [ "foo", "baz" ],
|
|
|
|
"type": "line"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Testing some template description."
|
2014-09-02 15:29:33 +00:00
|
|
|
},
|
|
|
|
"params": {
|
|
|
|
"bar": {
|
|
|
|
"label": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Bar label"
|
2014-09-02 15:29:33 +00:00
|
|
|
},
|
|
|
|
"description": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Bar description"
|
2014-09-02 15:29:33 +00:00
|
|
|
},
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Baz"
|
2015-03-09 21:45:04 +00:00
|
|
|
},
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": {
|
2021-08-28 10:48:23 +00:00
|
|
|
"qqx": "Foo bar baz"
|
2015-03-04 20:32:38 +00:00
|
|
|
},
|
2014-09-02 15:29:33 +00:00
|
|
|
"autovalue": "{{SomeTemplate}}",
|
|
|
|
"required": true,
|
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [ "baz", "boo" ],
|
2014-09-02 15:29:33 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [ "foo", "baz" ],
|
|
|
|
"type": "line"
|
|
|
|
}
|
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2014-09-02 15:29:33 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Parameter attributes preserve information.'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "maps": [] }',
|
|
|
|
'status' => '(templatedata-invalid-type: maps, object)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "maps": { "a": [] } }',
|
|
|
|
'status' => '(templatedata-invalid-type: maps.a, object)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "maps": { "a": { "b": "c" } } }',
|
|
|
|
'status' => '(templatedata-invalid-param: c, maps.a.b)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "maps": { "a": { "b": [ {} ] } } }',
|
|
|
|
'status' => '(templatedata-invalid-type: maps.a.b[0], string|array)'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "maps": { "a": { "b": [ "c" ] } } }',
|
|
|
|
'status' => '(templatedata-invalid-param: c, maps.a.b[0])'
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2014-10-18 15:28:26 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [],
|
|
|
|
"maps": {
|
|
|
|
"application": {
|
|
|
|
"things": [
|
|
|
|
"foo",
|
|
|
|
["bar", "quux"]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2021-08-28 11:27:15 +00:00
|
|
|
'status' => '(templatedata-invalid-param: quux, maps.application.things[1][1])'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2014-10-18 15:28:26 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [],
|
|
|
|
"maps": {
|
|
|
|
"application": {
|
|
|
|
"things": {
|
|
|
|
"appbar": "bar",
|
|
|
|
"appfoo": "foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-type: maps.application.things, string|array)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2014-10-18 15:28:26 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [],
|
|
|
|
"maps": {
|
|
|
|
"application": {
|
|
|
|
"things": [
|
|
|
|
[ true ]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-type: maps.application.things[0][0], string)'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2022-07-15 09:40:19 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "format": "" }',
|
|
|
|
'status' => '(templatedata-invalid-format: format)'
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2015-10-02 10:34:57 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {}
|
|
|
|
},
|
|
|
|
"format": "meshuggah format"
|
|
|
|
}',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-format: format)'
|
2016-08-07 01:03:53 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
|
|
|
"format": "inline"
|
|
|
|
}',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
|
|
|
"sets": [],
|
|
|
|
"format": "inline",
|
|
|
|
"maps": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => '"inline" is a valid format string',
|
|
|
|
'status' => true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
|
|
|
"format": "block"
|
|
|
|
}',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
|
|
|
"sets": [],
|
|
|
|
"format": "block",
|
|
|
|
"maps": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => '"block" is a valid format string',
|
|
|
|
'status' => true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
|
|
|
"format": "{{_ |\n ___ = _}}"
|
|
|
|
}',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
|
|
|
"sets": [],
|
|
|
|
"format": "{{_ |\n ___ = _}}",
|
|
|
|
"maps": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Custom parameter format string (1)',
|
|
|
|
'status' => true
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'input' => '{
|
|
|
|
"params": {},
|
|
|
|
"format": "{{_|_=_\n}}\n"
|
|
|
|
}',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
|
|
|
"sets": [],
|
|
|
|
"format": "{{_|_=_\n}}\n",
|
|
|
|
"maps": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Custom parameter format string (2)',
|
|
|
|
'status' => true
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
];
|
2019-07-08 01:46:33 +00:00
|
|
|
|
2013-05-15 23:05:06 +00:00
|
|
|
foreach ( $cases as $case ) {
|
2023-08-23 07:18:16 +00:00
|
|
|
yield [ $case ];
|
2013-05-15 23:05:06 +00:00
|
|
|
}
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:41:28 +00:00
|
|
|
private function getStatusText( Status $status ): string {
|
2018-08-13 16:57:20 +00:00
|
|
|
// Unescape char references for things like "[, "]" and "|" for
|
|
|
|
// cleaner test assertions and output
|
2024-04-08 10:51:10 +00:00
|
|
|
return html_entity_decode( $status->getMessage()->plain() );
|
2013-12-10 21:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 16:08:42 +00:00
|
|
|
private function ksort( array &$input ): void {
|
2014-09-02 15:29:33 +00:00
|
|
|
ksort( $input );
|
2021-08-28 10:10:22 +00:00
|
|
|
foreach ( $input as &$value ) {
|
2014-09-02 15:29:33 +00:00
|
|
|
if ( is_array( $value ) ) {
|
2021-10-02 08:43:07 +00:00
|
|
|
$this->ksort( $value );
|
2014-09-02 15:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHPUnit'a assertEquals does weak comparison, use strict instead.
|
|
|
|
*
|
|
|
|
* There is a built-in assertSame, but that only strictly compares
|
|
|
|
* the top level structure, not the invidual array values.
|
|
|
|
*
|
|
|
|
* so "array( 'a' => '' )" still equals "array( 'a' => null )"
|
|
|
|
* because empty string equals null in PHP's weak comparison.
|
|
|
|
*
|
2023-06-26 16:08:42 +00:00
|
|
|
* @param string $expected
|
2023-06-26 16:22:43 +00:00
|
|
|
* @param stdClass $actual
|
2021-01-17 19:17:57 +00:00
|
|
|
* @param string|null $message
|
2014-09-02 15:29:33 +00:00
|
|
|
*/
|
2023-06-26 16:22:43 +00:00
|
|
|
private function assertStrictJsonEquals( string $expected, stdClass $actual, string $message = null ): void {
|
2014-09-02 15:29:33 +00:00
|
|
|
// Lazy recursive strict comparison: Serialise to JSON and compare that
|
|
|
|
// Sort first to ensure key-order
|
|
|
|
$expected = json_decode( $expected, /* assoc = */ true );
|
2023-06-26 16:22:43 +00:00
|
|
|
$actual = json_decode( json_encode( $actual ), /* assoc = */ true );
|
2021-10-02 08:43:07 +00:00
|
|
|
$this->ksort( $expected );
|
|
|
|
$this->ksort( $actual );
|
2014-09-02 15:29:33 +00:00
|
|
|
|
2021-08-28 09:08:24 +00:00
|
|
|
$this->assertSame(
|
2014-09-02 15:29:33 +00:00
|
|
|
FormatJson::encode( $expected, true ),
|
|
|
|
FormatJson::encode( $actual, true ),
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-26 16:08:42 +00:00
|
|
|
private function assertTemplateData( array $case ): void {
|
2013-05-15 23:05:06 +00:00
|
|
|
// Expand defaults
|
2023-08-23 07:24:23 +00:00
|
|
|
$case['status'] ??= true;
|
|
|
|
$case['msg'] ??= $case['status'];
|
2013-04-16 14:18:10 +00:00
|
|
|
|
2019-07-08 01:46:33 +00:00
|
|
|
$t = TemplateDataBlob::newFromJSON( $this->db, $case['input'] );
|
2023-06-26 16:22:43 +00:00
|
|
|
$actual = $t->getData();
|
2013-04-16 14:18:10 +00:00
|
|
|
$status = $t->getStatus();
|
2014-09-02 15:29:33 +00:00
|
|
|
|
2024-04-08 10:51:10 +00:00
|
|
|
if ( $case['status'] === true ) {
|
|
|
|
$this->assertStatusGood( $status );
|
|
|
|
} elseif ( !str_starts_with( $case['status'], '(' ) ) {
|
|
|
|
$this->assertStatusError( $case['status'], $status );
|
|
|
|
} else {
|
|
|
|
$this->assertSame( $case['status'], $this->getStatusText( $status ), $case['msg'] );
|
|
|
|
}
|
2013-10-24 16:34:04 +00:00
|
|
|
|
2021-08-28 10:35:15 +00:00
|
|
|
if ( !isset( $case['output'] ) ) {
|
|
|
|
$expected = is_string( $case['status'] )
|
|
|
|
? '{ "description": null, "params": {}, "sets": [], "maps": {}, "format": null }'
|
|
|
|
: $case['input'];
|
|
|
|
$this->assertStrictJsonEquals( $expected, $actual, $case['msg'] );
|
|
|
|
} else {
|
|
|
|
$this->assertStrictJsonEquals( $case['output'], $actual, $case['msg'] );
|
2013-10-24 16:34:04 +00:00
|
|
|
|
2021-08-28 10:35:15 +00:00
|
|
|
// Assert this case roundtrips properly by running through the output as input.
|
|
|
|
$t = TemplateDataBlob::newFromJSON( $this->db, $case['output'] );
|
|
|
|
$status = $t->getStatus();
|
2013-10-24 16:34:04 +00:00
|
|
|
|
2021-08-28 10:35:15 +00:00
|
|
|
if ( !$status->isGood() ) {
|
2021-10-02 08:43:07 +00:00
|
|
|
$this->assertSame( $case['status'], $this->getStatusText( $status ),
|
2021-08-28 10:35:15 +00:00
|
|
|
'Roundtrip status: ' . $case['msg']
|
|
|
|
);
|
|
|
|
}
|
2023-06-26 16:22:43 +00:00
|
|
|
$this->assertStrictJsonEquals( $case['output'], $t->getData(),
|
2021-08-28 10:35:15 +00:00
|
|
|
'Roundtrip: ' . $case['msg']
|
2013-10-24 16:34:04 +00:00
|
|
|
);
|
|
|
|
}
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
2013-09-12 04:02:37 +00:00
|
|
|
|
2013-10-15 19:40:09 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideParse
|
|
|
|
*/
|
2016-05-10 00:00:14 +00:00
|
|
|
public function testParse( array $case ) {
|
2013-10-15 19:40:09 +00:00
|
|
|
$this->assertTemplateData( $case );
|
|
|
|
}
|
|
|
|
|
2019-07-08 01:46:33 +00:00
|
|
|
/**
|
|
|
|
* MySQL breaks if the input is too large even after compression
|
|
|
|
*/
|
|
|
|
public function testParseLongString() {
|
2024-07-05 15:44:05 +00:00
|
|
|
if ( $this->getDb()->getType() !== 'mysql' ) {
|
2019-07-08 01:46:33 +00:00
|
|
|
$this->markTestSkipped( 'long compressed strings break on MySQL only' );
|
|
|
|
}
|
2021-08-28 09:37:48 +00:00
|
|
|
|
|
|
|
// Should be long enough to trigger this condition after gzipping.
|
|
|
|
$json = '{
|
2021-10-02 08:43:07 +00:00
|
|
|
"description": "' . $this->generatePseudorandomString( 100000, 42 ) . '",
|
2021-08-28 09:37:48 +00:00
|
|
|
"params": {}
|
|
|
|
}';
|
|
|
|
$templateData = TemplateDataBlob::newFromJSON( $this->db, $json );
|
|
|
|
|
2024-04-08 10:51:10 +00:00
|
|
|
$this->assertStatusError( 'templatedata-invalid-length',
|
|
|
|
$templateData->getStatus() );
|
2019-07-08 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:29:55 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideInterfaceTexts
|
|
|
|
*/
|
|
|
|
public function testIsValidInterfaceText( $text, bool $expected ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
/** @var TemplateDataValidator $validator */
|
|
|
|
$validator = TestingAccessWrapper::newFromObject(
|
2021-12-23 15:08:01 +00:00
|
|
|
new TemplateDataValidator( [] )
|
2020-08-19 13:29:55 +00:00
|
|
|
);
|
2021-12-23 09:10:28 +00:00
|
|
|
$this->assertSame( $expected, $validator->isValidInterfaceText( $text ) );
|
2020-08-19 13:29:55 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideInterfaceTexts() {
|
2020-08-19 13:29:55 +00:00
|
|
|
return [
|
|
|
|
// Invalid stuff
|
|
|
|
[ null, false ],
|
|
|
|
[ [], false ],
|
|
|
|
[ [ 'en' => 'example' ], false ],
|
|
|
|
[ (object)[], false ],
|
|
|
|
[ (object)[ null ], false ],
|
|
|
|
[ (object)[ 'en' => null ], false ],
|
|
|
|
|
|
|
|
[ 'example', true ],
|
|
|
|
[ (object)[ 'de' => 'Beispiel', 'en' => 'example' ], true ],
|
|
|
|
|
|
|
|
// Empty strings are allowed
|
|
|
|
[ '', true ],
|
|
|
|
[ (object)[ 'en' => '' ], true ],
|
|
|
|
|
|
|
|
// Language code can not be empty
|
|
|
|
[ (object)[ '' => 'example' ], false ],
|
|
|
|
[ (object)[ ' ' => 'example' ], false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-09-12 04:02:37 +00:00
|
|
|
/**
|
2024-03-12 15:39:31 +00:00
|
|
|
* Verify we can gzdecode() which came in PHP 5.4.0. MediaWiki needs a
|
2013-09-12 04:02:37 +00:00
|
|
|
* fallback function for it.
|
|
|
|
* If this test fail, we are most probably attempting to use gzdecode()
|
|
|
|
* with PHP before 5.4.
|
|
|
|
*
|
2019-07-08 01:46:33 +00:00
|
|
|
* @see bug T56058
|
|
|
|
*
|
|
|
|
* Some databases will not be able to store compressed data cleanly
|
|
|
|
* but the object will be initialized properly even if compressed
|
|
|
|
* data are provided
|
|
|
|
*
|
|
|
|
* @see bug T203850
|
2013-09-12 04:02:37 +00:00
|
|
|
*/
|
|
|
|
public function testGetJsonForDatabase() {
|
|
|
|
// Compress JSON to trigger the code pass in newFromDatabase that ends
|
|
|
|
// up calling gzdecode().
|
|
|
|
$gzJson = gzencode( '{}' );
|
2019-07-08 01:46:33 +00:00
|
|
|
$templateData = TemplateDataBlob::newFromDatabase( $this->db, $gzJson );
|
2021-11-25 21:35:46 +00:00
|
|
|
$this->assertInstanceOf( TemplateDataBlob::class, $templateData );
|
2013-09-12 04:02:37 +00:00
|
|
|
}
|
2013-10-06 16:31:20 +00:00
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideGetDataInLanguage() {
|
2016-05-10 00:00:14 +00:00
|
|
|
$cases = [
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": {
|
|
|
|
"de": "German",
|
|
|
|
"nl": "Dutch",
|
2021-08-31 13:26:52 +00:00
|
|
|
"en": "English",
|
2013-10-06 16:31:20 +00:00
|
|
|
"de-formal": "German (formal address)"
|
|
|
|
},
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": "German",
|
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'de',
|
|
|
|
'msg' => 'Simple description'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": "Hi",
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": "Hi",
|
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
2021-08-31 13:26:52 +00:00
|
|
|
'msg' => 'Non multi-language value returned as is (expands to { "en": value } for' .
|
|
|
|
' content-lang, "fr" falls back to "en")'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": {
|
|
|
|
"nl": "Dutch",
|
|
|
|
"de": "German"
|
|
|
|
},
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": "Dutch",
|
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Try content language before giving up on user language and fallbacks'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": {
|
|
|
|
"es": "Spanish",
|
|
|
|
"de": "German"
|
|
|
|
},
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Description is optional, use null if no suitable fallback'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"description": {
|
|
|
|
"de": "German",
|
|
|
|
"nl": "Dutch",
|
2021-08-31 13:26:52 +00:00
|
|
|
"en": "English"
|
2013-10-06 16:31:20 +00:00
|
|
|
},
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": "German",
|
|
|
|
"params": {},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'de-formal',
|
|
|
|
'msg' => '"de-formal" falls back to "de"'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": {
|
|
|
|
"fr": "French",
|
2021-08-31 13:26:52 +00:00
|
|
|
"en": "English"
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": "French",
|
|
|
|
"required": false,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-06 16:31:20 +00:00
|
|
|
"description": null,
|
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Simple parameter label'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2015-03-09 21:45:04 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"default": {
|
|
|
|
"fr": "French",
|
2021-08-31 13:26:52 +00:00
|
|
|
"en": "English"
|
2015-03-09 21:45:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"default": "French",
|
|
|
|
"required": false,
|
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"description": null,
|
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
|
|
|
"label": null,
|
|
|
|
"type": "unknown",
|
2015-03-04 20:32:38 +00:00
|
|
|
"autovalue": null,
|
|
|
|
"example": null
|
2015-03-09 21:45:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2015-03-09 21:45:04 +00:00
|
|
|
"maps" : {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Simple parameter default value'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": {
|
|
|
|
"es": "Spanish",
|
|
|
|
"de": "German"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-06 16:31:20 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-06 16:31:20 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Parameter label is optional, use null if no matching fallback'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-06 16:31:20 +00:00
|
|
|
'input' => '{
|
2013-10-15 19:40:09 +00:00
|
|
|
"params": {
|
|
|
|
"foo": {}
|
|
|
|
},
|
2013-10-06 16:31:20 +00:00
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": {
|
|
|
|
"es": "Spanish",
|
|
|
|
"de": "German"
|
|
|
|
},
|
2013-10-15 19:40:09 +00:00
|
|
|
"params": ["foo"]
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
2013-10-15 19:40:09 +00:00
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-15 19:40:09 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-15 19:40:09 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-15 19:40:09 +00:00
|
|
|
}
|
|
|
|
},
|
2013-10-06 16:31:20 +00:00
|
|
|
"sets": [
|
|
|
|
{
|
|
|
|
"label": "Spanish",
|
2013-10-15 19:40:09 +00:00
|
|
|
"params": ["foo"]
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
2014-10-18 15:28:26 +00:00
|
|
|
],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps": {}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'lang' => 'fr',
|
|
|
|
'msg' => 'Set label is not optional, choose first available key as final fallback'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
];
|
2023-08-23 07:18:16 +00:00
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
foreach ( $cases as $case ) {
|
2023-08-23 07:18:16 +00:00
|
|
|
yield [ $case ];
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetDataInLanguage
|
|
|
|
*/
|
2016-05-10 00:00:14 +00:00
|
|
|
public function testGetDataInLanguage( array $case ) {
|
2013-10-06 16:31:20 +00:00
|
|
|
// Change content-language to be non-English so we can distinguish between the
|
|
|
|
// last 'en' fallback and the content language in our tests
|
2024-04-08 11:27:18 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'nl' );
|
2013-10-06 16:31:20 +00:00
|
|
|
|
2019-07-08 01:46:33 +00:00
|
|
|
$t = TemplateDataBlob::newFromJSON( $this->db, $case['input'] );
|
2013-10-06 16:31:20 +00:00
|
|
|
$status = $t->getStatus();
|
|
|
|
|
2024-04-08 10:51:10 +00:00
|
|
|
$this->assertStatusGood( $status, $case['msg'] );
|
2013-10-06 16:31:20 +00:00
|
|
|
|
|
|
|
$actual = $t->getDataInLanguage( $case['lang'] );
|
|
|
|
$this->assertJsonStringEqualsJsonString(
|
|
|
|
$case['output'],
|
|
|
|
json_encode( $actual ),
|
|
|
|
$case['msg']
|
|
|
|
);
|
|
|
|
}
|
2013-10-09 22:26:22 +00:00
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideParamOrder() {
|
2016-05-10 00:00:14 +00:00
|
|
|
$cases = [
|
|
|
|
[
|
2013-10-09 22:26:22 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"baz": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
}
|
|
|
|
},
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-09 22:26:22 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Normalisation adds paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-09 22:26:22 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"baz": {}
|
|
|
|
},
|
|
|
|
"paramOrder": ["baz", "foo", "bar"]
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {
|
|
|
|
"foo": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
},
|
|
|
|
"baz": {
|
|
|
|
"label": null,
|
|
|
|
"required": false,
|
2014-04-24 22:21:36 +00:00
|
|
|
"suggested": false,
|
2021-03-10 10:54:52 +00:00
|
|
|
"suggestedvalues": [],
|
2013-10-09 22:26:22 +00:00
|
|
|
"description": null,
|
2015-03-04 20:32:38 +00:00
|
|
|
"example": null,
|
2013-10-09 22:26:22 +00:00
|
|
|
"deprecated": false,
|
|
|
|
"aliases": [],
|
2015-03-09 21:45:04 +00:00
|
|
|
"default": null,
|
2014-09-02 15:29:33 +00:00
|
|
|
"type": "unknown",
|
|
|
|
"autovalue": null
|
2013-10-09 22:26:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"paramOrder": ["baz", "foo", "bar"],
|
2014-10-18 15:28:26 +00:00
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2014-10-18 15:28:26 +00:00
|
|
|
"maps" : {}
|
2013-10-09 22:26:22 +00:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Custom paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2021-08-28 11:27:15 +00:00
|
|
|
[
|
|
|
|
'input' => '{ "params": {}, "paramOrder": {} }',
|
|
|
|
'status' => '(templatedata-invalid-type: paramOrder, array)',
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2013-10-09 22:26:22 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"baz": {}
|
|
|
|
},
|
|
|
|
"paramOrder": ["foo", "bar"]
|
|
|
|
}
|
|
|
|
',
|
2023-08-21 10:42:33 +00:00
|
|
|
'status' => '(templatedata-invalid-missing: paramOrder[ "baz" ])',
|
2013-10-09 22:26:22 +00:00
|
|
|
'msg' => 'Incomplete paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2015-03-04 01:40:40 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'output' => '{
|
|
|
|
"description": null,
|
|
|
|
"params": {},
|
|
|
|
"sets": [],
|
2016-03-01 17:10:23 +00:00
|
|
|
"format": null,
|
2015-03-04 01:40:40 +00:00
|
|
|
"maps" : {}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'msg' => 'Empty parameter object produces empty array paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-09 22:26:22 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"baz": {}
|
|
|
|
},
|
|
|
|
"paramOrder": ["foo", "bar", "baz", "quux"]
|
|
|
|
}
|
|
|
|
',
|
2023-08-21 10:42:33 +00:00
|
|
|
'status' => '(templatedata-invalid-value: paramOrder[ "quux" ])',
|
2013-10-09 22:26:22 +00:00
|
|
|
'msg' => 'Unknown params in paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
[
|
2013-10-09 22:26:22 +00:00
|
|
|
'input' => '{
|
|
|
|
"params": {
|
|
|
|
"foo": {},
|
|
|
|
"bar": {},
|
|
|
|
"baz": {}
|
|
|
|
},
|
|
|
|
"paramOrder": ["foo", "bar", "baz", "bar"]
|
|
|
|
}
|
|
|
|
',
|
2021-08-28 10:48:23 +00:00
|
|
|
'status' => '(templatedata-invalid-duplicate-value: paramOrder[3], paramOrder[1], bar)',
|
2013-10-09 22:26:22 +00:00
|
|
|
'msg' => 'Duplicate params in paramOrder'
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
];
|
2023-08-23 07:18:16 +00:00
|
|
|
|
2013-10-09 22:26:22 +00:00
|
|
|
foreach ( $cases as $case ) {
|
2023-08-23 07:18:16 +00:00
|
|
|
yield [ $case ];
|
2013-10-09 22:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideParamOrder
|
|
|
|
*/
|
2016-05-10 00:00:14 +00:00
|
|
|
public function testParamOrder( array $case ) {
|
2013-10-09 22:26:22 +00:00
|
|
|
$this->assertTemplateData( $case );
|
|
|
|
}
|
2018-04-20 02:18:03 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-23 09:45:11 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\Api\ApiTemplateData
|
2018-04-20 02:18:03 +00:00
|
|
|
* @dataProvider provideGetRawParams
|
|
|
|
*/
|
2021-12-23 09:45:11 +00:00
|
|
|
public function testGetRawParams( string $inputWikitext, array $expectedParams ) {
|
|
|
|
/** @var ApiTemplateData $api */
|
|
|
|
$api = TestingAccessWrapper::newFromObject( $this->createMock( ApiTemplateData::class ) );
|
|
|
|
$params = $api->getRawParams( $inputWikitext );
|
2018-10-23 04:59:11 +00:00
|
|
|
$this->assertArrayEquals( $expectedParams, $params, true, true );
|
2018-04-20 02:18:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideGetRawParams() {
|
2018-04-20 02:18:03 +00:00
|
|
|
return [
|
2018-09-12 02:04:45 +00:00
|
|
|
'No params' => [
|
2018-04-20 02:18:03 +00:00
|
|
|
'Lorem ipsum {{tpl}}.',
|
|
|
|
[]
|
|
|
|
],
|
2018-09-12 02:04:45 +00:00
|
|
|
'Two plain params' => [
|
|
|
|
'Lorem {{{name}}} ipsum {{{surname}}}',
|
|
|
|
[ 'name' => [], 'surname' => [] ]
|
2018-04-20 02:18:03 +00:00
|
|
|
],
|
2018-09-12 02:04:45 +00:00
|
|
|
'Param with multiple casing and default value' => [
|
2018-04-20 02:18:03 +00:00
|
|
|
'Lorem {{{name|{{{Name|Default name}}}}}} ipsum',
|
2018-08-27 03:08:49 +00:00
|
|
|
[ 'name' => [] ]
|
2018-04-20 02:18:03 +00:00
|
|
|
],
|
2018-09-12 02:04:45 +00:00
|
|
|
'Param name contains comment' => [
|
2018-04-20 02:18:03 +00:00
|
|
|
'Lorem {{{name<!-- comment -->}}} ipsum',
|
|
|
|
[ 'name' => [] ]
|
|
|
|
],
|
2018-09-12 02:04:45 +00:00
|
|
|
'Letter-case and underscore-space normalization' => [
|
2018-08-27 03:08:49 +00:00
|
|
|
'Lorem {{{First name|{{{first_name}}}}}} ipsum {{{first-Name}}}',
|
|
|
|
[ 'First name' => [] ]
|
|
|
|
],
|
2018-09-12 02:04:45 +00:00
|
|
|
'Dynamic param name' => [
|
|
|
|
'{{{{{#if:{{{nominee|}}}|nominee|candidate}}|}}}',
|
|
|
|
[ 'nominee' => [] ]
|
|
|
|
],
|
|
|
|
'More complicated dynamic param name' => [
|
|
|
|
'{{{party{{#if:{{{party_election||}}}|_election||}}|}}}',
|
|
|
|
[ 'party_election' => [] ]
|
2018-10-23 04:59:11 +00:00
|
|
|
],
|
|
|
|
'Bang in a param name' => [
|
|
|
|
'{{{!}}} {{{foo!}}}',
|
|
|
|
[ '!' => [], 'foo!' => [] ]
|
|
|
|
],
|
|
|
|
'Bang as a magic word in a table construct' => [
|
|
|
|
'{{{!}} class=""',
|
|
|
|
[]
|
|
|
|
],
|
2019-12-09 18:12:11 +00:00
|
|
|
'Params within comments and nowiki tags' => [
|
|
|
|
'Lorem <!-- {{{name}}} --> ipsum <nowiki > {{{middlename}}}' .
|
2022-01-19 13:51:52 +00:00
|
|
|
'</nowiki> <pre>{{{pre}}}</pre> {{{surname}}}',
|
2019-12-09 18:12:11 +00:00
|
|
|
[ 'surname' => [] ]
|
|
|
|
],
|
|
|
|
'Param within comments and param name outside with comment' => [
|
|
|
|
'Lorem {{{name<!--comment-->}}} ipsum <!--{{{surname}}}-->',
|
|
|
|
[ 'name' => [] ]
|
|
|
|
],
|
2020-09-22 16:12:52 +00:00
|
|
|
'safesubst: hack with an unnamed parameter' => [
|
|
|
|
'{{ {{{|safesubst:}}}#invoke:…|{{{1}}}|{{{ 1 }}}}}',
|
|
|
|
[ '1' => [] ]
|
|
|
|
],
|
2021-09-03 11:18:42 +00:00
|
|
|
'Characters impossible in parameter names' => [
|
|
|
|
'{{test|a|b=c|d=e=f}} {{{a|b}}} {{{d=e}}}',
|
|
|
|
[ 'a' => [] ]
|
|
|
|
],
|
|
|
|
'Characters that are, while technically possible, almost certainly a mistake' => [
|
|
|
|
"{{{a{a}}} {{{b}b}}} {{{c\nc}}}",
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
'Table syntax escaped with {{!}}' => [
|
|
|
|
'{{{!}}\n! This is table syntax\n{{!}}}',
|
|
|
|
[]
|
|
|
|
],
|
2018-04-20 02:18:03 +00:00
|
|
|
];
|
|
|
|
}
|
2018-12-28 05:00:45 +00:00
|
|
|
|
2023-05-20 12:10:49 +00:00
|
|
|
public static function provideGetHtml() {
|
2018-12-28 05:00:45 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong
|
|
|
|
yield 'No params' => [
|
|
|
|
[ 'params' => [ (object)[] ] ],
|
|
|
|
<<<HTML
|
2021-07-12 09:45:22 +00:00
|
|
|
<section class="mw-templatedata-doc-wrap">
|
|
|
|
<header><p class="mw-templatedata-doc-desc mw-templatedata-doc-muted">(templatedata-doc-desc-empty)</p></header>
|
2020-06-06 14:07:12 +00:00
|
|
|
<table class="wikitable mw-templatedata-doc-params">
|
2022-08-31 13:31:48 +00:00
|
|
|
<caption>
|
2023-06-21 16:43:39 +00:00
|
|
|
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p>
|
2022-08-31 13:31:48 +00:00
|
|
|
</caption>
|
2018-12-28 05:00:45 +00:00
|
|
|
<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>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td class="mw-templatedata-doc-muted" colspan="7">(templatedata-doc-no-params-set)</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2021-07-12 09:45:22 +00:00
|
|
|
</section>
|
2018-12-28 05:00:45 +00:00
|
|
|
HTML
|
|
|
|
];
|
|
|
|
yield 'Basic params' => [
|
|
|
|
[ 'params' => [ 'foo' => (object)[], 'bar' => [ 'required' => true ] ] ],
|
|
|
|
<<<HTML
|
2021-07-12 09:45:22 +00:00
|
|
|
<section class="mw-templatedata-doc-wrap">
|
|
|
|
<header><p class="mw-templatedata-doc-desc mw-templatedata-doc-muted">(templatedata-doc-desc-empty)</p></header>
|
2018-12-28 05:00:45 +00:00
|
|
|
<table class="wikitable mw-templatedata-doc-params sortable">
|
2022-08-31 13:31:48 +00:00
|
|
|
<caption>
|
2023-06-21 16:43:39 +00:00
|
|
|
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p>
|
2022-08-31 13:31:48 +00:00
|
|
|
</caption>
|
2018-12-28 05:00:45 +00:00
|
|
|
<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>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
2020-12-23 07:29:05 +00:00
|
|
|
<th>foo</th>
|
2018-12-28 05:00:45 +00:00
|
|
|
<td class="mw-templatedata-doc-param-name"><code>foo</code></td>
|
2022-08-08 09:01:35 +00:00
|
|
|
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td>
|
2018-12-28 05:00:45 +00:00
|
|
|
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td>
|
2020-08-20 11:27:43 +00:00
|
|
|
<td class="mw-templatedata-doc-param-status-optional" data-sort-value="0">(templatedata-doc-param-status-optional)</td>
|
2018-12-28 05:00:45 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2020-12-23 07:29:05 +00:00
|
|
|
<th>bar</th>
|
2018-12-28 05:00:45 +00:00
|
|
|
<td class="mw-templatedata-doc-param-name"><code>bar</code></td>
|
2022-08-08 09:01:35 +00:00
|
|
|
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td>
|
2018-12-28 05:00:45 +00:00
|
|
|
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td>
|
2020-08-20 11:27:43 +00:00
|
|
|
<td class="mw-templatedata-doc-param-status-required" data-sort-value="2">(templatedata-doc-param-status-required)</td>
|
2018-12-28 05:00:45 +00:00
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2021-07-12 09:45:22 +00:00
|
|
|
</section>
|
2022-02-03 11:45:27 +00:00
|
|
|
HTML
|
|
|
|
];
|
|
|
|
yield [
|
|
|
|
[ 'description' => 'Template docs', 'params' => [
|
|
|
|
'suggestedParam' => [
|
|
|
|
'label' => 'Label',
|
|
|
|
'description' => 'Param docs',
|
|
|
|
'aliases' => [ 'Alias1', 'Alias2' ],
|
|
|
|
'suggestedvalues' => [ 'Suggested1', 'Suggested2' ],
|
|
|
|
'suggested' => true,
|
|
|
|
'default' => 'Default docs',
|
|
|
|
'example' => 'Example docs',
|
|
|
|
'autovalue' => 'Auto value',
|
|
|
|
],
|
|
|
|
'deprecatedParam' => [ 'type' => 'date', 'deprecated' => true ],
|
|
|
|
] ],
|
|
|
|
<<<HTML
|
|
|
|
<section class="mw-templatedata-doc-wrap">
|
|
|
|
<header><p class="mw-templatedata-doc-desc">Template docs</p></header>
|
|
|
|
<table class="wikitable mw-templatedata-doc-params sortable">
|
2022-08-31 13:31:48 +00:00
|
|
|
<caption>
|
2023-06-21 16:43:39 +00:00
|
|
|
<p class="mw-templatedata-caption">(templatedata-doc-params)<mw:edittemplatedata page="Template:Test/doc"></mw:edittemplatedata></p>
|
2022-08-31 13:31:48 +00:00
|
|
|
</caption>
|
2022-02-03 11:45:27 +00:00
|
|
|
<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>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<th>Label</th>
|
2023-06-25 14:53:54 +00:00
|
|
|
<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>
|
2022-08-08 09:01:35 +00:00
|
|
|
<td>
|
2022-02-03 11:45:27 +00:00
|
|
|
<p>Param docs</p>
|
|
|
|
<dl>
|
2023-06-25 14:53:54 +00:00
|
|
|
<dt>(templatedata-doc-param-suggestedvalues)</dt><dd><code>Suggested1</code> <code>Suggested2</code></dd>
|
2022-02-03 11:45:27 +00:00
|
|
|
<dt>(templatedata-doc-param-default)</dt><dd>Default docs</dd>
|
|
|
|
<dt>(templatedata-doc-param-example)</dt><dd>Example docs</dd>
|
|
|
|
<dt>(templatedata-doc-param-autovalue)</dt><dd><code>Auto value</code></dd>
|
|
|
|
</dl>
|
|
|
|
</td>
|
|
|
|
<td class="mw-templatedata-doc-param-type mw-templatedata-doc-muted">(templatedata-doc-param-type-unknown)</td>
|
|
|
|
<td class="mw-templatedata-doc-param-status-suggested" data-sort-value="1">(templatedata-doc-param-status-suggested)</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>deprecatedParam</th>
|
|
|
|
<td class="mw-templatedata-doc-param-name"><code>deprecatedParam</code></td>
|
2022-08-08 09:01:35 +00:00
|
|
|
<td><p class="mw-templatedata-doc-muted">(templatedata-doc-param-desc-empty)</p><dl></dl></td>
|
2022-02-03 11:45:27 +00:00
|
|
|
<td class="mw-templatedata-doc-param-type">(templatedata-doc-param-type-date)</td>
|
|
|
|
<td class="mw-templatedata-doc-param-status-deprecated" data-sort-value="-1">(templatedata-doc-param-status-deprecated)</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</section>
|
2018-12-28 05:00:45 +00:00
|
|
|
HTML
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-03 08:31:50 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateData\TemplateDataHtmlFormatter
|
2018-12-28 05:00:45 +00:00
|
|
|
* @dataProvider provideGetHtml
|
|
|
|
*/
|
2023-06-26 16:08:42 +00:00
|
|
|
public function testGetHtml( array $data, string $expected ) {
|
2019-07-08 01:46:33 +00:00
|
|
|
$t = TemplateDataBlob::newFromJSON( $this->db, json_encode( $data ) );
|
2022-02-03 08:31:50 +00:00
|
|
|
$localizer = new class implements MessageLocalizer {
|
2023-08-23 07:29:25 +00:00
|
|
|
|
2022-02-03 08:31:50 +00:00
|
|
|
public function msg( $key, ...$params ) {
|
|
|
|
return new RawMessage( "($key)" );
|
|
|
|
}
|
2023-08-23 07:29:25 +00:00
|
|
|
|
2022-02-03 08:31:50 +00:00
|
|
|
};
|
2022-08-31 13:31:48 +00:00
|
|
|
$title = Title::newFromText( 'Template:Test/doc' );
|
2022-02-03 08:31:50 +00:00
|
|
|
$formatter = new TemplateDataHtmlFormatter( $localizer );
|
2022-08-31 13:31:48 +00:00
|
|
|
$actual = $formatter->getHtml( $t, $title );
|
2018-12-28 05:00:45 +00:00
|
|
|
$linedActual = preg_replace( '/>\s*</', ">\n<", $actual );
|
|
|
|
|
|
|
|
$linedExpected = preg_replace( '/>\s*</', ">\n<", trim( $expected ) );
|
|
|
|
|
2022-02-03 08:31:50 +00:00
|
|
|
$this->assertSame( $linedExpected, $linedActual );
|
2018-12-28 05:00:45 +00:00
|
|
|
}
|
2023-08-23 07:29:25 +00:00
|
|
|
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|