MDL-40045 auth: removed unnecessary add_to_log calls
[moodle.git] / auth / ldap / tests / plugin_test.php
CommitLineData
a7aff74f
PS
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/**
18 * LDAP authentication plugin tests.
19 *
20 * NOTE: in order to execute this test you need to set up
21 * OpenLDAP server with core, cosine, nis and internet schemas
22 * and add configuration constants to config.php or phpunit.xml configuration file:
23 *
24 * define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
25 * define('TEST_AUTH_LDAP_BIND_DN', 'cn=someuser,dc=example,dc=local');
26 * define('TEST_AUTH_LDAP_BIND_PW', 'somepassword');
27 * define('TEST_AUTH_LDAP_DOMAIN', 'dc=example,dc=local');
28 *
29 * @package auth_ldap
30 * @category phpunit
31 * @copyright 2013 Petr Skoda {@link http://skodak.org}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34
35defined('MOODLE_INTERNAL') || die();
36
d03e4508 37class auth_ldap_plugin_testcase extends advanced_testcase {
a7aff74f
PS
38
39 public function test_auth_ldap() {
40 global $CFG, $DB;
41
42 if (!extension_loaded('ldap')) {
43 $this->markTestSkipped('LDAP extension is not loaded.');
44 }
45
46 $this->resetAfterTest();
47
48 require_once($CFG->dirroot.'/auth/ldap/auth.php');
49 require_once($CFG->libdir.'/ldaplib.php');
50
51 if (!defined('TEST_AUTH_LDAP_HOST_URL') or !defined('TEST_AUTH_LDAP_BIND_DN') or !defined('TEST_AUTH_LDAP_BIND_PW') or !defined('TEST_AUTH_LDAP_DOMAIN')) {
52 $this->markTestSkipped('External LDAP test server not configured.');
53 }
54
55 // Make sure we can connect the server.
56 $debuginfo = '';
57 if (!$connection = ldap_connect_moodle(TEST_AUTH_LDAP_HOST_URL, 3, 'rfc2307', TEST_AUTH_LDAP_BIND_DN, TEST_AUTH_LDAP_BIND_PW, LDAP_DEREF_NEVER, $debuginfo, false)) {
be094a59 58 $this->markTestSkipped('Can not connect to LDAP test server: '.$debuginfo);
a7aff74f
PS
59 }
60
61 $this->enable_plugin();
62
63 // Create new empty test container.
64 $topdn = 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN;
65
66 $this->recursive_delete($connection, TEST_AUTH_LDAP_DOMAIN, 'dc=moodletest');
67
68 $o = array();
69 $o['objectClass'] = array('dcObject', 'organizationalUnit');
70 $o['dc'] = 'moodletest';
71 $o['ou'] = 'MOODLETEST';
72 if (!ldap_add($connection, 'dc=moodletest,'.TEST_AUTH_LDAP_DOMAIN, $o)) {
73 $this->markTestSkipped('Can not create test LDAP container.');
74 }
75
76 // Create a few users.
77 $o = array();
78 $o['objectClass'] = array('organizationalUnit');
79 $o['ou'] = 'users';
80 ldap_add($connection, 'ou='.$o['ou'].','.$topdn, $o);
81
82 for ($i=1; $i<=5; $i++) {
83 $this->create_ldap_user($connection, $topdn, $i);
84 }
85
86 // Set up creators group.
87 $o = array();
88 $o['objectClass'] = array('posixGroup');
89 $o['cn'] = 'creators';
90 $o['gidNumber'] = 1;
91 $o['memberUid'] = array('username1', 'username2');
92 ldap_add($connection, 'cn='.$o['cn'].','.$topdn, $o);
93
94 $creatorrole = $DB->get_record('role', array('shortname'=>'coursecreator'));
95 $this->assertNotEmpty($creatorrole);
96
97
98 // Configure the plugin a bit.
99 set_config('host_url', TEST_AUTH_LDAP_HOST_URL, 'auth/ldap');
100 set_config('start_tls', 0, 'auth/ldap');
101 set_config('ldap_version', 3, 'auth/ldap');
102 set_config('ldapencoding', 'utf-8', 'auth/ldap');
103 set_config('pagesize', '2', 'auth/ldap');
104 set_config('bind_dn', TEST_AUTH_LDAP_BIND_DN, 'auth/ldap');
105 set_config('bind_pw', TEST_AUTH_LDAP_BIND_PW, 'auth/ldap');
106 set_config('user_type', 'rfc2307', 'auth/ldap');
107 set_config('contexts', 'ou=users,'.$topdn, 'auth/ldap');
108 set_config('search_sub', 0, 'auth/ldap');
109 set_config('opt_deref', LDAP_DEREF_NEVER, 'auth/ldap');
110 set_config('user_attribute', 'cn', 'auth/ldap');
111 set_config('memberattribute', 'memberuid', 'auth/ldap');
112 set_config('memberattribute_isdn', 0, 'auth/ldap');
113 set_config('creators', 'cn=creators,'.$topdn, 'auth/ldap');
114 set_config('removeuser', AUTH_REMOVEUSER_KEEP, 'auth/ldap');
115
116 set_config('field_map_email', 'mail', 'auth/ldap');
117 set_config('field_updatelocal_email', 'oncreate', 'auth/ldap');
118 set_config('field_updateremote_email', '0', 'auth/ldap');
119 set_config('field_lock_email', 'unlocked', 'auth/ldap');
120
121 set_config('field_map_firstname', 'givenName', 'auth/ldap');
122 set_config('field_updatelocal_firstname', 'oncreate', 'auth/ldap');
123 set_config('field_updateremote_firstname', '0', 'auth/ldap');
124 set_config('field_lock_firstname', 'unlocked', 'auth/ldap');
125
126 set_config('field_map_lastname', 'sn', 'auth/ldap');
127 set_config('field_updatelocal_lastname', 'oncreate', 'auth/ldap');
128 set_config('field_updateremote_lastname', '0', 'auth/ldap');
129 set_config('field_lock_lastname', 'unlocked', 'auth/ldap');
130
131
132 $this->assertEquals(2, $DB->count_records('user'));
133 $this->assertEquals(0, $DB->count_records('role_assignments'));
134
135 /** @var auth_plugin_ldap $auth */
136 $auth = get_auth_plugin('ldap');
137
138 ob_start();
139 $auth->sync_users(true);
140 ob_end_clean();
141
142 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
143 $this->assertEquals(2, $DB->count_records('role_assignments'));
144 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
145
146 for ($i=1; $i<=5; $i++) {
147 $this->assertTrue($DB->record_exists('user', array('username'=>'username'.$i, 'email'=>'user'.$i.'@example.com', 'firstname'=>'Firstname'.$i, 'lastname'=>'Lastname'.$i)));
148 }
149
150 $this->delete_ldap_user($connection, $topdn, 1);
151
152 ob_start();
153 $auth->sync_users(true);
154 ob_end_clean();
155
156 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
157 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
158 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
159 $this->assertEquals(2, $DB->count_records('role_assignments'));
160 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
161
162
163 set_config('removeuser', AUTH_REMOVEUSER_SUSPEND, 'auth/ldap');
164
165 /** @var auth_plugin_ldap $auth */
166 $auth = get_auth_plugin('ldap');
167
168 ob_start();
169 $auth->sync_users(true);
170 ob_end_clean();
171
d03e4508
PS
172 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
173 $this->assertEquals(0, $DB->count_records('user', array('auth'=>'nologin', 'username'=>'username1')));
174 $this->assertEquals(1, $DB->count_records('user', array('auth'=>'ldap', 'suspended'=>'1', 'username'=>'username1')));
a7aff74f
PS
175 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
176 $this->assertEquals(2, $DB->count_records('role_assignments'));
177 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
178
179 $this->create_ldap_user($connection, $topdn, 1);
180
181 ob_start();
182 $auth->sync_users(true);
183 ob_end_clean();
184
185 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
186 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
187 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
188 $this->assertEquals(2, $DB->count_records('role_assignments'));
189 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
190
d03e4508
PS
191 $DB->set_field('user', 'auth', 'nologin', array('username'=>'username1'));
192
193 ob_start();
194 $auth->sync_users(true);
195 ob_end_clean();
196
197 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
198 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
199 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
200 $this->assertEquals(2, $DB->count_records('role_assignments'));
201 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
a7aff74f
PS
202
203 set_config('removeuser', AUTH_REMOVEUSER_FULLDELETE, 'auth/ldap');
204
205 /** @var auth_plugin_ldap $auth */
206 $auth = get_auth_plugin('ldap');
207
208 $this->delete_ldap_user($connection, $topdn, 1);
209
210 ob_start();
211 $auth->sync_users(true);
212 ob_end_clean();
213
214 $this->assertEquals(5, $DB->count_records('user', array('auth'=>'ldap')));
215 $this->assertEquals(0, $DB->count_records('user', array('username'=>'username1')));
216 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
217 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
218 $this->assertEquals(1, $DB->count_records('role_assignments'));
219 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
220
221 $this->create_ldap_user($connection, $topdn, 1);
222
223 ob_start();
224 $auth->sync_users(true);
225 ob_end_clean();
226
227 $this->assertEquals(6, $DB->count_records('user', array('auth'=>'ldap')));
228 $this->assertEquals(1, $DB->count_records('user', array('username'=>'username1')));
229 $this->assertEquals(0, $DB->count_records('user', array('suspended'=>1)));
230 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
231 $this->assertEquals(2, $DB->count_records('role_assignments'));
232 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$creatorrole->id)));
233
234
235 $this->recursive_delete($connection, TEST_AUTH_LDAP_DOMAIN, 'dc=moodletest');
236 ldap_close($connection);
237 }
238
239 protected function create_ldap_user($connection, $topdn, $i) {
240 $o = array();
241 $o['objectClass'] = array('inetOrgPerson', 'organizationalPerson', 'person', 'posixAccount');
242 $o['cn'] = 'username'.$i;
243 $o['sn'] = 'Lastname'.$i;
244 $o['givenName'] = 'Firstname'.$i;
245 $o['uid'] = $o['cn'];
246 $o['uidnumber'] = 2000+$i;
247 $o['gidNumber'] = 1000+$i;
248 $o['homeDirectory'] = '/';
249 $o['mail'] = 'user'.$i.'@example.com';
250 $o['userPassword'] = 'pass'.$i;
251 ldap_add($connection, 'cn='.$o['cn'].',ou=users,'.$topdn, $o);
252 }
253
254 protected function delete_ldap_user($connection, $topdn, $i) {
255 ldap_delete($connection, 'cn=username'.$i.',ou=users,'.$topdn);
256 }
257
258 protected function enable_plugin() {
259 $auths = get_enabled_auth_plugins(true);
260 if (!in_array('ldap', $auths)) {
261 $auths[] = 'ldap';
262
263 }
264 set_config('auth', implode(',', $auths));
265 }
266
267 protected function recursive_delete($connection, $dn, $filter) {
268 if ($res = ldap_list($connection, $dn, $filter, array('dn'))) {
269 $info = ldap_get_entries($connection, $res);
270 ldap_free_result($res);
271 if ($info['count'] > 0) {
272 if ($res = ldap_search($connection, "$filter,$dn", 'cn=*', array('dn'))) {
273 $info = ldap_get_entries($connection, $res);
274 ldap_free_result($res);
275 foreach ($info as $i) {
276 if (isset($i['dn'])) {
277 ldap_delete($connection, $i['dn']);
278 }
279 }
280 }
281 if ($res = ldap_search($connection, "$filter,$dn", 'ou=*', array('dn'))) {
282 $info = ldap_get_entries($connection, $res);
283 ldap_free_result($res);
284 foreach ($info as $i) {
285 if (isset($i['dn']) and $info[0]['dn'] != $i['dn']) {
286 ldap_delete($connection, $i['dn']);
287 }
288 }
289 }
290 ldap_delete($connection, "$filter,$dn");
291 }
292 }
293 }
294}