From bbaf83322fb421ff0e35993f31d598ef30416f49 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Fri, 29 Apr 2022 16:38:41 +0200 Subject: [PATCH] MDL-74613 grade: Add missing generators --- .../grader/tests/behat/switch_views.feature | 45 ++++++++-------- lib/behat/classes/behat_core_generator.php | 22 ++++++++ lib/testing/generator/data_generator.php | 51 +++++++++++++++++++ 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/grade/report/grader/tests/behat/switch_views.feature b/grade/report/grader/tests/behat/switch_views.feature index 98b764b86c1..b81ca0c970a 100644 --- a/grade/report/grader/tests/behat/switch_views.feature +++ b/grade/report/grader/tests/behat/switch_views.feature @@ -12,7 +12,7 @@ Feature: We can change what we are viewing on the grader report | username | firstname | lastname | email | | teacher1 | Teacher | 1 | teacher1@example.com | | student1 | Student | 1 | student1@example.com | - | student2 | Student | 1 | student2@example.com | + | student2 | Student | 2 | student2@example.com | And the following "course enrolments" exist: | user | course | role | | teacher1 | C1 | editingteacher | @@ -26,18 +26,15 @@ Feature: We can change what we are viewing on the grader report | assign | user | onlinetext | | Test assignment name 1 | student1 | This is a submission for assignment 1 | | Test assignment name 2 | student1 | This is a submission for assignment 2 | - And I am on the "Test assignment name 1" "assign activity" page logged in as student1 - Then I should see "Submitted for grading" - And I am on the "Test assignment name 2" "assign activity" page - Then I should see "Submitted for grading" - And I log out + And the following "grade items" exist: + | itemname | grademin | grademax | course | + | Manual grade | 20 | 40 | C1 | + And the following "grade grades" exist: + | gradeitem | user | grade | + | Test assignment name 1 | student1 | 80 | + | Test assignment name 2 | student1 | 90 | + | Manual grade | student1 | 30 | And I log in as "teacher1" - And I am on "Course 1" course homepage - And I navigate to "View > Grader report" in the course gradebook - 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 1" - And I give the grade "90.00" to the user "Student 1" for the grade item "Test assignment name 2" - And I press "Save changes" @javascript Scenario: View and minimise the grader report containing hidden activities @@ -48,13 +45,15 @@ Feature: We can change what we are viewing on the grader report And I navigate to "View > Grader report" in the course gradebook And I should see "Test assignment name 1" And I should see "Test assignment name 2" + And I should see "Manual grade" And I should see "Course total" And the following should exist in the "user-grades" table: - | -1- | -4- | -5- | -6- | - | Student 1 | 80 | 90 | 170 | + | -1- | -4- | -5- | -6- | -7- | + | Student 1 | 80 | 90 | 30 | 170 | And I click on "Change to aggregates only" "link" And I should not see "Test assignment name 1" And I should not see "Test assignment name 2" + And I should not see "Manual grade" And I should see "Course total" And the following should exist in the "user-grades" table: | -1- | -4- | @@ -62,10 +61,11 @@ Feature: We can change what we are viewing on the grader report And I click on "Change to grades only" "link" And I should see "Test assignment name 1" And I should see "Test assignment name 2" + And I should see "Manual grade" And I should not see "Course total" And the following should exist in the "user-grades" table: - | -1- | -4- | -5- | - | Student 1 | 80 | 90 | + | -1- | -4- | -5- | -6- | + | Student 1 | 80 | 90 | 30 | @javascript @skip_chrome_zerosize Scenario: View and minimise the grader report containing hidden activities without the 'moodle/grade:viewhidden' capability @@ -83,21 +83,24 @@ Feature: We can change what we are viewing on the grader report And I navigate to "View > Grader report" in the course gradebook And I should see "Test assignment name 1" And I should see "Test assignment name 2" + And I should see "Manual grade" And I should see "Course total" And the following should exist in the "user-grades" table: - | -1- | -4- | -5- | -6- | - | Student 1 | 80 | - | 80 | + | -1- | -4- | -5- | -6- | -7- | + | Student 1 | 80 | - | 30 | 105.71 | And I click on "Change to aggregates only" "link" And I should not see "Test assignment name 1" And I should not see "Test assignment name 2" + And I should not see "Manual grade" And I should see "Course total" And the following should exist in the "user-grades" table: | -1- | -4- | - | Student 1 | 80 | + | Student 1 | 105.71 | And I click on "Change to grades only" "link" And I should see "Test assignment name 1" And I should see "Test assignment name 2" + And I should see "Manual grade" And I should not see "Course total" And the following should exist in the "user-grades" table: - | -1- | -4- | -5- | - | Student 1 | 80 | - | + | -1- | -4- | -5- | -6- | + | Student 1 | 80 | - | 30 | diff --git a/lib/behat/classes/behat_core_generator.php b/lib/behat/classes/behat_core_generator.php index 02ad43e3730..ea71e032849 100644 --- a/lib/behat/classes/behat_core_generator.php +++ b/lib/behat/classes/behat_core_generator.php @@ -162,6 +162,12 @@ class behat_core_generator extends behat_generator_base { 'required' => ['fullname', 'course'], 'switchids' => ['course' => 'courseid', 'gradecategory' => 'parent'], ], + 'grade grades' => [ + 'singular' => 'grade grade', + 'datagenerator' => 'grade_grade', + 'required' => ['gradeitem'], + 'switchids' => ['user' => 'userid', 'gradeitem' => 'itemid'], + ], 'grade items' => [ 'singular' => 'grade item', 'datagenerator' => 'grade_item', @@ -292,6 +298,22 @@ class behat_core_generator extends behat_generator_base { return $entities; } + /** + * Get the grade item id using a name. + * + * @param string $name + * @return int The grade item id + */ + protected function get_gradeitem_id(string $name): int { + global $DB; + + if (!$id = $DB->get_field('grade_items', 'id', ['itemname' => $name])) { + throw new Exception('The specified grade item with name "' . $name . '" could not be found.'); + } + + return $id; + } + /** * Remove any empty custom fields, to avoid errors when creating the course. * diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index ca66664314c..73ee0cb1047 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -1010,6 +1010,57 @@ EOD; return $gradecategory->get_record_data(); } + /** + * Create a grade_grade. + * + * @param array $record + * @return grade_grade the grade record + */ + public function create_grade_grade(?array $record = null): grade_grade { + global $DB, $USER; + + $item = $DB->get_record('grade_items', ['id' => $record['itemid']]); + $userid = $record['userid'] ?? $USER->id; + + unset($record['itemid']); + unset($record['userid']); + + if ($item->itemtype === 'mod') { + $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance); + $module = new $item->itemmodule(context_module::instance($cm->id), $cm, false); + $record['attemptnumber'] = $record['attemptnumber'] ?? 0; + + $module->save_grade($userid, (object) $record); + + $grade = grade_grade::fetch(['userid' => $userid, 'itemid' => $item->id]); + } else { + $grade = grade_grade::fetch(['userid' => $userid, 'itemid' => $item->id]); + $record['rawgrade'] = $record['rawgrade'] ?? $record['grade'] ?? null; + $record['finalgrade'] = $record['finalgrade'] ?? $record['grade'] ?? null; + + unset($record['grade']); + + if ($grade) { + $fields = $grade->required_fields + array_keys($grade->optional_fields); + + foreach ($fields as $field) { + $grade->{$field} = $record[$field] ?? $grade->{$field}; + } + + $grade->update(); + } else { + $record['userid'] = $userid; + $record['itemid'] = $item->id; + + $grade = new grade_grade($record, false); + + $grade->insert(); + } + } + + return $grade; + } + /** * Create a grade_item. *