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;
18
 
19
/**
20
 * Tests for myprofilelib apis.
21
 *
22
 * @package    core
23
 * @copyright  2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
25
 */
26
class myprofilelib_test extends \advanced_testcase {
27
 
28
    /**
29
     * @var stdClass The user.
30
     */
31
    private $user;
32
 
33
    /**
34
     * @var stdClass The course.
35
     */
36
    private $course;
37
 
38
    /**
39
     * @var \core_user\output\myprofile\tree The navigation tree.
40
     */
41
    private $tree;
42
 
43
    /**
44
     * Load required test libraries
45
     */
46
    public static function setUpBeforeClass(): void {
47
        global $CFG;
48
        require_once($CFG->dirroot . '/lib/myprofilelib.php');
49
        require_once($CFG->dirroot . '/user/profile/lib.php');
50
    }
51
 
52
    public function setUp(): void {
53
        // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain.
54
        global $PAGE;
55
        $PAGE->set_url('/test');
56
 
57
        $this->user = $this->getDataGenerator()->create_user();
58
        $this->course = $this->getDataGenerator()->create_course();
59
        $this->tree = new \core_user\output\myprofile\tree();
60
        $this->resetAfterTest();
61
    }
62
 
63
    /**
64
     * Tests the core_myprofile_navigation() function as an admin viewing a user's course profile.
65
     */
11 efrain 66
    public function test_core_myprofile_navigation_as_admin(): void {
1 efrain 67
        $this->setAdminUser();
68
        $iscurrentuser = false;
69
 
70
        // Test tree as admin user.
71
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
72
        $reflector = new \ReflectionObject($this->tree);
73
        $categories = $reflector->getProperty('categories');
74
        $cats = $categories->getValue($this->tree);
75
        $this->assertArrayHasKey('contact', $cats);
76
        $this->assertArrayHasKey('coursedetails', $cats);
77
        $this->assertArrayHasKey('miscellaneous', $cats);
78
        $this->assertArrayHasKey('reports', $cats);
79
        $this->assertArrayHasKey('administration', $cats);
80
        $this->assertArrayHasKey('loginactivity', $cats);
81
 
82
        $nodes = $reflector->getProperty('nodes');
83
        $this->assertArrayHasKey('fullprofile', $nodes->getValue($this->tree));
84
    }
85
 
86
    /**
87
     * Tests the core_myprofile_navigation() function as a user without permission to view the full
88
     * profile of another another user.
89
     */
11 efrain 90
    public function test_core_myprofile_navigation_course_without_permission(): void {
1 efrain 91
        // User without permission.
92
        $this->setUser($this->getDataGenerator()->create_user());
93
        $iscurrentuser = false;
94
 
95
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
96
        $reflector = new \ReflectionObject($this->tree);
97
        $nodes = $reflector->getProperty('nodes');
98
        $this->assertArrayNotHasKey('fullprofile', $nodes->getValue($this->tree));
99
    }
100
 
101
    /**
102
     * Tests the core_myprofile_navigation() function as the currently logged in user.
103
     */
11 efrain 104
    public function test_core_myprofile_navigation_profile_link_as_current_user(): void {
1 efrain 105
        $this->setUser($this->user);
106
        $iscurrentuser = true;
107
 
108
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
109
        $reflector = new \ReflectionObject($this->tree);
110
        $nodes = $reflector->getProperty('nodes');
111
        $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
112
    }
113
 
114
    /**
115
     * Tests the core_myprofile_navigation() function as the admin viewing another user.
116
     */
11 efrain 117
    public function test_core_myprofile_navigation_profile_link_as_admin(): void {
1 efrain 118
        $this->setAdminUser();
119
        $iscurrentuser = false;
120
 
121
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
122
        $reflector = new \ReflectionObject($this->tree);
123
        $nodes = $reflector->getProperty('nodes');
124
        $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree));
125
    }
126
 
