diff --git a/.upgradenotes/MDL-86231-2025080601561269.yml b/.upgradenotes/MDL-86231-2025080601561269.yml new file mode 100644 index 00000000000..df4a0931df6 --- /dev/null +++ b/.upgradenotes/MDL-86231-2025080601561269.yml @@ -0,0 +1,24 @@ +issueNumber: MDL-86231 +notes: + core: + - message: > + The Behat `::execute()` method now accepts an array-style callable in + addition to the string `classname::method` format. + + + The following formats are now accepted: + + + ```php + + // String format: + + $this->execute('behat_general::i_click_on', [...]); + + + // Array format: + + $this->execute([behat_general::class],' i_click_on'], [...]); + + ``` + type: improved diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index d74b6daccb4..52e0e327afe 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -1053,19 +1053,37 @@ EOF; /** * Helper function to execute api in a given context. * - * @param string $contextapi context in which api is defined. - * @param array $params list of params to pass. + * Note: The contextapi does not support a callback. + * + * @param string|array $contextapi context in which api is defined. + * @param array|mixed $params list of params to pass or a single parameter * @throws Exception + * @throws DriverException */ - protected function execute($contextapi, $params = array()) { + protected function execute( + $contextapi, + $params = [] + ): void { if (!is_array($params)) { - $params = array($params); + $params = [$params]; + } + + if (is_string($contextapi)) { + $contextapi = explode('::', $contextapi); + } + + if (count($contextapi) !== 2) { + throw new DriverException('Invalid contextapi format, expected "context::api" or ["context", "api"]'); } // Get required context and execute the api. - $contextapi = explode("::", $contextapi); - $context = behat_context_helper::get($contextapi[0]); - call_user_func_array(array($context, $contextapi[1]), $params); + [$classname, $method] = $contextapi; + if (!is_string($classname) || !is_string($method)) { + throw new DriverException('Invalid contextapi format, expected "context::api" or ["context", "api"]'); + } + + $context = behat_context_helper::get($classname); + call_user_func_array([$context, $method], $params); // NOTE: Wait for pending js and look for exception are not optional, as this might lead to unexpected results. // Don't make them optional for performance reasons. diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 63f5658ca0d..2c1826785ae 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -78,7 +78,7 @@ class behat_general extends behat_base { * @Given /^I am on homepage$/ */ public function i_am_on_homepage() { - $this->execute('behat_general::i_visit', ['/']); + $this->execute([self::class, 'i_visit'], ['/']); } /** @@ -87,7 +87,7 @@ class behat_general extends behat_base { * @Given /^I am on site homepage$/ */ public function i_am_on_site_homepage() { - $this->execute('behat_general::i_visit', ['/?redirect=0']); + $this->execute([self::class, 'i_visit'], ['/?redirect=0']); } /** @@ -96,7 +96,7 @@ class behat_general extends behat_base { * @Given /^I am on course index$/ */ public function i_am_on_course_index() { - $this->execute('behat_general::i_visit', ['/course/index.php']); + $this->execute([self::class, 'i_visit'], ['/course/index.php']); } /** @@ -442,7 +442,7 @@ class behat_general extends behat_base { */ public function i_click_on_confirming_the_dialogue($element, $selectortype) { $this->i_click_on($element, $selectortype); - $this->execute('behat_general::accept_currently_displayed_alert_dialog', []); + $this->execute([self::class, 'accept_currently_displayed_alert_dialog'], []); $this->wait_until_the_page_is_ready(); } @@ -456,7 +456,7 @@ class behat_general extends behat_base { */ public function i_click_on_dismissing_the_dialogue($element, $selectortype) { $this->i_click_on($element, $selectortype); - $this->execute('behat_general::dismiss_currently_displayed_alert_dialog', []); + $this->execute([self::class, 'dismiss_currently_displayed_alert_dialog'], []); $this->wait_until_the_page_is_ready(); } @@ -1145,7 +1145,7 @@ EOF; * @Given /^I trigger cron$/ */ public function i_trigger_cron() { - $this->execute('behat_general::i_visit', ['/admin/cron.php']); + $this->execute([self::class, 'i_visit'], ['/admin/cron.php']); } /** @@ -2138,8 +2138,8 @@ EOF; } // Gets the node based on the requested selector type and locator. $node = $this->get_selected_node($selectortype, $element); - $this->execute('behat_general::i_click_on', [$node, 'NodeElement']); - $this->execute('behat_general::i_press_named_key', ['', 'tab']); + $this->execute([self::class, 'i_click_on'], [$node, 'NodeElement']); + $this->execute([self::class, 'i_press_named_key'], ['', 'tab']); } /** @@ -2252,9 +2252,9 @@ EOF; */ public function i_manually_press_tab($shift = '') { if (empty($shift)) { - $this->execute('behat_general::i_press_named_key', ['', 'tab']); + $this->execute([self::class, 'i_press_named_key'], ['', 'tab']); } else { - $this->execute('behat_general::i_press_named_key', ['shift', 'tab']); + $this->execute([self::class, 'i_press_named_key'], ['shift', 'tab']); } } @@ -2317,7 +2317,7 @@ EOF; * @throws DriverException */ public function i_manually_press_enter() { - $this->execute('behat_general::i_press_named_key', ['', 'enter']); + $this->execute([self::class, 'i_press_named_key'], ['', 'enter']); } /** @@ -2352,7 +2352,7 @@ EOF; */ public function i_click_on_the_dynamic_tab(string $tabname): void { $xpath = "//*[@id='dynamictabs-tabs'][descendant::a[contains(text(), '" . $this->escape($tabname) . "')]]"; - $this->execute('behat_general::i_click_on_in_the', + $this->execute([self::class, 'i_click_on_in_the'], [$tabname, 'link', $xpath, 'xpath_element']); } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index c4e8df824b1..dad08af27b6 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 4.1.21 === + +* Moodle's Behat `::execute()` method now supports the use of array callable syntax for calling other steps. + === 4.1.20 === * Add a new method has_valid_group in \core\report_helper that will return true or false depending if the user has a valid group. This is mainly