Skip to content

Commit

Permalink
MDL-53571 behat: Use Escaper for escaping
Browse files Browse the repository at this point in the history
xpathLiteral has been depreacted, use Escaper instead
  • Loading branch information
Rajesh Taneja committed Apr 6, 2016
1 parent ccceab9 commit 921faad
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 67 deletions.
2 changes: 1 addition & 1 deletion admin/tests/behat/behat_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function i_set_the_following_administration_settings_values(TableNode $ta
$exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');

// The argument should be converted to an xpath literal.
$label = $this->getSession()->getSelectorsHandler()->xpathLiteral($label);
$label = behat_context_helper::escape($label);

// Single element settings.
try {
Expand Down
6 changes: 3 additions & 3 deletions backup/util/ui/tests/behat/behat_backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function i_import_course_into_course($fromcourse, $tocourse, $options = f
$this->getSession());

// The argument should be converted to an xpath literal.
$fromcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($fromcourse);
$fromcourse = behat_context_helper::escape($fromcourse);
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' ics-results ')]" .
"/descendant::tr[contains(., $fromcourse)]" .
"/descendant::input[@type='radio']";
Expand Down Expand Up @@ -212,7 +212,7 @@ public function i_restore_backup_into_course_using_this_options($backupfilename,
$this->select_backup($backupfilename);

// The argument should be converted to an xpath literal.
$existingcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($existingcourse);
$existingcourse = behat_context_helper::escape($existingcourse);

// Selecting the specified course (we can not call behat_forms::select_radio here as is in another behat subcontext).
$radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-existing-course ')]" .
Expand Down Expand Up @@ -333,7 +333,7 @@ protected function select_backup($backupfilename) {
$this->getSession());

// The argument should be converted to an xpath literal.
$backupfilename = $this->getSession()->getSelectorsHandler()->xpathLiteral($backupfilename);
$backupfilename = behat_context_helper::escape($backupfilename);

$xpath = "//tr[contains(., $backupfilename)]/descendant::a[contains(., '" . get_string('restore') . "')]";
$restorelink = $this->find('xpath', $xpath, $exception);
Expand Down
2 changes: 1 addition & 1 deletion blocks/comments/tests/behat/behat_block_comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function i_delete_comment_from_comments_block($comment) {
$exception = new ElementNotFoundException($this->getSession(), '"' . $comment . '" comment ');

// Using xpath liternal to avoid possible problems with comments containing quotes.
$commentliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($comment);
$commentliteral = behat_context_helper::escape($comment);

$commentxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' block_comments ')]" .
"/descendant::div[@class='comment-message'][contains(., $commentliteral)]";
Expand Down
4 changes: 2 additions & 2 deletions completion/tests/behat/behat_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class behat_completion extends behat_base {
public function user_has_completed_activity($userfullname, $activityname) {

// Will throw an exception if the element can not be hovered.
$titleliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname . ", " . $activityname . ": Completed");
$titleliteral = behat_context_helper::escape($userfullname . ", " . $activityname . ": Completed");
$xpath = "//table[@id='completion-progress']" .
"/descendant::img[contains(@title, $titleliteral)]";

Expand All @@ -69,7 +69,7 @@ public function user_has_completed_activity($userfullname, $activityname) {
public function user_has_not_completed_activity($userfullname, $activityname) {

// Will throw an exception if the element can not be hovered.
$titleliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname . ", " . $activityname . ": Not completed");
$titleliteral = behat_context_helper::escape($userfullname . ", " . $activityname . ": Not completed");
$xpath = "//table[@id='completion-progress']" .
"/descendant::img[contains(@title, $titleliteral)]";

Expand Down
12 changes: 6 additions & 6 deletions course/tests/behat/behat_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function i_add_to_section($activity, $section) {
$sectionxpath = "//li[@id='section-" . $section . "']";
}

$activityliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral(ucfirst($activity));
$activityliteral = behat_context_helper::escape(ucfirst($activity));

if ($this->running_javascript()) {

Expand Down Expand Up @@ -892,7 +892,7 @@ public function i_duplicate_activity($activityname) {
public function i_duplicate_activity_editing_the_new_copy_with($activityname, TableNode $data) {

$activity = $this->escape($activityname);
$activityliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($activityname);
$activityliteral = behat_context_helper::escape($activityname);

$this->execute("behat_course::i_duplicate_activity", $activity);

Expand Down Expand Up @@ -1023,7 +1023,7 @@ protected function show_section_icon_exists($sectionnumber) {
$courseformat = $this->get_course_format();

// Checking the show button alt text and show icon.
$showtext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('showfromothers', $courseformat));
$showtext = behat_context_helper::escape(get_string('showfromothers', $courseformat));
$linkxpath = $xpath . "/descendant::a[@title=$showtext]";
$imgxpath = $linkxpath . "/descendant::img[contains(@src, 'show')]";

Expand All @@ -1050,7 +1050,7 @@ protected function hide_section_icon_exists($sectionnumber) {
$courseformat = $this->get_course_format();

// Checking the hide button alt text and hide icon.
$hidetext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('hidefromothers', $courseformat));
$hidetext = behat_context_helper::escape(get_string('hidefromothers', $courseformat));
$linkxpath = $xpath . "/descendant::a[@title=$hidetext]";
$imgxpath = $linkxpath . "/descendant::img[contains(@src, 'hide')]";

Expand Down Expand Up @@ -1117,7 +1117,7 @@ protected function get_section_activities($sectionxpath) {
*/
protected function get_activity_node($activityname) {

$activityname = $this->getSession()->getSelectorsHandler()->xpathLiteral($activityname);
$activityname = behat_context_helper::escape($activityname);
$xpath = "//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][contains(., $activityname)]";

return $this->find('xpath', $xpath);
Expand Down Expand Up @@ -1677,7 +1677,7 @@ public function i_toggle_category_children_visibility_in_frontpage($categoryname
}

$exception = new ExpectationException('"' . $categoryname . '" category can not be found', $this->getSession());
$categoryliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($categoryname);
$categoryliteral = behat_context_helper::escape($categoryname);
$xpath = "//div[@class='info']/descendant::*[" . implode(' or ', $headingtags) . "][@class='categoryname'][./descendant::a[.=$categoryliteral]]";
$node = $this->find('xpath', $xpath, $exception);
$node->click();
Expand Down
2 changes: 1 addition & 1 deletion enrol/tests/behat/behat_enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function i_enrol_user_as($userfullname, $rolename) {
if ($this->running_javascript()) {

// We have a div here, not a tr.
$userliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname);
$userliteral = behat_context_helper::escape($userfullname);
$userrowxpath = "//div[contains(concat(' ',normalize-space(@class),' '),' user ')][contains(., $userliteral)]";

$this->execute('behat_general::i_click_on_in_the',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ public function i_define_the_following_rubric(TableNode $rubric) {
*/
public function i_replace_rubric_level_with($currentvalue, $value, $criterionname) {

$currentvalueliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($currentvalue);
$criterionliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($criterionname);
$currentvalueliteral = behat_context_helper::escape($currentvalue);
$criterionliteral = behat_context_helper::escape($criterionname);

$criterionxpath = "//div[@id='rubric-rubric']" .
"/descendant::td[contains(concat(' ', normalize-space(@class), ' '), ' description ')]";
Expand Down Expand Up @@ -488,7 +488,7 @@ protected function get_level_xpath($points) {
* @return string
*/
protected function get_criterion_xpath($criterionname) {
$literal = $this->getSession()->getSelectorsHandler()->xpathLiteral($criterionname);
$literal = behat_context_helper::escape($criterionname);
return "//tr[contains(concat(' ', normalize-space(@class), ' '), ' criterion ')]" .
"[./descendant::td[@class='description'][text()=$literal]]";
}
Expand Down
12 changes: 6 additions & 6 deletions grade/grading/tests/behat/behat_grading.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function i_go_to_advanced_grading_page($activityname) {
public function i_go_to_advanced_grading_definition_page($activityname) {

// Transforming to literals, probably not necessary, just in case.
$newactionliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string("manageactionnew", "grading"));
$editactionliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string("manageactionedit", "grading"));
$newactionliteral = behat_context_helper::escape(get_string("manageactionnew", "grading"));
$editactionliteral = behat_context_helper::escape(get_string("manageactionedit", "grading"));

// Working both when adding and editing.
$definitionxpath = "//a[@class='action']" .
Expand All @@ -85,7 +85,7 @@ public function i_go_to_activity_advanced_grading_page($userfullname, $activityn
$usergradetext = get_string('gradeuser', 'assign', $userfullname);

// Shortcut in case we already are in the grading page.
$usergradetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($usergradetext);
$usergradetextliteral = behat_context_helper::escape($usergradetext);
if ($this->getSession()->getPage()->find('named_partial', array('link', $usergradetextliteral))) {
$this->execute('behat_general::click_link', $this->escape($usergradetext));

Expand Down Expand Up @@ -123,14 +123,14 @@ public function i_publish_grading_form_definition_as_a_public_template($activity
*/
public function i_set_activity_to_use_grading_form($activityname, $templatename) {

$templateliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($templatename);
$templateliteral = behat_context_helper::escape($templatename);

$templatexpath = "//h2[@class='template-name'][contains(., $templateliteral)]/" .
"following-sibling::div[contains(concat(' ', normalize-space(@class), ' '), ' template-actions ')]";

// Should work with both templates and own forms.
$literaltemplate = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('templatepick', 'grading'));
$literalownform = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('templatepickownform', 'grading'));
$literaltemplate = behat_context_helper::escape(get_string('templatepick', 'grading'));
$literalownform = behat_context_helper::escape(get_string('templatepickownform', 'grading'));
$usetemplatexpath = "/a[./descendant::div[text()=$literaltemplate]]|" .
"/a[./descendant::div[text()=$literalownform]]";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function the_grade_should_match($student, $itemname, $value) {
}
} else {
// If there isn't a form field, just search for contents.
$valueliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($value);
$valueliteral = behat_context_helper::escape($value);

$xpath = $this->get_student_and_grade_cell_selector($student, $itemname);
$xpath .= "[contains(normalize-space(.)," . $valueliteral . ")]";
Expand Down
16 changes: 8 additions & 8 deletions grade/tests/behat/behat_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function i_give_the_feedback($feedback, $userfullname, $itemname) {
*/
public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {

$gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$gradeitem = behat_context_helper::escape($gradeitem);

if ($this->running_javascript()) {
$xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
Expand All @@ -83,7 +83,7 @@ public function i_set_the_following_settings_for_grade_item($gradeitem, TableNod
}

$savechanges = get_string('savechanges', 'grades');
$edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('edit') . ' ');
$edit = behat_context_helper::escape(get_string('edit') . ' ');
$linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";

$this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));
Expand All @@ -102,7 +102,7 @@ public function i_set_the_following_settings_for_grade_item($gradeitem, TableNod
*/
public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {

$gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$gradeitem = behat_context_helper::escape($gradeitem);

if ($this->running_javascript()) {
$xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
Expand All @@ -113,7 +113,7 @@ public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $g

// Going to edit calculation.
$savechanges = get_string('savechanges', 'grades');
$edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades'));
$edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
$linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
$this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));

Expand Down Expand Up @@ -147,8 +147,8 @@ public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $g
*/
public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) {

$gradecategorytotal = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem . ' total');
$gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$gradecategorytotal = behat_context_helper::escape($gradeitem . ' total');
$gradeitem = behat_context_helper::escape($gradeitem);

if ($this->running_javascript()) {
$xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" .
Expand All @@ -160,7 +160,7 @@ public function i_set_calculation_for_grade_category_with_idnumbers($calculation

// Going to edit calculation.
$savechanges = get_string('savechanges', 'grades');
$edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades'));
$edit = behat_context_helper::escape(get_string('editcalculation', 'grades'));
$linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
$this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element"));

Expand Down Expand Up @@ -197,7 +197,7 @@ public function i_reset_weights_for_grade_category($gradeitem) {
$steps = array();

if ($this->running_javascript()) {
$gradeitemliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$gradeitemliteral = behat_context_helper::escape($gradeitem);
$xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
$this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element"));
Expand Down
4 changes: 2 additions & 2 deletions group/tests/behat/behat_groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class behat_groups extends behat_base {
*/
public function i_add_user_to_group_members($userfullname, $groupname) {

$userfullname = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname);
$userfullname = behat_context_helper::escape($userfullname);

// Using a xpath liternal to avoid problems with quotes and double quotes.
$groupname = $this->getSession()->getSelectorsHandler()->xpathLiteral($groupname);
$groupname = behat_context_helper::escape($groupname);

// We don't know the option text as it contains the number of users in the group.
$select = $this->find_field('groups');
Expand Down
2 changes: 1 addition & 1 deletion lib/behat/behat_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function __call($name, $arguments) {
return $this->find('named_partial',
array(
$cleanname,
$this->getSession()->getSelectorsHandler()->xpathLiteral($arguments[0])
behat_context_helper::escape($arguments[0])
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/behat/behat_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function get_filepicker_node($filepickerelement) {
);
} else {
// Gets the ffilemanager node specified by the locator which contains the filepicker container.
$filepickerelement = $this->getSession()->getSelectorsHandler()->xpathLiteral($filepickerelement);
$filepickerelement = behat_context_helper::escape($filepickerelement);
$filepickercontainer = $this->find(
'xpath',
"//input[./@id = //label[normalize-space(.)=$filepickerelement]/@for]" .
Expand Down Expand Up @@ -133,7 +133,7 @@ protected function open_element_contextual_menu($name, $filemanagerelement = fal
$exception = new ExpectationException($exceptionmsg, $this->getSession());

// Avoid quote-related problems.
$name = $this->getSession()->getSelectorsHandler()->xpathLiteral($name);
$name = behat_context_helper::escape($name);

// Get a filepicker/filemanager element (folder or file).
try {
Expand Down Expand Up @@ -205,7 +205,7 @@ protected function open_add_file_window($filemanagernode, $repositoryname) {
$repoexception = new ExpectationException('The "' . $repositoryname . '" repository has not been found', $this->getSession());

// Avoid problems with both double and single quotes in the same string.
$repositoryname = $this->getSession()->getSelectorsHandler()->xpathLiteral($repositoryname);
$repositoryname = behat_context_helper::escape($repositoryname);

// Here we don't need to look inside the selected element because there can only be one modal window.
$repositorylink = $this->find(
Expand Down
19 changes: 19 additions & 0 deletions lib/behat/classes/behat_context_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class behat_context_helper {
*/
protected static $environment = null;


/**
* @var Escaper::escapeLiteral
*/
protected static $escaper;

/**
* Sets the browser session.
*
Expand Down Expand Up @@ -73,4 +79,17 @@ public static function get($classname) {

return $subcontext;
}

/**
* Translates string to XPath literal.
*
* @param string $label label to escape
* @return string escaped string.
*/
public static function escape($label) {
if (empty(self::$escaper)) {
self::$escaper = new \Behat\Mink\Selector\Xpath\Escaper();
}
return self::$escaper->escapeLiteral($label);
}
}
2 changes: 1 addition & 1 deletion lib/behat/classes/behat_selectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function get_behat_selector($selectortype, $element, Behat\Mink\Se
$locator = $element;
} else {
// Named selectors uses arrays as locators including the type of named selector.
$locator = array($selectortype, $session->getSelectorsHandler()->xpathLiteral($element));
$locator = array($selectortype, behat_context_helper::escape($element));
$selector = 'named_partial';
}

Expand Down
Loading

0 comments on commit 921faad

Please sign in to comment.