[ Index ]

PHP Cross Reference of Unnamed Project

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1