diff --git a/admin/tool/behat/tests/behat/data_generators.feature b/admin/tool/behat/tests/behat/data_generators.feature index 1081477abe0..19c8c73b744 100644 --- a/admin/tool/behat/tests/behat/data_generators.feature +++ b/admin/tool/behat/tests/behat/data_generators.feature @@ -271,3 +271,20 @@ Feature: Set up contextual data for tests | Name | Cohort size | | Cohort in category | 1 | | Empty cohort | 0 | + + Scenario: Add grade categories with data generator + Given the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "grade categories" exist: + | fullname | course | + | Grade category 1 | C1| + And the following "grade categories" exist: + | fullname | course | gradecategory | + | Grade sub category 2 | C1 | Grade category 1| + When I log in as "admin" + And I follow "Courses" + And I follow "Course 1" + And I navigate to "Grades" node in "Course administration" + Then I should see "Grade category 1" + And I should see "Grade sub category 2" diff --git a/grade/tests/behat/grade_aggregation.feature b/grade/tests/behat/grade_aggregation.feature index 3c159b36bb2..1ca89eeb428 100644 --- a/grade/tests/behat/grade_aggregation.feature +++ b/grade/tests/behat/grade_aggregation.feature @@ -16,11 +16,26 @@ Feature: We can use calculated grade totals | user | course | role | | teacher1 | C1 | editingteacher | | student1 | C1 | student | + And the following "grade categories" exist: + | fullname | course | + | Sub category 1 | C1| + | Sub category 2 | C1| And the following "activities" exist: - | activity | course | idnumber | name | intro | - | assign | C1 | a1 | Test assignment one | Submit something! | - | assign | C1 | a2 | Test assignment two | Submit something! | - | assign | C1 | a3 | Test assignment three | Submit something! | + | activity | course | idnumber | name | intro | grade | + | assign | C1 | a1 | Test assignment one | Submit something! | 300 | + | assign | C1 | a2 | Test assignment two | Submit something! | 100 | + | assign | C1 | a3 | Test assignment three | Submit something! | 150 | + | assign | C1 | a4 | Test assignment four | Submit nothing! | 150 | + And the following "activities" exist: + | activity | course | idnumber | name | intro | gradecategory | grade | + | assign | C1 | a5 | Test assignment five | Submit something! | Sub category 1 | 200 + | assign | C1 | a6 | Test assignment six | Submit something! | Sub category 1 | 100 + | assign | C1 | a7 | Test assignment seven | Submit nothing! | Sub category 1 | 150 + And the following "activities" exist: + | activity | course | idnumber | name | intro | gradecategory | grade | + | assign | C1 | a8 | Test assignment eight | Submit something! | Sub category 2 | 200 + | assign | C1 | a9 | Test assignment nine | Submit something! | Sub category 2 | 100 + | assign | C1 | 10 | Test assignment ten | Submit nothing! | Sub category 2 | 150 And I log in as "teacher1" And I follow "Course 1" And I follow "Grades" @@ -28,10 +43,20 @@ Feature: We can use calculated grade totals And I give the grade "60.00" to the user "Student 1" for the grade item "Test assignment one" And I give the grade "20.00" to the user "Student 1" for the grade item "Test assignment two" And I give the grade "40.00" to the user "Student 1" for the grade item "Test assignment three" + And I give the grade "10.00" to the user "Student 1" for the grade item "Test assignment five" + And I give the grade "5.00" to the user "Student 1" for the grade item "Test assignment six" + And I give the grade "10.00" to the user "Student 1" for the grade item "Test assignment eight" + And I give the grade "5.00" to the user "Student 1" for the grade item "Test assignment nine" And I press "Update" And I click on "Edit assign Test assignment two" "link" And I click on "Hidden" "checkbox" And I press "Save changes" + And I click on "Edit assign Test assignment five" "link" + And I click on "Hidden" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment eight" "link" + And I click on "Hidden" "checkbox" + And I press "Save changes" And I follow "Course grade settings" And I set the field "Grade display type" to "Real (percentage)" And I press "Save changes" @@ -41,8 +66,16 @@ Feature: We can use calculated grade totals And I follow "Edit Course 1" And I set the field "Aggregation" to "Mean of grades" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Mean of grades" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Mean of grades" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" And I turn editing mode off - Then I should see "40.00 (40.00 %)" in the ".course" "css_element" + Then I should see "30.00 (30.00 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -51,18 +84,30 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "50.00 (50.00 %)" in the "overview-grade" "table" + And I should see "30.42 (30.42 %)" in the "overview-grade" "table" @javascript Scenario: Weighted mean of grades aggregation And I follow "Edit Course 1" And I set the field "Aggregation" to "Weighted mean of grades" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Weighted mean of grades" + And I expand all fieldsets + And I set the field "Item weight" to "1" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Weighted mean of grades" + And I expand all fieldsets + And I set the field "Item weight" to "1" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" And I click on "Edit assign Test assignment one" "link" + And I expand all fieldsets And I set the field "Item weight" to "3" And I press "Save changes" And I turn editing mode off - Then I should see "48.00 (48.00 %)" in the ".course" "css_element" + Then I should see "27.14 (27.14 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -71,15 +116,27 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "55.00 (55.00 %)" in the "overview-grade" "table" + And I should see "26.94 (26.94 %)" in the "overview-grade" "table" @javascript Scenario: Simple weighted mean of grades aggregation And I follow "Edit Course 1" And I set the field "Aggregation" to "Simple weighted mean of grades" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Simple weighted mean of grades" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Simple weighted mean of grades" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment one" "link" + And I expand all fieldsets + And I click on "Extra credit" "checkbox" + And I press "Save changes" And I turn editing mode off - Then I should see "40.00 (40.00 %)" in the ".course" "css_element" + Then I should see "45.19 (45.19 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -88,15 +145,27 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "50.00 (50.00 %)" in the "overview-grade" "table" + And I should see "48.57 (48.57 %)" in the "overview-grade" "table" @javascript Scenario: Mean of grades (with extra credits) aggregation And I follow "Edit Course 1" And I set the field "Aggregation" to "Mean of grades (with extra credits)" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Mean of grades (with extra credits)" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Mean of grades (with extra credits)" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment one" "link" + And I expand all fieldsets + And I set the field "Extra credit weight" to "2" + And I press "Save changes" And I turn editing mode off - Then I should see "40.00 (40.00 %)" in the ".course" "css_element" + Then I should see "42.50 (42.50 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -105,15 +174,82 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "50.00 (50.00 %)" in the "overview-grade" "table" + And I should see "47.22 (47.22 %)" in the "overview-grade" "table" @javascript Scenario: Median of grades aggregation And I follow "Edit Course 1" And I set the field "Aggregation" to "Median of grades" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Median of grades" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Median of grades" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" And I turn editing mode off - Then I should see "40.00 (40.00 %)" in the ".course" "css_element" + Then I should see "26.67 (26.67 %)" in the ".course" "css_element" + And I follow "Course grade settings" + And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" + And I press "Save changes" + And I log out + And I log in as "student1" + And I follow "Course 1" + And I follow "Grades" + And I set the field "Grade report" to "Overview report" + And I should see "25.83 (25.83 %)" in the "overview-grade" "table" + + @javascript + Scenario: Lowest grade aggregation + And I follow "Edit Course 1" + And I set the field "Aggregation" to "Lowest grade" + And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Lowest grade" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Lowest grade" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment five" "link" + And I click on "Hidden" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment four" "link" + And I click on "Hidden" "checkbox" + And I press "Save changes" + And I turn editing mode off + Then I should see "0.00 (0.00 %)" in the ".course" "css_element" + And I follow "Course grade settings" + And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" + And I press "Save changes" + And I log out + And I log in as "student1" + And I follow "Course 1" + And I follow "Grades" + And I set the field "Grade report" to "Overview report" + And I should see "0.00 (0.00 %)" in the "overview-grade" "table" + + @javascript + Scenario: Highest grade aggregation + And I follow "Edit Course 1" + And I set the field "Aggregation" to "Highest grade" + And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Highest grade" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Highest grade" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" + And I click on "Edit assign Test assignment one" "link" + And I click on "Hidden" "checkbox" + And I press "Save changes" + And I turn editing mode off + Then I should see "50.00 (50.00 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -124,53 +260,24 @@ Feature: We can use calculated grade totals And I set the field "Grade report" to "Overview report" And I should see "50.00 (50.00 %)" in the "overview-grade" "table" - @javascript - Scenario: Lowest grade aggregation - And I follow "Edit Course 1" - And I set the field "Aggregation" to "Lowest grade" - And I press "Save changes" - And I turn editing mode off - Then I should see "20.00 (20.00 %)" in the ".course" "css_element" - And I follow "Course grade settings" - And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" - And I press "Save changes" - And I log out - And I log in as "student1" - And I follow "Course 1" - And I follow "Grades" - And I set the field "Grade report" to "Overview report" - And I should see "40.00 (40.00 %)" in the "overview-grade" "table" - - @javascript - Scenario: Highest grade aggregation - And I follow "Edit Course 1" - And I set the field "Aggregation" to "Highest grade" - And I press "Save changes" - And I click on "Edit assign Test assignment one" "link" - And I click on "Hidden" "checkbox" - And I press "Save changes" - And I turn editing mode off - Then I should see "60.00 (60.00 %)" in the ".course" "css_element" - And I follow "Course grade settings" - And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" - And I press "Save changes" - And I log out - And I log in as "student1" - And I follow "Course 1" - And I follow "Grades" - And I set the field "Grade report" to "Overview report" - And I should see "40.00 (40.00 %)" in the "overview-grade" "table" - @javascript Scenario: Mode of grades aggregation And I follow "Edit Course 1" And I set the field "Aggregation" to "Mode of grades" And I press "Save changes" + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Mode of grades" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Mode of grades" + And I click on "Show more..." "link" + And I click on "Aggregate only non-empty grades" "checkbox" + And I press "Save changes" And I click on "Edit assign Test assignment one" "link" And I click on "Hidden" "checkbox" And I press "Save changes" And I turn editing mode off - Then I should see "60.00 (60.00 %)" in the ".course" "css_element" + Then I should see "50.00 (50.00 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -179,15 +286,21 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "40.00 (40.00 %)" in the "overview-grade" "table" + And I should see "50.00 (50.00 %)" in the "overview-grade" "table" @javascript Scenario: Sum of grades aggregation + And I follow "Edit Sub category 1" + And I set the field "Aggregation" to "Sum of grades" + And I press "Save changes" + And I follow "Edit Sub category 2" + And I set the field "Aggregation" to "Sum of grades" + And I press "Save changes" And I follow "Edit Course 1" And I set the field "Aggregation" to "Sum of grades" And I press "Save changes" And I turn editing mode off - Then I should see "120.00 (40.00 %)" in the ".course" "css_element" + Then I should see "150.00 (18.99 %)" in the ".course" "css_element" And I follow "Course grade settings" And I set the field "Hide totals if they contain hidden items" to "Show totals excluding hidden items" And I press "Save changes" @@ -196,4 +309,4 @@ Feature: We can use calculated grade totals And I follow "Course 1" And I follow "Grades" And I set the field "Grade report" to "Overview report" - And I should see "100.00 (50.00 %)" in the "overview-grade" "table" + And I should see "110.00 (16.92 %)" in the "overview-grade" "table" diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 1171bda5740..32713b012c2 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -34,6 +34,7 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class testing_data_generator { + protected $gradecategorycounter = 0; protected $usercounter = 0; protected $categorycount = 0; protected $cohortcount = 0; @@ -740,6 +741,39 @@ EOD; return role_assign($roleid, $userid, $contextid); } + /** + * Create a grade_category. + * + * @param array|stdClass $record + * @return stdClass the grade category record + */ + public function create_grade_category($record = null) { + global $CFG; + + $this->gradecategorycounter++; + $i = $this->gradecategorycounter; + + if (!isset($record['fullname'])) { + $record['fullname'] = 'Grade category ' . $i; + } + + // For gradelib classes. + require_once($CFG->libdir . '/gradelib.php'); + // Create new grading category in this course. + $gradecategory = new grade_category($record, false); + $gradecategory->apply_default_settings(); + $gradecategory->apply_forced_settings(); + $gradecategory->insert(); + // This creates a default grade item for the category + $gradeitem = $gradecategory->load_grade_item(); + + if (isset($record->parentcategory)) { + $gradecategory->set_parent($data->parentcategory); + } + + $gradecategory->update_from_db(); + return $gradecategory->get_record_data(); + } } /** diff --git a/lib/testing/tests/generator_test.php b/lib/testing/tests/generator_test.php index 110f28fc225..a55a5a85e73 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/generator_test.php @@ -105,6 +105,15 @@ class core_test_generator_testcase extends advanced_testcase { $scale = $generator->create_scale(); $this->assertNotEmpty($scale); + + // Note we only count grade cats with depth > 1 because the course grade category + // is lazily created. + $count = $DB->count_records_select('grade_categories', 'depth <> 1'); + $gradecategory = $generator->create_grade_category(array('courseid'=>$course->id)); + $this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1')); + $this->assertEquals(2, $gradecategory->depth); + $this->assertEquals($course->id, $gradecategory->courseid); + $this->assertEquals('Grade category 1', $gradecategory->fullname); } public function test_create_module() { diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 285bf5ed054..0820ed26347 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -115,7 +115,7 @@ class behat_data_generators extends behat_base { 'activities' => array( 'datagenerator' => 'activity', 'required' => array('activity', 'idnumber', 'course'), - 'switchids' => array('course' => 'course') + 'switchids' => array('course' => 'course', 'gradecategory' => 'gradecat') ), 'group members' => array( 'datagenerator' => 'group_member', @@ -135,6 +135,11 @@ class behat_data_generators extends behat_base { 'datagenerator' => 'cohort_member', 'required' => array('user', 'cohort'), 'switchids' => array('user' => 'userid', 'cohort' => 'cohortid') + ), + 'grade categories' => array( + 'datagenerator' => 'grade_category', + 'required' => array('fullname', 'course'), + 'switchids' => array('course' => 'courseid', 'gradecategory' => 'parent') ) ); @@ -428,6 +433,21 @@ class behat_data_generators extends behat_base { cohort_add_member($data['cohortid'], $data['userid']); } + /** + * Gets the grade category id from the grade category fullname + * @throws Exception + * @param string $username + * @return int + */ + protected function get_gradecategory_id($fullname) { + global $DB; + + if (!$id = $DB->get_field('grade_categories', 'id', array('fullname' => $fullname))) { + throw new Exception('The specified grade category with fullname "' . $fullname . '" does not exist'); + } + return $id; + } + /** * Gets the user id from it's username. * @throws Exception