summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLee Rowlands2017-11-17 20:46:55 (GMT)
committerLee Rowlands2017-11-17 20:46:55 (GMT)
commit7dbf76d75a490ae7d9f1bc45832a4ad5b0649e37 (patch)
tree5f9db7daae6b440149d712829f22fc9aeb0bd2b6
parentdf5ddd22aeec07f9cc4df8c38548ab2965a5e31b (diff)
Issue #2897134 by MaskOta, Sam152, Dinesh18, xjm: Enforce that weights are numeric when settings state/transition weights
-rw-r--r--core/modules/workflows/src/Plugin/WorkflowTypeBase.php12
-rw-r--r--core/modules/workflows/tests/src/Unit/WorkflowTest.php21
2 files changed, 31 insertions, 2 deletions
diff --git a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php
index 32ade73..6045484 100644
--- a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php
+++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php
@@ -178,7 +178,11 @@ abstract class WorkflowTypeBase extends PluginBase implements WorkflowTypeInterf
*/
public function setStateWeight($state_id, $weight) {
if (!$this->hasState($state_id)) {
- throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow.'");
+ throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow.");
+ }
+ if (!is_numeric($weight)) {
+ $label = $this->getState($state_id)->label();
+ throw new \InvalidArgumentException("The weight '$weight' must be numeric for state '$label'.");
}
$this->configuration['states'][$state_id]['weight'] = $weight;
return $this;
@@ -390,7 +394,11 @@ abstract class WorkflowTypeBase extends PluginBase implements WorkflowTypeInterf
*/
public function setTransitionWeight($transition_id, $weight) {
if (!$this->hasTransition($transition_id)) {
- throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow.'");
+ throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow.");
+ }
+ if (!is_numeric($weight)) {
+ $label = $this->getTransition($transition_id)->label();
+ throw new \InvalidArgumentException("The weight '$weight' must be numeric for transition '$label'.");
}
$this->configuration['transitions'][$transition_id]['weight'] = $weight;
return $this;
diff --git a/core/modules/workflows/tests/src/Unit/WorkflowTest.php b/core/modules/workflows/tests/src/Unit/WorkflowTest.php
index 8cf4493..dc77a3d 100644
--- a/core/modules/workflows/tests/src/Unit/WorkflowTest.php
+++ b/core/modules/workflows/tests/src/Unit/WorkflowTest.php
@@ -226,6 +226,16 @@ class WorkflowTest extends UnitTestCase {
}
/**
+ * @covers ::setStateWeight
+ */
+ public function testSetStateWeightNonNumericException() {
+ $this->setExpectedException(\InvalidArgumentException::class, "The weight 'foo' must be numeric for state 'Published'.");
+ $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
+ $workflow->getTypePlugin()->addState('published', 'Published');
+ $workflow->getTypePlugin()->setStateWeight('published', 'foo');
+ }
+
+ /**
* @covers ::deleteState
*/
public function testDeleteState() {
@@ -557,6 +567,17 @@ class WorkflowTest extends UnitTestCase {
}
/**
+ * @covers ::setTransitionWeight
+ */
+ public function testSetTransitionWeightNonNumericException() {
+ $this->setExpectedException(\InvalidArgumentException::class, "The weight 'foo' must be numeric for transition 'Publish'.");
+ $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
+ $workflow->getTypePlugin()->addState('published', 'Published');
+ $workflow->getTypePlugin()->addTransition('publish', 'Publish', [], 'published');
+ $workflow->getTypePlugin()->setTransitionWeight('publish', 'foo');
+ }
+
+ /**
* @covers ::setTransitionFromStates
*/
public function testSetTransitionFromStates() {