You are here

public function SimpleLdapTest::testSimpleLdapClass in Simple LDAP 8

Test the SimpleLdap class.

File

tests/src/Functional/SimpleLdapTest.php, line 108

Class

SimpleLdapTest
Tests Simple LDAP configuration options.

Namespace

Drupal\Tests\simple_ldap\Functional

Code

public function testSimpleLdapClass() {

  // Add configuration required by SimpleLdap::connect().
  $config = \Drupal::configFactory()
    ->getEditable('simple_ldap.server');
  $config
    ->set('host', $this->host);
  $config
    ->set('port', $this->port);
  $config
    ->save();

  // Test SimpleLdap::connect() method.
  $server = \Drupal::service('simple_ldap.ldap_wrapper');
  $server
    ->connect();

  // Test that the server is connected.
  $this
    ->assertEqual("simple_ldap.ldap_wrapper", $server->_serviceId);

  // Test that the server is unbound.
  $this
    ->assertEqual(FALSE, $server
    ->isBound());

  // Test SimpleLdap::ldapBind().
  $server
    ->ldapBind($this->binddn, $this->bindpw);
  $this
    ->assertEqual(TRUE, $server
    ->isBound());

  // Test SimpleLdap::ldapSearch() and SimpleLdap::getEntries().
  $base_dn = 'dc=example,dc=com';
  $search_filter = 'uid=newton';
  $attributes = array();
  $search_results = $server
    ->ldapSearch($base_dn, $search_filter, $attributes);
  $search_info = $server
    ->getEntries($search_results);
  $this
    ->assertEqual(1, $search_info['count']);

  // Test SimpleLdap::ldapRead().
  $object_class_filter = 'objectClass=*';
  $read_results = $server
    ->ldapRead($base_dn, $object_class_filter, $attributes);
  $read_info = $server
    ->getEntries($read_results);
  $this
    ->assertEqual(1, $read_info['count']);

  // Test SimpleLdap::ldapList().
  $list_filter = "ou=mathematicians";
  $list_results = $server
    ->ldapList($base_dn, $list_filter, $attributes);
  $list_info = $server
    ->getEntries($list_results);
  $this
    ->assertEqual(1, $list_info['count']);
}