diff --git a/core/modules/workflows/src/Entity/Workflow.php b/core/modules/workflows/src/Entity/Workflow.php index 3bf8d22579a95aaf3144418dcc73dbdc6543446f..393930ba38b91e4698e8db0907aff5bab717f365 100644 --- a/core/modules/workflows/src/Entity/Workflow.php +++ b/core/modules/workflows/src/Entity/Workflow.php @@ -125,6 +125,7 @@ public function addState($state_id, $label) { 'label' => $label, 'weight' => $this->getNextWeight($this->states), ]; + ksort($this->states); return $this; } @@ -418,6 +419,7 @@ public function setTransitionFromStates($transition_id, array $from_state_ids) { $from_state_ids = array_values($from_state_ids); sort($from_state_ids); $this->transitions[$transition_id]['from'] = $from_state_ids; + ksort($this->transitions); return $this; } diff --git a/core/modules/workflows/tests/src/Unit/WorkflowTest.php b/core/modules/workflows/tests/src/Unit/WorkflowTest.php index 49e8671425cd48eb4eefc47dddf6626eec4f42fa..b055fabcbde64b84bb0db1a6a43dc2871cb865c0 100644 --- a/core/modules/workflows/tests/src/Unit/WorkflowTest.php +++ b/core/modules/workflows/tests/src/Unit/WorkflowTest.php @@ -86,12 +86,18 @@ public function testGetStates() { $this->assertArrayEquals([], array_keys($workflow->getStates())); $this->assertArrayEquals([], array_keys($workflow->getStates([]))); - // By default states are ordered in the order added. $workflow ->addState('draft', 'Draft') ->addState('published', 'Published') ->addState('archived', 'Archived'); + // States are stored in alphabetical key order. + $this->assertArrayEquals([ + 'archived', + 'draft', + 'published', + ], array_keys($workflow->get('states'))); + // Ensure we're returning state objects. $this->assertInstanceOf(State::class, $workflow->getStates()['draft']); @@ -379,21 +385,24 @@ public function testGetTransitions() { $workflow ->addState('a', 'A') ->addState('b', 'B') - ->addTransition('a_a', 'A to A', ['a'], 'a') - ->addTransition('a_b', 'A to B', ['a'], 'b'); + ->addTransition('a_b', 'A to B', ['a'], 'b') + ->addTransition('a_a', 'A to A', ['a'], 'a'); + + // Transitions are stored in alphabetical key order in configuration. + $this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->get('transitions'))); // Ensure we're returning transition objects. $this->assertInstanceOf(Transition::class, $workflow->getTransitions()['a_a']); // Passing in no IDs returns all transitions. - $this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTransitions())); + $this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTransitions())); // The order of states is by weight. - $workflow->setTransitionWeight('a_b', -1); - $this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTransitions())); + $workflow->setTransitionWeight('a_a', -1); + $this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTransitions())); // If all weights are equal it will fallback to labels. - $workflow->setTransitionWeight('a_b', 0); + $workflow->setTransitionWeight('a_a', 0); $this->assertArrayEquals(['a_a', 'a_b'], array_keys($workflow->getTransitions())); $workflow->setTransitionLabel('a_b', 'A B'); $this->assertArrayEquals(['a_b', 'a_a'], array_keys($workflow->getTransitions()));