[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/ldap/tests/ -> ldap_test.php (source)

   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 enrolment 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_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
  25   * define('TEST_ENROL_LDAP_BIND_DN', 'cn=someuser,dc=example,dc=local');
  26   * define('TEST_ENROL_LDAP_BIND_PW', 'somepassword');
  27   * define('TEST_ENROL_LDAP_DOMAIN', 'dc=example,dc=local');
  28   *
  29   * @package    enrol_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  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  global $CFG;
  38  
  39  
  40  class enrol_ldap_testcase extends advanced_testcase {
  41  
  42      public function test_enrol_ldap() {
  43          global $CFG, $DB;
  44  
  45          if (!extension_loaded('ldap')) {
  46              $this->markTestSkipped('LDAP extension is not loaded.');
  47          }
  48  
  49          $this->resetAfterTest();
  50  
  51          require_once($CFG->dirroot.'/enrol/ldap/lib.php');
  52          require_once($CFG->libdir.'/ldaplib.php');
  53  
  54          if (!defined('TEST_ENROL_LDAP_HOST_URL') or !defined('TEST_ENROL_LDAP_BIND_DN') or !defined('TEST_ENROL_LDAP_BIND_PW') or !defined('TEST_ENROL_LDAP_DOMAIN')) {
  55              $this->markTestSkipped('External LDAP test server not configured.');
  56          }
  57  
  58          // Make sure we can connect the server.
  59          $debuginfo = '';
  60          if (!$connection = ldap_connect_moodle(TEST_ENROL_LDAP_HOST_URL, 3, 'rfc2307', TEST_ENROL_LDAP_BIND_DN, TEST_ENROL_LDAP_BIND_PW, LDAP_DEREF_NEVER, $debuginfo, false)) {
  61              $this->markTestSkipped('Can not connect to LDAP test server: '.$debuginfo);
  62          }
  63  
  64          $this->enable_plugin();
  65  
  66          // Create new empty test container.
  67          $topdn = 'dc=moodletest,'.TEST_ENROL_LDAP_DOMAIN;
  68  
  69          $this->recursive_delete($connection, TEST_ENROL_LDAP_DOMAIN, 'dc=moodletest');
  70  
  71          $o = array();
  72          $o['objectClass'] = array('dcObject', 'organizationalUnit');
  73          $o['dc']         = 'moodletest';
  74          $o['ou']         = 'MOODLETEST';
  75          if (!ldap_add($connection, 'dc=moodletest,'.TEST_ENROL_LDAP_DOMAIN, $o)) {
  76              $this->markTestSkipped('Can not create test LDAP container.');
  77          }
  78  
  79          // Configure enrol plugin.
  80          /** @var enrol_ldap_plugin $enrol */
  81          $enrol = enrol_get_plugin('ldap');
  82          $enrol->set_config('host_url', TEST_ENROL_LDAP_HOST_URL);
  83          $enrol->set_config('start_tls', 0);
  84          $enrol->set_config('ldap_version', 3);
  85          $enrol->set_config('ldapencoding', 'utf-8');
  86          $enrol->set_config('pagesize', '2');
  87          $enrol->set_config('bind_dn', TEST_ENROL_LDAP_BIND_DN);
  88          $enrol->set_config('bind_pw', TEST_ENROL_LDAP_BIND_PW);
  89          $enrol->set_config('course_search_sub', 0);
  90          $enrol->set_config('memberattribute_isdn', 0);
  91          $enrol->set_config('user_contexts', '');
  92          $enrol->set_config('user_search_sub', 0);
  93          $enrol->set_config('user_type', 'rfc2307');
  94          $enrol->set_config('opt_deref', LDAP_DEREF_NEVER);
  95          $enrol->set_config('objectclass', '(objectClass=posixGroup)');
  96          $enrol->set_config('course_idnumber', 'cn');
  97          $enrol->set_config('course_shortname', 'cn');
  98          $enrol->set_config('course_fullname', 'cn');
  99          $enrol->set_config('course_summary', '');
 100          $enrol->set_config('ignorehiddencourses', 0);
 101          $enrol->set_config('nested_groups', 0);
 102          $enrol->set_config('autocreate', 0);
 103          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
 104  
 105          $roles = get_all_roles();
 106          foreach ($roles as $role) {
 107              $enrol->set_config('contexts_role'.$role->id, '');
 108              $enrol->set_config('memberattribute_role'.$role->id, '');
 109          }
 110  
 111          // Create group for teacher enrolments.
 112          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 113          $this->assertNotEmpty($teacherrole);
 114          $o = array();
 115          $o['objectClass'] = array('organizationalUnit');
 116          $o['ou']          = 'teachers';
 117          ldap_add($connection, 'ou=teachers,'.$topdn, $o);
 118          $enrol->set_config('contexts_role'.$teacherrole->id, 'ou=teachers,'.$topdn);
 119          $enrol->set_config('memberattribute_role'.$teacherrole->id, 'memberuid');
 120  
 121          // Create group for student enrolments.
 122          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 123          $this->assertNotEmpty($studentrole);
 124          $o = array();
 125          $o['objectClass'] = array('organizationalUnit');
 126          $o['ou']          = 'students';
 127          ldap_add($connection, 'ou=students,'.$topdn, $o);
 128          $enrol->set_config('contexts_role'.$studentrole->id, 'ou=students,'.$topdn);
 129          $enrol->set_config('memberattribute_role'.$studentrole->id, 'memberuid');
 130  
 131          // Create some users and courses.
 132          $user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'user1', 'username'=>'user1'));
 133          $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'user2', 'username'=>'user2'));
 134          $user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'user3', 'username'=>'user3'));
 135          $user4 = $this->getDataGenerator()->create_user(array('idnumber'=>'user4', 'username'=>'user4'));
 136          $user5 = $this->getDataGenerator()->create_user(array('idnumber'=>'user5', 'username'=>'user5'));
 137          $user6 = $this->getDataGenerator()->create_user(array('idnumber'=>'user6', 'username'=>'user6'));
 138  
 139          $course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'course1', 'shortname'=>'course1'));
 140          $course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'course2', 'shortname'=>'course2'));
 141          $course3 = $this->getDataGenerator()->create_course(array('idnumber'=>'course3', 'shortname'=>'course3'));
 142  
 143          // Set up some ldap data.
 144          $o = array();
 145          $o['objectClass'] = array('posixGroup');
 146          $o['cn']          = 'course1';
 147          $o['gidNumber']   = '1';
 148          $o['memberUid']   = array('user1', 'user2', 'user3', 'userx');
 149          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 150          $o = array();
 151          $o['objectClass'] = array('posixGroup');
 152          $o['cn']          = 'course1';
 153          $o['gidNumber']   = '2';
 154          $o['memberUid']   = array('user5');
 155          ldap_add($connection, 'cn='.$o['cn'].',ou=teachers,'.$topdn, $o);
 156  
 157          $o = array();
 158          $o['objectClass'] = array('posixGroup');
 159          $o['cn']          = 'course2';
 160          $o['gidNumber']   = '3';
 161          $o['memberUid']   = array('user1', 'user2', 'user3', 'user4');
 162          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 163  
 164          $o = array();
 165          $o['objectClass'] = array('posixGroup');
 166          $o['cn']          = 'course4';
 167          $o['gidNumber']   = '4';
 168          $o['memberUid']   = array('user1', 'user2');
 169          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 170          $o = array();
 171          $o['objectClass'] = array('posixGroup');
 172          $o['cn']          = 'course4';
 173          $o['gidNumber']   = '5';
 174          $o['memberUid']   = array('user5', 'user6');
 175          ldap_add($connection, 'cn='.$o['cn'].',ou=teachers,'.$topdn, $o);
 176  
 177  
 178          // Test simple test without creation.
 179  
 180          $this->assertEquals(0, $DB->count_records('user_enrolments'));
 181          $this->assertEquals(0, $DB->count_records('role_assignments'));
 182          $this->assertEquals(4, $DB->count_records('course'));
 183  
 184          $enrol->sync_enrolments(new null_progress_trace());
 185  
 186          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 187          $this->assertEquals(8, $DB->count_records('role_assignments'));
 188          $this->assertEquals(4, $DB->count_records('course'));
 189  
 190          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id);
 191          $this->assertIsEnrolled($course1->id, $user2->id, $studentrole->id);
 192          $this->assertIsEnrolled($course1->id, $user3->id, $studentrole->id);
 193          $this->assertIsEnrolled($course1->id, $user5->id, $teacherrole->id);
 194  
 195          $this->assertIsEnrolled($course2->id, $user1->id, $studentrole->id);
 196          $this->assertIsEnrolled($course2->id, $user2->id, $studentrole->id);
 197          $this->assertIsEnrolled($course2->id, $user3->id, $studentrole->id);
 198          $this->assertIsEnrolled($course2->id, $user4->id, $studentrole->id);
 199  
 200  
 201          // Test course creation.
 202          $enrol->set_config('autocreate', 1);
 203  
 204          $enrol->sync_enrolments(new null_progress_trace());
 205  
 206          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 207          $this->assertEquals(12, $DB->count_records('role_assignments'));
 208          $this->assertEquals(5, $DB->count_records('course'));
 209  
 210          $course4 = $DB->get_record('course', array('idnumber'=>'course4'), '*', MUST_EXIST);
 211  
 212          $this->assertIsEnrolled($course4->id, $user1->id, $studentrole->id);
 213          $this->assertIsEnrolled($course4->id, $user2->id, $studentrole->id);
 214          $this->assertIsEnrolled($course4->id, $user5->id, $teacherrole->id);
 215          $this->assertIsEnrolled($course4->id, $user6->id, $teacherrole->id);
 216  
 217  
 218          // Test unenrolment.
 219          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 220          $o = array();
 221          $o['objectClass'] = array('posixGroup');
 222          $o['cn']          = 'course1';
 223          $o['gidNumber']   = '1';
 224          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 225  
 226          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
 227          $enrol->sync_enrolments(new null_progress_trace());
 228          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 229          $this->assertEquals(12, $DB->count_records('role_assignments'));
 230          $this->assertEquals(5, $DB->count_records('course'));
 231  
 232          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 233          $enrol->sync_enrolments(new null_progress_trace());
 234          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 235          $this->assertEquals(12, $DB->count_records('role_assignments'));
 236          $this->assertEquals(5, $DB->count_records('course'));
 237          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_SUSPENDED);
 238          $this->assertIsEnrolled($course1->id, $user2->id, $studentrole->id, ENROL_USER_SUSPENDED);
 239          $this->assertIsEnrolled($course1->id, $user3->id, $studentrole->id, ENROL_USER_SUSPENDED);
 240  
 241          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 242          $o = array();
 243          $o['objectClass'] = array('posixGroup');
 244          $o['cn']          = 'course1';
 245          $o['gidNumber']   = '1';
 246          $o['memberUid']   = array('user1', 'user2', 'user3');
 247          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 248  
 249          $enrol->sync_enrolments(new null_progress_trace());
 250          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 251          $this->assertEquals(12, $DB->count_records('role_assignments'));
 252          $this->assertEquals(5, $DB->count_records('course'));
 253          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 254          $this->assertIsEnrolled($course1->id, $user2->id, $studentrole->id, ENROL_USER_ACTIVE);
 255          $this->assertIsEnrolled($course1->id, $user3->id, $studentrole->id, ENROL_USER_ACTIVE);
 256  
 257          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 258          $o = array();
 259          $o['objectClass'] = array('posixGroup');
 260          $o['cn']          = 'course1';
 261          $o['gidNumber']   = '1';
 262          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 263  
 264          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 265          $enrol->sync_enrolments(new null_progress_trace());
 266          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 267          $this->assertEquals(9, $DB->count_records('role_assignments'));
 268          $this->assertEquals(5, $DB->count_records('course'));
 269          $this->assertIsEnrolled($course1->id, $user1->id, 0, ENROL_USER_SUSPENDED);
 270          $this->assertIsEnrolled($course1->id, $user2->id, 0, ENROL_USER_SUSPENDED);
 271          $this->assertIsEnrolled($course1->id, $user3->id, 0, ENROL_USER_SUSPENDED);
 272  
 273          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 274          $o = array();
 275          $o['objectClass'] = array('posixGroup');
 276          $o['cn']          = 'course1';
 277          $o['gidNumber']   = '1';
 278          $o['memberUid']   = array('user1', 'user2', 'user3');
 279          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 280  
 281          $enrol->sync_enrolments(new null_progress_trace());
 282          $this->assertEquals(12, $DB->count_records('user_enrolments'));
 283          $this->assertEquals(12, $DB->count_records('role_assignments'));
 284          $this->assertEquals(5, $DB->count_records('course'));
 285          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 286          $this->assertIsEnrolled($course1->id, $user2->id, $studentrole->id, ENROL_USER_ACTIVE);
 287          $this->assertIsEnrolled($course1->id, $user3->id, $studentrole->id, ENROL_USER_ACTIVE);
 288  
 289          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 290          $o = array();
 291          $o['objectClass'] = array('posixGroup');
 292          $o['cn']          = 'course1';
 293          $o['gidNumber']   = '1';
 294          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 295  
 296          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 297          $enrol->sync_enrolments(new null_progress_trace());
 298          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 299          $this->assertEquals(9, $DB->count_records('role_assignments'));
 300          $this->assertEquals(5, $DB->count_records('course'));
 301          $this->assertIsNotEnrolled($course1->id, $user1->id);
 302          $this->assertIsNotEnrolled($course1->id, $user2->id);
 303          $this->assertIsNotEnrolled($course1->id, $user3->id);
 304  
 305  
 306          // Individual user enrolments-
 307  
 308          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 309          $o = array();
 310          $o['objectClass'] = array('posixGroup');
 311          $o['cn']          = 'course1';
 312          $o['gidNumber']   = '1';
 313          $o['memberUid']   = array('user1', 'user2', 'user3');
 314          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 315  
 316          $enrol->sync_user_enrolments($user1);
 317          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 318          $this->assertEquals(10, $DB->count_records('role_assignments'));
 319          $this->assertEquals(5, $DB->count_records('course'));
 320          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 321  
 322          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 323          $o = array();
 324          $o['objectClass'] = array('posixGroup');
 325          $o['cn']          = 'course1';
 326          $o['gidNumber']   = '1';
 327          $o['memberUid']   = array('user2', 'user3');
 328          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 329  
 330          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
 331          $enrol->sync_user_enrolments($user1);
 332          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 333          $this->assertEquals(10, $DB->count_records('role_assignments'));
 334          $this->assertEquals(5, $DB->count_records('course'));
 335          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 336  
 337          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 338          $enrol->sync_user_enrolments($user1);
 339          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 340          $this->assertEquals(10, $DB->count_records('role_assignments'));
 341          $this->assertEquals(5, $DB->count_records('course'));
 342          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_SUSPENDED);
 343  
 344          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 345          $o = array();
 346          $o['objectClass'] = array('posixGroup');
 347          $o['cn']          = 'course1';
 348          $o['gidNumber']   = '1';
 349          $o['memberUid']   = array('user1', 'user2', 'user3');
 350          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 351  
 352          $enrol->sync_user_enrolments($user1);
 353          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 354          $this->assertEquals(10, $DB->count_records('role_assignments'));
 355          $this->assertEquals(5, $DB->count_records('course'));
 356          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 357  
 358          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 359          $o = array();
 360          $o['objectClass'] = array('posixGroup');
 361          $o['cn']          = 'course1';
 362          $o['gidNumber']   = '1';
 363          $o['memberUid']   = array('user2', 'user3');
 364          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 365  
 366          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 367          $enrol->sync_user_enrolments($user1);
 368          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 369          $this->assertEquals(9, $DB->count_records('role_assignments'));
 370          $this->assertEquals(5, $DB->count_records('course'));
 371          $this->assertIsEnrolled($course1->id, $user1->id, 0, ENROL_USER_SUSPENDED);
 372  
 373          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 374          $o = array();
 375          $o['objectClass'] = array('posixGroup');
 376          $o['cn']          = 'course1';
 377          $o['gidNumber']   = '1';
 378          $o['memberUid']   = array('user1', 'user2', 'user3');
 379          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 380  
 381          $enrol->sync_user_enrolments($user1);
 382          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 383          $this->assertEquals(10, $DB->count_records('role_assignments'));
 384          $this->assertEquals(5, $DB->count_records('course'));
 385          $this->assertIsEnrolled($course1->id, $user1->id, $studentrole->id, ENROL_USER_ACTIVE);
 386  
 387          ldap_delete($connection, 'cn=course1,ou=students,'.$topdn);
 388          $o = array();
 389          $o['objectClass'] = array('posixGroup');
 390          $o['cn']          = 'course1';
 391          $o['gidNumber']   = '1';
 392          $o['memberUid']   = array('user2', 'user3');
 393          ldap_add($connection, 'cn='.$o['cn'].',ou=students,'.$topdn, $o);
 394  
 395          $enrol->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 396          $enrol->sync_user_enrolments($user1);
 397          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 398          $this->assertEquals(9, $DB->count_records('role_assignments'));
 399          $this->assertEquals(5, $DB->count_records('course'));
 400          $this->assertIsNotEnrolled($course1->id, $user1->id);
 401  
 402          $this->recursive_delete($connection, TEST_ENROL_LDAP_DOMAIN, 'dc=moodletest');
 403          ldap_close($connection);
 404  
 405          // NOTE: multiple roles in one course is not supported, sorry
 406      }
 407  
 408      public function assertIsEnrolled($courseid, $userid, $roleid, $status=null) {
 409          global $DB;
 410  
 411          $context = context_course::instance($courseid);
 412          $instance = $DB->get_record('enrol', array('courseid'=>$courseid, 'enrol'=>'ldap'));
 413          $this->assertNotEmpty($instance);
 414          $ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid));
 415          $this->assertNotEmpty($ue);
 416          if (isset($status)) {
 417              $this->assertEquals($status, $ue->status);
 418          }
 419          if ($roleid) {
 420              $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$userid, 'roleid'=>$roleid, 'component'=>'enrol_ldap')));
 421          } else {
 422              $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$userid, 'component'=>'enrol_ldap')));
 423          }
 424      }
 425  
 426      public function assertIsNotEnrolled($courseid, $userid) {
 427          $context = context_course::instance($courseid);
 428          $this->assertFalse(is_enrolled($context, $userid));
 429      }
 430  
 431      protected function enable_plugin() {
 432          $enabled = enrol_get_plugins(true);
 433          $enabled['ldap'] = true;
 434          $enabled = array_keys($enabled);
 435          set_config('enrol_plugins_enabled', implode(',', $enabled));
 436      }
 437  
 438      protected function disable_plugin() {
 439          $enabled = enrol_get_plugins(true);
 440          unset($enabled['ldap']);
 441          $enabled = array_keys($enabled);
 442          set_config('enrol_plugins_enabled', implode(',', $enabled));
 443      }
 444  
 445      protected function recursive_delete($connection, $dn, $filter) {
 446          if ($res = ldap_list($connection, $dn, $filter, array('dn'))) {
 447              $info = ldap_get_entries($connection, $res);
 448              ldap_free_result($res);
 449              if ($info['count'] > 0) {
 450                  if ($res = ldap_search($connection, "$filter,$dn", 'cn=*', array('dn'))) {
 451                      $info = ldap_get_entries($connection, $res);
 452                      ldap_free_result($res);
 453                      foreach ($info as $i) {
 454                          if (isset($i['dn'])) {
 455                              ldap_delete($connection, $i['dn']);
 456                          }
 457                      }
 458                  }
 459                  if ($res = ldap_search($connection, "$filter,$dn", 'ou=*', array('dn'))) {
 460                      $info = ldap_get_entries($connection, $res);
 461                      ldap_free_result($res);
 462                      foreach ($info as $i) {
 463                          if (isset($i['dn']) and $info[0]['dn'] != $i['dn']) {
 464                              ldap_delete($connection, $i['dn']);
 465                          }
 466                      }
 467                  }
 468                  ldap_delete($connection, "$filter,$dn");
 469              }
 470          }
 471      }
 472  
 473      /**
 474       * Test that normalisation of the use objectclass is completed successfully.
 475       *
 476       * @dataProvider objectclass_fetch_provider
 477       * @param string $usertype The supported user type
 478       * @param string $expected The expected filter value
 479       */
 480      public function test_objectclass_fetch($usertype, $expected) {
 481          $this->resetAfterTest();
 482          // Set the user type - this must be performed before the plugin is instantiated.
 483          set_config('user_type', $usertype, 'enrol_ldap');
 484  
 485          // Fetch the plugin.
 486          $instance = enrol_get_plugin('ldap');
 487  
 488          // Use reflection to sneak a look at the plugin.
 489          $rc = new ReflectionClass('enrol_ldap_plugin');
 490          $rcp = $rc->getProperty('userobjectclass');
 491          $rcp->setAccessible(true);
 492  
 493          // Fetch the current userobjectclass value.
 494          $value = $rcp->getValue($instance);
 495          $this->assertEquals($expected, $value);
 496      }
 497  
 498      /**
 499       * Data provider for the test_objectclass_fetch testcase.
 500       *
 501       * @return array of testcases.
 502       */
 503      public function objectclass_fetch_provider() {
 504          return array(
 505              // This is the list of values from ldap_getdefaults() normalised.
 506              'edir' => array(
 507                  'edir',
 508                  '(objectClass=user)'
 509              ),
 510              'rfc2307' => array(
 511                  'rfc2307',
 512                  '(objectClass=posixaccount)'
 513              ),
 514              'rfc2307bis' => array(
 515                  'rfc2307bis',
 516                  '(objectClass=posixaccount)'
 517              ),
 518              'samba' => array(
 519                  'samba',
 520                  '(objectClass=sambasamaccount)'
 521              ),
 522              'ad' => array(
 523                  'ad',
 524                  '(samaccounttype=805306368)'
 525              ),
 526              'default' => array(
 527                  'default',
 528                  '(objectClass=*)'
 529              ),
 530          );
 531      }
 532  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1