Skip to content

Commit 2e4ef85

Browse files
committed
Add tests for user_profile condition
1 parent 5b58a1e commit 2e4ef85

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition;
18+
19+
use tool_dynamic_cohorts\condition_base;
20+
21+
/**
22+
* Unit tests for user profile condition class.
23+
*
24+
* @package tool_dynamic_cohorts
25+
* @copyright 2024 Catalyst IT
26+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*
28+
* @covers \tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_profile
29+
*/
30+
class user_profile_test extends \advanced_testcase {
31+
32+
/**
33+
* Get condition instance for testing.
34+
*
35+
* @param array $configdata Config data to be set.
36+
* @return condition_base
37+
*/
38+
protected function get_condition(array $configdata = []): condition_base {
39+
$condition = condition_base::get_instance(0, (object)[
40+
'classname' => '\tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_profile',
41+
]);
42+
$condition->set_config_data($configdata);
43+
44+
return $condition;
45+
}
46+
47+
/**
48+
* Test retrieving of config data.
49+
*/
50+
public function test_retrieving_configdata() {
51+
$formdata = (object)[
52+
'id' => 1,
53+
'profilefield' => 'firstname',
54+
'firstname_operator' => 3,
55+
'firstname_value' => 123,
56+
'invalid_firstname' => 'invalid',
57+
'ruleid' => 1,
58+
'position' => 0,
59+
];
60+
61+
$actual = $this->get_condition()::retrieve_config_data($formdata);
62+
$expected = [
63+
'profilefield' => 'firstname',
64+
'firstname_operator' => 3,
65+
'firstname_value' => 123,
66+
];
67+
$this->assertEquals($expected, $actual);
68+
}
69+
70+
/**
71+
* Test setting and getting config data.
72+
*/
73+
public function test_set_and_get_configdata() {
74+
$instance = $this->get_condition([
75+
'profilefield' => 'firstname',
76+
'firstname_operator' => 3,
77+
'firstname_value' => 123,
78+
]);
79+
80+
$this->assertEquals(
81+
['profilefield' => 'firstname', 'firstname_operator' => 3, 'firstname_value' => 123],
82+
$instance->get_config_data()
83+
);
84+
}
85+
86+
/**
87+
* Data provider for testing config description.
88+
*
89+
* @return array[]
90+
*/
91+
public function config_description_data_provider(): array {
92+
return [
93+
[user_profile::TEXT_CONTAINS, 'First name contains 123'],
94+
[user_profile::TEXT_DOES_NOT_CONTAIN, 'First name doesn\'t contain 123'],
95+
[user_profile::TEXT_IS_EQUAL_TO, 'First name is equal to 123'],
96+
[user_profile::TEXT_IS_NOT_EQUAL_TO, 'First name isn\'t equal to 123'],
97+
[user_profile::TEXT_STARTS_WITH, 'First name starts with 123'],
98+
[user_profile::TEXT_ENDS_WITH, 'First name ends with 123'],
99+
[user_profile::TEXT_IS_EMPTY, 'First name is empty'],
100+
[user_profile::TEXT_IS_NOT_EMPTY, 'First name is not empty'],
101+
];
102+
}
103+
104+
/**
105+
* Test getting config description.
106+
*
107+
* @dataProvider config_description_data_provider
108+
* @param int $operator
109+
* @param string $expected
110+
*/
111+
public function test_config_description(int $operator, string $expected) {
112+
$instance = $this->get_condition([
113+
'profilefield' => 'firstname',
114+
'firstname_operator' => $operator,
115+
'firstname_value' => '123',
116+
]);
117+
118+
$this->assertSame($expected, $instance->get_config_description());
119+
}
120+
121+
/**
122+
* Test getting config description.
123+
*/
124+
public function test_config_description_for_auth_field() {
125+
$instance = $this->get_condition([
126+
'profilefield' => 'auth',
127+
'auth_operator' => user_profile::TEXT_IS_EQUAL_TO,
128+
'auth_value' => 'manual',
129+
]);
130+
131+
$this->assertSame('Authentication method is equal to Manual accounts', $instance->get_config_description());
132+
}
133+
134+
/**
135+
* Test setting and getting config data.
136+
*/
137+
public function test_get_sql_data() {
138+
global $DB;
139+
140+
$this->resetAfterTest();
141+
142+
$this->getDataGenerator()->create_user(['username' => 'user1username']);
143+
$this->getDataGenerator()->create_user(['username' => 'user2username']);
144+
145+
$condition = $this->get_condition([
146+
'profilefield' => 'username',
147+
'username_operator' => user_profile::TEXT_IS_EQUAL_TO,
148+
'username_value' => 'user1username',
149+
]);
150+
151+
$result = $condition->get_sql();
152+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
153+
$this->assertCount(1, $DB->get_records_sql($sql, $result->get_params()));
154+
155+
$condition->set_config_data([
156+
'profilefield' => 'username',
157+
'username_operator' => user_profile::TEXT_STARTS_WITH,
158+
'username_value' => 'user',
159+
]);
160+
161+
$result = $condition->get_sql();
162+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
163+
$this->assertCount(2, $DB->get_records_sql($sql, $result->get_params()));
164+
165+
$condition->set_config_data([
166+
'profilefield' => 'username',
167+
'username_operator' => user_profile::TEXT_ENDS_WITH,
168+
'username_value' => 'username',
169+
]);
170+
171+
$result = $condition->get_sql();
172+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
173+
$this->assertCount(2, $DB->get_records_sql($sql, $result->get_params()));
174+
175+
$condition->set_config_data([
176+
'profilefield' => 'username',
177+
'username_operator' => user_profile::TEXT_IS_NOT_EQUAL_TO,
178+
'username_value' => 'user1username',
179+
]);
180+
181+
$result = $condition->get_sql();
182+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
183+
$totalusers = $DB->count_records('user');
184+
$this->assertCount($totalusers - 1, $DB->get_records_sql($sql, $result->get_params()));
185+
}
186+
187+
/**
188+
* Test events that the condition is listening to.
189+
*/
190+
public function test_get_events() {
191+
$this->assertEquals([
192+
'\core\event\user_created',
193+
'\core\event\user_updated',
194+
], $this->get_condition()->get_events());
195+
}
196+
197+
/**
198+
* Test is broken.
199+
*/
200+
public function test_is_broken() {
201+
$condition = $this->get_condition();
202+
203+
// Not configured should be always valid.
204+
$this->assertFalse($condition->is_broken());
205+
206+
$condition->set_config_data([
207+
'profilefield' => 'username',
208+
'username_operator' => user_profile::TEXT_IS_EMPTY,
209+
'username_value' => '',
210+
]);
211+
$this->assertFalse($condition->is_broken());
212+
213+
$condition->set_config_data([
214+
'profilefield' => 'username',
215+
'username_operator' => user_profile::TEXT_IS_NOT_EMPTY,
216+
'username_value' => '',
217+
]);
218+
$this->assertFalse($condition->is_broken());
219+
220+
// Break condition.
221+
$condition->set_config_data([
222+
'profilefield' => 'username',
223+
'username_operator' => user_profile::TEXT_IS_EQUAL_TO,
224+
'username_value' => '',
225+
]);
226+
$this->assertTrue($condition->is_broken());
227+
228+
// Break condition.
229+
$condition->set_config_data([
230+
'profilefield' => 'notexisting',
231+
'username_operator' => user_profile::TEXT_IS_EQUAL_TO,
232+
'username_value' => '123',
233+
]);
234+
$this->assertTrue($condition->is_broken());
235+
}
236+
}

0 commit comments

Comments
 (0)