1441 |
ariadna |
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 customfield_number;
|
|
|
18 |
|
|
|
19 |
use context_module;
|
|
|
20 |
use customfield_number\local\numberproviders\nofactivities;
|
|
|
21 |
use customfield_number\task\recalculate;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Testing event observers
|
|
|
25 |
*
|
|
|
26 |
* @covers \customfield_number\observer
|
|
|
27 |
* @covers \customfield_number\task\recalculate
|
|
|
28 |
* @package customfield_number
|
|
|
29 |
* @category test
|
|
|
30 |
* @copyright Marina Glancy
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
final class observer_test extends \advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Create a number custom field
|
|
|
37 |
*
|
|
|
38 |
* @param array $configdata
|
|
|
39 |
* @return \customfield_number\field_controller
|
|
|
40 |
*/
|
|
|
41 |
protected function create_number_custom_field(array $configdata): field_controller {
|
|
|
42 |
/** @var \core_customfield_generator $generator */
|
|
|
43 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
44 |
|
|
|
45 |
// Create a category and field.
|
|
|
46 |
$category = $generator->create_category();
|
|
|
47 |
$field = $generator->create_field([
|
|
|
48 |
'categoryid' => $category->get('id'),
|
|
|
49 |
'type' => 'number',
|
|
|
50 |
'configdata' => $configdata,
|
|
|
51 |
]);
|
|
|
52 |
return $field;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Helper function that checks if the recalculate ad-hoc task is scheduled
|
|
|
57 |
*
|
|
|
58 |
* @param bool $mustbescheduled - when false checks that the adhoc task is NOT scheduled
|
|
|
59 |
* @return void
|
|
|
60 |
*/
|
|
|
61 |
protected function ensure_number_adhoc_task_is_scheduled(bool $mustbescheduled): void {
|
|
|
62 |
$tasks = array_filter(
|
|
|
63 |
\core\task\manager::get_candidate_adhoc_tasks(time(), 1200, null),
|
|
|
64 |
fn($task) => $task->classname === '\\' . recalculate::class
|
|
|
65 |
);
|
|
|
66 |
if ($mustbescheduled && empty($tasks)) {
|
|
|
67 |
$this->fail('Recalculate ad-hoc task is not scheduled.');
|
|
|
68 |
} else if (!$mustbescheduled && !empty($tasks)) {
|
|
|
69 |
$this->fail('Recalculate ad-hoc task is scheduled when it is not expected.');
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Test for observer for field_created event
|
|
|
75 |
*/
|
|
|
76 |
public function test_field_created(): void {
|
|
|
77 |
global $DB;
|
|
|
78 |
|
|
|
79 |
$this->resetAfterTest();
|
|
|
80 |
$this->setAdminUser();
|
|
|
81 |
|
|
|
82 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
83 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
84 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
85 |
$assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
86 |
$assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
87 |
|
|
|
88 |
// Create a number field with a provider.
|
|
|
89 |
$field = $this->create_number_custom_field(['fieldtype' => nofactivities::class, 'activitytypes' => ['assign', 'forum']]);
|
|
|
90 |
|
|
|
91 |
// Execute scheduled ad-hoc tasks and it will populate the data for the course.
|
|
|
92 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
93 |
$this->run_all_adhoc_tasks();
|
|
|
94 |
|
|
|
95 |
$alldata = $DB->get_records_menu('customfield_data',
|
|
|
96 |
['fieldid' => $field->get('id')], 'instanceid', 'instanceid, decvalue');
|
|
|
97 |
$this->assertEquals([$course1->id => 2], $alldata);
|
|
|
98 |
|
|
|
99 |
// Creating another field type does not schedule tasks.
|
|
|
100 |
$this->ensure_number_adhoc_task_is_scheduled(false);
|
|
|
101 |
$this->getDataGenerator()->get_plugin_generator('core_customfield')->create_field((object)[
|
|
|
102 |
'categoryid' => $field->get_category()->get('id'),
|
|
|
103 |
'type' => 'textarea',
|
|
|
104 |
]);
|
|
|
105 |
$this->ensure_number_adhoc_task_is_scheduled(false);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Test for observer for field_updated event
|
|
|
110 |
*/
|
|
|
111 |
public function test_field_updated(): void {
|
|
|
112 |
global $DB;
|
|
|
113 |
|
|
|
114 |
$this->resetAfterTest();
|
|
|
115 |
$this->setAdminUser();
|
|
|
116 |
|
|
|
117 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
118 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
119 |
/** @var \mod_assign_generator $assigngenerator */
|
|
|
120 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
121 |
$assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
122 |
$assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
123 |
|
|
|
124 |
// Create a simple number field.
|
|
|
125 |
$field = $this->create_number_custom_field([]);
|
|
|
126 |
|
|
|
127 |
// There is no data for this field yet.
|
|
|
128 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
129 |
$this->run_all_adhoc_tasks();
|
|
|
130 |
$this->assertEmpty($DB->get_records('customfield_data', ['fieldid' => $field->get('id')]));
|
|
|
131 |
|
|
|
132 |
// Update this field to use nofactivities as provider.
|
|
|
133 |
$params = ['fieldtype' => nofactivities::class, 'activitytypes' => ['assign', 'forum']];
|
|
|
134 |
\core_customfield\api::save_field_configuration($field, (object)['configdata' => $params]);
|
|
|
135 |
|
|
|
136 |
// Now an ad-hoc task is scheduled and the data is populated.
|
|
|
137 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
138 |
$this->run_all_adhoc_tasks();
|
|
|
139 |
|
|
|
140 |
$alldata = $DB->get_records_menu(
|
|
|
141 |
'customfield_data',
|
|
|
142 |
['fieldid' => $field->get('id')],
|
|
|
143 |
'instanceid',
|
|
|
144 |
'instanceid, decvalue'
|
|
|
145 |
);
|
|
|
146 |
$this->assertEquals([$course1->id => 2], $alldata);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Test for observer for course_module_created, course_module_updated and course_module_deleted events
|
|
|
151 |
*/
|
|
|
152 |
public function test_course_module_events(): void {
|
|
|
153 |
global $DB;
|
|
|
154 |
|
|
|
155 |
$this->resetAfterTest();
|
|
|
156 |
$this->setAdminUser();
|
|
|
157 |
|
|
|
158 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
159 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
160 |
|
|
|
161 |
// Create a number field with a provider.
|
|
|
162 |
$field = $this->create_number_custom_field(['fieldtype' => nofactivities::class, 'activitytypes' => ['assign', 'forum']]);
|
|
|
163 |
|
|
|
164 |
// There is no data for this field yet.
|
|
|
165 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
166 |
$this->run_all_adhoc_tasks();
|
|
|
167 |
$this->assertEmpty($DB->get_records('customfield_data', ['fieldid' => $field->get('id')]));
|
|
|
168 |
|
|
|
169 |
// Create modules.
|
|
|
170 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
171 |
$assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
172 |
$assign2 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
|
|
|
173 |
$assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
174 |
|
|
|
175 |
// Execute scheduled ad-hoc tasks and it will populate the data for the course.
|
|
|
176 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
177 |
$this->run_all_adhoc_tasks();
|
|
|
178 |
|
|
|
179 |
$alldata = $DB->get_records_menu('customfield_data',
|
|
|
180 |
['fieldid' => $field->get('id')], 'instanceid', 'instanceid, decvalue');
|
|
|
181 |
$this->assertEquals([$course1->id => 2], $alldata);
|
|
|
182 |
|
|
|
183 |
// Update visibility of one module.
|
|
|
184 |
set_coursemodule_visible($assign2->cmid, 1);
|
|
|
185 |
[$course, $cm] = get_course_and_cm_from_cmid($assign2->cmid);
|
|
|
186 |
\core\event\course_module_updated::create_from_cm($cm, context_module::instance($assign2->cmid))->trigger();
|
|
|
187 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
188 |
$this->run_all_adhoc_tasks();
|
|
|
189 |
|
|
|
190 |
$alldata = $DB->get_records_menu('customfield_data',
|
|
|
191 |
['fieldid' => $field->get('id')], 'instanceid', 'instanceid, decvalue');
|
|
|
192 |
$this->assertEquals([$course1->id => 3], $alldata);
|
|
|
193 |
|
|
|
194 |
// Delete one module.
|
|
|
195 |
course_delete_module($assign1->cmid);
|
|
|
196 |
|
|
|
197 |
// Execute scheduled ad-hoc tasks and it will update the data for the course.
|
|
|
198 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
199 |
$this->run_all_adhoc_tasks();
|
|
|
200 |
|
|
|
201 |
$alldata = $DB->get_records_menu('customfield_data',
|
|
|
202 |
['fieldid' => $field->get('id')], 'instanceid', 'instanceid, decvalue');
|
|
|
203 |
$this->assertEquals([$course1->id => 2], $alldata);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Creating, updating and deleting modules when there are no 'nofactivities' custom fields does not schedule the ad-hoc task
|
|
|
208 |
*
|
|
|
209 |
* @return void
|
|
|
210 |
*/
|
|
|
211 |
public function test_course_module_events_without_custom_fields(): void {
|
|
|
212 |
global $DB;
|
|
|
213 |
|
|
|
214 |
$this->resetAfterTest();
|
|
|
215 |
$this->setAdminUser();
|
|
|
216 |
|
|
|
217 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
218 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
219 |
|
|
|
220 |
// Create a number field without a provider.
|
|
|
221 |
$field = $this->create_number_custom_field([]);
|
|
|
222 |
|
|
|
223 |
// Initial ad-hoc task was scheduled.
|
|
|
224 |
$this->ensure_number_adhoc_task_is_scheduled(true);
|
|
|
225 |
$this->run_all_adhoc_tasks();
|
|
|
226 |
|
|
|
227 |
// Create modules.
|
|
|
228 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
229 |
$assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
230 |
$assign2 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
|
|
|
231 |
$assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
|
|
|
232 |
|
|
|
233 |
$this->ensure_number_adhoc_task_is_scheduled(false);
|
|
|
234 |
|
|
|
235 |
// Update visibility of one module.
|
|
|
236 |
set_coursemodule_visible($assign2->cmid, 1);
|
|
|
237 |
[$course, $cm] = get_course_and_cm_from_cmid($assign2->cmid);
|
|
|
238 |
\core\event\course_module_updated::create_from_cm($cm, context_module::instance($assign2->cmid))->trigger();
|
|
|
239 |
$this->ensure_number_adhoc_task_is_scheduled(false);
|
|
|
240 |
|
|
|
241 |
// Delete one module.
|
|
|
242 |
course_delete_module($assign1->cmid);
|
|
|
243 |
$this->ensure_number_adhoc_task_is_scheduled(false);
|
|
|
244 |
}
|
|
|
245 |
}
|