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_cohort\customfield;
18
 
19
use advanced_testcase;
20
use context_system;
21
use context_coursecat;
22
use moodle_url;
23
use core_customfield\field_controller;
24
 
25
/**
26
 * Unit tests for cohort custom field handler.
27
 *
28
 * @package    core_cohort
29
 * @covers     \core_cohort\customfield\cohort_handler
30
 * @copyright  2022 Dmitrii Metelkin <dmitriim@catalys-au.net>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class cohort_handler_test extends advanced_testcase {
1 efrain 34
    /**
35
     * Test custom field handler.
36
     * @var \core_customfield\handler
37
     */
38
    protected $handler;
39
 
40
    /**
41
     * Setup.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $this->handler = cohort_handler::create();
46
    }
47
 
48
    /**
49
     * Create Cohort custom field for testing.
50
     *
51
     * @return field_controller
52
     */
53
    protected function create_cohort_custom_field(): field_controller {
54
        $fieldcategory = self::getDataGenerator()->create_custom_field_category([
55
            'component' => 'core_cohort',
56
            'area' => 'cohort',
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('/cohort/customfield.php', $this->handler->get_configuration_url()->out_as_local_url());
79
    }
80
 
81
    /**
82
     * Test can configure check.
83
     */
11 efrain 84
    public function test_can_configure(): void {
1 efrain 85
        $this->resetAfterTest();
86
 
87
        $user = self::getDataGenerator()->create_user();
88
        self::setUser($user);
89
 
90
        $this->assertFalse($this->handler->can_configure());
91
 
92
        $roleid = self::getDataGenerator()->create_role();
93
        assign_capability('moodle/cohort:configurecustomfields', CAP_ALLOW, $roleid, context_system::instance()->id, true);
94
        role_assign($roleid, $user->id, context_system::instance()->id);
95
 
96
        $this->assertTrue($this->handler->can_configure());
97
    }
98
 
99
    /**
100
     * Test getting instance context.
101
     */
11 efrain 102
    public function test_get_instance_context(): void {
1 efrain 103
        $this->resetAfterTest();
104
 
105
        $category = self::getDataGenerator()->create_category();
106
        $catcontext = context_coursecat::instance($category->id);
107
        $systemcontext = context_system::instance();
108
        $cohortsystem = self::getDataGenerator()->create_cohort();
109
        $cohortcategory = self::getDataGenerator()->create_cohort(['contextid' => $catcontext->id]);
110
 
111
        $this->assertInstanceOf(context_system::class, $this->handler->get_instance_context($cohortsystem->id));
112
        $this->assertSame($systemcontext, $this->handler->get_instance_context($cohortsystem->id));
113
 
114
        $this->assertInstanceOf(context_coursecat::class, $this->handler->get_instance_context($cohortcategory->id));
115
        $this->assertSame($catcontext, $this->handler->get_instance_context($cohortcategory->id));
116
    }
117
 
118
    /**
119
     * Test can edit functionality.
120
     */
11 efrain 121
    public function test_can_edit(): void {
1 efrain 122
        $this->resetAfterTest();
123
 
124
        $roleid = self::getDataGenerator()->create_role();
125
        assign_capability('moodle/cohort:manage', CAP_ALLOW, $roleid, context_system::instance()->id, true);
126
 
127
        $field = $this->create_cohort_custom_field();
128
 
129
        $user = self::getDataGenerator()->create_user();
130
        self::setUser($user);
131
 
132
        $this->assertFalse($this->handler->can_edit($field, 0));
133
 
134
        role_assign($roleid, $user->id, context_system::instance()->id);
135
        $this->assertTrue($this->handler->can_edit($field, 0));
136
    }
137
 
138
    /**
139
     * Test can view functionality.
140
     */
11 efrain 141
    public function test_can_view(): void {
1 efrain 142
        $this->resetAfterTest();
143
 
144
        $manageroleid = self::getDataGenerator()->create_role();
145
        assign_capability('moodle/cohort:manage', CAP_ALLOW, $manageroleid, context_system::instance()->id, true);
146
 
147
        $viewroleid = self::getDataGenerator()->create_role();
148
        assign_capability('moodle/cohort:view', CAP_ALLOW, $viewroleid, context_system::instance()->id, true);
149
 
150
        $viewandmanageroleid = self::getDataGenerator()->create_role();
151
        assign_capability('moodle/cohort:manage', CAP_ALLOW, $viewandmanageroleid, context_system::instance()->id, true);
152
        assign_capability('moodle/cohort:view', CAP_ALLOW, $viewandmanageroleid, context_system::instance()->id, true);
153
 
154
        $field = $this->create_cohort_custom_field();
155
        $cohort = self::getDataGenerator()->create_cohort();
156
 
157
        $user1 = self::getDataGenerator()->create_user();
158
        $user2 = self::getDataGenerator()->create_user();
159
        $user3 = self::getDataGenerator()->create_user();
160
 
161
        self::setUser($user1);
162
        $this->assertFalse($this->handler->can_view($field, $cohort->id));
163
 
164
        self::setUser($user2);
165
        $this->assertFalse($this->handler->can_view($field, $cohort->id));
166
 
167
        self::setUser($user3);
168
        $this->assertFalse($this->handler->can_view($field, $cohort->id));
169
 
170
        role_assign($manageroleid, $user1->id, context_system::instance()->id);
171
        role_assign($viewroleid, $user2->id, context_system::instance()->id);
172
        role_assign($viewandmanageroleid, $user3->id, context_system::instance()->id);
173
 
174
        self::setUser($user1);
175
        $this->assertTrue($this->handler->can_view($field, $cohort->id));
176
 
177
        self::setUser($user2);
178
        $this->assertTrue($this->handler->can_view($field, $cohort->id));
179
 
180
        self::setUser($user3);
181
        $this->assertTrue($this->handler->can_view($field, $cohort->id));
182
    }
183
}