| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           namespace gradereport_overview;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Overview grade report lib functions unit tests
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package gradereport_overview
  | 
        
        
            | 
            | 
           23 | 
            * @copyright 2023 The Open University
  | 
        
        
            | 
            | 
           24 | 
            * @covers \grade_report_overview
  | 
        
        
            | 
            | 
           25 | 
            * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           27 | 
           final class lib_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Require the library file we're about to test, and other requirements.
  | 
        
        
            | 
            | 
           31 | 
                */
  | 
        
        
            | 
            | 
           32 | 
               protected function setUp(): void {
  | 
        
        
            | 
            | 
           33 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           34 | 
                   require_once($CFG->dirroot . '/grade/report/overview/lib.php');
  | 
        
        
            | 
            | 
           35 | 
                   require_once($CFG->dirroot . '/grade/querylib.php');
  | 
        
        
           | 1441 | 
           ariadna | 
           36 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           37 | 
               }
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               /**
  | 
        
        
            | 
            | 
           40 | 
                * Data provider for true or false value.
  | 
        
        
            | 
            | 
           41 | 
                *
  | 
        
        
            | 
            | 
           42 | 
                * @return array Two options, one with true and one with false
  | 
        
        
            | 
            | 
           43 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           44 | 
               public static function true_or_false(): array {
  | 
        
        
           | 1 | 
           efrain | 
           45 | 
                   return [
  | 
        
        
            | 
            | 
           46 | 
                       [true],
  | 
        
        
            | 
            | 
           47 | 
                       [false]
  | 
        
        
            | 
            | 
           48 | 
                   ];
  | 
        
        
            | 
            | 
           49 | 
               }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /**
  | 
        
        
            | 
            | 
           52 | 
                * Tests the regrade_all_courses_if_needed function, which is supposed to regrade all the
  | 
        
        
            | 
            | 
           53 | 
                * courses that the current user is enrolled on (if they need update).
  | 
        
        
            | 
            | 
           54 | 
                *
  | 
        
        
            | 
            | 
           55 | 
                * This is tested with both frontend and backend - the frontend option should not actually
  | 
        
        
            | 
            | 
           56 | 
                * do the progress bar/continue button (which can't be tested from here because it calls exit)
  | 
        
        
            | 
            | 
           57 | 
                * because these courses are small.
  | 
        
        
            | 
            | 
           58 | 
                *
  | 
        
        
            | 
            | 
           59 | 
                * @dataProvider true_or_false
  | 
        
        
            | 
            | 
           60 | 
                * @param bool $frontend True to use the front-end parameter to the function under test
  | 
        
        
            | 
            | 
           61 | 
                */
  | 
        
        
            | 
            | 
           62 | 
               public function test_regrade_all_courses_if_needed(bool $frontend): void {
  | 
        
        
            | 
            | 
           63 | 
                   global $DB;
  | 
        
        
            | 
            | 
           64 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   $generator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
                   // Create 3 courses and a test user. The test user belongs to 2 of them, while another user
  | 
        
        
            | 
            | 
           69 | 
                   // belongs to the other course.
  | 
        
        
            | 
            | 
           70 | 
                   $user = $generator->create_user();
  | 
        
        
            | 
            | 
           71 | 
                   $otheruser = $generator->create_user();
  | 
        
        
            | 
            | 
           72 | 
                   $course1 = $generator->create_course();
  | 
        
        
            | 
            | 
           73 | 
                   $generator->enrol_user($user->id, $course1->id, 'student');
  | 
        
        
            | 
            | 
           74 | 
                   $course2 = $generator->create_course();
  | 
        
        
            | 
            | 
           75 | 
                   $generator->enrol_user($otheruser->id, $course2->id, 'student');
  | 
        
        
            | 
            | 
           76 | 
                   $course3 = $generator->create_course();
  | 
        
        
            | 
            | 
           77 | 
                   $generator->enrol_user($user->id, $course3->id, 'student');
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
                   // We need permission to create grades (even though it's a data generator).
  | 
        
        
            | 
            | 
           80 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   // Set up each course grade.
  | 
        
        
            | 
            | 
           83 | 
                   foreach ([$course1, $course2, $course3] as $course) {
  | 
        
        
            | 
            | 
           84 | 
                       // Create an assignment and get its grade item.
  | 
        
        
            | 
            | 
           85 | 
                       $assign = $generator->create_module('assign', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           86 | 
                       $modinfo = get_fast_modinfo($course->id);
  | 
        
        
            | 
            | 
           87 | 
                       $cm = $modinfo->get_cm($assign->cmid);
  | 
        
        
            | 
            | 
           88 | 
                       $items = grade_get_grade_items_for_activity($cm, true);
  | 
        
        
            | 
            | 
           89 | 
                       $item = reset($items);
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                       // Set a grade in the assignment, either for the normal test user or the other user
  | 
        
        
            | 
            | 
           92 | 
                       // depending on course.
  | 
        
        
            | 
            | 
           93 | 
                       if ($course === $course2) {
  | 
        
        
            | 
            | 
           94 | 
                           $userid = $otheruser->id;
  | 
        
        
            | 
            | 
           95 | 
                       } else {
  | 
        
        
            | 
            | 
           96 | 
                           $userid = $user->id;
  | 
        
        
            | 
            | 
           97 | 
                       }
  | 
        
        
            | 
            | 
           98 | 
                       $generator->create_grade_grade([
  | 
        
        
            | 
            | 
           99 | 
                           'itemid' => $item->id,
  | 
        
        
            | 
            | 
           100 | 
                           'userid' => $userid,
  | 
        
        
            | 
            | 
           101 | 
                           'teamsubmission' => false,
  | 
        
        
            | 
            | 
           102 | 
                           'attemptnumber' => 0,
  | 
        
        
            | 
            | 
           103 | 
                           'grade' => 50
  | 
        
        
            | 
            | 
           104 | 
                       ]);
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                       // Bodge the final grade so that it needs regrading and is set wrong.
  | 
        
        
            | 
            | 
           107 | 
                       $course->gradeitemid = $DB->get_field('grade_items', 'id',
  | 
        
        
            | 
            | 
           108 | 
                               ['courseid' => $course->id, 'itemtype' => 'course'], MUST_EXIST);
  | 
        
        
            | 
            | 
           109 | 
                       $DB->set_field('grade_items', 'needsupdate', 1, ['id' => $course->gradeitemid]);
  | 
        
        
            | 
            | 
           110 | 
                       $DB->set_field('grade_grades', 'finalgrade', 25.0, ['itemid' => $course->gradeitemid]);
  | 
        
        
            | 
            | 
           111 | 
                   }
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   // Construct the overview report and call regrade_all_courses_if_needed.
  | 
        
        
            | 
            | 
           114 | 
                   $gpr = new \stdClass();
  | 
        
        
            | 
            | 
           115 | 
                   $report = new \grade_report_overview($user->id, $gpr, '');
  | 
        
        
            | 
            | 
           116 | 
                   $report->regrade_all_courses_if_needed($frontend);
  | 
        
        
           | 1441 | 
           ariadna | 
           117 | 
                   if (!$frontend) {
  | 
        
        
            | 
            | 
           118 | 
                       $this->expectOutputRegex("~^Recalculating grades for course ID {$course->id}~");
  | 
        
        
            | 
            | 
           119 | 
                       $this->run_all_adhoc_tasks();
  | 
        
        
            | 
            | 
           120 | 
                   }
  | 
        
        
           | 1 | 
           efrain | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   // This should have regraded courses 1 and 3, but not 2 (because the user doesn't belong).
  | 
        
        
            | 
            | 
           123 | 
                   $this->assertEqualsWithDelta(50.0, $DB->get_field('grade_grades', 'finalgrade',
  | 
        
        
            | 
            | 
           124 | 
                           ['itemid' => $course1->gradeitemid]), 1.0);
  | 
        
        
            | 
            | 
           125 | 
                   $this->assertEqualsWithDelta(25.0, $DB->get_field('grade_grades', 'finalgrade',
  | 
        
        
            | 
            | 
           126 | 
                           ['itemid' => $course2->gradeitemid]), 1.0);
  | 
        
        
            | 
            | 
           127 | 
                   $this->assertEqualsWithDelta(50.0, $DB->get_field('grade_grades', 'finalgrade',
  | 
        
        
            | 
            | 
           128 | 
                           ['itemid' => $course3->gradeitemid]), 1.0);
  | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
           }
  |