127
    /**
128
     * Tests the core_myprofile_navigation() function when viewing the preference page as an admin.
129
     */
11 efrain 130
    public function test_core_myprofile_navigation_preference_as_admin(): void {
1 efrain 131
        $this->setAdminUser();
132
        $iscurrentuser = false;
133
 
134
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
135
        $reflector = new \ReflectionObject($this->tree);
136
        $nodes = $reflector->getProperty('nodes');
137
        $this->assertArrayHasKey('preferences', $nodes->getValue($this->tree));
138
        $this->assertArrayHasKey('loginas', $nodes->getValue($this->tree));
139
    }
140
 
141
    /**
142
     * Tests the core_myprofile_navigation() function when viewing the preference
143
     * page as another user without the ability to use the 'loginas' functionality.
144
     */
11 efrain 145
    public function test_core_myprofile_navigation_preference_without_permission(): void {
1 efrain 146
        // Login as link for a user who doesn't have the capability to login as.
147
        $this->setUser($this->getDataGenerator()->create_user());
148
        $iscurrentuser = false;
149
 
150
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
151
        $reflector = new \ReflectionObject($this->tree);
152
        $nodes = $reflector->getProperty('nodes');
153
        $this->assertArrayNotHasKey('loginas', $nodes->getValue($this->tree));
154
    }
155
 
156
    /**
157
     * Tests the core_myprofile_navigation() function as an admin viewing another user's contact details.
158
     */
11 efrain 159
    public function test_core_myprofile_navigation_contact_fields_as_admin(): void {
1 efrain 160
        global $CFG;
161
 
162
        // User contact fields.
163
        set_config("hiddenuserfields", "country,city");
164
        set_config("showuseridentity", "email,address,phone1,phone2,institution,department,idnumber");
165
        $hiddenfields = explode(',', $CFG->hiddenuserfields);
166
        $identityfields = explode(',', $CFG->showuseridentity);
167
        $this->setAdminUser();
168
        $iscurrentuser = false;
169
 
170
        // Make sure fields are not empty.
171
        $fields = array(
172
            'country' => 'AU',
173
            'city' => 'Silent hill',
174
            'email' => 'Rulelikeaboss@example.com',
175
            'address' => 'Didn\'t I mention silent hill already ?',
176
            'phone1' => '123',
177
            'phone2' => '234',
178
            'institution' => 'strange land',
179
            'department' => 'video game/movie',
180
            'idnumber' => 'SLHL'
181
        );
182
        foreach ($fields as $field => $value) {
183
            $this->user->$field = $value;
184
        }
185
 
186
        // User with proper permissions.
187
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
188
        $reflector = new \ReflectionObject($this->tree);
189
        $nodes = $reflector->getProperty('nodes');
190
        foreach ($hiddenfields as $field) {
191
            $this->assertArrayHasKey($field, $nodes->getValue($this->tree));
192
        }
193
        foreach ($identityfields as $field) {
194
            $this->assertArrayHasKey($field, $nodes->getValue($this->tree));
195
        }
196
    }
197
 
198
    /**
199
     * Tests the core_myprofile_navigation() function as a user viewing another user's profile
200
     * ensuring that the contact details are not shown.
201
     */
11 efrain 202
    public function test_core_myprofile_navigation_contact_field_without_permission(): void {
1 efrain 203
        global $CFG;
204
 
205
        $iscurrentuser = false;
206
        $hiddenfields = explode(',', $CFG->hiddenuserfields);
207
        $identityfields = explode(',', $CFG->showuseridentity);
208
 
209
        // User without permission.
210
        $this->setUser($this->getDataGenerator()->create_user());
211
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
212
        $reflector = new \ReflectionObject($this->tree);
213
        $nodes = $reflector->getProperty('nodes');
214
        foreach ($hiddenfields as $field) {
215
            $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree));
216
        }
217
        foreach ($identityfields as $field) {
218
            $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree));
219
        }
220
    }
221
 
