1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Tests for edit user command |
||
5 | * |
||
6 | * It would be good to check to see if the hooks get called, but that's impossible |
||
7 | * with static hook calls. |
||
8 | * |
||
9 | * @link http://www.egroupware.org |
||
10 | * @author Nathan Gray |
||
11 | * @copyright (c) 2018 Nathan Gray |
||
12 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
||
13 | */ |
||
14 | |||
15 | // test base providing common stuff |
||
16 | require_once __DIR__.'/CommandBase.php'; |
||
17 | |||
18 | use EGroupware\Api; |
||
19 | |||
20 | class UserCommandTest extends CommandBase { |
||
21 | |||
22 | // User for testing |
||
23 | protected $account_id; |
||
24 | |||
25 | // Define account details once, then modify as needed for tests |
||
26 | protected $account = array( |
||
27 | 'account_lid' => 'user_test', |
||
28 | 'account_firstname' => 'UserCommand', |
||
29 | 'account_lastname' => 'Test' |
||
30 | ); |
||
31 | |||
32 | protected function tearDown() : void |
||
33 | { |
||
34 | if($this->account_id) |
||
35 | { |
||
36 | $GLOBALS['egw']->accounts->delete($this->account_id); |
||
37 | } |
||
38 | if($this->group_id) |
||
39 | { |
||
40 | $GLOBALS['egw']->accounts->delete($this->group_id); |
||
41 | } |
||
42 | parent::tearDown(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Test that adding a user works when we give it what it needs |
||
47 | */ |
||
48 | public function testAddUser() |
||
49 | { |
||
50 | // Set up |
||
51 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
52 | $log_count = $this->get_log_count(); |
||
53 | |||
54 | // Execute |
||
55 | $command = new admin_cmd_edit_user(false, $this->account); |
||
56 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
57 | $command->run(); |
||
58 | $this->account_id = $command->account; |
||
59 | |||
60 | // Check |
||
61 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
62 | |||
63 | $this->assertNotEmpty($this->account_id, 'Did not create test user account'); |
||
64 | $this->assertEquals(count($pre_search) + 1, count($post_search), 'Should have one more account than before'); |
||
65 | $this->assertArrayHasKey($this->account_id, $post_search); |
||
66 | $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Try to add a new user with the same login as current user. It should |
||
71 | * throw an exception |
||
72 | */ |
||
73 | public function testUserAlreadyExists() |
||
74 | { |
||
75 | // Set up |
||
76 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
77 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
78 | |||
79 | // Execute |
||
80 | $this->account['account_lid'] = $GLOBALS['egw_info']['user']['account_lid']; |
||
81 | $command = new admin_cmd_edit_user(false, $this->account); |
||
82 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
83 | $command->run(); |
||
84 | $this->account_id = $command->account; |
||
85 | |||
86 | // Check |
||
87 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
88 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Try to add a new user without specifying the login. It should throw an |
||
93 | * exception |
||
94 | */ |
||
95 | public function testLoginMissing() |
||
96 | { |
||
97 | // Set up |
||
98 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
99 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
100 | $account = $this->account; |
||
101 | unset($account['account_lid']); |
||
102 | |||
103 | // Execute |
||
104 | $command = new admin_cmd_edit_user(false, $account); |
||
105 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
106 | $command->run(); |
||
107 | $this->account_id = $command->account; |
||
108 | |||
109 | // Check |
||
110 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
111 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Try to add a new user without specifying the last name. It should throw |
||
116 | * an exception |
||
117 | */ |
||
118 | public function testLastnameMissing() |
||
119 | { |
||
120 | // Set up |
||
121 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
122 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
123 | $account = $this->account; |
||
124 | unset($account['account_lastname']); |
||
125 | |||
126 | // Execute |
||
127 | $command = new admin_cmd_edit_user(false, $account); |
||
128 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
129 | $command->run(); |
||
130 | $this->account_id = $command->account; |
||
131 | |||
132 | // Check |
||
133 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
134 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * If password is provided, password2 must be provided or an exception is thrown |
||
139 | */ |
||
140 | public function testPasswordOnce() |
||
141 | { |
||
142 | // Set up |
||
143 | $command = new admin_cmd_edit_user(false, $this->account); |
||
144 | $command->comment = 'Setup for unit test ' . $this->getName(); |
||
145 | $command->run(); |
||
146 | $this->account_id = $command->account; |
||
147 | |||
148 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
149 | |||
150 | $account = $this->account; |
||
151 | $account['account_passwd'] = 'passw0rd'; |
||
152 | |||
153 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
154 | |||
155 | // Execute |
||
156 | $command = new admin_cmd_edit_user(false, $account); |
||
157 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
158 | $command->run(); |
||
159 | |||
160 | // Check |
||
161 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
162 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * If password is provided, password2 must be provided and the same or an |
||
167 | * an exception is thrown |
||
168 | */ |
||
169 | public function testPasswordMismatch() |
||
170 | { |
||
171 | // Set up |
||
172 | $command = new admin_cmd_edit_user(false, $this->account); |
||
173 | $command->comment = 'Setup for unit test ' . $this->getName(); |
||
174 | $command->run(); |
||
175 | $this->account_id = $command->account; |
||
176 | |||
177 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
178 | |||
179 | $account = $this->account; |
||
180 | $account['account_passwd'] = 'passw0rd'; |
||
181 | $account['account_passwd_2'] = 'pAssw0rd'; |
||
182 | |||
183 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
184 | |||
185 | // Execute |
||
186 | $command = new admin_cmd_edit_user(false, $account); |
||
187 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
188 | $command->run(); |
||
189 | |||
190 | // Check |
||
191 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
192 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Check that changing the primary group works and does not change any other |
||
197 | * fields (such as other groups) |
||
198 | */ |
||
199 | public function testChangePrimaryGroup() |
||
200 | { |
||
201 | // Set up |
||
202 | $command = new admin_cmd_edit_user(false, $this->account); |
||
203 | $command->comment = 'Setup for unit test ' . $this->getName(); |
||
204 | $command->run(); |
||
205 | $this->account_id = $command->account; |
||
206 | $pre_group = $GLOBALS['egw']->accounts->id2name($this->account_id, 'account_primary_group'); |
||
207 | $pre_groups = $GLOBALS['egw']->accounts->memberships($this->account_id); |
||
208 | |||
209 | $group = array( |
||
210 | 'account_lid' => $this->getName(), |
||
211 | 'account_members' => array($GLOBALS['egw_info']['user']['account_id']) |
||
212 | ); |
||
213 | if(($account_id = $GLOBALS['egw']->accounts->name2id($group['account_lid']))) |
||
214 | { |
||
215 | // Delete if there in case something went wrong |
||
216 | $GLOBALS['egw']->accounts->delete($account_id); |
||
217 | } |
||
218 | $command = new admin_cmd_edit_group(false, $group); |
||
219 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
220 | $command->run(); |
||
221 | $this->group_id = $command->account; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
222 | |||
223 | $account = $this->account; |
||
224 | $account['account_primary_group'] = $this->group_id; |
||
225 | |||
226 | // Execute |
||
227 | $command = new admin_cmd_edit_user($this->account_id, $account); |
||
228 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
229 | $command->run(); |
||
230 | |||
231 | // Check |
||
232 | $post_group = $GLOBALS['egw']->accounts->id2name($this->account_id, 'account_primary_group'); |
||
233 | $post_groups = $GLOBALS['egw']->accounts->memberships($this->account_id); |
||
234 | $this->assertEquals($this->group_id, $post_group); |
||
235 | $this->assertNotEquals($pre_group, $post_group); |
||
236 | |||
237 | // Changing primary group does not remove membership of previous |
||
238 | $this->assertEquals($pre_groups + array($this->group_id => $group['account_lid']), $post_groups); |
||
239 | |||
240 | $this->assertArraySubset($this->account, $GLOBALS['egw']->accounts->read($this->account_id)); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Check that changing an accounts group memberships works and does not change any |
||
245 | * other fields (such as primary group) |
||
246 | */ |
||
247 | public function testChangeGroups() |
||
248 | { |
||
249 | $command = new admin_cmd_edit_user(false, $this->account); |
||
250 | $command->comment = 'Setup for unit test ' . $this->getName(); |
||
251 | $command->run(); |
||
252 | $this->account_id = $command->account; |
||
253 | $pre_group = $GLOBALS['egw']->accounts->id2name($this->account_id, 'account_primary_group'); |
||
254 | $pre_groups = $GLOBALS['egw']->accounts->memberships($this->account_id); |
||
255 | |||
256 | $group = array( |
||
257 | 'account_lid' => $this->getName(), |
||
258 | 'account_members' => array($GLOBALS['egw_info']['user']['account_id']) |
||
259 | ); |
||
260 | if(($account_id = $GLOBALS['egw']->accounts->name2id($group['account_lid']))) |
||
261 | { |
||
262 | // Delete if there in case something went wrong |
||
263 | $GLOBALS['egw']->accounts->delete($account_id); |
||
264 | } |
||
265 | $command = new admin_cmd_edit_group(false, $group); |
||
266 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
267 | $command->run(); |
||
268 | $this->group_id = $command->account; |
||
0 ignored issues
–
show
|
|||
269 | |||
270 | $account = $this->account; |
||
271 | $account['account_groups'] = $GLOBALS['egw']->accounts->memberships($this->account_id) + |
||
272 | array($this->group_id); |
||
273 | |||
274 | // Execute |
||
275 | $command = new admin_cmd_edit_user($this->account_id, $account); |
||
276 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
277 | $command->run(); |
||
278 | |||
279 | // Check |
||
280 | $post_group = $GLOBALS['egw']->accounts->id2name($this->account_id, 'account_primary_group'); |
||
281 | $post_groups = $GLOBALS['egw']->accounts->memberships($this->account_id); |
||
282 | $this->assertEquals($pre_group, $post_group); |
||
283 | $this->assertEquals($pre_groups + array($this->group_id => $group['account_lid']), $post_groups); |
||
284 | |||
285 | $this->assertArraySubset($this->account, $GLOBALS['egw']->accounts->read($this->account_id)); |
||
286 | } |
||
287 | } |