diff --git a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php index 32ade73bd5d70c6025fd557e55fca1c9e60936bc..6045484244243e9e88dc3a7d6365fe804aad49f2 100644 --- a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php +++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php @@ -178,7 +178,11 @@ public function setStateLabel($state_id, $label) { */ 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 @@ public function setTransitionLabel($transition_id, $label) { */ 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 8cf449367cdac702007f6a1573434fe40564cf98..dc77a3d2f622946821348d7caa48d4fccec039ef 100644 --- a/core/modules/workflows/tests/src/Unit/WorkflowTest.php +++ b/core/modules/workflows/tests/src/Unit/WorkflowTest.php @@ -225,6 +225,16 @@ public function testSetStateWeightException() { $workflow->getTypePlugin()->setStateWeight('draft', 10); } + /** + * @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 */ @@ -556,6 +566,17 @@ public function testSetTransitionWeightException() { $workflow->getTypePlugin()->setTransitionWeight('draft-published', 10); } + /** + * @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 */