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 |
/**
|
|
|
18 |
* File contains the unit tests for the webservices.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
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 |
namespace mod_customcert;
|
|
|
27 |
|
|
|
28 |
use core_external\external_api;
|
|
|
29 |
use advanced_testcase;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Unit tests for the webservices.
|
|
|
33 |
*
|
|
|
34 |
* @package mod_customcert
|
|
|
35 |
* @category test
|
|
|
36 |
* @copyright 2018 Mark Nelson <markn@moodle.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class external_test extends advanced_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Test set up.
|
|
|
43 |
*/
|
|
|
44 |
public function setUp(): void {
|
|
|
45 |
$this->resetAfterTest();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Test the delete_issue web service.
|
|
|
50 |
*
|
|
|
51 |
* @covers \external::delete_issue
|
|
|
52 |
*/
|
|
|
53 |
public function test_delete_issue() {
|
|
|
54 |
global $DB;
|
|
|
55 |
|
|
|
56 |
$this->setAdminUser();
|
|
|
57 |
|
|
|
58 |
// Create a course.
|
|
|
59 |
$course = $this->getDataGenerator()->create_course();
|
|
|
60 |
|
|
|
61 |
// Create a custom certificate in the course.
|
|
|
62 |
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
|
|
|
63 |
|
|
|
64 |
// Create two users.
|
|
|
65 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
66 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
67 |
|
|
|
68 |
// Enrol them into the course.
|
|
|
69 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
70 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
71 |
|
|
|
72 |
// Issue them both certificates.
|
|
|
73 |
$i1 = certificate::issue_certificate($customcert->id, $student1->id);
|
|
|
74 |
$i2 = certificate::issue_certificate($customcert->id, $student2->id);
|
|
|
75 |
|
|
|
76 |
$this->assertEquals(2, $DB->count_records('customcert_issues'));
|
|
|
77 |
|
|
|
78 |
$result = external::delete_issue($customcert->id, $i2);
|
|
|
79 |
|
|
|
80 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
81 |
external_api::clean_returnvalue(external::delete_issue_returns(), $result);
|
|
|
82 |
|
|
|
83 |
$issues = $DB->get_records('customcert_issues');
|
|
|
84 |
$this->assertCount(1, $issues);
|
|
|
85 |
|
|
|
86 |
$issue = reset($issues);
|
|
|
87 |
$this->assertEquals($student1->id, $issue->userid);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Test the delete_issue web service.
|
|
|
92 |
*
|
|
|
93 |
* @covers \external::delete_issue
|
|
|
94 |
*/
|
|
|
95 |
public function test_delete_issue_no_login() {
|
|
|
96 |
global $DB;
|
|
|
97 |
|
|
|
98 |
// Create a course.
|
|
|
99 |
$course = $this->getDataGenerator()->create_course();
|
|
|
100 |
|
|
|
101 |
// Create a custom certificate in the course.
|
|
|
102 |
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
|
|
|
103 |
|
|
|
104 |
// Create two users.
|
|
|
105 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
106 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
107 |
|
|
|
108 |
// Enrol them into the course.
|
|
|
109 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
110 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
111 |
|
|
|
112 |
// Issue them both certificates.
|
|
|
113 |
$i1 = certificate::issue_certificate($customcert->id, $student1->id);
|
|
|
114 |
$i2 = certificate::issue_certificate($customcert->id, $student2->id);
|
|
|
115 |
|
|
|
116 |
$this->assertEquals(2, $DB->count_records('customcert_issues'));
|
|
|
117 |
|
|
|
118 |
// Try and delete without logging in.
|
|
|
119 |
$this->expectException('require_login_exception');
|
|
|
120 |
external::delete_issue($customcert->id, $i2);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Test the delete_issue web service.
|
|
|
125 |
*
|
|
|
126 |
* @covers \external::delete_issue
|
|
|
127 |
*/
|
|
|
128 |
public function test_delete_issue_no_capability() {
|
|
|
129 |
global $DB;
|
|
|
130 |
|
|
|
131 |
// Create a course.
|
|
|
132 |
$course = $this->getDataGenerator()->create_course();
|
|
|
133 |
|
|
|
134 |
// Create a custom certificate in the course.
|
|
|
135 |
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
|
|
|
136 |
|
|
|
137 |
// Create two users.
|
|
|
138 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
139 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
140 |
|
|
|
141 |
$this->setUser($student1);
|
|
|
142 |
|
|
|
143 |
// Enrol them into the course.
|
|
|
144 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
145 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
146 |
|
|
|
147 |
// Issue them both certificates.
|
|
|
148 |
$i1 = certificate::issue_certificate($customcert->id, $student1->id);
|
|
|
149 |
$i2 = certificate::issue_certificate($customcert->id, $student2->id);
|
|
|
150 |
|
|
|
151 |
$this->assertEquals(2, $DB->count_records('customcert_issues'));
|
|
|
152 |
|
|
|
153 |
// Try and delete without the required capability.
|
|
|
154 |
$this->expectException('required_capability_exception');
|
|
|
155 |
external::delete_issue($customcert->id, $i2);
|
|
|
156 |
}
|
|
|
157 |
}
|