MDL-67613 availability_completion: add previous activity condition
[moodle.git] / availability / condition / completion / tests / condition_test.php
CommitLineData
e01efa2c 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 * Unit tests for the completion condition.
19 *
20 * @package availability_completion
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use availability_completion\condition;
28
29global $CFG;
30require_once($CFG->libdir . '/completionlib.php');
31
32/**
33 * Unit tests for the completion condition.
34 *
35 * @package availability_completion
36 * @copyright 2014 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class availability_completion_condition_testcase extends advanced_testcase {
c54b183c 40
e01efa2c 41 /**
c54b183c 42 * Setup to ensure that fixtures are loaded.
e01efa2c 43 */
c54b183c 44 public static function setupBeforeClass(): void {
e01efa2c 45 global $CFG;
c54b183c 46 // Load the mock info class so that it can be used.
e01efa2c 47 require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
c54b183c
FR
48 require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info_module.php');
49 require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info_section.php');
50 }
51
52 /**
53 * Load required classes.
54 */
55 public function setUp() {
56 availability_completion\condition::wipe_static_cache();
e01efa2c 57 }
58
59 /**
60 * Tests constructing and using condition as part of tree.
61 */
62 public function test_in_tree() {
63 global $USER, $CFG;
64 $this->resetAfterTest();
65
66 $this->setAdminUser();
67
68 // Create course with completion turned on and a Page.
69 $CFG->enablecompletion = true;
70 $CFG->enableavailability = true;
71 $generator = $this->getDataGenerator();
72 $course = $generator->create_course(array('enablecompletion' => 1));
73 $page = $generator->get_plugin_generator('mod_page')->create_instance(
74 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
c54b183c
FR
75 $selfpage = $generator->get_plugin_generator('mod_page')->create_instance(
76 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
e01efa2c 77
78 $modinfo = get_fast_modinfo($course);
79 $cm = $modinfo->get_cm($page->cmid);
80 $info = new \core_availability\mock_info($course, $USER->id);
81
82 $structure = (object)array('op' => '|', 'show' => true, 'c' => array(
83 (object)array('type' => 'completion', 'cm' => (int)$cm->id,
84 'e' => COMPLETION_COMPLETE)));
85 $tree = new \core_availability\tree($structure);
86
87 // Initial check (user has not completed activity).
88 $result = $tree->check_available(false, $info, true, $USER->id);
89 $this->assertFalse($result->is_available());
90
91 // Mark activity complete.
92 $completion = new completion_info($course);
93 $completion->update_state($cm, COMPLETION_COMPLETE);
94
95 // Now it's true!
96 $result = $tree->check_available(false, $info, true, $USER->id);
97 $this->assertTrue($result->is_available());
98 }
99
100 /**
101 * Tests the constructor including error conditions. Also tests the
102 * string conversion feature (intended for debugging only).
103 */
104 public function test_constructor() {
105 // No parameters.
106 $structure = new stdClass();
107 try {
108 $cond = new condition($structure);
109 $this->fail();
110 } catch (coding_exception $e) {
111 $this->assertContains('Missing or invalid ->cm', $e->getMessage());
112 }
113
114 // Invalid $cm.
115 $structure->cm = 'hello';
116 try {
117 $cond = new condition($structure);
118 $this->fail();
119 } catch (coding_exception $e) {
120 $this->assertContains('Missing or invalid ->cm', $e->getMessage());
121 }
122
123 // Missing $e.
124 $structure->cm = 42;
125 try {
126 $cond = new condition($structure);
127 $this->fail();
128 } catch (coding_exception $e) {
129 $this->assertContains('Missing or invalid ->e', $e->getMessage());
130 }
131
132 // Invalid $e.
133 $structure->e = 99;
134 try {
135 $cond = new condition($structure);
136 $this->fail();
137 } catch (coding_exception $e) {
138 $this->assertContains('Missing or invalid ->e', $e->getMessage());
139 }
140
141 // Successful construct & display with all different expected values.
142 $structure->e = COMPLETION_COMPLETE;
143 $cond = new condition($structure);
144 $this->assertEquals('{completion:cm42 COMPLETE}', (string)$cond);
145
146 $structure->e = COMPLETION_COMPLETE_PASS;
147 $cond = new condition($structure);
148 $this->assertEquals('{completion:cm42 COMPLETE_PASS}', (string)$cond);
149
150 $structure->e = COMPLETION_COMPLETE_FAIL;
151 $cond = new condition($structure);
152 $this->assertEquals('{completion:cm42 COMPLETE_FAIL}', (string)$cond);
153
154 $structure->e = COMPLETION_INCOMPLETE;
155 $cond = new condition($structure);
156 $this->assertEquals('{completion:cm42 INCOMPLETE}', (string)$cond);
c54b183c
FR
157
158 // Successful contruct with previous activity.
159 $structure->cm = condition::OPTION_PREVIOUS;
160 $cond = new condition($structure);
161 $this->assertEquals('{completion:cmopprevious INCOMPLETE}', (string)$cond);
162
e01efa2c 163 }
164
165 /**
166 * Tests the save() function.
167 */
168 public function test_save() {
169 $structure = (object)array('cm' => 42, 'e' => COMPLETION_COMPLETE);
170 $cond = new condition($structure);
171 $structure->type = 'completion';
172 $this->assertEquals($structure, $cond->save());
173 }
174
175 /**
176 * Tests the is_available and get_description functions.
177 */
178 public function test_usage() {
179 global $CFG, $DB;
180 require_once($CFG->dirroot . '/mod/assign/locallib.php');
181 $this->resetAfterTest();
182
183 // Create course with completion turned on.
184 $CFG->enablecompletion = true;
185 $CFG->enableavailability = true;
186 $generator = $this->getDataGenerator();
187 $course = $generator->create_course(array('enablecompletion' => 1));
188 $user = $generator->create_user();
189 $generator->enrol_user($user->id, $course->id);
190 $this->setUser($user);
191
192 // Create a Page with manual completion for basic checks.
193 $page = $generator->get_plugin_generator('mod_page')->create_instance(
194 array('course' => $course->id, 'name' => 'Page!',
195 'completion' => COMPLETION_TRACKING_MANUAL));
196
197 // Create an assignment - we need to have something that can be graded
198 // so as to test the PASS/FAIL states. Set it up to be completed based
199 // on its grade item.
200 $assignrow = $this->getDataGenerator()->create_module('assign', array(
201 'course' => $course->id, 'name' => 'Assign!',
202 'completion' => COMPLETION_TRACKING_AUTOMATIC));
203 $DB->set_field('course_modules', 'completiongradeitemnumber', 0,
204 array('id' => $assignrow->cmid));
205 $assign = new assign(context_module::instance($assignrow->cmid), false, false);
206
207 // Get basic details.
208 $modinfo = get_fast_modinfo($course);
209 $pagecm = $modinfo->get_cm($page->cmid);
210 $assigncm = $assign->get_course_module();
211 $info = new \core_availability\mock_info($course, $user->id);
212
213 // COMPLETE state (false), positive and NOT.
214 $cond = new condition((object)array(
215 'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE));
216 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
217 $information = $cond->get_description(false, false, $info);
218 $information = \core_availability\info::format_info($information, $course);
219 $this->assertRegExp('~Page!.*is marked complete~', $information);
220 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
221
222 // INCOMPLETE state (true).
223 $cond = new condition((object)array(
224 'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE));
225 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
226 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
227 $information = $cond->get_description(false, true, $info);
228 $information = \core_availability\info::format_info($information, $course);
229 $this->assertRegExp('~Page!.*is marked complete~', $information);
230
231 // Mark page complete.
232 $completion = new completion_info($course);
233 $completion->update_state($pagecm, COMPLETION_COMPLETE);
234
235 // COMPLETE state (true).
236 $cond = new condition((object)array(
237 'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE));
238 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
239 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
240 $information = $cond->get_description(false, true, $info);
241 $information = \core_availability\info::format_info($information, $course);
242 $this->assertRegExp('~Page!.*is incomplete~', $information);
243
244 // INCOMPLETE state (false).
245 $cond = new condition((object)array(
246 'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE));
247 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
248 $information = $cond->get_description(false, false, $info);
249 $information = \core_availability\info::format_info($information, $course);
250 $this->assertRegExp('~Page!.*is incomplete~', $information);
251 $this->assertTrue($cond->is_available(true, $info,
252 true, $user->id));
253
254 // We are going to need the grade item so that we can get pass/fails.
255 $gradeitem = $assign->get_grade_item();
256 grade_object::set_properties($gradeitem, array('gradepass' => 50.0));
257 $gradeitem->update();
258
259 // With no grade, it should return true for INCOMPLETE and false for
260 // the other three.
261 $cond = new condition((object)array(
262 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE));
263 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
264 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
265
266 $cond = new condition((object)array(
267 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE));
268 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
269 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
270
271 // Check $information for COMPLETE_PASS and _FAIL as we haven't yet.
272 $cond = new condition((object)array(
273 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS));
274 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
275 $information = $cond->get_description(false, false, $info);
276 $information = \core_availability\info::format_info($information, $course);
277 $this->assertRegExp('~Assign!.*is complete and passed~', $information);
278 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
279
280 $cond = new condition((object)array(
281 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL));
282 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
283 $information = $cond->get_description(false, false, $info);
284 $information = \core_availability\info::format_info($information, $course);
285 $this->assertRegExp('~Assign!.*is complete and failed~', $information);
286 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
287
288 // Change the grade to be complete and failed.
289 self::set_grade($assignrow, $user->id, 40);
290
291 $cond = new condition((object)array(
292 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE));
293 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
294 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
295
296 $cond = new condition((object)array(
297 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE));
298 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
299 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
300
301 $cond = new condition((object)array(
302 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS));
303 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
304 $information = $cond->get_description(false, false, $info);
305 $information = \core_availability\info::format_info($information, $course);
306 $this->assertRegExp('~Assign!.*is complete and passed~', $information);
307 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
308
309 $cond = new condition((object)array(
310 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL));
311 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
312 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
313 $information = $cond->get_description(false, true, $info);
314 $information = \core_availability\info::format_info($information, $course);
315 $this->assertRegExp('~Assign!.*is not complete and failed~', $information);
316
317 // Now change it to pass.
318 self::set_grade($assignrow, $user->id, 60);
319
320 $cond = new condition((object)array(
321 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE));
322 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
323 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
324
325 $cond = new condition((object)array(
326 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE));
327 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
328 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
329
330 $cond = new condition((object)array(
331 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS));
332 $this->assertTrue($cond->is_available(false, $info, true, $user->id));
333 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
334 $information = $cond->get_description(false, true, $info);
335 $information = \core_availability\info::format_info($information, $course);
336 $this->assertRegExp('~Assign!.*is not complete and passed~', $information);
337
338 $cond = new condition((object)array(
339 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL));
340 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
341 $information = $cond->get_description(false, false, $info);
342 $information = \core_availability\info::format_info($information, $course);
343 $this->assertRegExp('~Assign!.*is complete and failed~', $information);
344 $this->assertTrue($cond->is_available(true, $info, true, $user->id));
345
346 // Simulate deletion of an activity by using an invalid cmid. These
347 // conditions always fail, regardless of NOT flag or INCOMPLETE.
348 $cond = new condition((object)array(
349 'cm' => ($assigncm->id + 100), 'e' => COMPLETION_COMPLETE));
350 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
351 $information = $cond->get_description(false, false, $info);
352 $information = \core_availability\info::format_info($information, $course);
353 $this->assertRegExp('~(Missing activity).*is marked complete~', $information);
354 $this->assertFalse($cond->is_available(true, $info, true, $user->id));
355 $cond = new condition((object)array(
356 'cm' => ($assigncm->id + 100), 'e' => COMPLETION_INCOMPLETE));
357 $this->assertFalse($cond->is_available(false, $info, true, $user->id));
358 }
359
c54b183c
FR
360 /**
361 * Tests the is_available and get_description functions for previous activity option.
362 *
363 * @dataProvider test_previous_activity_data
364 * @param int $grade the current assign grade (0 for none)
365 * @param int $condition true for complete, false for incomplete
366 * @param string $mark activity to mark as complete
367 * @param string $activity activity name to test
368 * @param bool $result if it must be available or not
369 * @param bool $resultnot if it must be available when the condition is inverted
370 * @param string $description the availabiklity text to check
371 */
372 public function test_previous_activity(int $grade, int $condition, string $mark, string $activity,
373 bool $result, bool $resultnot, string $description): void {
374 global $CFG, $DB;
375 require_once($CFG->dirroot . '/mod/assign/locallib.php');
376 $this->resetAfterTest();
377
378 // Create course with completion turned on.
379 $CFG->enablecompletion = true;
380 $CFG->enableavailability = true;
381 $generator = $this->getDataGenerator();
382 $course = $generator->create_course(array('enablecompletion' => 1));
383 $user = $generator->create_user();
384 $generator->enrol_user($user->id, $course->id);
385 $this->setUser($user);
386
387 // Page 1 (manual completion).
388 $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
389 array('course' => $course->id, 'name' => 'Page1!',
390 'completion' => COMPLETION_TRACKING_MANUAL));
391
392 // Page 2 (manual completion).
393 $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
394 array('course' => $course->id, 'name' => 'Page2!',
395 'completion' => COMPLETION_TRACKING_MANUAL));
396
397 // Page ignored (no completion).
398 $pagenocompletion = $generator->get_plugin_generator('mod_page')->create_instance(
399 array('course' => $course->id, 'name' => 'Page ignored!'));
400
401 // Create an assignment - we need to have something that can be graded
402 // so as to test the PASS/FAIL states. Set it up to be completed based
403 // on its grade item.
404 $assignrow = $this->getDataGenerator()->create_module('assign', array(
405 'course' => $course->id, 'name' => 'Assign!',
406 'completion' => COMPLETION_TRACKING_AUTOMATIC));
407 $DB->set_field('course_modules', 'completiongradeitemnumber', 0,
408 array('id' => $assignrow->cmid));
409 $assign = new assign(context_module::instance($assignrow->cmid), false, false);
410
411 // Page 3 (manual completion).
412 $page3 = $generator->get_plugin_generator('mod_page')->create_instance(
413 array('course' => $course->id, 'name' => 'Page3!',
414 'completion' => COMPLETION_TRACKING_MANUAL));
415
416 // Get basic details.
417 $activities = [];
418 $modinfo = get_fast_modinfo($course);
419 $activities['page1'] = $modinfo->get_cm($page1->cmid);
420 $activities['page2'] = $modinfo->get_cm($page2->cmid);
421 $activities['assign'] = $assign->get_course_module();
422 $activities['page3'] = $modinfo->get_cm($page3->cmid);
423 $prevvalue = condition::OPTION_PREVIOUS;
424
425 // Setup gradings and completion.
426 if ($grade) {
427 $gradeitem = $assign->get_grade_item();
428 grade_object::set_properties($gradeitem, array('gradepass' => 50.0));
429 $gradeitem->update();
430 self::set_grade($assignrow, $user->id, $grade);
431 }
432 if ($mark) {
433 $completion = new completion_info($course);
434 $completion->update_state($activities[$mark], COMPLETION_COMPLETE);
435 }
436
437 // Set opprevious WITH non existent previous activity.
438 $info = new \core_availability\mock_info_module($user->id, $activities[$activity]);
439 $cond = new condition((object)array(
440 'cm' => (int)$prevvalue, 'e' => $condition));
441
442 // Do the checks.
443 $this->assertEquals($result, $cond->is_available(false, $info, true, $user->id));
444 $this->assertEquals($resultnot, $cond->is_available(true, $info, true, $user->id));
445 $information = $cond->get_description(false, false, $info);
446 $information = \core_availability\info::format_info($information, $course);
447 $this->assertRegExp($description, $information);
448 }
449
450 public function test_previous_activity_data(): array {
451 // Assign grade, condition, activity to complete, activity to test, result, resultnot, description.
452 return [
453 'Missing previous activity complete' => [
454 0, COMPLETION_COMPLETE, '', 'page1', false, false, '~Missing activity.*is marked complete~'
455 ],
456 'Missing previous activity incomplete' => [
457 0, COMPLETION_INCOMPLETE, '', 'page1', false, false, '~Missing activity.*is incomplete~'
458 ],
459 'Previous complete condition with previous activity incompleted' => [
460 0, COMPLETION_COMPLETE, '', 'page2', false, true, '~Page1!.*is marked complete~'
461 ],
462 'Previous incomplete condition with previous activity incompleted' => [
463 0, COMPLETION_INCOMPLETE, '', 'page2', true, false, '~Page1!.*is incomplete~'
464 ],
465 'Previous complete condition with previous activity completed' => [
466 0, COMPLETION_COMPLETE, 'page1', 'page2', true, false, '~Page1!.*is marked complete~'
467 ],
468 'Previous incomplete condition with previous activity completed' => [
469 0, COMPLETION_INCOMPLETE, 'page1', 'page2', false, true, '~Page1!.*is incomplete~'
470 ],
471 // Depenging on page pass fail (pages are not gradable).
472 'Previous complete pass condition with previous no gradable activity incompleted' => [
473 0, COMPLETION_COMPLETE_PASS, '', 'page2', false, true, '~Page1!.*is complete and passed~'
474 ],
475 'Previous complete fail condition with previous no gradable activity incompleted' => [
476 0, COMPLETION_COMPLETE_FAIL, '', 'page2', false, true, '~Page1!.*is complete and failed~'
477 ],
478 'Previous complete pass condition with previous no gradable activity completed' => [
479 0, COMPLETION_COMPLETE_PASS, 'page1', 'page2', false, true, '~Page1!.*is complete and passed~'
480 ],
481 'Previous complete fail condition with previous no gradable activity completed' => [
482 0, COMPLETION_COMPLETE_FAIL, 'page1', 'page2', false, true, '~Page1!.*is complete and failed~'
483 ],
484 // There's an page without completion between page2 ans assign.
485 'Previous complete condition with sibling activity incompleted' => [
486 0, COMPLETION_COMPLETE, '', 'assign', false, true, '~Page2!.*is marked complete~'
487 ],
488 'Previous incomplete condition with sibling activity incompleted' => [
489 0, COMPLETION_INCOMPLETE, '', 'assign', true, false, '~Page2!.*is incomplete~'
490 ],
491 'Previous complete condition with sibling activity completed' => [
492 0, COMPLETION_COMPLETE, 'page2', 'assign', true, false, '~Page2!.*is marked complete~'
493 ],
494 'Previous incomplete condition with sibling activity completed' => [
495 0, COMPLETION_INCOMPLETE, 'page2', 'assign', false, true, '~Page2!.*is incomplete~'
496 ],
497 // Depending on assign without grade.
498 'Previous complete condition with previous without grade' => [
499 0, COMPLETION_COMPLETE, '', 'page3', false, true, '~Assign!.*is marked complete~'
500 ],
501 'Previous incomplete condition with previous without grade' => [
502 0, COMPLETION_INCOMPLETE, '', 'page3', true, false, '~Assign!.*is incomplete~'
503 ],
504 'Previous complete pass condition with previous without grade' => [
505 0, COMPLETION_COMPLETE_PASS, '', 'page3', false, true, '~Assign!.*is complete and passed~'
506 ],
507 'Previous complete fail condition with previous without grade' => [
508 0, COMPLETION_COMPLETE_FAIL, '', 'page3', false, true, '~Assign!.*is complete and failed~'
509 ],
510 // Depending on assign with grade.
511 'Previous complete condition with previous fail grade' => [
512 40, COMPLETION_COMPLETE, '', 'page3', true, false, '~Assign!.*is marked complete~'
513 ],
514 'Previous incomplete condition with previous fail grade' => [
515 40, COMPLETION_INCOMPLETE, '', 'page3', false, true, '~Assign!.*is incomplete~'
516 ],
517 'Previous complete pass condition with previous fail grade' => [
518 40, COMPLETION_COMPLETE_PASS, '', 'page3', false, true, '~Assign!.*is complete and passed~'
519 ],
520 'Previous complete fail condition with previous fail grade' => [
521 40, COMPLETION_COMPLETE_FAIL, '', 'page3', true, false, '~Assign!.*is complete and failed~'
522 ],
523 'Previous complete condition with previous pass grade' => [
524 60, COMPLETION_COMPLETE, '', 'page3', true, false, '~Assign!.*is marked complete~'
525 ],
526 'Previous incomplete condition with previous pass grade' => [
527 60, COMPLETION_INCOMPLETE, '', 'page3', false, true, '~Assign!.*is incomplete~'
528 ],
529 'Previous complete pass condition with previous pass grade' => [
530 60, COMPLETION_COMPLETE_PASS, '', 'page3', true, false, '~Assign!.*is complete and passed~'
531 ],
532 'Previous complete fail condition with previous pass grade' => [
533 60, COMPLETION_COMPLETE_FAIL, '', 'page3', false, true, '~Assign!.*is complete and failed~'
534 ],
535 ];
536 }
537
538 /**
539 * Tests the is_available and get_description functions for
540 * previous activity option in course sections.
541 *
542 * @dataProvider test_section_previous_activity_data
543 * @param int $condition condition value
544 * @param bool $mark if Page 1 must be mark as completed
545 * @param string $section section to add the availability
546 * @param bool $result expected result
547 * @param bool $resultnot expected negated result
548 * @param string $description description to match
549 */
550 public function test_section_previous_activity(int $condition, bool $mark, string $section,
551 bool $result, bool $resultnot, string $description): void {
552 global $CFG, $DB;
553 require_once($CFG->dirroot . '/mod/assign/locallib.php');
554 $this->resetAfterTest();
555
556 // Create course with completion turned on.
557 $CFG->enablecompletion = true;
558 $CFG->enableavailability = true;
559 $generator = $this->getDataGenerator();
560 $course = $generator->create_course(
561 array('numsections' => 4, 'enablecompletion' => 1),
562 array('createsections' => true));
563 $user = $generator->create_user();
564 $generator->enrol_user($user->id, $course->id);
565 $this->setUser($user);
566
567 // Section 1 - page1 (manual completion).
568 $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
569 array('course' => $course->id, 'name' => 'Page1!', 'section' => 1,
570 'completion' => COMPLETION_TRACKING_MANUAL));
571
572 // Section 1 - page ignored 1 (no completion).
573 $pagenocompletion1 = $generator->get_plugin_generator('mod_page')->create_instance(
574 array('course' => $course, 'name' => 'Page ignored!', 'section' => 1));
575
576 // Section 2 - page ignored 2 (no completion).
577 $pagenocompletion2 = $generator->get_plugin_generator('mod_page')->create_instance(
578 array('course' => $course, 'name' => 'Page ignored!', 'section' => 2));
579
580 // Section 3 - page2 (manual completion).
581 $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
582 array('course' => $course->id, 'name' => 'Page2!', 'section' => 3,
583 'completion' => COMPLETION_TRACKING_MANUAL));
584
585 // Section 4 is empty.
586
587 // Get basic details.
588 get_fast_modinfo(0, 0, true);
589 $modinfo = get_fast_modinfo($course);
590 $sections['section1'] = $modinfo->get_section_info(1);
591 $sections['section2'] = $modinfo->get_section_info(2);
592 $sections['section3'] = $modinfo->get_section_info(3);
593 $sections['section4'] = $modinfo->get_section_info(4);
594 $page1cm = $modinfo->get_cm($page1->cmid);
595 $prevvalue = condition::OPTION_PREVIOUS;
596
597 if ($mark) {
598 // Mark page1 complete.
599 $completion = new completion_info($course);
600 $completion->update_state($page1cm, COMPLETION_COMPLETE);
601 }
602
603 $info = new \core_availability\mock_info_section($user->id, $sections[$section]);
604 $cond = new condition((object)array(
605 'cm' => (int)$prevvalue, 'e' => $condition));
606 $this->assertEquals($result, $cond->is_available(false, $info, true, $user->id));
607 $this->assertEquals($resultnot, $cond->is_available(true, $info, true, $user->id));
608 $information = $cond->get_description(false, false, $info);
609 $information = \core_availability\info::format_info($information, $course);
610 $this->assertRegExp($description, $information);
611
612 }
613
614 public function test_section_previous_activity_data(): array {
615 return [
616 // Condition, Activity completion, section to test, result, resultnot, description.
617 'Completion complete Section with no previous activity' => [
618 COMPLETION_COMPLETE, false, 'section1', false, false, '~Missing activity.*is marked complete~'
619 ],
620 'Completion incomplete Section with no previous activity' => [
621 COMPLETION_INCOMPLETE, false, 'section1', false, false, '~Missing activity.*is incomplete~'
622 ],
623 // Section 2 depending on section 1 -> Page 1 (no grading).
624 'Completion complete Section with previous activity incompleted' => [
625 COMPLETION_COMPLETE, false, 'section2', false, true, '~Page1!.*is marked complete~'
626 ],
627 'Completion incomplete Section with previous activity incompleted' => [
628 COMPLETION_INCOMPLETE, false, 'section2', true, false, '~Page1!.*is incomplete~'
629 ],
630 'Completion complete Section with previous activity completed' => [
631 COMPLETION_COMPLETE, true, 'section2', true, false, '~Page1!.*is marked complete~'
632 ],
633 'Completion incomplete Section with previous activity completed' => [
634 COMPLETION_INCOMPLETE, true, 'section2', false, true, '~Page1!.*is incomplete~'
635 ],
636 // Section 3 depending on section 1 -> Page 1 (no grading).
637 'Completion complete Section ignoring empty sections and activity incompleted' => [
638 COMPLETION_COMPLETE, false, 'section3', false, true, '~Page1!.*is marked complete~'
639 ],
640 'Completion incomplete Section ignoring empty sections and activity incompleted' => [
641 COMPLETION_INCOMPLETE, false, 'section3', true, false, '~Page1!.*is incomplete~'
642 ],
643 'Completion complete Section ignoring empty sections and activity completed' => [
644 COMPLETION_COMPLETE, true, 'section3', true, false, '~Page1!.*is marked complete~'
645 ],
646 'Completion incomplete Section ignoring empty sections and activity completed' => [
647 COMPLETION_INCOMPLETE, true, 'section3', false, true, '~Page1!.*is incomplete~'
648 ],
649 // Section 4 depending on section 3 -> Page 2 (no grading).
650 'Completion complete Last section with previous activity incompleted' => [
651 COMPLETION_COMPLETE, false, 'section4', false, true, '~Page2!.*is marked complete~'
652 ],
653 'Completion incomplete Last section with previous activity incompleted' => [
654 COMPLETION_INCOMPLETE, false, 'section4', true, false, '~Page2!.*is incomplete~'
655 ],
656 'Completion complete Last section with previous activity completed' => [
657 COMPLETION_COMPLETE, true, 'section4', false, true, '~Page2!.*is marked complete~'
658 ],
659 'Completion incomplete Last section with previous activity completed' => [
660 COMPLETION_INCOMPLETE, true, 'section4', true, false, '~Page2!.*is incomplete~'
661 ],
662 ];
663 }
664
e01efa2c 665 /**
666 * Tests completion_value_used static function.
667 */
668 public function test_completion_value_used() {
669 global $CFG, $DB;
670 $this->resetAfterTest();
c54b183c 671 $prevvalue = condition::OPTION_PREVIOUS;
e01efa2c 672
673 // Create course with completion turned on and some sections.
674 $CFG->enablecompletion = true;
675 $CFG->enableavailability = true;
676 $generator = $this->getDataGenerator();
677 $course = $generator->create_course(
678 array('numsections' => 1, 'enablecompletion' => 1),
679 array('createsections' => true));
e01efa2c 680
c54b183c 681 // Create six pages with manual completion.
e01efa2c 682 $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
683 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
684 $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
685 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
686 $page3 = $generator->get_plugin_generator('mod_page')->create_instance(
687 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
c54b183c
FR
688 $page4 = $generator->get_plugin_generator('mod_page')->create_instance(
689 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
690 $page5 = $generator->get_plugin_generator('mod_page')->create_instance(
691 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
692 $page6 = $generator->get_plugin_generator('mod_page')->create_instance(
693 array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
e01efa2c 694
695 // Set up page3 to depend on page1, and section1 to depend on page2.
696 $DB->set_field('course_modules', 'availability',
697 '{"op":"|","show":true,"c":[' .
698 '{"type":"completion","e":1,"cm":' . $page1->cmid . '}]}',
699 array('id' => $page3->cmid));
700 $DB->set_field('course_sections', 'availability',
701 '{"op":"|","show":true,"c":[' .
702 '{"type":"completion","e":1,"cm":' . $page2->cmid . '}]}',
703 array('course' => $course->id, 'section' => 1));
c54b183c
FR
704 // Set up page5 and page6 to depend on previous activity.
705 $DB->set_field('course_modules', 'availability',
706 '{"op":"|","show":true,"c":[' .
707 '{"type":"completion","e":1,"cm":' . $prevvalue . '}]}',
708 array('id' => $page5->cmid));
709 $DB->set_field('course_modules', 'availability',
710 '{"op":"|","show":true,"c":[' .
711 '{"type":"completion","e":1,"cm":' . $prevvalue . '}]}',
712 array('id' => $page6->cmid));
e01efa2c 713
c54b183c 714 // Check 1: nothing depends on page3 and page6 but something does on the others.
e01efa2c 715 $this->assertTrue(availability_completion\condition::completion_value_used(
716 $course, $page1->cmid));
717 $this->assertTrue(availability_completion\condition::completion_value_used(
718 $course, $page2->cmid));
719 $this->assertFalse(availability_completion\condition::completion_value_used(
720 $course, $page3->cmid));
c54b183c
FR
721 $this->assertTrue(availability_completion\condition::completion_value_used(
722 $course, $page4->cmid));
723 $this->assertTrue(availability_completion\condition::completion_value_used(
724 $course, $page5->cmid));
725 $this->assertFalse(availability_completion\condition::completion_value_used(
726 $course, $page6->cmid));
e01efa2c 727 }
728
729 /**
730 * Updates the grade of a user in the given assign module instance.
731 *
732 * @param stdClass $assignrow Assignment row from database
733 * @param int $userid User id
734 * @param float $grade Grade
735 */
736 protected static function set_grade($assignrow, $userid, $grade) {
737 $grades = array();
738 $grades[$userid] = (object)array(
739 'rawgrade' => $grade, 'userid' => $userid);
740 $assignrow->cmidnumber = null;
741 assign_grade_item_update($assignrow, $grades);
742 }
743
744 /**
745 * Tests the update_dependency_id() function.
746 */
747 public function test_update_dependency_id() {
748 $cond = new condition((object)array(
c54b183c
FR
749 'cm' => 42, 'e' => COMPLETION_COMPLETE, 'selfid' => 43));
750 $this->assertFalse($cond->update_dependency_id('frogs', 42, 540));
e01efa2c 751 $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
c54b183c 752 $this->assertTrue($cond->update_dependency_id('course_modules', 42, 456));
e01efa2c 753 $after = $cond->save();
754 $this->assertEquals(456, $after->cm);
c54b183c
FR
755
756 // Test selfid updating.
757 $cond = new condition((object)array(
758 'cm' => 42, 'e' => COMPLETION_COMPLETE));
759 $this->assertFalse($cond->update_dependency_id('frogs', 43, 540));
760 $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
761 $after = $cond->save();
762 $this->assertEquals(42, $after->cm);
763
764 // Test on previous activity.
765 $cond = new condition((object)array(
766 'cm' => condition::OPTION_PREVIOUS,
767 'e' => COMPLETION_COMPLETE));
768 $this->assertFalse($cond->update_dependency_id('frogs', 43, 80));
769 $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
770 $after = $cond->save();
771 $this->assertEquals(condition::OPTION_PREVIOUS, $after->cm);
e01efa2c 772 }
773}