Commit 9a59b708 authored by alexpott's avatar alexpott

Issue #2845114 by tstoeckler, Sam152: Add deterministic sorting of workflow...

Issue #2845114 by tstoeckler, Sam152: Add deterministic sorting of workflow states in and state transitions in config
parent 87c1e2ee
......@@ -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;
}
......
......@@ -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()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment