From 708bbad12d54c4d7186896670b93d28a37bb5580 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 16 May 2013 00:15:52 +0200 Subject: [PATCH] Implement Sets Change-Id: I93a0ba7287fec6e5566994d03dff1d8eaeeb8430 --- TemplateDataBlob.php | 43 ++++++++++- spec.templatedata.json | 6 +- tests/TemplateDataBlobTest.php | 126 +++++++++++++++++++++++++++++++-- 3 files changed, 165 insertions(+), 10 deletions(-) diff --git a/TemplateDataBlob.php b/TemplateDataBlob.php index 9bd91141..e9eb497f 100644 --- a/TemplateDataBlob.php +++ b/TemplateDataBlob.php @@ -49,8 +49,9 @@ class TemplateDataBlob { $data = $this->data; static $rootKeys = array( - 'params', 'description', + 'params', + 'sets', ); static $paramKeys = array( '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(); } diff --git a/spec.templatedata.json b/spec.templatedata.json index 75869b40..d57a6f14 100644 --- a/spec.templatedata.json +++ b/spec.templatedata.json @@ -98,10 +98,10 @@ "required": false } }, - "sets": { - "date": { + "sets": [ + { "label": "Date", "params": ["year", "month", "day"] } - } + ] } diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php index 77d72670..9b823d44 100644 --- a/tests/TemplateDataBlobTest.php +++ b/tests/TemplateDataBlobTest.php @@ -24,7 +24,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase { ', 'output' => '{ "description": null, - "params": {} + "params": {}, + "sets": [] } ', 'status' => true, @@ -71,7 +72,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase { "aliases": [], "type": "unknown" } - } + }, + "sets": [] } ', 'msg' => 'Optional properties are added if missing' @@ -110,7 +112,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase { ], "type": "unknown" } - } + }, + "sets": [] } ', 'msg' => 'InterfaceText is expanded to langcode-keyed object, assuming content language' @@ -134,7 +137,8 @@ class TemplateDataBlobTest extends MediaWikiTestCase { ], "type": "unknown" } - } + }, + "sets": [] } ', 'msg' => 'Fully normalised json should be valid input and stay unchanged' @@ -182,11 +186,123 @@ class TemplateDataBlobTest extends MediaWikiTestCase { "aliases": [], "type": "unknown" } - } + }, + "sets": [] } ', '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(); foreach ( $cases as $case ) {