-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcourse_lookup_step_test.php
More file actions
272 lines (236 loc) · 8.97 KB
/
course_lookup_step_test.php
File metadata and controls
272 lines (236 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* "Fail" filter step's unit tests.
*
* @package tool_trigger
* @author Aaron Wells <aaronw@catalyst.net.nz>
* @copyright Catalyst IT 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_trigger;
class course_lookup_step_test extends \advanced_testcase {
/**
* Test user.
* @var
*/
protected $user;
/**
* Test user.
* @var
*/
protected $course;
/**
* Event for testing.
* @var \core\event\user_graded
*/
private $event;
/**
* Create a "user_profile_viewed" event, of user1 viewing user2's
* profile. And then run everything else as the cron user.
*/
public function setup():void {
$this->resetAfterTest(true);
$this->user = \core_user::get_user_by_username('admin');
$this->course = $this->getDataGenerator()->create_course();
$this->setUser($this->user);
$this->event = \core\event\course_created::create([
'objectid' => $this->course->id,
'context' => \context_course::instance($this->course->id),
'other' => [
'shortname' => $this->course->shortname,
'fullname' => $this->course->fullname
]
]);
// Run as the cron user .
\core\cron::setup_user();
}
/**
* Find the course identified at "objectid", and add their data with the
* prefix "course_".
*/
public function test_execute_basic() {
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => 'objectid',
'outputprefix' => 'course_'
])
);
list($status, $stepresults) = $step->execute(null, null, $this->event, []);
$context = \context_course::instance($this->course->id);
$this->assertTrue($status);
$this->assertEquals($this->course->id, $stepresults['course_id']);
$this->assertEquals($this->course->fullname, $stepresults['course_fullname']);
$this->assertEquals($context->id, $stepresults['course_contextid']);
}
/**
* Basic test, but this time with additional custom course field.
*/
public function test_execute_basic_with_custom_profile_fields() {
// Create custom course field.
$customfieldg = $this->getDataGenerator()->get_plugin_generator('core_customfield');
$category = $customfieldg->create_category();
$customfield = $customfieldg->create_field([
'categoryid' => $category->get('id'),
'type' => 'text',
'shortname' => 'testfield1',
'configdata' => [],
]);
// Add data to the customfield_data table.
$this->add_course_custom_course_field_data($customfield->get('id'), $this->course->id, 'CourseFieldValue');
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => 'objectid',
'outputprefix' => 'course_'
])
);
list($status, $stepresults) = $step->execute(null, null, $this->event, []);
$this->assertTrue($status);
// Check that the custom field data is returned as a step result.
$this->assertEquals('CourseFieldValue', $stepresults['course_testfield1']);
}
/**
* Test for exception if an invalid field name is entered.
*/
public function test_execute_nosuchfield() {
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => 'nosuchfield',
'outputprefix' => 'course_'
])
);
$this->expectException('\moodle_exception');
$step->execute(null, null, $this->event, []);
}
/**
* Test for failure if a course is no longer present in the database.
*/
public function test_execute_nosuchcourse() {
delete_course($this->course, false);
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => 'objectid',
'outputprefix' => 'course_'
])
);
list($status) = $step->execute(null, null, $this->event, []);
$this->assertFalse($status);
}
/**
* Data provided to test hardcoded category id.
* @return array
*/
public function hardcoded_course_id_data_provider() {
return [
'Non-existing Course id.' => [
777777,
false,
false,
],
'Nil Course id.' => [
0,
false,
true,
],
'Empty string Course id.' => [
'',
false,
true,
],
'Null Course id.' => [
null,
false,
true,
],
];
}
/**
* Test for exception if course id entered directly.
*
* @dataProvider hardcoded_course_id_data_provider
*/
public function test_execute_course_id($courseid, $status, $exception) {
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => $courseid,
'outputprefix' => 'course_'
])
);
if ($exception) {
$this->expectException(\invalid_parameter_exception::class);
$this->expectExceptionMessageMatches("/Specified courseid field not present in the workflow data:*/");
}
list($statusresult, $stepresults) = $step->execute(null, null, $this->event, []);
if ($status) {
$context = \context_course::instance($this->course->id);
$this->assertTrue($statusresult);
$this->assertEquals($this->course->id, $stepresults['course_id']);
$this->assertEquals($this->course->fullname, $stepresults['course_fullname']);
$this->assertEquals($context->id, $stepresults['course_contextid']);
} else {
$this->assertFalse($statusresult);
}
}
/**
* Test for exception if course id entered directly with dynamic id as integer.
*/
public function test_execute_course_id_integer() {
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => $this->course->id,
'outputprefix' => 'course_'
])
);
list($status, $stepresults) = $step->execute(null, null, $this->event, []);
$context = \context_course::instance($this->course->id);
$this->assertTrue($status);
$this->assertEquals($this->course->id, $stepresults['course_id']);
$this->assertEquals($this->course->fullname, $stepresults['course_fullname']);
$this->assertEquals($context->id, $stepresults['course_contextid']);
}
/**
* Test for exception if course id entered directly with dynamic id as string.
*/
public function test_execute_course_id_string() {
$step = new \tool_trigger\steps\lookups\course_lookup_step(
json_encode([
'courseidfield' => (string)$this->course->id,
'outputprefix' => 'course_'
])
);
list($status, $stepresults) = $step->execute(null, null, $this->event, []);
$context = \context_course::instance($this->course->id);
$this->assertTrue($status);
$this->assertEquals($this->course->id, $stepresults['course_id']);
$this->assertEquals($this->course->fullname, $stepresults['course_fullname']);
$this->assertEquals($context->id, $stepresults['course_contextid']);
}
public function add_course_custom_course_field_data($fieldid, $courseid, $customfielddata) {
global $DB;
// Add data to the customfield_data table.
$data = new \stdClass();
$data->fieldid = $fieldid;
$data->instanceid = $courseid;
$data->value = $customfielddata;
$data->charvalue = $customfielddata;
$data->timecreated = time();
$data->timemodified = time();
$data->valueformat = 0;
$data->valuetrust = 0;
$data->contextid = \context_course::instance($courseid)->id;
return $DB->insert_record('customfield_data', $data);
}
}