diff --git a/grade/tests/behat/grade_view.feature b/grade/tests/behat/grade_view.feature index 1520026dbfa..59bb3b8fa15 100644 --- a/grade/tests/behat/grade_view.feature +++ b/grade/tests/behat/grade_view.feature @@ -21,16 +21,27 @@ Feature: We can enter in grades and view reports from the gradebook And I follow "Course 1" And I turn editing mode on And I add a "Assignment" to section "1" and I fill the form with: - | Assignment name | Test assignment name | + | Assignment name | Test assignment name 1 | + | Description | Submit your online text | + | assignsubmission_onlinetext_enabled | 1 | + And I add a "Assignment" to section "1" and I fill the form with: + | Assignment name | Test assignment name 2 | | Description | Submit your online text | | assignsubmission_onlinetext_enabled | 1 | And I log out And I log in as "student1" And I follow "Course 1" - And I follow "Test assignment name" + And I follow "Test assignment name 1" When I press "Add submission" And I set the following fields to these values: - | Online text | This is a submission | + | Online text | This is a submission for assignment 1 | + And I press "Save changes" + Then I should see "Submitted for grading" + And I follow "Course 1" + And I follow "Test assignment name 2" + When I press "Add submission" + And I set the following fields to these values: + | Online text | This is a submission for assignment 2 | And I press "Save changes" Then I should see "Submitted for grading" And I log out @@ -38,7 +49,8 @@ Feature: We can enter in grades and view reports from the gradebook And I follow "Course 1" And I follow "Grades" And I turn editing mode on - And I give the grade "80.00" to the user "Student 1" for the grade item "Test assignment name" + And I give the grade "80.00" to the user "Student 1" for the grade item "Test assignment name 1" + And I give the grade "90.00" to the user "Student 1" for the grade item "Test assignment name 2" And I press "Update" @javascript @@ -52,9 +64,17 @@ Feature: We can enter in grades and view reports from the gradebook And I log in as "student1" And I follow "Course 1" And I follow "Grades" - And I should see "80.00" in the "Test assignment name" "table_row" + Then the following should exist in the "user-grade" table: + | Grade item | Grade | Range | Percentage | + | Test assignment name 1 | 80.00 | 0–100 | 80.00 % | + | Test assignment name 2 | 90.00 | 0–100 | 90.00 % | + | Course total | 85.00 | 0–100 | 85.00 % | + And the following should not exist in the "user-grade" table: + | Grade item | Grade | Range | Percentage | + | Course total | 90.00 | 0–110 | 90.00 % | And I set the field "Grade report" to "Overview report" - And I should see "80.00" in the "overview-grade" "table" + And "C1" row "Grade" column of "overview-grade" table should contain "85.00" + And "C1" row "Grade" column of "overview-grade" table should not contain "90.00" @javascript Scenario: We can add a weighting to a grade item and it is displayed properly in the user report @@ -72,5 +92,13 @@ Feature: We can enter in grades and view reports from the gradebook And I log in as "student1" And I follow "Course 1" And I follow "Grades" - Then I should see "0.72" in the "Test assignment name" "table_row" - And I should not see "0.72%" in the "Test assignment name" "table_row" + Then the following should exist in the "user-grade" table: + | Grade item | Weight | Grade | Range | Percentage | + | Test assignment name 1 | 0.72 | 80.00 | 0–100 | 80.00 % | + | Test assignment name 2 | 1.00 | 90.00 | 0–100 | 90.00 % | + | Course total | - | 85.81 | 0–100 | 85.81 % | + And the following should not exist in the "user-grade" table: + | Grade item | Weight | Percentage | + | Test assignment name 1 | 0.72% | 0.72% | + | Test assignment name 2 | 1.00% | 1.00% | + | Course total | 1.00% | 1.00% | diff --git a/lib/behat/classes/behat_selectors.php b/lib/behat/classes/behat_selectors.php index 85dde956af4..e38b2dbb225 100644 --- a/lib/behat/classes/behat_selectors.php +++ b/lib/behat/classes/behat_selectors.php @@ -110,6 +110,9 @@ XPATH , 'filemanager' => << <<getSession()); } } + + /** + * Checks the provided value exists in specific row/column of table. + * + * @Then /^"(?P[^"]*)" row "(?P[^"]*)" column of "(?P[^"]*)" table should contain "(?P[^"]*)"$/ + * @throws ElementNotFoundException + * @param string $row row text which will be looked in. + * @param string $column column text to search + * @param string $table table id/class/caption + * @param string $value text to check. + */ + public function row_column_of_table_should_contain($row, $column, $table, $value) { + $tablenode = $this->get_selected_node('table', $table); + $tablexpath = $tablenode->getXpath(); + + // Check if column exists, it can be in thead or tbody first row. + $columnheaderxpath = $tablexpath . "[thead/tr/th[normalize-space(.)='$column'] | " + . "tbody/tr[1]/th[normalize-space(.)='" . $column . "']]"; + $columnheader = $this->getSession()->getDriver()->find($columnheaderxpath); + if (empty($columnheader)) { + $columnexceptionmsg = $column . '" in table "' . $table . '"'; + throw new ElementNotFoundException($this->getSession(), 'Column', null, $columnexceptionmsg); + } + + // Check if value exists in specific row/column. + // Get row xpath. + $rowxpath = $tablexpath."/tbody/tr[th[normalize-space(.)='" . $row . "'] | td[normalize-space(.)='" . $row . "']]"; + + // Following conditions were considered before finding column count. + // 1. Table header can be in thead/tr/th or tbody/tr/td[1]. + // 2. First column can have th (Gradebook -> user report), so having lenient sibling check. + $columnpositionxpath = "/child::*[position() = count(" . $tablexpath . "/thead/tr[1]/th[normalize-space(.)='" . + $column . "']/preceding-sibling::*) + 1]"; + $columnvaluexpath = $rowxpath . $columnpositionxpath . "[text()[contains(normalize-space(.),'" . $value . "')]]"; + + // Looks for the requested node inside the container node. + $coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath); + if (empty($coumnnode)) { + // Check if tbody/tr[1] contains header selector. + $columnpositionxpath = "/child::*[position() = count(" . $tablexpath . "/tbody/tr[1]/td[normalize-space(.)='" . + $column . "']/preceding-sibling::*) + 1]"; + $columnvaluexpath = $rowxpath . $columnpositionxpath . "[text()[contains(normalize-space(.),'" . $value . "')]]"; + $coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath); + if (empty($coumnnode)) { + $locatorexceptionmsg = $value . '" in "' . $row . '" row with column "' . $column; + throw new ElementNotFoundException($this->getSession(), 'Column value', null, $locatorexceptionmsg); + } + } + } + + /** + * Checks the provided value should not exist in specific row/column of table. + * + * @Then /^"(?P[^"]*)" row "(?P[^"]*)" column of "(?P[^"]*)" table should not contain "(?P[^"]*)"$/ + * @throws ElementNotFoundException + * @param string $row row text which will be looked in. + * @param string $column column text to search + * @param string $table table id/class/caption + * @param string $value text to check. + */ + public function row_column_of_table_should_not_contain($row, $column, $table, $value) { + try { + $this->row_column_of_table_should_contain($row, $column, $table, $value); + // Throw exception if found. + throw new ExpectationException( + '"' . $column . '" with value "' . $value . '" is present in "' . $row . '" row for table "' . $table . '"', + $this->getSession() + ); + } catch (ElementNotFoundException $e) { + // Table row/column doesn't contain this value. Nothing to do. + return; + } + } + + /** + * Checks that the provided value exist in table. + * More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. + * + * @Then /^the following should exist in the "(?P[^"]*)" table:$/ + * @throws ExpectationException + * @param string $table name of table + * @param TableNode $data table with first row as header and following values + * | Header 1 | Header 2 | Header 3 | + * | Value 1 | Value 2 | Value 3| + */ + public function following_should_exit_in_the_table($table, TableNode $data) { + $datahash = $data->getHash(); + + foreach ($datahash as $value) { + $row = array_shift($value); + foreach ($value as $column => $value) { + $this->row_column_of_table_should_contain($row, $column, $table, $value); + } + } + } + + /** + * Checks that the provided value exist in table. + * More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. + * + * @Then /^the following should not exist in the "(?P[^"]*)" table:$/ + * @throws ExpectationException + * @param string $table name of table + * @param TableNode $data table with first row as header and following values + * | Header 1 | Header 2 | Header 3 | + * | Value 1 | Value 2 | Value 3| + */ + public function following_should_not_exit_in_the_table($table, TableNode $data) { + $datahash = $data->getHash(); + + foreach ($datahash as $value) { + $row = array_shift($value); + foreach ($value as $column => $value) { + try { + $this->row_column_of_table_should_contain($row, $column, $table, $value); + // Throw exception if found. + throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' . + $row . '" row for table "' . $table . '"', $this->getSession() + ); + } catch (ElementNotFoundException $e) { + // Table row/column doesn't contain this value. Nothing to do. + continue; + } + } + } + } }