/** * Test that we can add competency to plan if we have the right capability. * * @return void */ public function test_add_competency_to_plan() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $usermanage = $dg->create_user(); $user = $dg->create_user(); $syscontext = context_system::instance(); // Creating specific roles. $managerole = $dg->create_role(array('name' => 'User manage', 'shortname' => 'manage')); assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id); assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id); $dg->role_assign($managerole, $usermanage->id, $syscontext->id); $this->setUser($usermanage); $plan = array('userid' => $usermanage->id, 'status' => \core_competency\plan::STATUS_ACTIVE); $pl1 = $lpg->create_plan($plan); $framework = $lpg->create_framework(); $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $this->assertTrue(external::add_competency_to_plan($pl1->get_id(), $competency->get_id())); // A competency cannot be added to plan based on template. $template = $lpg->create_template(); $plan = array('userid' => $usermanage->id, 'status' => \core_competency\plan::STATUS_ACTIVE, 'templateid' => $template->get_id()); $pl2 = $lpg->create_plan($plan); try { external::add_competency_to_plan($pl2->get_id(), $competency->get_id()); $this->fail('A competency cannot be added to plan based on template'); } catch (coding_exception $ex) { $this->assertTrue(true); } // User without capability cannot add competency to a plan. $this->setUser($user); try { external::add_competency_to_plan($pl1->get_id(), $competency->get_id()); $this->fail('User without capability cannot add competency to a plan'); } catch (required_capability_exception $ex) { $this->assertTrue(true); } }