PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tests/accesslib_test.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 2846 lines | 2118 code | 452 blank | 276 comment | 75 complexity | 2d364cea25b1d0ac0a2f662b2a5305a7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  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. * Full functional accesslib test.
  18. *
  19. * @package core
  20. * @category phpunit
  21. * @copyright 2011 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Functional test for accesslib.php
  27. *
  28. * Note: execution may take many minutes especially on slower servers.
  29. */
  30. class core_accesslib_testcase extends advanced_testcase {
  31. /**
  32. * Verify comparison of context instances in phpunit asserts.
  33. */
  34. public function test_context_comparisons() {
  35. $frontpagecontext1 = context_course::instance(SITEID);
  36. context_helper::reset_caches();
  37. $frontpagecontext2 = context_course::instance(SITEID);
  38. $this->assertEquals($frontpagecontext1, $frontpagecontext2);
  39. $user1 = context_user::instance(1);
  40. $user2 = context_user::instance(2);
  41. $this->assertNotEquals($user1, $user2);
  42. }
  43. /**
  44. * Test resetting works.
  45. */
  46. public function test_accesslib_clear_all_caches() {
  47. global $ACCESSLIB_PRIVATE;
  48. $this->resetAfterTest();
  49. $this->setAdminUser();
  50. load_all_capabilities();
  51. $this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
  52. $this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
  53. $this->assertNotEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
  54. accesslib_clear_all_caches_for_unit_testing();
  55. $this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
  56. $this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
  57. $this->assertEmpty($ACCESSLIB_PRIVATE->dirtycontexts);
  58. $this->assertEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
  59. }
  60. /**
  61. * Test getting of role access
  62. */
  63. public function test_get_role_access() {
  64. global $DB;
  65. $roles = $DB->get_records('role');
  66. foreach ($roles as $role) {
  67. $access = get_role_access($role->id);
  68. $this->assertTrue(is_array($access));
  69. $this->assertTrue(is_array($access['ra']));
  70. $this->assertTrue(is_array($access['rdef']));
  71. $this->assertTrue(isset($access['rdef_count']));
  72. $this->assertTrue(is_array($access['loaded']));
  73. $this->assertTrue(isset($access['time']));
  74. $this->assertTrue(is_array($access['rsw']));
  75. }
  76. // Note: the data is validated in the functional permission evaluation test at the end of this testcase.
  77. }
  78. /**
  79. * Test getting of guest role.
  80. */
  81. public function test_get_guest_role() {
  82. global $CFG;
  83. $guest = get_guest_role();
  84. $this->assertEquals('guest', $guest->archetype);
  85. $this->assertEquals('guest', $guest->shortname);
  86. $this->assertEquals($CFG->guestroleid, $guest->id);
  87. }
  88. /**
  89. * Test if user is admin.
  90. */
  91. public function test_is_siteadmin() {
  92. global $DB, $CFG;
  93. $this->resetAfterTest();
  94. $users = $DB->get_records('user');
  95. foreach ($users as $user) {
  96. $this->setUser(0);
  97. if ($user->username === 'admin') {
  98. $this->assertTrue(is_siteadmin($user));
  99. $this->assertTrue(is_siteadmin($user->id));
  100. $this->setUser($user);
  101. $this->assertTrue(is_siteadmin());
  102. $this->assertTrue(is_siteadmin(null));
  103. } else {
  104. $this->assertFalse(is_siteadmin($user));
  105. $this->assertFalse(is_siteadmin($user->id));
  106. $this->setUser($user);
  107. $this->assertFalse(is_siteadmin());
  108. $this->assertFalse(is_siteadmin(null));
  109. }
  110. }
  111. // Change the site admin list and check that it still works with
  112. // multiple admins. We do this with userids only (not real user
  113. // accounts) because it makes the test simpler.
  114. $before = $CFG->siteadmins;
  115. set_config('siteadmins', '666,667,668');
  116. $this->assertTrue(is_siteadmin(666));
  117. $this->assertTrue(is_siteadmin(667));
  118. $this->assertTrue(is_siteadmin(668));
  119. $this->assertFalse(is_siteadmin(669));
  120. set_config('siteadmins', '13');
  121. $this->assertTrue(is_siteadmin(13));
  122. $this->assertFalse(is_siteadmin(666));
  123. set_config('siteadmins', $before);
  124. }
  125. /**
  126. * Test if user is enrolled in a course
  127. */
  128. public function test_is_enrolled() {
  129. global $DB;
  130. $this->resetAfterTest();
  131. // Generate data.
  132. $user = $this->getDataGenerator()->create_user();
  133. $course = $this->getDataGenerator()->create_course();
  134. $coursecontext = context_course::instance($course->id);
  135. $role = $DB->get_record('role', array('shortname'=>'student'));
  136. // There should be a manual enrolment as part of the default install.
  137. $plugin = enrol_get_plugin('manual');
  138. $instance = $DB->get_record('enrol', array(
  139. 'courseid' => $course->id,
  140. 'enrol' => 'manual',
  141. ));
  142. $this->assertNotSame(false, $instance);
  143. // Enrol the user in the course.
  144. $plugin->enrol_user($instance, $user->id, $role->id);
  145. // We'll test with the mod/assign:submit capability.
  146. $capability= 'mod/assign:submit';
  147. $this->assertTrue($DB->record_exists('capabilities', array('name' => $capability)));
  148. // Switch to our user.
  149. $this->setUser($user);
  150. // Ensure that the user has the capability first.
  151. $this->assertTrue(has_capability($capability, $coursecontext, $user->id));
  152. // We first test whether the user is enrolled on the course as this
  153. // seeds the cache, then we test for the capability.
  154. $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
  155. $this->assertTrue(is_enrolled($coursecontext, $user, $capability));
  156. // Prevent the capability for this user role.
  157. assign_capability($capability, CAP_PROHIBIT, $role->id, $coursecontext);
  158. $coursecontext->mark_dirty();
  159. $this->assertFalse(has_capability($capability, $coursecontext, $user->id));
  160. // Again, we seed the cache first by checking initial enrolment,
  161. // and then we test the actual capability.
  162. $this->assertTrue(is_enrolled($coursecontext, $user, '', true));
  163. $this->assertFalse(is_enrolled($coursecontext, $user, $capability));
  164. }
  165. /**
  166. * Test logged in test.
  167. */
  168. public function test_isloggedin() {
  169. global $USER;
  170. $this->resetAfterTest();
  171. $USER->id = 0;
  172. $this->assertFalse(isloggedin());
  173. $USER->id = 1;
  174. $this->assertTrue(isloggedin());
  175. }
  176. /**
  177. * Test guest user test.
  178. */
  179. public function test_isguestuser() {
  180. global $DB;
  181. $this->resetAfterTest();
  182. $guest = $DB->get_record('user', array('username'=>'guest'));
  183. $this->setUser(0);
  184. $this->assertFalse(isguestuser());
  185. $this->setAdminUser();
  186. $this->assertFalse(isguestuser());
  187. $this->assertTrue(isguestuser($guest));
  188. $this->assertTrue(isguestuser($guest->id));
  189. $this->setUser($guest);
  190. $this->assertTrue(isguestuser());
  191. $users = $DB->get_records('user');
  192. foreach ($users as $user) {
  193. if ($user->username === 'guest') {
  194. continue;
  195. }
  196. $this->assertFalse(isguestuser($user));
  197. }
  198. }
  199. /**
  200. * Test capability riskiness.
  201. */
  202. public function test_is_safe_capability() {
  203. global $DB;
  204. // Note: there is not much to test, just make sure no notices are throw for the most dangerous cap.
  205. $capability = $DB->get_record('capabilities', array('name'=>'moodle/site:config'), '*', MUST_EXIST);
  206. $this->assertFalse(is_safe_capability($capability));
  207. }
  208. /**
  209. * Test context fetching.
  210. */
  211. public function test_get_context_info_array() {
  212. $this->resetAfterTest();
  213. $syscontext = context_system::instance();
  214. $user = $this->getDataGenerator()->create_user();
  215. $usercontext = context_user::instance($user->id);
  216. $course = $this->getDataGenerator()->create_course();
  217. $catcontext = context_coursecat::instance($course->category);
  218. $coursecontext = context_course::instance($course->id);
  219. $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
  220. $modcontext = context_module::instance($page->cmid);
  221. $cm = get_coursemodule_from_instance('page', $page->id);
  222. $block1 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$coursecontext->id));
  223. $block1context = context_block::instance($block1->id);
  224. $block2 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid'=>$modcontext->id));
  225. $block2context = context_block::instance($block2->id);
  226. $result = get_context_info_array($syscontext->id);
  227. $this->assertCount(3, $result);
  228. $this->assertEquals($syscontext, $result[0]);
  229. $this->assertNull($result[1]);
  230. $this->assertNull($result[2]);
  231. $result = get_context_info_array($usercontext->id);
  232. $this->assertCount(3, $result);
  233. $this->assertEquals($usercontext, $result[0]);
  234. $this->assertNull($result[1]);
  235. $this->assertNull($result[2]);
  236. $result = get_context_info_array($catcontext->id);
  237. $this->assertCount(3, $result);
  238. $this->assertEquals($catcontext, $result[0]);
  239. $this->assertNull($result[1]);
  240. $this->assertNull($result[2]);
  241. $result = get_context_info_array($coursecontext->id);
  242. $this->assertCount(3, $result);
  243. $this->assertEquals($coursecontext, $result[0]);
  244. $this->assertEquals($course->id, $result[1]->id);
  245. $this->assertSame($course->shortname, $result[1]->shortname);
  246. $this->assertNull($result[2]);
  247. $result = get_context_info_array($block1context->id);
  248. $this->assertCount(3, $result);
  249. $this->assertEquals($block1context, $result[0]);
  250. $this->assertEquals($course->id, $result[1]->id);
  251. $this->assertEquals($course->shortname, $result[1]->shortname);
  252. $this->assertNull($result[2]);
  253. $result = get_context_info_array($modcontext->id);
  254. $this->assertCount(3, $result);
  255. $this->assertEquals($modcontext, $result[0]);
  256. $this->assertEquals($course->id, $result[1]->id);
  257. $this->assertSame($course->shortname, $result[1]->shortname);
  258. $this->assertEquals($cm->id, $result[2]->id);
  259. $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly);
  260. $result = get_context_info_array($block2context->id);
  261. $this->assertCount(3, $result);
  262. $this->assertEquals($block2context, $result[0]);
  263. $this->assertEquals($course->id, $result[1]->id);
  264. $this->assertSame($course->shortname, $result[1]->shortname);
  265. $this->assertEquals($cm->id, $result[2]->id);
  266. $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly);
  267. }
  268. /**
  269. * Test looking for course contacts.
  270. */
  271. public function test_has_coursecontact_role() {
  272. global $DB, $CFG;
  273. $this->resetAfterTest();
  274. $users = $DB->get_records('user');
  275. // Nobody is expected to have any course level roles.
  276. $this->assertNotEmpty($CFG->coursecontact);
  277. foreach ($users as $user) {
  278. $this->assertFalse(has_coursecontact_role($user->id));
  279. }
  280. $user = $this->getDataGenerator()->create_user();
  281. $course = $this->getDataGenerator()->create_course();
  282. role_assign($CFG->coursecontact, $user->id, context_course::instance($course->id));
  283. $this->assertTrue(has_coursecontact_role($user->id));
  284. }
  285. /**
  286. * Test creation of roles.
  287. */
  288. public function test_create_role() {
  289. global $DB;
  290. $this->resetAfterTest();
  291. $id = create_role('New student role', 'student2', 'New student description', 'student');
  292. $role = $DB->get_record('role', array('id'=>$id));
  293. $this->assertNotEmpty($role);
  294. $this->assertSame('New student role', $role->name);
  295. $this->assertSame('student2', $role->shortname);
  296. $this->assertSame('New student description', $role->description);
  297. $this->assertSame('student', $role->archetype);
  298. }
  299. /**
  300. * Test adding of capabilities to roles.
  301. */
  302. public function test_assign_capability() {
  303. global $DB;
  304. $this->resetAfterTest();
  305. $user = $this->getDataGenerator()->create_user();
  306. $syscontext = context_system::instance();
  307. $frontcontext = context_course::instance(SITEID);
  308. $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  309. $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to student by default.
  310. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
  311. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse')));
  312. $this->setUser($user);
  313. $result = assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $student->id, $frontcontext->id);
  314. $this->assertTrue($result);
  315. $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
  316. $this->assertNotEmpty($permission);
  317. $this->assertEquals(CAP_ALLOW, $permission->permission);
  318. $this->assertEquals($user->id, $permission->modifierid);
  319. $this->setUser(0);
  320. $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, false);
  321. $this->assertTrue($result);
  322. $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
  323. $this->assertNotEmpty($permission);
  324. $this->assertEquals(CAP_ALLOW, $permission->permission);
  325. $this->assertEquals(3, $permission->modifierid);
  326. $result = assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $student->id, $frontcontext->id, true);
  327. $this->assertTrue($result);
  328. $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
  329. $this->assertNotEmpty($permission);
  330. $this->assertEquals(CAP_PROHIBIT, $permission->permission);
  331. $this->assertEquals(0, $permission->modifierid);
  332. $result = assign_capability('moodle/backup:backupcourse', CAP_INHERIT, $student->id, $frontcontext->id);
  333. $this->assertTrue($result);
  334. $permission = $DB->get_record('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$student->id, 'capability'=>'moodle/backup:backupcourse'));
  335. $this->assertEmpty($permission);
  336. // Test event trigger.
  337. $rolecapabilityevent = \core\event\role_capabilities_updated::create(array('context' => $syscontext,
  338. 'objectid' => $student->id,
  339. 'other' => array('name' => $student->shortname)
  340. ));
  341. $expectedlegacylog = array(SITEID, 'role', 'view', 'admin/roles/define.php?action=view&roleid=' . $student->id,
  342. $student->shortname, '', $user->id);
  343. $rolecapabilityevent->set_legacy_logdata($expectedlegacylog);
  344. $rolecapabilityevent->add_record_snapshot('role', $student);
  345. $sink = $this->redirectEvents();
  346. $rolecapabilityevent->trigger();
  347. $events = $sink->get_events();
  348. $sink->close();
  349. $event = array_pop($events);
  350. $this->assertInstanceOf('\core\event\role_capabilities_updated', $event);
  351. $expectedurl = new moodle_url('admin/roles/define.php', array('action' => 'view', 'roleid' => $student->id));
  352. $this->assertEquals($expectedurl, $event->get_url());
  353. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  354. }
  355. /**
  356. * Test removing of capabilities from roles.
  357. */
  358. public function test_unassign_capability() {
  359. global $DB;
  360. $this->resetAfterTest();
  361. $syscontext = context_system::instance();
  362. $frontcontext = context_course::instance(SITEID);
  363. $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
  364. $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability assigned to manager by default.
  365. assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
  366. $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  367. $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  368. $result = unassign_capability('moodle/backup:backupcourse', $manager->id, $syscontext->id);
  369. $this->assertTrue($result);
  370. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  371. $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  372. unassign_capability('moodle/backup:backupcourse', $manager->id, $frontcontext);
  373. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  374. assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
  375. assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $frontcontext->id);
  376. $this->assertTrue($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  377. $result = unassign_capability('moodle/backup:backupcourse', $manager->id);
  378. $this->assertTrue($result);
  379. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  380. $this->assertFalse($DB->record_exists('role_capabilities', array('contextid'=>$frontcontext->id, 'roleid'=>$manager->id, 'capability'=>'moodle/backup:backupcourse')));
  381. }
  382. /**
  383. * Test role assigning.
  384. */
  385. public function test_role_assign() {
  386. global $DB, $USER;
  387. $this->resetAfterTest();
  388. $user = $this->getDataGenerator()->create_user();
  389. $course = $this->getDataGenerator()->create_course();
  390. $role = $DB->get_record('role', array('shortname'=>'student'));
  391. $this->setUser(0);
  392. $context = context_system::instance();
  393. $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  394. role_assign($role->id, $user->id, $context->id);
  395. $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
  396. $this->assertNotEmpty($ras);
  397. $this->assertSame('', $ras->component);
  398. $this->assertSame('0', $ras->itemid);
  399. $this->assertEquals($USER->id, $ras->modifierid);
  400. $this->setAdminUser();
  401. $context = context_course::instance($course->id);
  402. $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  403. role_assign($role->id, $user->id, $context->id, 'enrol_self', 1, 666);
  404. $ras = $DB->get_record('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id));
  405. $this->assertNotEmpty($ras);
  406. $this->assertSame('enrol_self', $ras->component);
  407. $this->assertSame('1', $ras->itemid);
  408. $this->assertEquals($USER->id, $ras->modifierid);
  409. $this->assertEquals(666, $ras->timemodified);
  410. // Test event triggered.
  411. $user2 = $this->getDataGenerator()->create_user();
  412. $sink = $this->redirectEvents();
  413. $raid = role_assign($role->id, $user2->id, $context->id);
  414. $events = $sink->get_events();
  415. $sink->close();
  416. $this->assertCount(1, $events);
  417. $event = $events[0];
  418. $this->assertInstanceOf('\core\event\role_assigned', $event);
  419. $this->assertSame('role', $event->target);
  420. $this->assertSame('role', $event->objecttable);
  421. $this->assertEquals($role->id, $event->objectid);
  422. $this->assertEquals($context->id, $event->contextid);
  423. $this->assertEquals($user2->id, $event->relateduserid);
  424. $this->assertCount(3, $event->other);
  425. $this->assertEquals($raid, $event->other['id']);
  426. $this->assertSame('', $event->other['component']);
  427. $this->assertEquals(0, $event->other['itemid']);
  428. $this->assertInstanceOf('moodle_url', $event->get_url());
  429. $this->assertSame('role_assigned', $event::get_legacy_eventname());
  430. $roles = get_all_roles();
  431. $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
  432. $expectedlegacylog = array($course->id, 'role', 'assign',
  433. 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
  434. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  435. }
  436. /**
  437. * Test role unassigning.
  438. */
  439. public function test_role_unassign() {
  440. global $DB, $USER;
  441. $this->resetAfterTest();
  442. $user = $this->getDataGenerator()->create_user();
  443. $course = $this->getDataGenerator()->create_course();
  444. $role = $DB->get_record('role', array('shortname'=>'student'));
  445. $context = context_course::instance($course->id);
  446. role_assign($role->id, $user->id, $context->id);
  447. $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  448. role_unassign($role->id, $user->id, $context->id);
  449. $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  450. role_assign($role->id, $user->id, $context->id, 'enrol_self', 1);
  451. $this->assertTrue($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  452. role_unassign($role->id, $user->id, $context->id, 'enrol_self', 1);
  453. $this->assertFalse($DB->record_exists('role_assignments', array('userid'=>$user->id, 'roleid'=>$role->id, 'contextid'=>$context->id)));
  454. // Test event triggered.
  455. role_assign($role->id, $user->id, $context->id);
  456. $sink = $this->redirectEvents();
  457. role_unassign($role->id, $user->id, $context->id);
  458. $events = $sink->get_events();
  459. $sink->close();
  460. $this->assertCount(1, $events);
  461. $event = $events[0];
  462. $this->assertInstanceOf('\core\event\role_unassigned', $event);
  463. $this->assertSame('role', $event->target);
  464. $this->assertSame('role', $event->objecttable);
  465. $this->assertEquals($role->id, $event->objectid);
  466. $this->assertEquals($context->id, $event->contextid);
  467. $this->assertEquals($user->id, $event->relateduserid);
  468. $this->assertCount(3, $event->other);
  469. $this->assertSame('', $event->other['component']);
  470. $this->assertEquals(0, $event->other['itemid']);
  471. $this->assertInstanceOf('moodle_url', $event->get_url());
  472. $roles = get_all_roles();
  473. $rolenames = role_fix_names($roles, $context, ROLENAME_ORIGINAL, true);
  474. $expectedlegacylog = array($course->id, 'role', 'unassign',
  475. 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$role->id, $rolenames[$role->id], '', $USER->id);
  476. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  477. }
  478. /**
  479. * Test role unassigning.
  480. */
  481. public function test_role_unassign_all() {
  482. global $DB;
  483. $this->resetAfterTest();
  484. $user = $this->getDataGenerator()->create_user();
  485. $course = $this->getDataGenerator()->create_course();
  486. $role = $DB->get_record('role', array('shortname'=>'student'));
  487. $role2 = $DB->get_record('role', array('shortname'=>'teacher'));
  488. $syscontext = context_system::instance();
  489. $coursecontext = context_course::instance($course->id);
  490. $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
  491. $modcontext = context_module::instance($page->cmid);
  492. role_assign($role->id, $user->id, $syscontext->id);
  493. role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
  494. $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  495. role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role->id));
  496. $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  497. role_assign($role->id, $user->id, $syscontext->id);
  498. role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
  499. role_assign($role->id, $user->id, $modcontext->id);
  500. $this->assertEquals(3, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  501. role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), false);
  502. $this->assertEquals(2, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  503. role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id), true);
  504. $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  505. role_unassign_all(array('userid'=>$user->id));
  506. $this->assertEquals(0, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  507. role_assign($role->id, $user->id, $syscontext->id);
  508. role_assign($role->id, $user->id, $coursecontext->id, 'enrol_self', 1);
  509. role_assign($role->id, $user->id, $coursecontext->id);
  510. role_assign($role->id, $user->id, $modcontext->id);
  511. $this->assertEquals(4, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  512. role_unassign_all(array('userid'=>$user->id, 'contextid'=>$coursecontext->id, 'component'=>'enrol_self'), true, true);
  513. $this->assertEquals(1, $DB->count_records('role_assignments', array('userid'=>$user->id)));
  514. // Test events triggered.
  515. role_assign($role2->id, $user->id, $coursecontext->id);
  516. role_assign($role2->id, $user->id, $modcontext->id);
  517. $sink = $this->redirectEvents();
  518. role_unassign_all(array('userid'=>$user->id, 'roleid'=>$role2->id));
  519. $events = $sink->get_events();
  520. $sink->close();
  521. $this->assertCount(2, $events);
  522. $this->assertInstanceOf('\core\event\role_unassigned', $events[0]);
  523. $this->assertInstanceOf('\core\event\role_unassigned', $events[1]);
  524. }
  525. /**
  526. * Test role queries.
  527. */
  528. public function test_get_roles_with_capability() {
  529. global $DB;
  530. $this->resetAfterTest();
  531. $syscontext = context_system::instance();
  532. $frontcontext = context_course::instance(SITEID);
  533. $manager = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
  534. $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
  535. $this->assertTrue($DB->record_exists('capabilities', array('name'=>'moodle/backup:backupcourse'))); // Any capability is ok.
  536. $DB->delete_records('role_capabilities', array('capability'=>'moodle/backup:backupcourse'));
  537. $roles = get_roles_with_capability('moodle/backup:backupcourse');
  538. $this->assertEquals(array(), $roles);
  539. assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $manager->id, $syscontext->id);
  540. assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $manager->id, $frontcontext->id);
  541. assign_capability('moodle/backup:backupcourse', CAP_PREVENT, $teacher->id, $frontcontext->id);
  542. $roles = get_roles_with_capability('moodle/backup:backupcourse');
  543. $this->assertEquals(array($teacher->id, $manager->id), array_keys($roles), '', 0, 10, true);
  544. $roles = get_roles_with_capability('moodle/backup:backupcourse', CAP_ALLOW);
  545. $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
  546. $roles = get_roles_with_capability('moodle/backup:backupcourse', null, $syscontext);
  547. $this->assertEquals(array($manager->id), array_keys($roles), '', 0, 10, true);
  548. }
  549. /**
  550. * Test deleting of roles.
  551. */
  552. public function test_delete_role() {
  553. global $DB;
  554. $this->resetAfterTest();
  555. $role = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
  556. $user = $this->getDataGenerator()->create_user();
  557. role_assign($role->id, $user->id, context_system::instance());
  558. $course = $this->getDataGenerator()->create_course();
  559. $rolename = (object)array('roleid'=>$role->id, 'name'=>'Man', 'contextid'=>context_course::instance($course->id)->id);
  560. $DB->insert_record('role_names', $rolename);
  561. $this->assertTrue($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
  562. $this->assertTrue($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
  563. $this->assertTrue($DB->record_exists('role_names', array('roleid'=>$role->id)));
  564. $this->assertTrue($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
  565. $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
  566. $this->assertTrue($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
  567. $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
  568. $this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
  569. // Delete role and get event.
  570. $sink = $this->redirectEvents();
  571. $result = delete_role($role->id);
  572. $events = $sink->get_events();
  573. $sink->close();
  574. $event = array_pop($events);
  575. $this->assertTrue($result);
  576. $this->assertFalse($DB->record_exists('role', array('id'=>$role->id)));
  577. $this->assertFalse($DB->record_exists('role_assignments', array('roleid'=>$role->id)));
  578. $this->assertFalse($DB->record_exists('role_capabilities', array('roleid'=>$role->id)));
  579. $this->assertFalse($DB->record_exists('role_names', array('roleid'=>$role->id)));
  580. $this->assertFalse($DB->record_exists('role_context_levels', array('roleid'=>$role->id)));
  581. $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$role->id)));
  582. $this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign'=>$role->id)));
  583. $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$role->id)));
  584. $this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride'=>$role->id)));
  585. // Test triggered event.
  586. $this->assertInstanceOf('\core\event\role_deleted', $event);
  587. $this->assertSame('role', $event->target);
  588. $this->assertSame('role', $event->objecttable);
  589. $this->assertSame($role->id, $event->objectid);
  590. $this->assertEquals(context_system::instance(), $event->get_context());
  591. $this->assertSame($role->shortname, $event->other['shortname']);
  592. $this->assertSame($role->description, $event->other['description']);
  593. $this->assertSame($role->archetype, $event->other['archetype']);
  594. $expectedlegacylog = array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid='.$role->id,
  595. $role->shortname, '');
  596. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  597. }
  598. /**
  599. * Test fetching of all roles.
  600. */
  601. public function test_get_all_roles() {
  602. global $DB;
  603. $this->resetAfterTest();
  604. $allroles = get_all_roles();
  605. $this->assertInternalType('array', $allroles);
  606. $this->assertCount(8, $allroles); // There are 8 roles is standard install.
  607. $role = reset($allroles);
  608. $role = (array)$role;
  609. $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype'), array_keys($role), '', 0, 10, true);
  610. foreach ($allroles as $roleid => $role) {
  611. $this->assertEquals($role->id, $roleid);
  612. }
  613. $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
  614. $course = $this->getDataGenerator()->create_course();
  615. $coursecontext = context_course::instance($course->id);
  616. $otherid = create_role('Other role', 'other', 'Some other role', '');
  617. $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
  618. $DB->insert_record('role_names', $teacherename);
  619. $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
  620. $DB->insert_record('role_names', $otherrename);
  621. $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
  622. $allroles = get_all_roles($coursecontext);
  623. $this->assertInternalType('array', $allroles);
  624. $this->assertCount(9, $allroles);
  625. $role = reset($allroles);
  626. $role = (array)$role;
  627. $this->assertEquals(array('id', 'name', 'shortname', 'description', 'sortorder', 'archetype', 'coursealias'), array_keys($role), '', 0, 10, true);
  628. foreach ($allroles as $roleid => $role) {
  629. $this->assertEquals($role->id, $roleid);
  630. if (isset($renames[$roleid])) {
  631. $this->assertSame($renames[$roleid], $role->coursealias);
  632. } else {
  633. $this->assertNull($role->coursealias);
  634. }
  635. }
  636. }
  637. /**
  638. * Test getting of all archetypes.
  639. */
  640. public function test_get_role_archetypes() {
  641. $archetypes = get_role_archetypes();
  642. $this->assertCount(8, $archetypes); // There are 8 archetypes in standard install.
  643. foreach ($archetypes as $k => $v) {
  644. $this->assertSame($k, $v);
  645. }
  646. }
  647. /**
  648. * Test getting of roles with given archetype.
  649. */
  650. public function test_get_archetype_roles() {
  651. $this->resetAfterTest();
  652. // New install should have 1 role for each archetype.
  653. $archetypes = get_role_archetypes();
  654. foreach ($archetypes as $archetype) {
  655. $roles = get_archetype_roles($archetype);
  656. $this->assertCount(1, $roles);
  657. $role = reset($roles);
  658. $this->assertSame($archetype, $role->archetype);
  659. }
  660. create_role('New student role', 'student2', 'New student description', 'student');
  661. $roles = get_archetype_roles('student');
  662. $this->assertCount(2, $roles);
  663. }
  664. /**
  665. * Test aliased role names.
  666. */
  667. public function test_role_get_name() {
  668. global $DB;
  669. $this->resetAfterTest();
  670. $allroles = $DB->get_records('role');
  671. $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
  672. $course = $this->getDataGenerator()->create_course();
  673. $coursecontext = context_course::instance($course->id);
  674. $otherid = create_role('Other role', 'other', 'Some other role', '');
  675. $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
  676. $DB->insert_record('role_names', $teacherename);
  677. $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
  678. $DB->insert_record('role_names', $otherrename);
  679. $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
  680. foreach ($allroles as $role) {
  681. // Get localised name from lang pack.
  682. $this->assertSame('', $role->name);
  683. $name = role_get_name($role, null, ROLENAME_ORIGINAL);
  684. $this->assertNotEmpty($name);
  685. $this->assertNotEquals($role->shortname, $name);
  686. if (isset($renames[$role->id])) {
  687. $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext));
  688. $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS));
  689. $this->assertSame($renames[$role->id], role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
  690. $this->assertSame("{$renames[$role->id]} ($name)", role_get_name($role, $coursecontext, ROLENAME_BOTH));
  691. } else {
  692. $this->assertSame($name, role_get_name($role, $coursecontext));
  693. $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ALIAS));
  694. $this->assertNull(role_get_name($role, $coursecontext, ROLENAME_ALIAS_RAW));
  695. $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_BOTH));
  696. }
  697. $this->assertSame($name, role_get_name($role));
  698. $this->assertSame($name, role_get_name($role, $coursecontext, ROLENAME_ORIGINAL));
  699. $this->assertSame($name, role_get_name($role, null, ROLENAME_ORIGINAL));
  700. $this->assertSame($role->shortname, role_get_name($role, $coursecontext, ROLENAME_SHORT));
  701. $this->assertSame($role->shortname, role_get_name($role, null, ROLENAME_SHORT));
  702. $this->assertSame("$name ($role->shortname)", role_get_name($role, $coursecontext, ROLENAME_ORIGINALANDSHORT));
  703. $this->assertSame("$name ($role->shortname)", role_get_name($role, null, ROLENAME_ORIGINALANDSHORT));
  704. $this->assertNull(role_get_name($role, null, ROLENAME_ALIAS_RAW));
  705. }
  706. }
  707. /**
  708. * Test tweaking of role name arrays.
  709. */
  710. public function test_role_fix_names() {
  711. global $DB;
  712. $this->resetAfterTest();
  713. $teacher = $DB->get_record('role', array('shortname'=>'teacher'), '*', MUST_EXIST);
  714. $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  715. $otherid = create_role('Other role', 'other', 'Some other role', '');
  716. $anotherid = create_role('Another role', 'another', 'Yet another other role', '');
  717. $allroles = $DB->get_records('role');
  718. $syscontext = context_system::instance();
  719. $frontcontext = context_course::instance(SITEID);
  720. $course = $this->getDataGenerator()->create_course();
  721. $coursecontext = context_course::instance($course->id);
  722. $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
  723. $categorycontext = context_coursecat::instance($category->id);
  724. $teacherename = (object)array('roleid'=>$teacher->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
  725. $DB->insert_record('role_names', $teacherename);
  726. $otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
  727. $DB->insert_record('role_names', $otherrename);
  728. $renames = $DB->get_records_menu('role_names', array('contextid'=>$coursecontext->id), '', 'roleid, name');
  729. // Make sure all localname contain proper values for each ROLENAME_ constant,
  730. // note role_get_name() on frontpage is used to get the original name for future compatibility.
  731. $roles = $allroles;
  732. unset($roles[$student->id]); // Remove one role to make sure no role is added or removed.
  733. $rolenames = array();
  734. foreach ($roles as $role) {
  735. $rolenames[$role->id] = $role->name;
  736. }
  737. $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT);
  738. foreach ($alltypes as $type) {
  739. $fixed = role_fix_names($roles, $coursecontext, $type);
  740. $this->assertCount(count($roles), $fixed);
  741. foreach ($fixed as $roleid => $rolename) {
  742. $this->assertInstanceOf('stdClass', $rolename);
  743. $role = $allroles[$roleid];
  744. $name = role_get_name($role, $coursecontext, $type);
  745. $this->assertSame($name, $rolename->localname);
  746. }
  747. $fixed = role_fix_names($rolenames, $coursecontext, $type);
  748. $this->assertCount(count($rolenames), $fixed);
  749. foreach ($fixed as $roleid => $rolename) {
  750. $role = $allroles[$roleid];
  751. $name = role_get_name($role, $coursecontext, $type);
  752. $this->assertSame($name, $rolename);
  753. }
  754. }
  755. }
  756. /**
  757. * Test role default allows.
  758. */
  759. public function test_get_default_role_archetype_allows() {
  760. $archetypes = get_role_archetypes();
  761. foreach ($archetypes as $archetype) {
  762. $result = get_default_role_archetype_allows('assign', $archetype);
  763. $this->assertInternalType('array', $result);
  764. $result = get_default_role_archetype_allows('override', $archetype);
  765. $this->assertInternalType('array', $result);
  766. $result = get_default_role_archetype_allows('switch', $archetype);
  767. $this->assertInternalType('array', $result);
  768. }
  769. $result = get_default_role_archetype_allows('assign', '');
  770. $this->assertSame(array(), $result);
  771. $result = get_default_role_archetype_allows('override', '');
  772. $this->assertSame(array(), $result);
  773. $result = get_default_role_archetype_allows('switch', '');
  774. $this->assertSame(array(), $result);
  775. $result = get_default_role_archetype_allows('assign', 'wrongarchetype');
  776. $this->assertSame(array(), $result);
  777. $this->assertDebuggingCalled();
  778. $result = get_default_role_archetype_allows('override', 'wrongarchetype');
  779. $this->assertSame(array(), $result);
  780. $this->assertDebuggingCalled();
  781. $result = get_default_role_archetype_allows('switch', 'wrongarchetype');
  782. $this->assertSame(array(), $result);
  783. $this->assertDebuggingCalled();
  784. }
  785. /**
  786. * Test allowing of role assignments.
  787. */
  788. public function test_allow_assign() {
  789. global $DB, $CFG;
  790. $this->resetAfterTest();
  791. $otherid = create_role('Other role', 'other', 'Some other role', '');
  792. $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  793. $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
  794. allow_assign($otherid, $student->id);
  795. $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid'=>$otherid, 'allowassign'=>$student->id)));
  796. // Test event trigger.
  797. $allowroleassignevent = \core\event\role_allow_assign_updated::create(array('context' => context_system::instance()));
  798. $sink = $this->redirectEvents();
  799. $allowroleassignevent->trigger();
  800. $events = $sink->get_events();
  801. $sink->close();
  802. $event = array_pop($events);
  803. $this->assertInstanceOf('\core\event\role_allow_assign_updated', $event);
  804. $mode = 'assign';
  805. $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
  806. $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
  807. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  808. }
  809. /**
  810. * Test allowing of role overrides.
  811. */
  812. public function test_allow_override() {
  813. global $DB, $CFG;
  814. $this->resetAfterTest();
  815. $otherid = create_role('Other role', 'other', 'Some other role', '');
  816. $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  817. $this->assertFalse($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
  818. allow_override($otherid, $student->id);
  819. $this->assertTrue($DB->record_exists('role_allow_override', array('roleid'=>$otherid, 'allowoverride'=>$student->id)));
  820. // Test event trigger.
  821. $allowroleassignevent = \core\event\role_allow_override_updated::create(array('context' => context_system::instance()));
  822. $sink = $this->redirectEvents();
  823. $allowroleassignevent->trigger();
  824. $events = $sink->get_events();
  825. $sink->close();
  826. $event = array_pop($events);
  827. $this->assertInstanceOf('\core\event\role_allow_override_updated', $event);
  828. $mode = 'override';
  829. $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
  830. $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
  831. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  832. }
  833. /**
  834. * Test allowing of role switching.
  835. */
  836. public function test_allow_switch() {
  837. global $DB, $CFG;
  838. $this->resetAfterTest();
  839. $otherid = create_role('Other role', 'other', 'Some other role', '');
  840. $student = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  841. $this->assertFalse($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
  842. allow_switch($otherid, $student->id);
  843. $this->assertTrue($DB->record_exists('role_allow_switch', array('roleid'=>$otherid, 'allowswitch'=>$student->id)));
  844. // Test event trigger.
  845. $allowroleassignevent = \core\event\role_allow_switch_updated::create(array('context' => context_system::instance()));
  846. $sink = $this->redirectEvents();
  847. $allowroleassignevent->trigger();
  848. $events = $sink->get_events();
  849. $sink->close();
  850. $event = array_pop($events);
  851. $this->assertInstanceOf('\core\event\role_allow_switch_updated', $event);
  852. $mode = 'switch';
  853. $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode));
  854. $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl));
  855. $this->assertEventLegacyLogData($expectedlegacylog, $event);
  856. }
  857. /**
  858. * Test returning of assignable roles in context.
  859. */
  860. public function test_get_assignable_roles() {
  861. global $DB;
  862. $this->resetAfterTest();
  863. $course = $this->getDataGenerator()->create_course();
  864. $coursecontext = context_course::instance($course->id);
  865. $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
  866. $teacher = $this->getDataGenerator()->create_user();
  867. role_assign($teacherrole->id, $teacher->id, $coursecontext);
  868. $teacherename = (object)array('roleid'=>$teacherrole->id, 'name'=>'Učitel', 'contextid'=>$coursecontext->id);
  869. $DB->insert_record('role_names', $teacherename);
  870. $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
  871. $student = $this->getDataGenerator()->create_user();
  872. role_assign($studentrole->id, $student->id, $coursecontext);
  873. $contexts = $DB->get_records('context');
  874. $users = $DB->get_records('user');
  875. $allroles = $DB->get_records('role');
  876. // Evaluate all results for all users in all contexts.
  877. foreach ($users as $user) {
  878. $this->setUser($user);
  879. foreach ($contexts as $contextid => $unused) {
  880. $context = context_helper::instance_by_id($contextid);
  881. $roles = get_assignable_roles($context, ROLENAME_SHORT);
  882. foreach ($allroles as $roleid => $role) {
  883. if (isset($roles[$roleid])) {
  884. if (is_siteadmin()) {
  885. $this->assertTrue($DB->record_exists('role_context_levels', array('contextlevel'=>$context->contextlevel, 'roleid'=>$roleid)));
  886. } else {
  887. $this->assertTrue(user_can_assign($context, $roleid), "u:$user->id r:$roleid");
  888. }
  889. $this->assertEquals($role->shortname, $roles[$roleid]);
  890. } else {

Large files files are truncated, but you can click here to view the full file