public function test_count_templates() { $syscontextid = context_system::instance()->id; $catcontextid = context_coursecat::instance($this->category->id)->id; // Creating a few templates. $this->setUser($this->creator); $sys1 = $this->create_template(1, true); $sys2 = $this->create_template(2, true); $cat1 = $this->create_template(3, false); $cat2 = $this->create_template(4, false); $cat3 = $this->create_template(5, false); // User without permission. $this->setUser($this->user); assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true); accesslib_clear_all_caches_for_unit_testing(); try { external::count_templates(array('contextid' => $syscontextid), 'children'); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { // All good. } // User with category permissions. assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true); assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true); accesslib_clear_all_caches_for_unit_testing(); $result = external::count_templates(array('contextid' => $syscontextid), 'children'); $result = external_api::clean_returnvalue(external::count_templates_returns(), $result); $this->assertEquals(3, $result); // User with system permissions. assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true); accesslib_clear_all_caches_for_unit_testing(); $result = external::count_templates(array('contextid' => $catcontextid), 'parents'); $result = external_api::clean_returnvalue(external::count_templates_returns(), $result); $this->assertEquals(5, $result); }