Verified Commit 7dbf76d7 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2897134 by MaskOta, Sam152, Dinesh18, xjm: Enforce that weights are...

Issue #2897134 by MaskOta, Sam152, Dinesh18, xjm: Enforce that weights are numeric when settings state/transition weights
parent df5ddd22
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -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;
+21 −0
Original line number Diff line number Diff line
@@ -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
   */