summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-01-28 13:42:11 (GMT)
committerAlex Pott2017-01-28 13:42:11 (GMT)
commit9a59b7088ce7ae602c3297391ca07815ba336c8d (patch)
tree9baa969c7e98135454c82e52b865d2aec268a238
parent87c1e2ee24dbd8a3b60477b110775b6ce0bf6daa (diff)
Issue #2845114 by tstoeckler, Sam152: Add deterministic sorting of workflow states in and state transitions in config
-rw-r--r--core/modules/workflows/src/Entity/Workflow.php2
-rw-r--r--core/modules/workflows/tests/src/Unit/WorkflowTest.php23
2 files changed, 18 insertions, 7 deletions
diff --git a/core/modules/workflows/src/Entity/Workflow.php b/core/modules/workflows/src/Entity/Workflow.php
index 3bf8d22..393930b 100644
--- a/core/modules/workflows/src/Entity/Workflow.php
+++ b/core/modules/workflows/src/Entity/Workflow.php
@@ -125,6 +125,7 @@ class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWith
'label' => $label,
'weight' => $this->getNextWeight($this->states),
];
+ ksort($this->states);
return $this;
}
@@ -418,6 +419,7 @@ class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWith
$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 49e8671..b055fab 100644
--- a/core/modules/workflows/tests/src/Unit/WorkflowTest.php
+++ b/core/modules/workflows/tests/src/Unit/WorkflowTest.php
@@ -86,12 +86,18 @@ class WorkflowTest extends UnitTestCase {
$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 @@ class WorkflowTest extends UnitTestCase {
$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()));