[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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() { 53 global $DB; 54 55 $this->resetAfterTest(true); 56 57 // Create some users. 58 $this->cohort = $this->getDataGenerator()->create_cohort(); 59 $this->userassignto = $this->getDataGenerator()->create_user(); 60 $this->userassignover = $this->getDataGenerator()->create_user(); 61 $this->roleid = create_role('Sausage Roll', 'sausageroll', 'mmmm'); 62 cohort_add_member($this->cohort->id, $this->userassignover->id); 63 } 64 65 /** 66 * @expectedException required_capability_exception 67 */ 68 public function test_create_cohort_role_assignment_without_permission() { 69 $this->setUser($this->userassignto); 70 $params = (object) array( 71 'userid' => $this->userassignto->id, 72 'roleid' => $this->roleid, 73 'cohortid' => $this->cohort->id 74 ); 75 api::create_cohort_role_assignment($params); 76 } 77 78 /** 79 * @expectedException core_competency\invalid_persistent_exception 80 */ 81 public function test_create_cohort_role_assignment_with_invalid_data() { 82 $this->setAdminUser(); 83 $params = (object) array( 84 'userid' => $this->userassignto->id, 85 'roleid' => -8, 86 'cohortid' => $this->cohort->id 87 ); 88 api::create_cohort_role_assignment($params); 89 } 90 91 public function test_create_cohort_role_assignment() { 92 $this->setAdminUser(); 93 $params = (object) array( 94 'userid' => $this->userassignto->id, 95 'roleid' => $this->roleid, 96 'cohortid' => $this->cohort->id 97 ); 98 $result = api::create_cohort_role_assignment($params); 99 $this->assertNotEmpty($result->get_id()); 100 $this->assertEquals($result->get_userid(), $this->userassignto->id); 101 $this->assertEquals($result->get_roleid(), $this->roleid); 102 $this->assertEquals($result->get_cohortid(), $this->cohort->id); 103 } 104 105 /** 106 * @expectedException required_capability_exception 107 */ 108 public function test_delete_cohort_role_assignment_without_permission() { 109 $this->setAdminUser(); 110 $params = (object) array( 111 'userid' => $this->userassignto->id, 112 'roleid' => $this->roleid, 113 'cohortid' => $this->cohort->id 114 ); 115 $result = api::create_cohort_role_assignment($params); 116 $this->setUser($this->userassignto); 117 api::delete_cohort_role_assignment($result->get_id()); 118 } 119 120 /** 121 * @expectedException dml_missing_record_exception 122 */ 123 public function test_delete_cohort_role_assignment_with_invalid_data() { 124 $this->setAdminUser(); 125 $params = (object) array( 126 'userid' => $this->userassignto->id, 127 'roleid' => $this->roleid, 128 'cohortid' => $this->cohort->id 129 ); 130 $result = api::create_cohort_role_assignment($params); 131 api::delete_cohort_role_assignment($result->get_id() + 1); 132 } 133 134 public function test_delete_cohort_role_assignment() { 135 $this->setAdminUser(); 136 $params = (object) array( 137 'userid' => $this->userassignto->id, 138 'roleid' => $this->roleid, 139 'cohortid' => $this->cohort->id 140 ); 141 $result = api::create_cohort_role_assignment($params); 142 $worked = api::delete_cohort_role_assignment($result->get_id()); 143 $this->assertTrue($worked); 144 } 145 146 public function test_list_cohort_role_assignments() { 147 $this->setAdminUser(); 148 $params = (object) array( 149 'userid' => $this->userassignto->id, 150 'roleid' => $this->roleid, 151 'cohortid' => $this->cohort->id 152 ); 153 $result = api::create_cohort_role_assignment($params); 154 155 $list = api::list_cohort_role_assignments(); 156 $list[0]->is_valid(); 157 $this->assertEquals($list[0], $result); 158 } 159 160 public function test_count_cohort_role_assignments() { 161 $this->setAdminUser(); 162 $params = (object) array( 163 'userid' => $this->userassignto->id, 164 'roleid' => $this->roleid, 165 'cohortid' => $this->cohort->id 166 ); 167 $result = api::create_cohort_role_assignment($params); 168 169 $count = api::count_cohort_role_assignments(); 170 $this->assertEquals($count, 1); 171 } 172 173 public function test_sync_all_cohort_roles() { 174 $this->setAdminUser(); 175 $params = (object) array( 176 'userid' => $this->userassignto->id, 177 'roleid' => $this->roleid, 178 'cohortid' => $this->cohort->id 179 ); 180 $result = api::create_cohort_role_assignment($params); 181 182 // Verify roles are assigned when users enter the cohort. 183 $sync = api::sync_all_cohort_roles(); 184 185 $rolesadded = array(array( 186 'useridassignedto' => $this->userassignto->id, 187 'useridassignedover' => $this->userassignover->id, 188 'roleid' => $this->roleid 189 )); 190 $rolesremoved = array(); 191 $expected = array('rolesadded' => $rolesadded, 192 'rolesremoved' => $rolesremoved); 193 $this->assertEquals($sync, $expected); 194 195 // Verify roles are removed when users leave the cohort. 196 cohort_remove_member($this->cohort->id, $this->userassignover->id); 197 $sync = api::sync_all_cohort_roles(); 198 199 $rolesadded = array(); 200 $rolesremoved = array(array( 201 'useridassignedto' => $this->userassignto->id, 202 'useridassignedover' => $this->userassignover->id, 203 'roleid' => $this->roleid 204 )); 205 $expected = array('rolesadded' => $rolesadded, 206 'rolesremoved' => $rolesremoved); 207 $this->assertEquals($sync, $expected); 208 209 // Verify roles assigned by any other component are not removed. 210 $usercontext = context_user::instance($this->userassignover->id); 211 role_assign($this->roleid, $this->userassignto->id, $usercontext->id); 212 $sync = api::sync_all_cohort_roles(); 213 214 $rolesadded = array(); 215 $rolesremoved = array(); 216 $expected = array('rolesadded' => $rolesadded, 217 'rolesremoved' => $rolesremoved); 218 $this->assertEquals($sync, $expected); 219 220 // Remove manual role assignment. 221 role_unassign($this->roleid, $this->userassignto->id, $usercontext->id); 222 // Add someone to the cohort again... 223 cohort_add_member($this->cohort->id, $this->userassignover->id); 224 $sync = api::sync_all_cohort_roles(); 225 $rolesadded = array(array( 226 'useridassignedto' => $this->userassignto->id, 227 'useridassignedover' => $this->userassignover->id, 228 'roleid' => $this->roleid 229 )); 230 $rolesremoved = array(); 231 $expected = array('rolesadded' => $rolesadded, 232 'rolesremoved' => $rolesremoved); 233 $this->assertEquals($sync, $expected); 234 235 // Verify no fatal errors when a cohort is deleted. 236 cohort_delete_cohort($this->cohort); 237 $sync = api::sync_all_cohort_roles(); 238 239 $rolesadded = array(); 240 $rolesremoved = array(array( 241 'useridassignedto' => $this->userassignto->id, 242 'useridassignedover' => $this->userassignover->id, 243 'roleid' => $this->roleid 244 )); 245 $expected = array('rolesadded' => $rolesadded, 246 'rolesremoved' => $rolesremoved); 247 $this->assertEquals($sync, $expected); 248 } 249 250 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |