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_group\customfield;
18
 
19
use advanced_testcase;
20
use context_course;
21
use context_system;
22
use moodle_url;
23
use core_customfield\field_controller;
24
 
25
/**
26
 * Unit tests for group custom field handler.
27
 *
28
 * @package   core_group
29
 * @covers    \core_group\customfield\group_handler
30
 * @author    Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
31
 * @copyright 2023 Catalyst IT Pty Ltd
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class group_handler_test extends advanced_testcase {
35
    /**
36
     * Test custom field handler.
37
     * @var group_handler
38
     */
39
    protected $handler;
40
 
41
    /**
42
     * Setup.
43
     */
44
    public function setUp(): void {
45
        $this->handler = group_handler::create();
46
    }
47
 
48
    /**
49
     * Create group custom field for testing.
50
     *
51
     * @return field_controller
52
     */
53
    protected function create_group_custom_field(): field_controller {
54
        $fieldcategory = self::getDataGenerator()->create_custom_field_category([
55
            'component' => 'core_group',
56
            'area' => 'group',
57
        ]);
58
 
59
        return self::getDataGenerator()->create_custom_field([
60
            'shortname' => 'testfield1',
61
            'type' => 'text',
62
            'categoryid' => $fieldcategory->get('id'),
63
        ]);
64
    }
65
 
66
    /**
67
     * Test configuration context.
68
     */
11 efrain 69
    public function test_get_configuration_context(): void {
1 efrain 70
        $this->assertInstanceOf(context_system::class, $this->handler->get_configuration_context());
71
    }
72
 
73
    /**
74
     * Test getting config URL.
75
     */
11 efrain 76
    public function test_get_configuration_url(): void {
1 efrain 77
        $this->assertInstanceOf(moodle_url::class, $this->handler->get_configuration_url());
78
        $this->assertEquals('/group/customfield.php', $this->handler->get_configuration_url()->out_as_local_url());
79
    }
80
 
81
    /**
82
     * Test getting instance context.
83
     */
11 efrain 84
    public function test_get_instance_context(): void {
1 efrain 85
        global $COURSE;
86
        $this->resetAfterTest();
87
 
88
        $course = self::getDataGenerator()->create_course();
89
        $group = self::getDataGenerator()->create_group(['courseid' => $course->id]);
90
 
91
        $this->assertInstanceOf(context_course::class, $this->handler->get_instance_context());
92
        $this->assertSame(context_course::instance($COURSE->id), $this->handler->get_instance_context());
93
 
94
        $this->assertInstanceOf(context_course::class, $this->handler->get_instance_context($group->id));
95
        $this->assertSame(context_course::instance($course->id), $this->handler->get_instance_context($group->id));
96
    }
97
 
98
    /**
99
     * Test can configure check.
100
     */
11 efrain 101
    public function test_can_configure(): void {
1 efrain 102
        $this->resetAfterTest();
103
 
104
        $user = self::getDataGenerator()->create_user();
105
        self::setUser($user);
106
 
107
        $this->assertFalse($this->handler->can_configure());
108
 
109
        $roleid = self::getDataGenerator()->create_role();
110
        assign_capability('moodle/group:configurecustomfields', CAP_ALLOW, $roleid, context_system::instance()->id, true);
111
        role_assign($roleid, $user->id, context_system::instance()->id);
112
 
113
        $this->assertTrue($this->handler->can_configure());
114
    }
115
 
116
    /**
117
     * Test can edit functionality.
118
     */
11 efrain 119
    public function test_can_edit(): void {
1 efrain 120
        $this->resetAfterTest();
121
 
122
        $course = self::getDataGenerator()->create_course();
123
        $contextid = context_course::instance($course->id)->id;
124
        $group = self::getDataGenerator()->create_group(['courseid' => $course->id]);
125
        $roleid = self::getDataGenerator()->create_role();
126
        assign_capability('moodle/course:managegroups', CAP_ALLOW, $roleid, $contextid, true);
127
 
128
        $field = $this->create_group_custom_field();
129
 
130
        $user = self::getDataGenerator()->create_user();
131
        self::setUser($user);
132
 
133
        $this->assertFalse($this->handler->can_edit($field, $group->id));
134
 
135
        role_assign($roleid, $user->id, $contextid);
136
        $this->assertTrue($this->handler->can_edit($field, $group->id));
137
    }
138
 
139
    /**
140
     * Test can view functionality.
141
     */
11 efrain 142
    public function test_can_view(): void {
1 efrain 143
        $this->resetAfterTest();
144
 
145
        $course = self::getDataGenerator()->create_course();
146
        $contextid = context_course::instance($course->id)->id;
147
        $group = self::getDataGenerator()->create_group(['courseid' => $course->id]);
148
        $manageroleid = self::getDataGenerator()->create_role();
149
        assign_capability('moodle/course:managegroups', CAP_ALLOW, $manageroleid, $contextid, true);
150
 
151
        $viewroleid = self::getDataGenerator()->create_role();
152
        assign_capability('moodle/course:view', CAP_ALLOW, $viewroleid, $contextid, true);
153
 
154
        $viewandmanageroleid = self::getDataGenerator()->create_role();
155
        assign_capability('moodle/course:managegroups', CAP_ALLOW, $viewandmanageroleid, $contextid, true);
156
        assign_capability('moodle/course:view', CAP_ALLOW, $viewandmanageroleid, $contextid, true);
157
 
158
        $field = $this->create_group_custom_field();
159
 
160
        $user1 = self::getDataGenerator()->create_user();
161
        $user2 = self::getDataGenerator()->create_user();
162
        $user3 = self::getDataGenerator()->create_user();
163
 
164
        self::setUser($user1);
165
        $this->assertFalse($this->handler->can_view($field, $group->id));
166
 
167
        self::setUser($user2);
168
        $this->assertFalse($this->handler->can_view($field, $group->id));
169
 
170
        self::setUser($user3);
171
        $this->assertFalse($this->handler->can_view($field, $group->id));
172
 
173
        role_assign($manageroleid, $user1->id, $contextid);
174
        role_assign($viewroleid, $user2->id, $contextid);
175
        role_assign($viewandmanageroleid, $user3->id, $contextid);
176
 
177
        self::setUser($user1);
178
        $this->assertTrue($this->handler->can_view($field, $group->id));
179
 
180
        self::setUser($user2);
181
        $this->assertTrue($this->handler->can_view($field, $group->id));
182
 
183
        self::setUser($user3);
184
        $this->assertTrue($this->handler->can_view($field, $group->id));
185
    }
186
}