222
    /**
223
     * Data provider for {@see test_core_myprofile_navigation_contact_timezone}
224
     *
225
     * @return array[]
226
     */
227
    public function core_myprofile_navigation_contact_timezone_provider(): array {
228
        return [
229
            'Hidden field' => ['timezone', '99', '99', null],
230
            'Forced timezone' => ['', 'Europe/London', 'Pacific/Tahiti', 'Europe/London'],
231
            'User timezone (default)' => ['', '99', '99', 'Australia/Perth'],
232
            'User timezone (selected)' => ['', '99', 'Pacific/Tahiti', 'Pacific/Tahiti'],
233
        ];
234
    }
235
 
236
    /**
237
     * Test timezone node added to user profile navigation
238
     *
239
     * @param string $hiddenuserfields
240
     * @param string $forcetimezone Timezone identifier or '99' (User can choose their own)
241
     * @param string $usertimezone Timezone identifier or '99' (Use server default)
242
     * @param string|null $expectresult
243
     * @return bool
244
     *
245
     * @dataProvider core_myprofile_navigation_contact_timezone_provider
246
     */
247
    public function test_core_myprofile_navigation_contact_timezone(string $hiddenuserfields, string $forcetimezone,
248
            string $usertimezone, ?string $expectresult = null): void {
249
 
250
        set_config('hiddenuserfields', $hiddenuserfields);
251
        set_config('forcetimezone', $forcetimezone);
252
 
253
        // Set the timezone of our test user, and load their navigation tree.
254
        $this->user->timezone = $usertimezone;
255
        $this->setUser($this->user);
256
 
257
        core_myprofile_navigation($this->tree, $this->user, true, null);
258
 
259
        $reflector = new \ReflectionObject($this->tree);
260
        $nodes = $reflector->getProperty('nodes');
261
 
262
        /** @var \core_user\output\myprofile\node[] $tree */
263
        $tree = $nodes->getValue($this->tree);
264
        if ($expectresult !== null) {
265
            $this->assertArrayHasKey('timezone', $tree);
266
            $this->assertEquals($expectresult, $tree['timezone']->content);
267
        } else {
268
            $this->assertArrayNotHasKey('timezone', $tree);
269
        }
270
    }
271
 
272
    /**
273
     * Tests the core_myprofile_navigation() function as an admin viewing another user's
274
     * profile ensuring the login activity links are shown.
275
     */
11 efrain 276
    public function test_core_myprofile_navigation_login_activity(): void {
1 efrain 277
        // First access, last access, last ip.
278
        $this->setAdminUser();
279
        $iscurrentuser = false;
280
 
281
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
282
        $reflector = new \ReflectionObject($this->tree);
283
        $nodes = $reflector->getProperty('nodes');
284
        $this->assertArrayHasKey('firstaccess', $nodes->getValue($this->tree));
285
        $this->assertArrayHasKey('lastaccess', $nodes->getValue($this->tree));
286
        $this->assertArrayHasKey('lastip', $nodes->getValue($this->tree));
287
    }
288
 
289
    /**
290
     * Tests the core_myprofile_navigation() function as a user viewing another user's profile
291
     * ensuring the login activity links are not shown.
292
     */
11 efrain 293
    public function test_core_myprofile_navigationn_login_activity_without_permission(): void {
1 efrain 294
        // User without permission.
295
        set_config("hiddenuserfields", "firstaccess,lastaccess,lastip");
296
        $this->setUser($this->getDataGenerator()->create_user());
297
        $iscurrentuser = false;
298
 
299
        core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null);
300
        $reflector = new \ReflectionObject($this->tree);
301
        $nodes = $reflector->getProperty('nodes');
302
        $this->assertArrayNotHasKey('firstaccess', $nodes->getValue($this->tree));
303
        $this->assertArrayNotHasKey('lastaccess', $nodes->getValue($this->tree));
304
        $this->assertArrayNotHasKey('lastip', $nodes->getValue($this->tree));
305
    }
306
}