Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 ltiservice_gradebookservices\task;
  18  
  19  /**
  20   * Tests cleaning up the gradebook services task.
  21   *
  22   * @package ltiservice_gradebookservices
  23   * @category test
  24   * @copyright 2018 Mark Nelson <markn@moodle.com>
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class cleanup_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test set up.
  31       *
  32       * This is executed before running any test in this file.
  33       */
  34      public function setUp(): void {
  35          $this->resetAfterTest();
  36      }
  37  
  38      /**
  39       * Test the cleanup task.
  40       */
  41      public function test_cleanup_task() {
  42          global $DB;
  43  
  44          // Create a course.
  45          $course = $this->getDataGenerator()->create_course();
  46  
  47          // Create a few LTI items.
  48          $lti = $this->getDataGenerator()->create_module('lti', ['course' => $course->id]);
  49          $lti2 = $this->getDataGenerator()->create_module('lti', ['course' => $course->id]);
  50  
  51          $conditions = [
  52              'courseid' => $course->id,
  53              'itemtype' => 'mod',
  54              'itemmodule' => 'lti',
  55              'iteminstance' => $lti->id
  56          ];
  57  
  58          // Get the grade items.
  59          $gradeitem = $DB->get_record('grade_items', $conditions);
  60  
  61          $conditions['iteminstance'] = $lti2->id;
  62          $gradeitem2 = $DB->get_record('grade_items', $conditions);
  63  
  64          // Insert these into the 'ltiservice_gradebookservices' table.
  65          $data = new \stdClass();
  66          $data->gradeitemid = $gradeitem->id;
  67          $data->courseid = $course->id;
  68          $DB->insert_record('ltiservice_gradebookservices', $data);
  69  
  70          $data->gradeitemid = $gradeitem2->id;
  71          $DB->insert_record('ltiservice_gradebookservices', $data);
  72  
  73          $task = new cleanup_task();
  74          $task->execute();
  75  
  76          // Check they both still exist.
  77          $this->assertEquals(2, $DB->count_records('ltiservice_gradebookservices'));
  78  
  79          // Delete the first LTI activity.
  80          course_delete_module($lti->cmid);
  81  
  82          // Run the task again.
  83          $task = new cleanup_task();
  84          $task->execute();
  85  
  86          // Check only the second grade item exists.
  87          $gradebookserviceitems = $DB->get_records('ltiservice_gradebookservices');
  88          $this->assertCount(1, $gradebookserviceitems);
  89  
  90          $gradebookserviceitem = reset($gradebookserviceitems);
  91  
  92          $this->assertEquals($gradeitem2->id, $gradebookserviceitem->gradeitemid);
  93      }
  94  
  95      /**
  96       * Test the cleanup task with a manual grade item.
  97       */
  98      public function test_cleanup_task_with_manual_item() {
  99          global $CFG, $DB;
 100  
 101          // This is required when running the unit test in isolation.
 102          require_once($CFG->libdir . '/gradelib.php');
 103  
 104          // Create a manual grade item for a course.
 105          $course = $this->getDataGenerator()->create_course();
 106          $params = [
 107              'courseid' => $course->id,
 108              'itemtype' => 'manual'
 109          ];
 110          $gradeitem = new \grade_item($params);
 111          $gradeitem->insert();
 112  
 113          // Insert it into the 'ltiservice_gradebookservices' table.
 114          $data = new \stdClass();
 115          $data->gradeitemid = $gradeitem->id;
 116          $data->courseid = $course->id;
 117          $DB->insert_record('ltiservice_gradebookservices', $data);
 118  
 119          // Run the task.
 120          $task = new cleanup_task();
 121          $task->execute();
 122  
 123          // Check it still exist.
 124          $this->assertEquals(1, $DB->count_records('ltiservice_gradebookservices'));
 125  
 126          // Delete the manual item.
 127          $gradeitem->delete();
 128  
 129          // Run the task again.
 130          $task = new cleanup_task();
 131          $task->execute();
 132  
 133          // Check it has been removed.
 134          $this->assertEquals(0, $DB->count_records('ltiservice_gradebookservices'));
 135      }
 136  }