Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

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