/** * Test delete_cohort_members */ public function test_delete_cohort_members() { global $DB; $this->resetAfterTest(true); // Reset all changes automatically after this test. $cohort1 = self::getDataGenerator()->create_cohort(); $user1 = self::getDataGenerator()->create_user(); $cohort2 = self::getDataGenerator()->create_cohort(); $user2 = self::getDataGenerator()->create_user(); $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/cohort:assign', $context->id); $cohortaddmember1 = array('cohorttype' => array('type' => 'id', 'value' => $cohort1->id), 'usertype' => array('type' => 'id', 'value' => $user1->id)); $cohortmembers1 = core_cohort_external::add_cohort_members(array($cohortaddmember1)); $cohortmembers1 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers1); $cohortaddmember2 = array('cohorttype' => array('type' => 'id', 'value' => $cohort2->id), 'usertype' => array('type' => 'id', 'value' => $user2->id)); $cohortmembers2 = core_cohort_external::add_cohort_members(array($cohortaddmember2)); $cohortmembers2 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers2); // Check we retrieve no cohorts + no error on capability. $this->assertEquals(2, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1) OR (cohortid = :idcohort2 AND userid = :iduser2))', array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id))); // Call the external function. $cohortdel1 = array('cohortid' => $cohort1->id, 'userid' => $user1->id); $cohortdel2 = array('cohortid' => $cohort2->id, 'userid' => $user2->id); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); // Check we retrieve no cohorts + no error on capability. $this->assertEquals(0, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1) OR (cohortid = :idcohort2 AND userid = :iduser2))', array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id))); // Call without required capability. $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid); $this->setExpectedException('required_capability_exception'); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); }