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