Implement Sets

Change-Id: I93a0ba7287fec6e5566994d03dff1d8eaeeb8430
This commit is contained in:
Timo Tijhof 2013-05-16 00:15:52 +02:00
parent 1a38b42241
commit 708bbad12d
3 changed files with 165 additions and 10 deletions

View file

@ -49,8 +49,9 @@ class TemplateDataBlob {
$data = $this->data; $data = $this->data;
static $rootKeys = array( static $rootKeys = array(
'params',
'description', 'description',
'params',
'sets',
); );
static $paramKeys = array( static $paramKeys = array(
'label', 'label',
@ -213,7 +214,45 @@ class TemplateDataBlob {
} }
} }
// TODO: Root.sets // Root.sets
if ( isset( $data->sets ) ) {
if ( !is_array( $data->sets ) ) {
return Status::newFatal( 'templatedata-invalid-type', 'sets', 'array' );
}
} else {
$data->sets = array();
}
foreach ( $data->sets as $setNr => $setObj ) {
if ( !is_object( $setObj ) ) {
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}", 'object' );
}
if ( !isset( $setObj->label ) ) {
return Status::newFatal( 'templatedata-invalid-missing', "sets.{$setNr}.label", 'string|object' );
}
if ( !is_object( $setObj->label ) && !is_string( $setObj->label ) ) {
// TODO: Also validate that if it is an object, the keys are valid lang codes and the values strings.
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}.label", 'string|object' );
}
$setObj->label = self::normaliseInterfaceText( $setObj->label );
if ( !isset( $setObj->params ) ) {
return Status::newFatal( 'templatedata-invalid-missing', "sets.{$setNr}.params", 'array' );
}
if ( !is_array( $setObj->params ) ) {
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}.params", 'array' );
}
foreach ( $setObj->params as $param ) {
if ( !isset( $data->params->$param ) ) {
return Status::newFatal( 'templatedata-invalid-missing', "params.{$param}" );
}
}
}
return Status::newGood(); return Status::newGood();
} }

View file

@ -98,10 +98,10 @@
"required": false "required": false
} }
}, },
"sets": { "sets": [
"date": { {
"label": "Date", "label": "Date",
"params": ["year", "month", "day"] "params": ["year", "month", "day"]
} }
} ]
} }

View file

@ -24,7 +24,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
', ',
'output' => '{ 'output' => '{
"description": null, "description": null,
"params": {} "params": {},
"sets": []
} }
', ',
'status' => true, 'status' => true,
@ -71,7 +72,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
"aliases": [], "aliases": [],
"type": "unknown" "type": "unknown"
} }
} },
"sets": []
} }
', ',
'msg' => 'Optional properties are added if missing' 'msg' => 'Optional properties are added if missing'
@ -110,7 +112,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
], ],
"type": "unknown" "type": "unknown"
} }
} },
"sets": []
} }
', ',
'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language' 'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language'
@ -134,7 +137,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
], ],
"type": "unknown" "type": "unknown"
} }
} },
"sets": []
} }
', ',
'msg' => 'Fully normalised json should be valid input and stay unchanged' 'msg' => 'Fully normalised json should be valid input and stay unchanged'
@ -182,11 +186,123 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
"aliases": [], "aliases": [],
"type": "unknown" "type": "unknown"
} }
} },
"sets": []
} }
', ',
'msg' => 'The inherits property copies over properties from another parameter (preserving overides)' 'msg' => 'The inherits property copies over properties from another parameter (preserving overides)'
), ),
array(
'input' => '{
"params": {},
"sets": [
{
"label": "Example"
}
]
}',
'status' => 'Required property "sets.0.params" not found.'
),
array(
'input' => '{
"params": {
"foo": {
}
},
"sets": [
{
"params": ["foo"]
}
]
}',
'status' => 'Required property "sets.0.label" not found.'
),
array(
'input' => '{
"params": {
"foo": {
},
"bar": {
}
},
"sets": [
{
"label": "Foo with Quux",
"params": ["foo", "quux"]
}
]
}',
'status' => 'Required property "params.quux" not found.'
),
array(
'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,
"description": null,
"deprecated": false,
"aliases": [],
"default": "",
"type": "unknown"
},
"bar": {
"label": null,
"required": false,
"description": null,
"deprecated": false,
"aliases": [],
"default": "",
"type": "unknown"
},
"quux": {
"label": null,
"required": false,
"description": null,
"deprecated": false,
"aliases": [],
"default": "",
"type": "unknown"
}
},
"sets": [
{
"label": {
"en": "Foo with Quux"
},
"params": ["foo", "quux"]
},
{
"label": {
"en": "Bar with Quux"
},
"params": ["bar", "quux"]
}
]
}',
'status' => true
),
); );
$calls = array(); $calls = array();
foreach ( $cases as $case ) { foreach ( $cases as $case ) {