MDL-52282 tool_lp: Create a scheduled task to sync plans from cohorts
[moodle.git] / admin / tool / lp / tests / task_test.php
CommitLineData
ed549dbf
IT
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 * Task tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use tool_lp\api;
28use tool_lp\plan;
29
30/**
31 * Task tests.
32 *
33 * @package tool_lp
34 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_task_testcase extends advanced_testcase {
38
39 public function test_sync_plans_from_cohorts_task() {
40 $this->resetAfterTest(true);
41 $this->setAdminUser();
42 $dg = $this->getDataGenerator();
43 $lpg = $dg->get_plugin_generator('tool_lp');
44
45 $user1 = $dg->create_user();
46 $user2 = $dg->create_user();
47 $user3 = $dg->create_user();
48 $user4 = $dg->create_user();
49
50 $cohort = $dg->create_cohort();
51 $tpl = $lpg->create_template();
52
53 // Add 2 users to the cohort.
54 cohort_add_member($cohort->id, $user1->id);
55 cohort_add_member($cohort->id, $user2->id);
56
57 // Creating plans from template cohort.
58 $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
59 $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
60
61 $this->assertEquals(2, $created);
62
63 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\sync_plans_from_template_cohorts_task');
64 $this->assertInstanceOf('\tool_lp\task\sync_plans_from_template_cohorts_task', $task);
65
66 // Add two more users to the cohort.
67 cohort_add_member($cohort->id, $user3->id);
68 cohort_add_member($cohort->id, $user4->id);
69
70 $task->execute();
71
72 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
73
74 // Test if remove user from cohort will affect plans.
75 cohort_remove_member($cohort->id, $user3->id);
76 cohort_remove_member($cohort->id, $user4->id);
77
78 $task->execute();
79 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
80
81 // Adding users to cohort that already exist in plans.
82 cohort_add_member($cohort->id, $user3->id);
83 cohort_add_member($cohort->id, $user4->id);
84
85 $task->execute();
86 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
87 }
88}