Skip to content

Commit

Permalink
MDL-56614 behat: new steps for navigation without blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Dec 6, 2016
1 parent e6cb76d commit ebcff7e
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 75 deletions.
16 changes: 16 additions & 0 deletions blocks/tests/behat/behat_blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;

require_once(__DIR__ . '/../../../lib/behat/behat_base.php');

/**
Expand Down Expand Up @@ -56,6 +58,20 @@ public function i_add_the_block($blockname) {
}
}

/**
* Adds the selected block if it is not already present. Editing mode must be previously enabled.
*
* @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block if not present$/
* @param string $blockname
*/
public function i_add_the_block_if_not_present($blockname) {
try {
$this->get_text_selector_node('block', $blockname);
} catch (ElementNotFoundException $e) {
$this->execute('behat_blocks::i_add_the_block', [$blockname]);
}
}

/**
* Docks a block. Editing mode should be previously enabled.
*
Expand Down
16 changes: 14 additions & 2 deletions course/tests/behat/behat_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function i_turn_editing_mode_on() {
try {
$this->execute("behat_forms::press_button", get_string('turneditingon'));
} catch (Exception $e) {
$this->execute("behat_general::i_click_on_in_the", [get_string('turneditingon'), 'link', 'Administration', 'block']);
$this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingon')]);
}
}

Expand All @@ -64,7 +64,7 @@ public function i_turn_editing_mode_off() {
try {
$this->execute("behat_forms::press_button", get_string('turneditingoff'));
} catch (Exception $e) {
$this->execute("behat_general::i_click_on_in_the", [get_string('turneditingoff'), 'link', 'Administration', 'block']);
$this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingoff')]);
}
}

Expand Down Expand Up @@ -1728,4 +1728,16 @@ public function i_click_on_category_in_the_management_category_listing($name) {
$node = $this->get_management_category_listing_node_by_name($name);
$node->find('css', 'a.categoryname')->click();
}

/**
* Go to the course participants
*
* @Given /^I navigate to course participants$/
*/
public function i_navigate_to_course_participants() {
$coursestr = behat_context_helper::escape(get_string('courses'));
$mycoursestr = behat_context_helper::escape(get_string('mycourses'));
$xpath = "//div[contains(@class,'block')]//li[p/*[string(.)=$coursestr or string(.)=$mycoursestr]]";
$this->execute('behat_general::i_click_on_in_the', [get_string('participants'), 'link', $xpath, 'xpath_element']);
}
}
27 changes: 6 additions & 21 deletions grade/grading/tests/behat/behat_grading.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ public function i_go_to_advanced_grading_page($activityname) {

$this->execute('behat_general::click_link', $this->escape($activityname));

$this->execute('behat_general::i_click_on_in_the',
array(
get_string('gradingmanagement', 'grading'),
'link',
'Administration',
'block'
));
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration',
get_string('gradingmanagement', 'grading'));
}

/**
Expand Down Expand Up @@ -92,13 +87,8 @@ public function i_go_to_activity_advanced_grading_page($userfullname, $activityn

$this->execute('behat_general::click_link', $this->escape($activityname));

$this->execute('behat_general::i_click_on_in_the',
array(
get_string('viewgrading', 'mod_assign'),
'link',
'Administration',
'block'
));
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration',
get_string('viewgrading', 'mod_assign'));

$this->execute('behat_general::i_click_on_in_the',
array(
Expand Down Expand Up @@ -169,13 +159,8 @@ public function i_save_the_advanced_grading_form() {
$this->execute('behat_forms::press_button', 'Ok');
$this->execute('behat_general::i_click_on', array($this->escape(get_string('editsettings')), 'link'));
$this->execute('behat_forms::press_button', get_string('cancel'));
$this->execute('behat_general::i_click_on_in_the',
array(
get_string('viewgrading', 'mod_assign'),
'link',
'Administration',
'block'
));
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration',
get_string('viewgrading', 'mod_assign'));
}

/**
Expand Down
57 changes: 40 additions & 17 deletions grade/tests/behat/behat_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,17 @@ public function gradebook_calculations_for_the_course_are_frozen_at_version($cou
}

/**
* Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
*
* Examples:
* - I go to "Setup > Gradebook setup" in the course gradebook
* - I go to "Scales" in the course gradebook
* - I go to "Letters > View" in the course gradebook
* - I go to "View > User report" in the course gradebook // for teachers
* - I go to "User report" in the course gradebook // for students
* Select the tab in the gradebook. We must be on one of the gradebook pages already.
*
* @Given /^I go to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
* @param string $gradepath
* @param string $gradepath examples: "View > User report", "Letters > View", "Scales"
*/
public function i_go_to_in_the_course_gradebook($gradepath) {
protected function select_in_gradebook_tabs($gradepath) {
$gradepath = preg_split('/\s*>\s*/', trim($gradepath));
if (count($gradepath) > 2) {
throw new DriverException('Grade path is too long (must have no more than two items separated with ">")');
throw new coding_exception('Grade path is too long (must have no more than two items separated with ">")');
}

// If we are not on one of the gradebook pages already, follow "Grades" link in the navigation block.
$xpath = '//div[contains(@class,\'grade-navigation\')]';
if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
$this->execute("behat_general::i_click_on_in_the", array(get_string('grades'), 'link',
get_string('pluginname', 'block_navigation'), 'block'));
}

// If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
$link = '\'' . $this->escape($gradepath[0]) . '\'';
Expand All @@ -290,4 +277,40 @@ public function i_go_to_in_the_course_gradebook($gradepath) {
}
}
}

