Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 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
 */
1441 ariadna 27
final class cleanup_test extends \advanced_testcase {
1 efrain 28
 
29
    /**
30
     * Test set up.
31
     *
32
     * This is executed before running any test in this file.
33
     */
34
    public function setUp(): void {
1441 ariadna 35
        parent::setUp();
1 efrain 36
        $this->resetAfterTest();
37
    }
38
 
39
    /**
40
     * Test the cleanup task.
41
     */
11 efrain 42
    public function test_cleanup_task(): void {
1 efrain 43
        global $DB;
44
 
45
        // Create a course.
46
        $course = $this->getDataGenerator()->create_course();
47
 
48
        // Create a few LTI items.
49
        $lti = $this->getDataGenerator()->create_module('lti', ['course' => $course->id]);
50
        $lti2 = $this->getDataGenerator()->create_module('lti', ['course' => $course->id]);
51
 
52
        $conditions = [
53
            'courseid' => $course->id,
54
            'itemtype' => 'mod',
55
            'itemmodule' => 'lti',
56
            'iteminstance' => $lti->id
57
        ];
58
 
59
        // Get the grade items.
60
        $gradeitem = $DB->get_record('grade_items', $conditions);
61
 
62
        $conditions['iteminstance'] = $lti2->id;
63
        $gradeitem2 = $DB->get_record('grade_items', $conditions);
64
 
65
        // Insert these into the 'ltiservice_gradebookservices' table.
66
        $data = new \stdClass();
67
        $data->gradeitemid = $gradeitem->id;
68
        $data->courseid = $course->id;
69
        $DB->insert_record('ltiservice_gradebookservices', $data);
70
 
71
        $data->gradeitemid = $gradeitem2->id;
72
        $DB->insert_record('ltiservice_gradebookservices', $data);
73
 
74
        $task = new cleanup_task();
75
        $task->execute();
76
 
77
        // Check they both still exist.
78
        $this->assertEquals(2, $DB->count_records('ltiservice_gradebookservices'));
79
 
80
        // Delete the first LTI activity.
81
        course_delete_module($lti->cmid);
82
 
83
        // Run the task again.
84
        $task = new cleanup_task();
85
        $task->execute();
86
 
87
        // Check only the second grade item exists.
88
        $gradebookserviceitems = $DB->get_records('ltiservice_gradebookservices');
89
        $this->assertCount(1, $gradebookserviceitems);
90
 
91
        $gradebookserviceitem = reset($gradebookserviceitems);
92
 
93
        $this->assertEquals($gradeitem2->id, $gradebookserviceitem->gradeitemid);
94
    }
95
 
96
    /**
97
     * Test the cleanup task with a manual grade item.
98
     */
11 efrain 99
    public function test_cleanup_task_with_manual_item(): void {
1 efrain 100
        global $CFG, $DB;
101
 
102
        // This is required when running the unit test in isolation.
103
        require_once($CFG->libdir . '/gradelib.php');
104
 
105
        // Create a manual grade item for a course.
106
        $course = $this->getDataGenerator()->create_course();
107
        $params = [
108
            'courseid' => $course->id,
109
            'itemtype' => 'manual'
110
        ];
111
        $gradeitem = new \grade_item($params);
112
        $gradeitem->insert();
113
 
114
        // Insert it into the 'ltiservice_gradebookservices' table.
115
        $data = new \stdClass();
116
        $data->gradeitemid = $gradeitem->id;
117
        $data->courseid = $course->id;
118
        $DB->insert_record('ltiservice_gradebookservices', $data);
119
 
120
        // Run the task.
121
        $task = new cleanup_task();
122
        $task->execute();
123
 
124
        // Check it still exist.
125
        $this->assertEquals(1, $DB->count_records('ltiservice_gradebookservices'));
126
 
127
        // Delete the manual item.
128
        $gradeitem->delete();
129
 
130
        // Run the task again.
131
        $task = new cleanup_task();
132
        $task->execute();
133
 
134
        // Check it has been removed.
135
        $this->assertEquals(0, $DB->count_records('ltiservice_gradebookservices'));
136
    }
137
}