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 profilefield_checkbox;
18
 
19
use advanced_testcase;
20
use profile_field_checkbox;
21
 
22
/**
23
 * Unit tests for the field class
24
 *
25
 * @package     profilefield_checkbox
26
 * @covers      \profile_field_checkbox
27
 * @copyright   2024 Paul Holden <paulh@moodle.com>
28
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
1441 ariadna 30
final class profile_field_checkbox_test extends advanced_testcase {
1 efrain 31
 
32
    /**
33
     * Load required test libraries
34
     */
35
    public static function setUpBeforeClass(): void {
36
        global $CFG;
37
        require_once("{$CFG->dirroot}/user/profile/lib.php");
1441 ariadna 38
        parent::setUpBeforeClass();
1 efrain 39
    }
40
 
41
    /**
42
     * Data provider for {@see test_is_empty}
43
     *
44
     * @return array[]
45
     */
46
    public static function is_empty_provider(): array {
47
        return [
48
            'No value' => [
49
                [],
50
                true,
51
            ],
52
            'Value equals 0' => [
53
                ['profile_field_check' => 0],
54
                false,
55
            ],
56
            'Value equals 1' => [
57
                ['profile_field_check' => 1],
58
                false,
59
            ],
60
        ];
61
    }
62
 
63
    /**
64
     * Test field empty state
65
     *
66
     * @param array $userrecord
67
     * @param bool $expected
68
     *
69
     * @dataProvider is_empty_provider
70
     */
71
    public function test_is_empty(array $userrecord, bool $expected): void {
72
        $this->resetAfterTest();
73
 
74
        $this->getDataGenerator()->create_custom_profile_field([
75
            'datatype' => 'checkbox',
76
            'name' => 'My check',
77
            'shortname' => 'check',
78
        ]);
79
 
80
        $user = $this->getDataGenerator()->create_user($userrecord);
81
 
82
        /** @var profile_field_checkbox[] $fields */
83
        $fields = profile_get_user_fields_with_data($user->id);
84
        $fieldinstance = reset($fields);
85
 
86
        $this->assertEquals($expected, $fieldinstance->is_empty());
87
    }
88
 
89
    /**
90
     * Test whether to show field content
91
     */
92
    public function test_show_field_content(): void {
93
        $this->resetAfterTest();
94
 
95
        $this->getDataGenerator()->create_custom_profile_field([
96
            'datatype' => 'checkbox',
97
            'name' => 'My check',
98
            'shortname' => 'check',
99
            'visible' => PROFILE_VISIBLE_PRIVATE,
100
        ]);
101
 
102
        // User can view their own value.
103
        $userwith = $this->getDataGenerator()->create_user(['profile_field_check' => 1]);
104
        $this->setUser($userwith);
105
 
106
        /** @var profile_field_checkbox[] $fields */
107
        $fields = profile_get_user_fields_with_data($userwith->id);
108
        $fieldinstance = reset($fields);
109
 
110
        $this->assertTrue($fieldinstance->show_field_content());
111
 
112
        // Another user cannot view the value.
113
        $userview = $this->getDataGenerator()->create_user();
114
        $this->setUser($userview);
115
 
116
        $this->assertFalse($fieldinstance->show_field_content());
117
 
118
        // Another user with appropriate access can view the value.
119
        $this->setAdminUser();
120
        $this->assertTrue($fieldinstance->show_field_content());
121
 
122
    }
123
}