/**
* Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
*
* Examples:
* - I navigate to "Setup > Gradebook setup" in the course gradebook
* - I navigate to "Scales" in the course gradebook
* - I navigate to "Letters > View" in the course gradebook
* - I navigate to "View > User report" in the course gradebook // for teachers
* - I navigate to "User report" in the course gradebook // for students
*
* @Given /^I navigate to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
* @param string $gradepath
*/
public function i_navigate_to_in_the_course_gradebook($gradepath) {
// If we are not on one of the gradebook pages already, follow "Grades" link in the navigation block.
$xpath = '//div[contains(@class,\'grade-navigation\')]';
if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
$this->execute("behat_general::i_click_on_in_the", array(get_string('grades'), 'link',
get_string('pluginname', 'block_navigation'), 'block'));
}

$this->select_in_gradebook_tabs($gradepath);
}

/**
* Navigates to the course gradebook and selects a specified item from the grade navigation tabs.
*
* @todo MDL-57282 deprecate in Moodle 3.3
*
* @Given /^I go to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
* @param string $gradepath
*/
public function i_go_to_in_the_course_gradebook($gradepath) {
$this->execute('behat_grade::i_navigate_to_in_the_course_gradebook', $gradepath);
}
}
5 changes: 1 addition & 4 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ public static function get_required_by_theme_block_types() {
$requiredbythemeblocks = $PAGE->theme->requiredblocks;
}

