[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/cohort/tests/ -> externallib_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * External cohort API
  19   *
  20   * @package    core_cohort
  21   * @category   external
  22   * @copyright  MediaTouch 2000 srl
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  31  require_once($CFG->dirroot . '/cohort/externallib.php');
  32  
  33  class core_cohort_externallib_testcase extends externallib_advanced_testcase {
  34  
  35      /**
  36       * Test create_cohorts
  37       */
  38      public function test_create_cohorts() {
  39          global $USER, $CFG, $DB;
  40  
  41          $this->resetAfterTest(true);
  42  
  43          $contextid = context_system::instance()->id;
  44          $category = $this->getDataGenerator()->create_category();
  45  
  46          $cohort1 = array(
  47              'categorytype' => array('type' => 'id', 'value' => $category->id),
  48              'name' => 'cohort test 1',
  49              'idnumber' => 'cohorttest1',
  50              'description' => 'This is a description for cohorttest1'
  51              );
  52  
  53          $cohort2 = array(
  54              'categorytype' => array('type' => 'system', 'value' => ''),
  55              'name' => 'cohort test 2',
  56              'idnumber' => 'cohorttest2',
  57              'description' => 'This is a description for cohorttest2',
  58              'visible' => 0
  59              );
  60  
  61          $cohort3 = array(
  62              'categorytype' => array('type' => 'id', 'value' => $category->id),
  63              'name' => 'cohort test 3',
  64              'idnumber' => 'cohorttest3',
  65              'description' => 'This is a description for cohorttest3'
  66              );
  67          $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
  68  
  69          // Call the external function.
  70          $this->setCurrentTimeStart();
  71          $createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2));
  72  
  73          // Check we retrieve the good total number of created cohorts + no error on capability.
  74          $this->assertEquals(2, count($createdcohorts));
  75  
  76          foreach ($createdcohorts as $createdcohort) {
  77              $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id']));
  78              if ($createdcohort['idnumber'] == $cohort1['idnumber']) {
  79                  $conid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'],
  80                          'contextlevel' => CONTEXT_COURSECAT));
  81                  $this->assertEquals($dbcohort->contextid, $conid);
  82                  $this->assertEquals($dbcohort->name, $cohort1['name']);
  83                  $this->assertEquals($dbcohort->description, $cohort1['description']);
  84                  $this->assertEquals($dbcohort->visible, 1); // Field was not specified, ensure it is visible by default.
  85              } else if ($createdcohort['idnumber'] == $cohort2['idnumber']) {
  86                  $this->assertEquals($dbcohort->contextid, context_system::instance()->id);
  87                  $this->assertEquals($dbcohort->name, $cohort2['name']);
  88                  $this->assertEquals($dbcohort->description, $cohort2['description']);
  89                  $this->assertEquals($dbcohort->visible, $cohort2['visible']);
  90              } else {
  91                  $this->fail('Unrecognised cohort found');
  92              }
  93              $this->assertTimeCurrent($dbcohort->timecreated);
  94              $this->assertTimeCurrent($dbcohort->timemodified);
  95          }
  96  
  97          // Call without required capability.
  98          $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
  99          $this->setExpectedException('required_capability_exception');
 100          $createdcohorts = core_cohort_external::create_cohorts(array($cohort3));
 101      }
 102  
 103      /**
 104       * Test delete_cohorts
 105       */
 106      public function test_delete_cohorts() {
 107          global $USER, $CFG, $DB;
 108  
 109          $this->resetAfterTest(true);
 110  
 111          $cohort1 = self::getDataGenerator()->create_cohort();
 112          $cohort2 = self::getDataGenerator()->create_cohort();
 113          // Check the cohorts were correctly created.
 114          $this->assertEquals(2, $DB->count_records_select('cohort', ' (id = :cohortid1 OR id = :cohortid2)',
 115                  array('cohortid1' => $cohort1->id, 'cohortid2' => $cohort2->id)));
 116  
 117          $contextid = $cohort1->contextid;
 118          $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
 119  
 120          // Call the external function.
 121          core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id));
 122  
 123          // Check we retrieve no cohorts + no error on capability.
 124          $this->assertEquals(0, $DB->count_records_select('cohort', ' (id = :cohortid1 OR id = :cohortid2)',
 125                  array('cohortid1' => $cohort1->id, 'cohortid2' => $cohort2->id)));
 126  
 127          // Call without required capability.
 128          $cohort1 = self::getDataGenerator()->create_cohort();
 129          $cohort2 = self::getDataGenerator()->create_cohort();
 130          $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
 131          $this->setExpectedException('required_capability_exception');
 132          core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id));
 133      }
 134  
 135      /**
 136       * Test get_cohorts
 137       */
 138      public function test_get_cohorts() {
 139          global $USER, $CFG;
 140  
 141          $this->resetAfterTest(true);
 142  
 143          $cohort1 = array(
 144              'contextid' => 1,
 145              'name' => 'cohortnametest1',
 146              'idnumber' => 'idnumbertest1',
 147              'description' => 'This is a description for cohort 1'
 148              );
 149          $cohort1 = self::getDataGenerator()->create_cohort($cohort1);
 150          $cohort2 = self::getDataGenerator()->create_cohort();
 151  
 152          $context = context_system::instance();
 153          $roleid = $this->assignUserCapability('moodle/cohort:view', $context->id);
 154  
 155          // Call the external function.
 156          $returnedcohorts = core_cohort_external::get_cohorts(array(
 157              $cohort1->id, $cohort2->id));
 158  
 159          // Check we retrieve the good total number of enrolled cohorts + no error on capability.
 160          $this->assertEquals(2, count($returnedcohorts));
 161  
 162          foreach ($returnedcohorts as $enrolledcohort) {
 163              if ($enrolledcohort['idnumber'] == $cohort1->idnumber) {
 164                  $this->assertEquals($cohort1->name, $enrolledcohort['name']);
 165                  $this->assertEquals($cohort1->description, $enrolledcohort['description']);
 166                  $this->assertEquals($cohort1->visible, $enrolledcohort['visible']);
 167              }
 168          }
 169  
 170          // Check that a user with cohort:manage can see the cohort.
 171          $this->unassignUserCapability('moodle/cohort:view', $context->id, $roleid);
 172          $roleid = $this->assignUserCapability('moodle/cohort:manage', $context->id, $roleid);
 173          // Call the external function.
 174          $returnedcohorts = core_cohort_external::get_cohorts(array(
 175              $cohort1->id, $cohort2->id));
 176  
 177          // Check we retrieve the good total number of enrolled cohorts + no error on capability.
 178          $this->assertEquals(2, count($returnedcohorts));
 179      }
 180  
 181      /**
 182       * Test update_cohorts
 183       */
 184      public function test_update_cohorts() {
 185          global $USER, $CFG, $DB;
 186  
 187          $this->resetAfterTest(true);
 188  
 189          $cohort1 = self::getDataGenerator()->create_cohort(array('visible' => 0));
 190  
 191          $cohort1 = array(
 192              'id' => $cohort1->id,
 193              'categorytype' => array('type' => 'id', 'value' => '1'),
 194              'name' => 'cohortnametest1',
 195              'idnumber' => 'idnumbertest1',
 196              'description' => 'This is a description for cohort 1'
 197              );
 198  
 199          $context = context_system::instance();
 200          $roleid = $this->assignUserCapability('moodle/cohort:manage', $context->id);
 201  
 202          // Call the external function.
 203          core_cohort_external::update_cohorts(array($cohort1));
 204  
 205          $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
 206          $contextid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'],
 207          'contextlevel' => CONTEXT_COURSECAT));
 208          $this->assertEquals($dbcohort->contextid, $contextid);
 209          $this->assertEquals($dbcohort->name, $cohort1['name']);
 210          $this->assertEquals($dbcohort->idnumber, $cohort1['idnumber']);
 211          $this->assertEquals($dbcohort->description, $cohort1['description']);
 212          $this->assertEquals($dbcohort->visible, 0);
 213  
 214          // Since field 'visible' was added in 2.8, make sure that update works correctly with and without this parameter.
 215          core_cohort_external::update_cohorts(array($cohort1 + array('visible' => 1)));
 216          $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
 217          $this->assertEquals(1, $dbcohort->visible);
 218          core_cohort_external::update_cohorts(array($cohort1));
 219          $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
 220          $this->assertEquals(1, $dbcohort->visible);
 221  
 222          // Call without required capability.
 223          $this->unassignUserCapability('moodle/cohort:manage', $context->id, $roleid);
 224          $this->setExpectedException('required_capability_exception');
 225          core_cohort_external::update_cohorts(array($cohort1));
 226      }
 227  
 228      /**
 229       * Verify handling of 'id' param.
 230       */
 231      public function test_update_cohorts_invalid_id_param() {
 232          $this->resetAfterTest(true);
 233          $cohort = self::getDataGenerator()->create_cohort();
 234  
 235          $cohort1 = array(
 236              'id' => 'THIS IS NOT AN ID',
 237              'name' => 'Changed cohort name',
 238              'categorytype' => array('type' => 'id', 'value' => '1'),
 239              'idnumber' => $cohort->idnumber,
 240          );
 241  
 242          try {
 243              core_cohort_external::update_cohorts(array($cohort1));
 244              $this->fail('Expecting invalid_parameter_exception exception, none occured');
 245          } catch (invalid_parameter_exception $e1) {
 246              $this->assertContains('Invalid external api parameter: the value is "THIS IS NOT AN ID"', $e1->debuginfo);
 247          }
 248  
 249          $cohort1['id'] = 9.999; // Also not a valid id of a cohort.
 250          try {
 251              core_cohort_external::update_cohorts(array($cohort1));
 252              $this->fail('Expecting invalid_parameter_exception exception, none occured');
 253          } catch (invalid_parameter_exception $e2) {
 254              $this->assertContains('Invalid external api parameter: the value is "9.999"', $e2->debuginfo);
 255          }
 256      }
 257  
 258      /**
 259       * Test update_cohorts without permission on the dest category.
 260       */
 261      public function test_update_cohorts_missing_dest() {
 262          global $USER, $CFG, $DB;
 263  
 264          $this->resetAfterTest(true);
 265  
 266          $category1 = self::getDataGenerator()->create_category(array(
 267              'name' => 'Test category 1'
 268          ));
 269          $category2 = self::getDataGenerator()->create_category(array(
 270              'name' => 'Test category 2'
 271          ));
 272          $context1 = context_coursecat::instance($category1->id);
 273          $context2 = context_coursecat::instance($category2->id);
 274  
 275          $cohort = array(
 276              'contextid' => $context1->id,
 277              'name' => 'cohortnametest1',
 278              'idnumber' => 'idnumbertest1',
 279              'description' => 'This is a description for cohort 1'
 280              );
 281          $cohort1 = self::getDataGenerator()->create_cohort($cohort);
 282  
 283          $roleid = $this->assignUserCapability('moodle/cohort:manage', $context1->id);
 284  
 285          $cohortupdate = array(
 286              'id' => $cohort1->id,
 287              'categorytype' => array('type' => 'id', 'value' => $category2->id),
 288              'name' => 'cohort update',
 289              'idnumber' => 'idnumber update',
 290              'description' => 'This is a description update'
 291              );
 292  
 293          // Call the external function.
 294          // Should fail because we don't have permission on the dest category
 295          $this->setExpectedException('required_capability_exception');
 296          core_cohort_external::update_cohorts(array($cohortupdate));
 297      }
 298  
 299      /**
 300       * Test update_cohorts without permission on the src category.
 301       */
 302      public function test_update_cohorts_missing_src() {
 303          global $USER, $CFG, $DB;
 304  
 305          $this->resetAfterTest(true);
 306  
 307          $category1 = self::getDataGenerator()->create_category(array(
 308              'name' => 'Test category 1'
 309          ));
 310          $category2 = self::getDataGenerator()->create_category(array(
 311              'name' => 'Test category 2'
 312          ));
 313          $context1 = context_coursecat::instance($category1->id);
 314          $context2 = context_coursecat::instance($category2->id);
 315  
 316          $cohort = array(
 317              'contextid' => $context1->id,
 318              'name' => 'cohortnametest1',
 319              'idnumber' => 'idnumbertest1',
 320              'description' => 'This is a description for cohort 1'
 321              );
 322          $cohort1 = self::getDataGenerator()->create_cohort($cohort);
 323  
 324          $roleid = $this->assignUserCapability('moodle/cohort:manage', $context2->id);
 325  
 326          $cohortupdate = array(
 327              'id' => $cohort1->id,
 328              'categorytype' => array('type' => 'id', 'value' => $category2->id),
 329              'name' => 'cohort update',
 330              'idnumber' => 'idnumber update',
 331              'description' => 'This is a description update'
 332              );
 333  
 334          // Call the external function.
 335          // Should fail because we don't have permission on the src category
 336          $this->setExpectedException('required_capability_exception');
 337          core_cohort_external::update_cohorts(array($cohortupdate));
 338      }
 339  
 340      /**
 341       * Test add_cohort_members
 342       */
 343      public function test_add_cohort_members() {
 344          global $DB;
 345  
 346          $this->resetAfterTest(true); // Reset all changes automatically after this test.
 347  
 348          $contextid = context_system::instance()->id;
 349  
 350          $cohort = array(
 351              'contextid' => $contextid,
 352              'name' => 'cohortnametest1',
 353              'idnumber' => 'idnumbertest1',
 354              'description' => 'This is a description for cohort 1'
 355              );
 356          $cohort0 = self::getDataGenerator()->create_cohort($cohort);
 357          // Check the cohorts were correctly created.
 358          $this->assertEquals(1, $DB->count_records_select('cohort', ' (id = :cohortid0)',
 359              array('cohortid0' => $cohort0->id)));
 360  
 361          $cohort1 = array(
 362              'cohorttype' => array('type' => 'id', 'value' => $cohort0->id),
 363              'usertype' => array('type' => 'id', 'value' => '1')
 364              );
 365  
 366          $roleid = $this->assignUserCapability('moodle/cohort:assign', $contextid);
 367  
 368          // Call the external function.
 369          $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort1));
 370  
 371          // Check we retrieve the good total number of created cohorts + no error on capability.
 372          $this->assertEquals(1, count($addcohortmembers));
 373  
 374          foreach ($addcohortmembers as $addcohortmember) {
 375              $dbcohort = $DB->get_record('cohort_members', array('cohortid' => $cohort0->id));
 376              $this->assertEquals($dbcohort->cohortid, $cohort1['cohorttype']['value']);
 377              $this->assertEquals($dbcohort->userid, $cohort1['usertype']['value']);
 378          }
 379  
 380          // Call without required capability.
 381          $cohort2 = array(
 382              'cohorttype' => array('type' => 'id', 'value' => $cohort0->id),
 383              'usertype' => array('type' => 'id', 'value' => '2')
 384              );
 385          $this->unassignUserCapability('moodle/cohort:assign', $contextid, $roleid);
 386          $this->setExpectedException('required_capability_exception');
 387          $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort2));
 388      }
 389  
 390      /**
 391       * Test delete_cohort_members
 392       */
 393      public function test_delete_cohort_members() {
 394          global $DB;
 395  
 396          $this->resetAfterTest(true); // Reset all changes automatically after this test.
 397  
 398          $cohort1 = self::getDataGenerator()->create_cohort();
 399          $user1 = self::getDataGenerator()->create_user();
 400          $cohort2 = self::getDataGenerator()->create_cohort();
 401          $user2 = self::getDataGenerator()->create_user();
 402  
 403          $context = context_system::instance();
 404          $roleid = $this->assignUserCapability('moodle/cohort:assign', $context->id);
 405  
 406          $cohortaddmember1 = array(
 407              'cohorttype' => array('type' => 'id', 'value' => $cohort1->id),
 408              'usertype' => array('type' => 'id', 'value' => $user1->id)
 409              );
 410          $cohortmembers1 = core_cohort_external::add_cohort_members(array($cohortaddmember1));
 411          $cohortaddmember2 = array(
 412              'cohorttype' => array('type' => 'id', 'value' => $cohort2->id),
 413              'usertype' => array('type' => 'id', 'value' => $user2->id)
 414              );
 415          $cohortmembers2 = core_cohort_external::add_cohort_members(array($cohortaddmember2));
 416  
 417          // Check we retrieve no cohorts + no error on capability.
 418          $this->assertEquals(2, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1)
 419              OR (cohortid = :idcohort2 AND userid = :iduser2))',
 420              array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id)));
 421  
 422          // Call the external function.
 423           $cohortdel1 = array(
 424              'cohortid' => $cohort1->id,
 425              'userid' => $user1->id
 426              );
 427           $cohortdel2 = array(
 428              'cohortid' => $cohort2->id,
 429              'userid' => $user2->id
 430              );
 431          core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2));
 432  
 433          // Check we retrieve no cohorts + no error on capability.
 434          $this->assertEquals(0, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1)
 435              OR (cohortid = :idcohort2 AND userid = :iduser2))',
 436              array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id)));
 437  
 438          // Call without required capability.
 439          $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid);
 440          $this->setExpectedException('required_capability_exception');
 441          core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2));
 442      }
 443  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1