Exemplo n.º 1
0
 /**
  * Test create_cohorts
  */
 public function test_create_cohorts()
 {
     global $USER, $CFG, $DB;
     $this->resetAfterTest(true);
     $contextid = context_system::instance()->id;
     $cohort1 = array('categorytype' => array('type' => 'id', 'value' => '1'), 'name' => 'cohort test 1', 'idnumber' => 'cohorttest1', 'description' => 'This is a description for cohorttest1');
     $cohort2 = array('categorytype' => array('type' => 'system', 'value' => ''), 'name' => 'cohort test 2', 'idnumber' => 'cohorttest2', 'description' => 'This is a description for cohorttest2');
     $cohort3 = array('categorytype' => array('type' => 'id', 'value' => '1'), 'name' => 'cohort test 3', 'idnumber' => 'cohorttest3', 'description' => 'This is a description for cohorttest3');
     $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
     // Call the external function.
     $createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2));
     $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts);
     // Check we retrieve the good total number of created cohorts + no error on capability.
     $this->assertEquals(2, count($createdcohorts));
     foreach ($createdcohorts as $createdcohort) {
         if ($createdcohort['idnumber'] == $cohort1['idnumber']) {
             $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id']));
             $conid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'], 'contextlevel' => CONTEXT_COURSECAT));
             $this->assertEquals($dbcohort->contextid, $conid);
             $this->assertEquals($dbcohort->name, $cohort1['name']);
             $this->assertEquals($dbcohort->idnumber, $cohort1['idnumber']);
             $this->assertEquals($dbcohort->description, $cohort1['description']);
         }
     }
     // Call without required capability.
     $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
     $this->setExpectedException('required_capability_exception');
     $createdcohorts = core_cohort_external::create_cohorts(array($cohort3));
 }
Exemplo n.º 2
0
 /**
  * Test create_cohorts
  */
 public function test_create_cohorts()
 {
     global $USER, $CFG, $DB;
     $this->resetAfterTest(true);
     $contextid = context_system::instance()->id;
     $category = $this->getDataGenerator()->create_category();
     $cohort1 = array('categorytype' => array('type' => 'id', 'value' => $category->id), 'name' => 'cohort test 1', 'idnumber' => 'cohorttest1', 'description' => 'This is a description for cohorttest1');
     $cohort2 = array('categorytype' => array('type' => 'system', 'value' => ''), 'name' => 'cohort test 2', 'idnumber' => 'cohorttest2', 'description' => 'This is a description for cohorttest2', 'visible' => 0);
     $cohort3 = array('categorytype' => array('type' => 'id', 'value' => $category->id), 'name' => 'cohort test 3', 'idnumber' => 'cohorttest3', 'description' => 'This is a description for cohorttest3');
     $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
     // Call the external function.
     $this->setCurrentTimeStart();
     $createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2));
     $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts);
     // Check we retrieve the good total number of created cohorts + no error on capability.
     $this->assertEquals(2, count($createdcohorts));
     foreach ($createdcohorts as $createdcohort) {
         $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id']));
         if ($createdcohort['idnumber'] == $cohort1['idnumber']) {
             $conid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'], 'contextlevel' => CONTEXT_COURSECAT));
             $this->assertEquals($dbcohort->contextid, $conid);
             $this->assertEquals($dbcohort->name, $cohort1['name']);
             $this->assertEquals($dbcohort->description, $cohort1['description']);
             $this->assertEquals($dbcohort->visible, 1);
             // Field was not specified, ensure it is visible by default.
         } else {
             if ($createdcohort['idnumber'] == $cohort2['idnumber']) {
                 $this->assertEquals($dbcohort->contextid, context_system::instance()->id);
                 $this->assertEquals($dbcohort->name, $cohort2['name']);
                 $this->assertEquals($dbcohort->description, $cohort2['description']);
                 $this->assertEquals($dbcohort->visible, $cohort2['visible']);
             } else {
                 $this->fail('Unrecognised cohort found');
             }
         }
         $this->assertTimeCurrent($dbcohort->timecreated);
         $this->assertTimeCurrent($dbcohort->timemodified);
     }
     // Call without required capability.
     $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
     $this->setExpectedException('required_capability_exception');
     $createdcohorts = core_cohort_external::create_cohorts(array($cohort3));
 }