From 2648f0196635c6b9d306d63a92ff0ab266defe55 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 3 Jun 2015 13:46:25 -0700 Subject: [PATCH] [Composition] Disallow composition for leaf types Disallow composition for domain object types which will not have a composition property; WTD-1221. --- .../browse/src/creation/CreateWizard.js | 5 ++-- platform/containment/bundle.json | 5 ++++ .../containment/src/CompositionModelPolicy.js | 28 +++++++++++++++++++ .../test/CompositionModelPolicySpec.js | 26 +++++++++++++++++ .../test/CompositionMutabilityPolicySpec.js | 2 +- platform/containment/test/suite.json | 1 + 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 platform/containment/src/CompositionModelPolicy.js create mode 100644 platform/containment/test/CompositionModelPolicySpec.js diff --git a/platform/commonUI/browse/src/creation/CreateWizard.js b/platform/commonUI/browse/src/creation/CreateWizard.js index 7f60bdd883..29fe953e18 100644 --- a/platform/commonUI/browse/src/creation/CreateWizard.js +++ b/platform/commonUI/browse/src/creation/CreateWizard.js @@ -45,8 +45,9 @@ define( properties = type.getProperties(); function validateLocation(locatingObject) { - var locatingType = locatingObject.getCapability('type'); - return policyService.allow( + var locatingType = locatingObject && + locatingObject.getCapability('type'); + return locatingType && policyService.allow( "composition", locatingType, type diff --git a/platform/containment/bundle.json b/platform/containment/bundle.json index e61085f66f..e6e24f0f79 100644 --- a/platform/containment/bundle.json +++ b/platform/containment/bundle.json @@ -12,6 +12,11 @@ "implementation": "CompositionMutabilityPolicy.js", "message": "Objects of this type cannot be modified." }, + { + "category": "composition", + "implementation": "CompositionModelPolicy.js", + "message": "Objects of this type cannot contain other objects." + }, { "category": "action", "implementation": "ComposeActionPolicy.js", diff --git a/platform/containment/src/CompositionModelPolicy.js b/platform/containment/src/CompositionModelPolicy.js new file mode 100644 index 0000000000..74f1200530 --- /dev/null +++ b/platform/containment/src/CompositionModelPolicy.js @@ -0,0 +1,28 @@ +/*global define*/ + +define( + [], + function () { + "use strict"; + + /** + * Policy allowing composition only for domain object types which + * have a composition property. + */ + function CompositionModelPolicy() { + return { + /** + * Is the type identified by the candidate allowed to + * contain the type described by the context? + */ + allow: function (candidate, context) { + return Array.isArray( + (candidate.getInitialModel() || {}).composition + ); + } + }; + } + + return CompositionModelPolicy; + } +); \ No newline at end of file diff --git a/platform/containment/test/CompositionModelPolicySpec.js b/platform/containment/test/CompositionModelPolicySpec.js new file mode 100644 index 0000000000..bace49246d --- /dev/null +++ b/platform/containment/test/CompositionModelPolicySpec.js @@ -0,0 +1,26 @@ +/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/ + +define( + ["../src/CompositionModelPolicy"], + function (CompositionModelPolicy) { + "use strict"; + + describe("The composition model policy", function () { + var mockType, + policy; + + beforeEach(function () { + mockType = jasmine.createSpyObj('type', ['getInitialModel']); + policy = new CompositionModelPolicy(); + }); + + it("only allows composition for types which will have a composition property", function () { + mockType.getInitialModel.andReturn({}); + expect(policy.allow(mockType)).toBeFalsy(); + mockType.getInitialModel.andReturn({ composition: [] }); + expect(policy.allow(mockType)).toBeTruthy(); + }); + }); + + } +); diff --git a/platform/containment/test/CompositionMutabilityPolicySpec.js b/platform/containment/test/CompositionMutabilityPolicySpec.js index c537a5000f..1f49883939 100644 --- a/platform/containment/test/CompositionMutabilityPolicySpec.js +++ b/platform/containment/test/CompositionMutabilityPolicySpec.js @@ -35,7 +35,7 @@ define( policy = new CompositionMutabilityPolicy(); }); - it("only allows composition for types which will have a composition capability", function () { + it("only allows composition for types which can be created/modified", function () { expect(policy.allow(mockType)).toBeFalsy(); mockType.hasFeature.andReturn(true); expect(policy.allow(mockType)).toBeTruthy(); diff --git a/platform/containment/test/suite.json b/platform/containment/test/suite.json index 987ef9a86c..81218a969e 100644 --- a/platform/containment/test/suite.json +++ b/platform/containment/test/suite.json @@ -1,6 +1,7 @@ [ "CapabilityTable", "ComposeActionPolicy", + "CompositionModelPolicy", "CompositionMutabilityPolicy", "CompositionPolicy", "ContainmentTable"