Merge branch 'MDL-80426-main-2' of https://github.com/rezaies/moodle
[moodle.git] / user / tests / userroleseditable_test.php
blob04f75e254101bf151edb2e54b540ebb32f637e27
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.
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/>.
17 namespace core_user;
19 /**
20 * Unit tests for user roles editable class.
22 * @package core_user
23 * @copyright 2017 Damyon Wiese
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class userroleseditable_test extends \advanced_testcase {
27 /**
28 * Test user roles editable.
30 public function test_update() {
31 global $DB;
33 $this->resetAfterTest();
35 // Create user and modify user profile.
36 $user1 = $this->getDataGenerator()->create_user();
37 $user2 = $this->getDataGenerator()->create_user();
39 $course1 = $this->getDataGenerator()->create_course();
40 $coursecontext = \context_course::instance($course1->id);
41 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
42 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
43 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
44 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
45 role_assign($teacherrole->id, $user1->id, $coursecontext->id);
46 role_assign($teacherrole->id, $user2->id, $coursecontext->id);
48 $this->setAdminUser();
49 accesslib_clear_all_caches_for_unit_testing();
51 // Use the userroleseditable api to remove all roles from user1 and give user2 student and teacher.
52 $itemid = $course1->id . ':' . $user1->id;
53 $newvalue = json_encode([]);
55 $result = \core_user\output\user_roles_editable::update($itemid, $newvalue);
56 $this->assertTrue($result instanceof \core_user\output\user_roles_editable);
58 $currentroles = get_user_roles_in_course($user1->id, $course1->id);
60 $this->assertEmpty($currentroles);
62 $this->setAdminUser();
63 accesslib_clear_all_caches_for_unit_testing();
65 $itemid = $course1->id . ':' . $user2->id;
66 $newvalue = json_encode([$teacherrole->id, $studentrole->id]);
68 $result = \core_user\output\user_roles_editable::update($itemid, $newvalue);
69 $this->assertTrue($result instanceof \core_user\output\user_roles_editable);
70 $currentroles = get_user_roles_in_course($user2->id, $course1->id);
72 $this->assertStringContainsString('Non-editing teacher', $currentroles);
73 $this->assertStringContainsString('Student', $currentroles);