diff --git a/lib/behat/classes/behat_config_util.php b/lib/behat/classes/behat_config_util.php index 86f263bd709..baa1ae1ac46 100644 --- a/lib/behat/classes/behat_config_util.php +++ b/lib/behat/classes/behat_config_util.php @@ -333,6 +333,34 @@ class behat_config_util { return $this->get_component_contexts($component); } + /** + * Sort the list of components contexts. + * + * This ensures that contexts are sorted consistently. + * Core hooks defined in the behat_hooks class _must_ be defined first. + * + * @param array $contexts + * @return array The sorted context list + */ + protected function sort_component_contexts(array $contexts): array { + // Ensure that the lib_tests are first as they include the root of all tests, hooks, and more. + uksort($contexts, function($a, $b): int { + if ($a === 'behat_hooks') { + return -1; + } + if ($b === 'behat_hooks') { + return 1; + } + + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }); + + return $contexts; + } + /** * Behat config file specifing the main context class, * the required Behat extensions and Moodle test wwwroot. @@ -1537,8 +1565,9 @@ class behat_config_util { } // Sort the list of contexts. - ksort($contexts); + $contexts = $this->sort_component_contexts($contexts); + // Cache it for subsequent fetches. $this->themecontexts[$theme] = $contexts; return $contexts;