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 |
*/
|
|
|
30 |
class profile_field_checkbox_test extends advanced_testcase {
|
|
|
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");
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Data provider for {@see test_is_empty}
|
|
|
42 |
*
|
|
|
43 |
* @return array[]
|
|
|
44 |
*/
|
|
|
45 |
public static function is_empty_provider(): array {
|
|
|
46 |
return [
|
|
|
47 |
'No value' => [
|
|
|
48 |
[],
|
|
|
49 |
true,
|
|
|
50 |
],
|
|
|
51 |
'Value equals 0' => [
|
|
|
52 |
['profile_field_check' => 0],
|
|
|
53 |
false,
|
|
|
54 |
],
|
|
|
55 |
'Value equals 1' => [
|
|
|
56 |
['profile_field_check' => 1],
|
|
|
57 |
false,
|
|
|
58 |
],
|
|
|
59 |
];
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Test field empty state
|
|
|
64 |
*
|
|
|
65 |
* @param array $userrecord
|
|
|
66 |
* @param bool $expected
|
|
|
67 |
*
|
|
|
68 |
* @dataProvider is_empty_provider
|
|
|
69 |
*/
|
|
|
70 |
public function test_is_empty(array $userrecord, bool $expected): void {
|
|
|
71 |
$this->resetAfterTest();
|
|
|
72 |
|
|
|
73 |
$this->getDataGenerator()->create_custom_profile_field([
|
|
|
74 |
'datatype' => 'checkbox',
|
|
|
75 |
'name' => 'My check',
|
|
|
76 |
'shortname' => 'check',
|
|
|
77 |
]);
|
|
|
78 |
|
|
|
79 |
$user = $this->getDataGenerator()->create_user($userrecord);
|
|
|
80 |
|
|
|
81 |
/** @var profile_field_checkbox[] $fields */
|
|
|
82 |
$fields = profile_get_user_fields_with_data($user->id);
|
|
|
83 |
$fieldinstance = reset($fields);
|
|
|
84 |
|
|
|
85 |
$this->assertEquals($expected, $fieldinstance->is_empty());
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Test whether to show field content
|
|
|
90 |
*/
|
|
|
91 |
public function test_show_field_content(): void {
|
|
|
92 |
$this->resetAfterTest();
|
|
|
93 |
|
|
|
94 |
$this->getDataGenerator()->create_custom_profile_field([
|
|
|
95 |
'datatype' => 'checkbox',
|
|
|
96 |
'name' => 'My check',
|
|
|
97 |
'shortname' => 'check',
|
|
|
98 |
'visible' => PROFILE_VISIBLE_PRIVATE,
|
|
|
99 |
]);
|
|
|
100 |
|
|
|
101 |
// User can view their own value.
|
|
|
102 |
$userwith = $this->getDataGenerator()->create_user(['profile_field_check' => 1]);
|
|
|
103 |
$this->setUser($userwith);
|
|
|
104 |
|
|
|
105 |
/** @var profile_field_checkbox[] $fields */
|
|
|
106 |
$fields = profile_get_user_fields_with_data($userwith->id);
|
|
|
107 |
$fieldinstance = reset($fields);
|
|
|
108 |
|
|
|
109 |
$this->assertTrue($fieldinstance->show_field_content());
|
|
|
110 |
|
|
|
111 |
// Another user cannot view the value.
|
|
|
112 |
$userview = $this->getDataGenerator()->create_user();
|
|
|
113 |
$this->setUser($userview);
|
|
|
114 |
|
|
|
115 |
$this->assertFalse($fieldinstance->show_field_content());
|
|
|
116 |
|
|
|
117 |
// Another user with appropriate access can view the value.
|
|
|
118 |
$this->setAdminUser();
|
|
|
119 |
$this->assertTrue($fieldinstance->show_field_content());
|
|
|
120 |
|
|
|
121 |
}
|
|
|
122 |
}
|