diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php index 243ca616b12..c715d4e682e 100644 --- a/lib/behat/classes/partial_named_selector.php +++ b/lib/behat/classes/partial_named_selector.php @@ -86,6 +86,7 @@ class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSele */ protected static $allowedselectors = array( 'activity' => 'activity', + 'actionmenu' => 'actionmenu', 'badge' => 'badge', 'block' => 'block', 'button' => 'button', @@ -138,6 +139,17 @@ class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSele protected static $moodleselectors = array( 'activity' => << << <<ensure_node_is_visible($node); $node->click(); } + + /** + * Select a specific item in an action menu. + * + * @When /^I choose the "(?P(?:[^"]|\\")*)" item in the "(?P(?:[^"]|\\")*)" action menu$/ + * @param string $item The item to choose + * @param string $actionmenu The text used in the description of the action menu + */ + public function i_choose_in_the_named_menu(string $item, string $actionmenu): void { + $menu = $this->find('actionmenu', $actionmenu); + $this->select_item_in_action_menu($item, $menu); + } + + /** + * Select a specific item in an action menu within a container. + * + * @When /^I choose the "(?P(?:[^"]|\\")*)" item in the "(?P(?:[^"]|\\")*)" action menu of the "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)"$/ + * @param string $item The item to choose + * @param string $actionmenu The text used in the description of the action menu + * @param string|NodeElement $locator The identifer used for the container + * @param string $selector The type of container to locate + */ + public function i_choose_in_the_named_menu_in_container(string $item, string $actionmenu, $locator, $selector): void { + $container = $this->find($selector, $locator); + $menu = $this->find('actionmenu', $actionmenu, false, $container); + $this->select_item_in_action_menu($item, $menu); + } + + /** + * Select an item in the specified menu. + * + * Note: This step does work both with, and without, JavaScript. + * + * @param string $item Item string value + * @param NodeElement $menu The menu NodeElement to select from + */ + protected function select_item_in_action_menu(string $item, NodeElement $menu): void { + if ($this->running_javascript()) { + // Open the menu by clicking on the trigger. + $this->execute( + 'behat_general::i_click_on', + [$menu, "NodeElement"] + ); + } + + // Select the menu item. + $this->execute( + 'behat_general::i_click_on_in_the', + [$item, "link", $menu, "NodeElement"] + ); + } + + /** + * The action menu item should not exist. + * + * @Then /^the "(?P(?:[^"]|\\")*)" item should not exist in the "(?P(?:[^"]|\\")*)" action menu$/ + * @param string $item The item to check + * @param string $actionmenu The text used in the description of the action menu + */ + public function item_should_not_exist(string $item, string $actionmenu): void { + $menu = $this->find('actionmenu', $actionmenu); + $this->execute('behat_general::should_not_exist_in_the', [ + $item, 'link', + $menu, 'NodeElement' + ]); + } + + /** + * The action menu item should not exist within a container. + * + * @Then /^the "(?P(?:[^"]|\\")*)" item should not exist in the "(?P(?:[^"]|\\")*)" action menu of the "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)"$/ + * @param string $item The item to check + * @param string $actionmenu The text used in the description of the action menu + * @param string|NodeElement $locator The identifer used for the container + * @param string $selector The type of container to locate + */ + public function item_should_not_exist_in_the(string $item, string $actionmenu, $locator, $selector): void { + $container = $this->find($selector, $locator); + $menu = $this->find('actionmenu', $actionmenu, false, $container); + $this->execute('behat_general::should_not_exist_in_the', [ + $item, 'link', + $menu, 'NodeElement' + ]); + } + + + /** + * The action menu item should exist. + * + * @Then /^the "(?P(?:[^"]|\\")*)" item should exist in the "(?P(?:[^"]|\\")*)" action menu$/ + * @param string $item The item to check + * @param string $actionmenu The text used in the description of the action menu + */ + public function item_should_exist(string $item, string $actionmenu): void { + $menu = $this->find('actionmenu', $actionmenu); + $this->execute('behat_general::should_exist_in_the', [ + $item, 'link', + $menu, 'NodeElement' + ]); + } + + /** + * The action menu item should exist within a container. + * + * @Then /^the "(?P(?:[^"]|\\")*)" item should exist in the "(?P(?:[^"]|\\")*)" action menu of the "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)"$/ + * @param string $item The item to check + * @param string $actionmenu The text used in the description of the action menu + * @param string|NodeElement $locator The identifer used for the container + * @param string $selector The type of container to locate + */ + public function item_should_exist_in_the(string $item, string $actionmenu, $locator, $selector): void { + $container = $this->find($selector, $locator); + $menu = $this->find('actionmenu', $actionmenu, false, $container); + $this->execute('behat_general::should_exist_in_the', [ + $item, 'link', + $menu, 'NodeElement' + ]); + } } diff --git a/mod/data/tests/behat/data_presets.feature b/mod/data/tests/behat/data_presets.feature index 7039da43ec8..2379933530e 100644 --- a/mod/data/tests/behat/data_presets.feature +++ b/mod/data/tests/behat/data_presets.feature @@ -1,27 +1,27 @@ @mod @mod_data Feature: Users can view and manage data presets - In order to use presets - As a user - I need to view, manage and use presets + In order to use presets + As a user + I need to view, manage and use presets Background: Given the following "users" exist: - | username | firstname | lastname | email | - | teacher1 | Teacher | 1 | teacher1@example.com | + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | And the following "courses" exist: | fullname | shortname | category | - | Course 1 | C1 | 0 | + | Course 1 | C1 | 0 | And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | + | user | course | role | + | teacher1 | C1 | editingteacher | And the following "activities" exist: | activity | name | intro | course | idnumber | | data | Mountain landscapes | n | C1 | data1 | And the following "mod_data > presets" exist: - | database | name | description | user | - | data1 | Saved preset 1 | The preset1 has description | admin | - | data1 | Saved preset 2 | | admin | - | data1 | Saved preset by teacher1 | This preset has also a description | teacher1 | + | database | name | description | user | + | data1 | Saved preset 1 | The preset1 has description | admin | + | data1 | Saved preset 2 | | admin | + | data1 | Saved preset by teacher1 | This preset has also a description | teacher1 | @javascript Scenario: Admins can delete saved presets @@ -71,11 +71,11 @@ Feature: Users can view and manage data presets @javascript Scenario: Only users with the viewalluserpresets capability can see presets created by other users Given the following "permission override" exists: - | role | editingteacher | - | capability | mod/data:viewalluserpresets | - | permission | Prohibit | - | contextlevel | System | - | reference | | + | role | editingteacher | + | capability | mod/data:viewalluserpresets | + | permission | Prohibit | + | contextlevel | System | + | reference | | When I am on the "Mountain landscapes" "data activity" page logged in as teacher1 And I follow "Presets" Then I should see "Image gallery" @@ -86,8 +86,8 @@ Feature: Users can view and manage data presets @javascript Scenario: Teachers can save presets Given the following "mod_data > fields" exist: - | database | type | name | description | - | data1 | text | Test field name | Test field description | + | database | type | name | description | + | data1 | text | Test field name | Test field description | And I am on the "Mountain landscapes" "data activity" page logged in as teacher1 And I follow "Templates" When I click on "Save as preset" "button" @@ -173,10 +173,10 @@ Feature: Users can view and manage data presets @javascript Scenario: Teachers can edit presets and overwrite them if they are the authors Given the following "mod_data > preset" exists: - | database | data1 | - | name | Another preset created by teacher1 | - | description | This description will be overwritten | - | user | teacher1 | + | database | data1 | + | name | Another preset created by teacher1 | + | description | This description will be overwritten | + | user | teacher1 | And I am on the "Mountain landscapes" "data activity" page logged in as teacher1 When I follow "Presets" And I open the action menu in "Saved preset by teacher1" "table_row" @@ -240,11 +240,11 @@ Feature: Users can view and manage data presets @javascript Scenario: Teachers can delete their own presets Given the following "mod_data > fields" exist: - | database | type | name | description | - | data1 | text | Test field name | Test field description | + | database | type | name | description | + | data1 | text | Test field name | Test field description | And the following "mod_data > presets" exist: - | database | name | description | user | - | data1 | Saved preset by teacher1 | My funny description goes here. | teacher1 | + | database | name | description | user | + | data1 | Saved preset by teacher1 | My funny description goes here. | teacher1 | And I am on the "Mountain landscapes" "data activity" page logged in as teacher1 When I follow "Presets" And I should see "Image gallery"