// We need blocks for behat, till MDL-56614 gets in.
if (defined('BEHAT_SITE_RUNNING')) {
return array('navigation', 'settings');
} else if ($requiredbythemeblocks === false) {
if ($requiredbythemeblocks === false) {
return array('navigation', 'settings');
} else if ($requiredbythemeblocks === '') {
return array();
Expand Down
109 changes: 108 additions & 1 deletion lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,37 @@ public function i_collapse_node($nodetext) {
*
* @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
*
* @todo MDL-57281 deprecate in Moodle 3.1
*
* @throws ExpectationException
* @param string $nodetext navigation node to click.
* @param string $parentnodes comma seperated list of parent nodes.
* @return void
*/
public function i_navigate_to_node_in($nodetext, $parentnodes) {
// This step needs to be deprecated and replaced with one of:
// - I navigate to "PATH" in current page administration
// - I navigate to "PATH" in site administration
// - I navigate to course participants
// - I navigate to "PATH" in the course gradebook
// - I click on "LINK" "link" in the "Navigation" "block" .
$parentnodes = array_map('trim', explode('>', $parentnodes));
$this->select_node_in_navigation($nodetext, $parentnodes);
}

/**
* Finds a node in the Navigation or Administration tree and clicks on it.
*
* @param string $nodetext
* @param array $parentnodes
* @throws ExpectationException
*/
protected function select_node_in_navigation($nodetext, $parentnodes) {

// Site admin is different and needs special treatment.
$siteadminstr = get_string('administrationsite');

// Create array of all parentnodes.
$parentnodes = array_map('trim', explode('>', $parentnodes));
$countparentnode = count($parentnodes);

// If JS is disabled and Site administration is not expanded we
Expand Down Expand Up @@ -415,4 +434,92 @@ public function get_expand_navbar_step() {

$this->execute('behat_general::i_click_on', array(".btn-navbar", "css_element"));
}

/**
* Go to current page setting item
*
* This can be used on front page, course, category or modules pages.
*
* @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" in current page administration$/
*
* @throws ExpectationException
* @param string $nodetext navigation node to click, may contain path, for example "Reports > Overview"
* @return void
*/
public function i_navigate_to_in_current_page_administration($nodetext) {
$parentnodes = array_map('trim', explode('>', $nodetext));
// Find the name of the first category of the administration block tree.
$xpath = '//div[contains(@class,\'block_settings\')]//div[@id=\'settingsnav\']/ul/li[1]/p[1]/span';
$node = $this->find('xpath', $xpath);
array_unshift($parentnodes, $node->getText());
$lastnode = array_pop($parentnodes);
$this->select_node_in_navigation($lastnode, $parentnodes);
}

/**
* Checks that current page administration contains text
*
* @Given /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in current page administration$/
*
* @throws ExpectationException
* @param string $element The locator of the specified selector.
* This may be a path, for example "Subscription mode > Forced subscription"
* @param string $selectortype The selector type
* @return void
*/
public function should_exist_in_current_page_administration($element, $selectortype) {
$parentnodes = array_map('trim', explode('>', $element));
$element = array_pop($parentnodes);

foreach ($parentnodes as $parentnode) {
try {
$this->i_expand_node($parentnode);
} catch (ExpectationException $e) {
// Parent node not found.
return;
}
}

$xpath = '//div[contains(@class,\'block_settings\')]//div[@id=\'settingsnav\']/ul/li[1]';
$this->execute('behat_general::should_exist_in_the', [$element, $selectortype, $xpath, 'xpath_element']);
}

/**
* Checks that current page administration contains text
*
* @Given /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in current page administration$/
*
* @throws ExpectationException
* @param string $element The locator of the specified selector.
* This may be a path, for example "Subscription mode > Forced subscription"
* @param string $selectortype The selector type
* @return void
*/
public function should_not_exist_in_current_page_administration($element, $selectortype) {
$parentnodes = array_map('trim', explode('>', $element));
$element = array_pop($parentnodes);

foreach ($parentnodes as $parentnode) {
$this->i_expand_node($parentnode);
}

$xpath = '//div[contains(@class,\'block_settings\')]//div[@id=\'settingsnav\']/ul/li[1]';
$this->execute('behat_general::should_not_exist_in_the', [$element, $selectortype, $xpath, 'xpath_element']);
}

/**
* Go to site administration item
*
* @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" in site administration$/
*
* @throws ExpectationException
* @param string $nodetext navigation node to click, may contain path, for example "Reports > Overview"
* @return void
*/
public function i_navigate_to_in_site_administration($nodetext) {
$parentnodes = array_map('trim', explode('>', $nodetext));
array_unshift($parentnodes, get_string('administrationsite'));
$lastnode = array_pop($parentnodes);
$this->select_node_in_navigation($lastnode, $parentnodes);
}
}
10 changes: 8 additions & 2 deletions mod/data/tests/behat/behat_mod_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ class behat_mod_data extends behat_base {
public function i_add_a_field_to_database_and_i_fill_the_form_with($fieldtype, $activityname, TableNode $fielddata) {

$this->execute("behat_general::click_link", $this->escape($activityname));
$this->execute("behat_navigation::i_navigate_to_node_in", array(get_string('fields', 'mod_data'),
get_string('pluginadministration', 'mod_data')));

// Open "Fields" tab if it is not already open.
$fieldsstr = get_string('fields', 'mod_data');
$xpath = '//ul[contains(@class,\'nav-tabs\')]//*[contains(@class,\'active\') and contains(normalize-space(.), \'' .
$fieldsstr . '\')]';
if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
$this->execute("behat_general::i_click_on_in_the", array($fieldsstr, 'link', '.nav-tabs', 'css_element'));
}

$this->execute('behat_forms::i_set_the_field_to', array('newtype', $this->escape($fieldtype)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ public function i_add_a_reviewer_for_workshop_participant($reviewername, $partic
public function i_allocate_submissions_in_workshop_as($workshopname, TableNode $table) {

$this->find_link($workshopname)->click();
$this->execute('behat_general::i_click_on_in_the',
array(
get_string('allocate', 'workshop'),
'link',
'Administration',
'block'
));
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration', get_string('allocate', 'workshop'));
$rows = $table->getRows();
$reviewer = $participant = null;
for ($i = 0; $i < count($rows[0]); $i++) {
Expand Down
15 changes: 3 additions & 12 deletions mod/workshop/tests/behat/behat_mod_workshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,14 @@ public function i_add_a_submission_in_workshop_as($workshopname, $table) {
* @param TableNode $table data to fill the submission form with, must contain 'Title'
*/
public function i_edit_assessment_form_in_workshop_as($workshopname, $table) {
$workshopname = $this->escape($workshopname);
$editassessmentform = $this->escape(get_string('editassessmentform', 'workshop'));
$saveandclose = $this->escape(get_string('saveandclose', 'workshop'));

$this->execute('behat_general::click_link', $workshopname);

$this->execute('behat_general::i_click_on_in_the',
array(
$editassessmentform,
'link',
'Administration',
'block'
));
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration',
get_string('editassessmentform', 'workshop'));

$this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table);

$this->execute("behat_forms::press_button", $saveandclose);
$this->execute("behat_forms::press_button", get_string('saveandclose', 'workshop'));
}

/**
Expand Down
Loading

0 comments on commit ebcff7e

Please sign in to comment.