/** * Test remove plan and the managing of archived user competencies. */ public function test_delete_plan_manage_archived_competencies() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $syscontext = context_system::instance(); // Create user and role for the test. $user = $dg->create_user(); $managerole = $dg->create_role(array('name' => 'User manage own', 'shortname' => 'manageown')); assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $managerole, $syscontext->id); assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $managerole, $syscontext->id); $dg->role_assign($managerole, $user->id, $syscontext->id); $this->setUser($user); // Create a framework and assign competencies. $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // Create completed plan with records in user_competency. $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \core_competency\plan::STATUS_COMPLETE)); $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id())); $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id())); $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id())); $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id())); $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(), 'planid' => $completedplan->get_id())); $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(), 'planid' => $completedplan->get_id())); api::delete_plan($completedplan->get_id()); // Check that achived user competencies are deleted. $this->assertEquals(0, \core_competency\plan::count_records()); $this->assertEquals(2, \core_competency\user_competency::count_records()); $this->assertEquals(0, \core_competency\user_competency_plan::count_records()); }
/** * Delete a plan. * * @param int $id The plan id * @return boolean */ public static function delete_plan($id) { $params = self::validate_parameters(self::delete_plan_parameters(), array('id' => $id)); $plan = api::read_plan($params['id']); self::validate_context($plan->get_context()); return external_api::clean_returnvalue(self::delete_plan_returns(), api::delete_plan($params['id'])); }
/** * Test the plan deleted event. * */ public function test_plan_deleted() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user1 = $dg->create_user(); $plan = $lpg->create_plan(array('userid' => $user1->id)); $planid = $plan->get_id(); $contextid = $plan->get_context()->id; // Trigger and capture the event. $sink = $this->redirectEvents(); $result = api::delete_plan($plan->get_id()); $this->assertTrue($result); // Get our event event. $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\competency_plan_deleted', $event); $this->assertEquals($planid, $event->objectid); $this->assertEquals($contextid, $event->contextid); $this->assertEventContextNotUsed($event); $this->assertDebuggingNotCalled(); }
public function test_sync_plans_from_cohorts_task() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); // Sql to simulate the execution in time. $cmsql = "UPDATE {cohort_members} SET timeadded = :currenttime WHERE cohortid = :cohortid AND userid = :userid"; $tplsql = "UPDATE {" . \core_competency\template::TABLE . "} SET timemodified = :currenttime WHERE id = :templateid"; $plansql = "UPDATE {" . \core_competency\plan::TABLE . "} SET timemodified = :currenttime WHERE id = :planid"; $currenttime = time(); $user1 = $dg->create_user(); $user2 = $dg->create_user(); $user3 = $dg->create_user(); $user4 = $dg->create_user(); $user5 = $dg->create_user(); $cohort = $dg->create_cohort(); $tpl = $lpg->create_template(); // Add 2 users to the cohort. cohort_add_member($cohort->id, $user1->id); cohort_add_member($cohort->id, $user2->id); // Creating plans from template cohort. $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id); $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id); $this->assertEquals(2, $created); $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task'); $this->assertInstanceOf('\\core\\task\\sync_plans_from_template_cohorts_task', $task); // Add two more users to the cohort. cohort_add_member($cohort->id, $user3->id); cohort_add_member($cohort->id, $user4->id); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Test if remove user from cohort will affect plans. cohort_remove_member($cohort->id, $user3->id); cohort_remove_member($cohort->id, $user4->id); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // The template is now hidden, and I've added a user with a missing plan. Nothing should happen. $currenttime = $currenttime + 1; $tpl->set_visible(false); $tpl->update(); $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id())); $currenttime = $currenttime + 1; cohort_add_member($cohort->id, $user5->id); $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user5->id)); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Now I set the template as visible again, the plan is created. $currenttime = $currenttime + 1; $tpl->set_visible(true); $tpl->update(); $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id())); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id()))); // Let's unlink the plan and run the task again, it should not be recreated. $currenttime = $currenttime + 1; $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id())); \core_competency\api::unlink_plan_from_template($plan); $DB->execute($plansql, array('currenttime' => $currenttime, 'planid' => $plan->get_id())); $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id))); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id))); $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id()))); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Adding users to cohort that already exist in plans. $currenttime = $currenttime + 1; cohort_add_member($cohort->id, $user3->id); cohort_add_member($cohort->id, $user4->id); $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id)); $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id)); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id()))); // Test a user plan deleted will not be recreated. $currenttime = $currenttime + 1; $plan = plan::get_record(array('userid' => $user4->id, 'templateid' => $tpl->get_id())); \core_competency\api::delete_plan($plan->get_id()); $currenttime = $currenttime + 1; $task->execute(); $task->set_last_run_time($currenttime); $this->assertEquals(3, plan::count_records(array('templateid' => $tpl->get_id()))); }
public function test_delete_plan_removes_relations() { $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $user = $dg->create_user(); $plan = $lpg->create_plan(array('userid' => $user->id)); $framework = $lpg->create_framework(); $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp1->get_id())); $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp2->get_id())); $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp3->get_id())); // Complete the plan to generate user_competency_plan entries. api::complete_plan($plan); // Confirm the data we have. $this->assertEquals(3, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(3, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); // Delete the plan now. api::delete_plan($plan->get_id()); $this->assertEquals(0, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(0, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); }