Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * API tests.
  19   *
  20   * @package    tool_cohortroles
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use tool_cohortroles\api;
  28  
  29  /**
  30   * API tests.
  31   *
  32   * @package    tool_cohortroles
  33   * @copyright  2015 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_cohortroles_api_testcase extends advanced_testcase {
  37      /** @var stdClass $cohort */
  38      protected $cohort = null;
  39  
  40      /** @var stdClass $userassignto */
  41      protected $userassignto = null;
  42  
  43      /** @var stdClass $userassignover */
  44      protected $userassignover = null;
  45  
  46      /** @var stdClass $role */
  47      protected $role = null;
  48  
  49      /**
  50       * Setup function- we will create a course and add an assign instance to it.
  51       */
  52      protected function setUp(): void {
  53          $this->resetAfterTest(true);
  54  
  55          // Create some users.
  56          $this->cohort = $this->getDataGenerator()->create_cohort();
  57          $this->userassignto = $this->getDataGenerator()->create_user();
  58          $this->userassignover = $this->getDataGenerator()->create_user();
  59          $this->roleid = create_role('Sausage Roll', 'sausageroll', 'mmmm');
  60          cohort_add_member($this->cohort->id, $this->userassignover->id);
  61      }
  62  
  63      public function test_create_cohort_role_assignment_without_permission() {
  64          $this->setUser($this->userassignto);
  65          $params = (object) array(
  66              'userid' => $this->userassignto->id,
  67              'roleid' => $this->roleid,
  68              'cohortid' => $this->cohort->id
  69          );
  70          $this->expectException(required_capability_exception::class);
  71          api::create_cohort_role_assignment($params);
  72      }
  73  
  74      public function test_create_cohort_role_assignment_with_invalid_data() {
  75          $this->setAdminUser();
  76          $params = (object) array(
  77              'userid' => $this->userassignto->id,
  78              'roleid' => -8,
  79              'cohortid' => $this->cohort->id
  80          );
  81          $this->expectException(\core_competency\invalid_persistent_exception::class);
  82          api::create_cohort_role_assignment($params);
  83      }
  84  
  85      public function test_create_cohort_role_assignment() {
  86          $this->setAdminUser();
  87          $params = (object) array(
  88              'userid' => $this->userassignto->id,
  89              'roleid' => $this->roleid,
  90              'cohortid' => $this->cohort->id
  91          );
  92          $result = api::create_cohort_role_assignment($params);
  93          $this->assertNotEmpty($result->get('id'));
  94          $this->assertEquals($result->get('userid'), $this->userassignto->id);
  95          $this->assertEquals($result->get('roleid'), $this->roleid);
  96          $this->assertEquals($result->get('cohortid'), $this->cohort->id);
  97      }
  98  
  99      public function test_delete_cohort_role_assignment_without_permission() {
 100          $this->setAdminUser();
 101          $params = (object) array(
 102              'userid' => $this->userassignto->id,
 103              'roleid' => $this->roleid,
 104              'cohortid' => $this->cohort->id
 105          );
 106          $result = api::create_cohort_role_assignment($params);
 107          $this->setUser($this->userassignto);
 108          $this->expectException(required_capability_exception::class);
 109          api::delete_cohort_role_assignment($result->get('id'));
 110      }
 111  
 112      public function test_delete_cohort_role_assignment_with_invalid_data() {
 113          $this->setAdminUser();
 114          $params = (object) array(
 115              'userid' => $this->userassignto->id,
 116              'roleid' => $this->roleid,
 117              'cohortid' => $this->cohort->id
 118          );
 119          $result = api::create_cohort_role_assignment($params);
 120          $this->expectException(dml_missing_record_exception::class);
 121          api::delete_cohort_role_assignment($result->get('id') + 1);
 122      }
 123  
 124      public function test_delete_cohort_role_assignment() {
 125          $this->setAdminUser();
 126          // Create a cohort role assigment.
 127          $params = (object) [
 128              'userid' => $this->userassignto->id,
 129              'roleid' => $this->roleid,
 130              'cohortid' => $this->cohort->id
 131          ];
 132          $cohortroleassignment = api::create_cohort_role_assignment($params);
 133          $sync = api::sync_all_cohort_roles();
 134          $rolesadded = [
 135              [
 136                  'useridassignedto' => $this->userassignto->id,
 137                  'useridassignedover' => $this->userassignover->id,
 138                  'roleid' => $this->roleid
 139              ]
 140          ];
 141          $expected = [
 142              'rolesadded' => $rolesadded,
 143              'rolesremoved' => []
 144          ];
 145          $this->assertEquals($sync, $expected);
 146  
 147          // Delete the cohort role assigment and confirm the roles are removed.
 148          $result = api::delete_cohort_role_assignment($cohortroleassignment->get('id'));
 149          $this->assertTrue($result);
 150          $sync = api::sync_all_cohort_roles();
 151          $expected = [
 152              'rolesadded' => [],
 153              'rolesremoved' => $rolesadded
 154          ];
 155          $this->assertEquals($expected, $sync);
 156      }
 157  
 158      /**
 159       * Test case verifying that syncing won't remove role assignments if they are valid for another cohort role assignment.
 160       */
 161      public function test_delete_cohort_role_assignment_cohorts_having_same_members() {
 162          $this->setAdminUser();
 163  
 164          // Create 2 cohorts, with a 1 user (user1) present in both,
 165          // and user2 and user3 members of 1 cohort each.
 166          $cohort1 = $this->getDataGenerator()->create_cohort();
 167          $cohort2 = $this->getDataGenerator()->create_cohort();
 168          $user1 = $this->getDataGenerator()->create_user();
 169          $user2 = $this->getDataGenerator()->create_user();
 170          $user3 = $this->getDataGenerator()->create_user();
 171          cohort_add_member($cohort1->id, $user1->id);
 172          cohort_add_member($cohort1->id, $user2->id);
 173          cohort_add_member($cohort2->id, $user1->id);
 174          cohort_add_member($cohort2->id, $user3->id);
 175  
 176          // And a role and a user to assign that role to.
 177          $user4 = $this->getDataGenerator()->create_user(); // A cohort manager, for example.
 178          $roleid = create_role('Role 1', 'myrole', 'test');
 179  
 180          // Assign the role for user4 in both cohorts.
 181          $params = (object) [
 182              'userid' => $user4->id,
 183              'roleid' => $roleid,
 184              'cohortid' => $cohort1->id
 185          ];
 186          $cohort1roleassignment = api::create_cohort_role_assignment($params);
 187          $params->cohortid = $cohort2->id;
 188          $cohort2roleassignment = api::create_cohort_role_assignment($params);
 189  
 190          $sync = api::sync_all_cohort_roles();
 191  
 192          // There is no guarantee about the order of roles assigned.
 193          // so confirm we have 3 role assignments, and they are for the users 1, 2 and 3.
 194          $this->assertCount(3, $sync['rolesadded']);
 195          $addedusers = array_column($sync['rolesadded'], 'useridassignedover');
 196          $this->assertContains($user1->id, $addedusers);
 197          $this->assertContains($user2->id, $addedusers);
 198          $this->assertContains($user3->id, $addedusers);
 199  
 200          // Remove the role assignment for user4/cohort1.
 201          // Verify only 1 role is unassigned as the others are still valid for the other cohort role assignment.
 202          $result = api::delete_cohort_role_assignment($cohort1roleassignment->get('id'));
 203          $this->assertTrue($result);
 204  
 205          $sync = api::sync_all_cohort_roles();
 206  
 207          $this->assertCount(0, $sync['rolesadded']);
 208          $this->assertCount(1, $sync['rolesremoved']);
 209          $removedusers = array_column($sync['rolesremoved'], 'useridassignedover');
 210          $this->assertContains($user2->id, $removedusers);
 211      }
 212  
 213      public function test_list_cohort_role_assignments() {
 214          $this->setAdminUser();
 215          $params = (object) array(
 216              'userid' => $this->userassignto->id,
 217              'roleid' => $this->roleid,
 218              'cohortid' => $this->cohort->id
 219          );
 220          $result = api::create_cohort_role_assignment($params);
 221  
 222          $list = api::list_cohort_role_assignments();
 223          $list[0]->is_valid();
 224          $this->assertEquals($list[0], $result);
 225      }
 226  
 227      public function test_count_cohort_role_assignments() {
 228          $this->setAdminUser();
 229          $params = (object) array(
 230              'userid' => $this->userassignto->id,
 231              'roleid' => $this->roleid,
 232              'cohortid' => $this->cohort->id
 233          );
 234          $result = api::create_cohort_role_assignment($params);
 235  
 236          $count = api::count_cohort_role_assignments();
 237          $this->assertEquals($count, 1);
 238      }
 239  
 240      public function test_sync_all_cohort_roles() {
 241          $this->setAdminUser();
 242          $params = (object) array(
 243              'userid' => $this->userassignto->id,
 244              'roleid' => $this->roleid,
 245              'cohortid' => $this->cohort->id
 246          );
 247          $result = api::create_cohort_role_assignment($params);
 248  
 249          // Verify roles are assigned when users enter the cohort.
 250          $sync = api::sync_all_cohort_roles();
 251  
 252          $rolesadded = array(array(
 253              'useridassignedto' => $this->userassignto->id,
 254              'useridassignedover' => $this->userassignover->id,
 255              'roleid' => $this->roleid
 256          ));
 257          $rolesremoved = array();
 258          $expected = array('rolesadded' => $rolesadded,
 259                            'rolesremoved' => $rolesremoved);
 260          $this->assertEquals($sync, $expected);
 261  
 262          // Verify roles are removed when users leave the cohort.
 263          cohort_remove_member($this->cohort->id, $this->userassignover->id);
 264          $sync = api::sync_all_cohort_roles();
 265  
 266          $rolesadded = array();
 267          $rolesremoved = array(array(
 268              'useridassignedto' => $this->userassignto->id,
 269              'useridassignedover' => $this->userassignover->id,
 270              'roleid' => $this->roleid
 271          ));
 272          $expected = array('rolesadded' => $rolesadded,
 273                            'rolesremoved' => $rolesremoved);
 274          $this->assertEquals($sync, $expected);
 275  
 276          // Verify roles assigned by any other component are not removed.
 277          $usercontext = context_user::instance($this->userassignover->id);
 278          role_assign($this->roleid, $this->userassignto->id, $usercontext->id);
 279          $sync = api::sync_all_cohort_roles();
 280  
 281          $rolesadded = array();
 282          $rolesremoved = array();
 283          $expected = array('rolesadded' => $rolesadded,
 284                            'rolesremoved' => $rolesremoved);
 285          $this->assertEquals($sync, $expected);
 286  
 287          // Remove manual role assignment.
 288          role_unassign($this->roleid, $this->userassignto->id, $usercontext->id);
 289          // Add someone to the cohort again...
 290          cohort_add_member($this->cohort->id, $this->userassignover->id);
 291          $sync = api::sync_all_cohort_roles();
 292          $rolesadded = array(array(
 293              'useridassignedto' => $this->userassignto->id,
 294              'useridassignedover' => $this->userassignover->id,
 295              'roleid' => $this->roleid
 296          ));
 297          $rolesremoved = array();
 298          $expected = array('rolesadded' => $rolesadded,
 299                            'rolesremoved' => $rolesremoved);
 300          $this->assertEquals($sync, $expected);
 301  
 302          // Verify no fatal errors when a cohort is deleted.
 303          cohort_delete_cohort($this->cohort);
 304          $sync = api::sync_all_cohort_roles();
 305  
 306          $rolesadded = array();
 307          $rolesremoved = array(array(
 308              'useridassignedto' => $this->userassignto->id,
 309              'useridassignedover' => $this->userassignover->id,
 310              'roleid' => $this->roleid
 311          ));
 312          $expected = array('rolesadded' => $rolesadded,
 313                            'rolesremoved' => $rolesremoved);
 314          $this->assertEquals($sync, $expected);
 315      }
 316  
 317  }