Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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\output;
18
 
19
/**
20
 * Unit tests for the user_picture class.
21
 *
22
 * @package core
23
 * @copyright 2024 The Open University
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers \core\output\user_picture
26
 */
27
final class user_picture_test extends \advanced_testcase {
28
 
29
    /**
30
     * Assert appropriate debugging is emitted if required user fields are absent
31
     */
32
    public function test_constructor_missing_fields(): void {
33
        $user = get_admin();
34
        unset($user->picture);
35
 
36
        // Assert debugging notice when required field isn't present.
37
        $userpicture = new user_picture($user);
38
        $this->assertDebuggingCalled('Missing \'picture\' property in $user object, this is a performance problem that needs ' .
39
            'to be fixed by a developer. Please use the \core_user\fields API to get the full list of required fields.');
40
    }
41
 
42
    /**
43
     * Tests {@see user_picture::allow_view()} for a not-logged-in request.
44
     */
45
    public function test_allow_view_not_logged_in(): void {
46
        global $DB;
47
 
48
        $this->resetAfterTest();
49
 
50
        $adminid = $DB->get_field('user', 'id', ['username' => 'admin'], MUST_EXIST);
51
 
52
        // Default config allows user pictures when not logged in.
53
        $this->assertTrue(user_picture::allow_view($adminid));
54
 
55
        // Not allowed with either or both forcelogin options.
56
        set_config('forcelogin', 1);
57
        $this->assertFalse(user_picture::allow_view($adminid));
58
        set_config('forcelogin', 0);
59
        set_config('forceloginforprofileimage', 1);
60
        $this->assertFalse(user_picture::allow_view($adminid));
61
        set_config('forcelogin', 1);
62
        $this->assertFalse(user_picture::allow_view($adminid));
63
    }
64
 
65
    /**
66
     * Tests {@see user_picture::allow_view()} for a guest user request.
67
     */
68
    public function test_allow_view_guest(): void {
69
        global $DB;
70
 
71
        $this->resetAfterTest();
72
        $this->setGuestUser();
73
 
74
        $adminid = $DB->get_field('user', 'id', ['username' => 'admin'], MUST_EXIST);
75
 
76
        // Default config allows user pictures for guests.
77
        $this->assertTrue(user_picture::allow_view($adminid));
78
 
79
        // Not allowed with forceloginforprofileimage.
80
        set_config('forceloginforprofileimage', 1);
81
        $this->assertFalse(user_picture::allow_view($adminid));
82
 
83
        // Allowed by default with just forcelogin.
84
        set_config('forceloginforprofileimage', 0);
85
        set_config('forcelogin', 1);
86
        $this->assertTrue(user_picture::allow_view($adminid));
87
 
88
        // But would not be allowed if we change guest role to remove capability.
89
        $guestroleid = $DB->get_field('role', 'id', ['shortname' => 'guest'], MUST_EXIST);
90
        assign_capability('moodle/user:viewprofilepictures', CAP_INHERIT, $guestroleid,
91
            \context_system::instance()->id, true);
92
        $this->assertFalse(user_picture::allow_view($adminid));
93
    }
94
 
95
    /**
96
     * Tests {@see user_picture::allow_view()} for a logged in user.
97
     */
98
    public function test_allow_view_user(): void {
99
        global $DB;
100
 
101
        $this->resetAfterTest();
102
 
103
        $adminid = $DB->get_field('user', 'id', ['username' => 'admin'], MUST_EXIST);
104
 
105
        $generator = self::getDataGenerator();
106
        $user = $generator->create_user();
107
        $this->setUser($user);
108
 
109
        // Default config allows user pictures.
110
        $this->assertTrue(user_picture::allow_view($adminid));
111
 
112
        // Also allowed with either or both forcelogin option, because they are logged in.
113
        set_config('forcelogin', 1);
114
        $this->assertTrue(user_picture::allow_view($adminid));
115
        set_config('forcelogin', 0);
116
        set_config('forceloginforprofileimage', 1);
117
        $this->assertTrue(user_picture::allow_view($adminid));
118
        set_config('forcelogin', 1);
119
        $this->assertTrue(user_picture::allow_view($adminid));
120
 
121
        // But would not be allowed if we change user role to remove capability.
122
        $userroleid = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
123
        assign_capability('moodle/user:viewprofilepictures', CAP_INHERIT, $userroleid,
124
            \context_system::instance()->id, true);
125
        $this->assertFalse(user_picture::allow_view($adminid));
126
 
127
        // Except you are still allowed to view your own user picture.
128
        $this->assertTrue(user_picture::allow_view($user->id));
129
    }
130
}