Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core\moodlenet;
18
 
19
use context_course;
20
use stdClass;
21
use testing_data_generator;
22
 
23
/**
24
 * Unit tests for {@see utilities}.
25
 *
26
 * @coversDefaultClass \core\moodlenet\utilities
27
 * @package core
28
 * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class utilities_test extends \advanced_testcase {
32
 
33
    /** @var testing_data_generator Data generator. */
34
    private testing_data_generator $generator;
35
 
36
    /** @var stdClass Activity object, */
37
    private stdClass $course;
38
 
39
    /** @var context_course Course context instance. */
40
    private context_course $coursecontext;
41
 
42
    /**
43
     * Set up function for tests.
44
     */
45
    protected function setUp(): void {
46
        parent::setUp();
47
 
48
        $this->resetAfterTest();
49
        $this->generator = $this->getDataGenerator();
50
        $this->course = $this->generator->create_course();
51
        $this->coursecontext = context_course::instance($this->course->id);
52
    }
53
 
54
    /**
55
     * Test is_valid_instance method.
56
     *
57
     * @covers ::is_valid_instance
58
     * @return void
59
     */
11 efrain 60
    public function test_is_valid_instance(): void {
1 efrain 61
        global $CFG;
62
        $this->setAdminUser();
63
 
64
        // Create dummy issuer.
65
        $issuer = new \core\oauth2\issuer(0);
66
        $issuer->set('enabled', 0);
67
        $issuer->set('servicetype', 'google');
68
 
69
        // Can not share if the experimental flag it set to false.
70
        $CFG->enablesharingtomoodlenet = false;
71
        $this->assertFalse(utilities::is_valid_instance($issuer));
72
 
73
        // Enable the experimental flag.
74
        $CFG->enablesharingtomoodlenet = true;
75
 
76
        // Can not share if the OAuth 2 service in the outbound setting is not matched the given one.
77
        set_config('oauthservice', random_int(1, 30), 'moodlenet');
78
        $this->assertFalse(utilities::is_valid_instance($issuer));
79
 
80
        // Can not share if the OAuth 2 service in the outbound setting is not enabled.
81
        set_config('oauthservice', $issuer->get('id'), 'moodlenet');
82
        $this->assertFalse(utilities::is_valid_instance($issuer));
83
 
84
        // Can not share if the OAuth 2 service type is not moodlenet.
85
        $issuer->set('enabled', 1);
86
        $this->assertFalse(utilities::is_valid_instance($issuer));
87
 
88
        // All good now.
89
        $issuer->set('servicetype', 'moodlenet');
90
        $this->assertTrue(utilities::is_valid_instance($issuer));
91
    }
92
 
93
 
94
    /**
95
     * Test can_user_share method.
96
     *
97
     * @covers ::can_user_share
98
     * @return void
99
     */
11 efrain 100
    public function test_can_user_share(): void {
1 efrain 101
        global $DB;
102
 
103
        // Generate data.
104
        $student1 = $this->generator->create_user();
105
        $teacher1 = $this->generator->create_user();
106
        $teacher2 = $this->generator->create_user();
107
        $manager1 = $this->generator->create_user();
108
 
109
        // Enrol users.
110
        $this->generator->enrol_user($student1->id, $this->course->id, 'student');
111
        $this->generator->enrol_user($teacher1->id, $this->course->id, 'teacher');
112
        $this->generator->enrol_user($teacher2->id, $this->course->id, 'editingteacher');
113
        $this->generator->enrol_user($manager1->id, $this->course->id, 'manager');
114
 
115
        // Get roles.
116
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
117
        $editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher'], 'id', MUST_EXIST);
118
 
119
        // Test with default settings.
120
        // Student and Teacher cannot share the activity.
121
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $student1->id));
122
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher1->id));
123
        // Editing-teacher and Manager can share the activity.
124
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher2->id));
125
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $manager1->id));
126
 
127
        // Teacher who has the capabilities can share the activity.
128
        assign_capability('moodle/moodlenet:shareactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
129
        assign_capability('moodle/backup:backupactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
130
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher1->id));
131
 
132
        // Editing-teacher who does not have the capabilities can not share the activity.
133
        assign_capability('moodle/moodlenet:shareactivity', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
134
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id));
135
 
136
        // Test with default settings for course.
137
        // Student and Teacher cannot share the course.
138
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $student1->id, 'course'));
139
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher1->id, 'course'));
140
        // Editing-teacher and Manager can share the course.
141
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher2->id, 'course'));
142
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $manager1->id, 'course'));
143
 
144
        // Teacher who has the capabilities can share the course.
145
        assign_capability('moodle/moodlenet:sharecourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
146
        assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
147
        $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher1->id, 'course'));
148
 
149
        // Editing-teacher who does not have the capabilities can not share the course.
150
        assign_capability('moodle/moodlenet:sharecourse', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
151
        $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id, 'course'));
152
    }
153
 
154
    /**
155
     * Test does_user_have_capability_in_any_course method.
156
     *
157
     * @covers ::does_user_have_capability_in_any_course
158
     * @return void
159
     */
11 efrain 160
    public function test_does_user_have_capability_in_any_course(): void {
1 efrain 161
        global $DB;
162
 
163
        // Prepare data.
164
        $teacher1 = $this->generator->create_user();
165
        $student1 = $this->generator->create_user();
166
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
167
        $studentrole = $DB->get_record('role', ['shortname' => 'student'], 'id', MUST_EXIST);
168
 
169
        // Enrol users,
170
        $this->generator->enrol_user($teacher1->id, $this->course->id, $teacherrole->id);
171
        $this->generator->enrol_user($student1->id, $this->course->id, $studentrole->id);
172
 
173
        // Assign a valid capability to the teacher.
174
        assign_capability('moodle/moodlenet:shareactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
175
 
176
        // Check the method's results are as expected (this is cached).
177
        $this->assertSame('yes', utilities::does_user_have_capability_in_any_course($teacher1->id));
178
        $this->assertSame('no', utilities::does_user_have_capability_in_any_course($student1->id));
179
 
180
        // Compare to cache.
181
        $teachercachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($teacher1->id);
182
        $this->assertSame('yes', $teachercachedvalue);
183
        $studentcachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($student1->id);
184
        $this->assertSame('no', $studentcachedvalue);
185
 
186
        // Change the teacher's role and check the moodlenet_usercanshare cache is invalidated for everyone.
187
        $this->generator->role_assign($studentrole->id, $teacher1->id, $this->coursecontext->id);
188
        $teachercachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($teacher1->id);
189
        $this->assertFalse($teachercachedvalue);
190
        $studentcachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($student1->id);
191
        $this->assertFalse($studentcachedvalue);
192
    }
193
}