Proyectos de Subversion Moodle

Rev

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