View source
<?php
namespace Drupal\Tests\simple_ldap\Functional;
use Drupal\Core\Url;
use Drupal\simple_ldap\SimpleLdap;
use Drupal\Tests\BrowserTestBase;
class SimpleLdapTest extends BrowserTestBase {
public static $modules = [
'simple_ldap',
];
protected $permissions = [
'access administration pages',
'administer site configuration',
];
protected $adminUser;
protected $host = 'ldap.forumsys.com';
protected $port = 389;
protected $binddn = 'cn=read-only-admin,dc=example,dc=com';
protected $bindpw = 'password';
protected function setUp() {
if (!extension_loaded('ldap')) {
$this
->markTestSkipped("Skipping because the PHP LDAP extension is not enabled.");
}
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalLogin($this->adminUser);
}
public function testSimpleLdapServerConfiguration() {
$ldap_config_url = Url::fromRoute("simple_ldap.server")
->toString();
$this
->drupalGet($ldap_config_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Not connected');
$edit = [
'host' => $this->host,
'port' => $this->port,
'binddn' => $this->binddn,
'bindpw' => $this->bindpw,
];
$this
->submitForm($edit, t('Save'));
$this
->drupalGet($ldap_config_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Successfully binded to');
$edit = [
'host' => 'not.a.real.ldap.server',
'port' => 123,
'binddn' => 'fake',
'bindpw' => 'credentials',
];
$this
->submitForm($edit, t('Save'));
$this
->drupalGet($ldap_config_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Could not bind to not.a.real.ldap.server');
}
public function testSimpleLdapClass() {
$config = \Drupal::configFactory()
->getEditable('simple_ldap.server');
$config
->set('host', $this->host);
$config
->set('port', $this->port);
$config
->save();
$server = \Drupal::service('simple_ldap.ldap_wrapper');
$server
->connect();
$this
->assertEqual("simple_ldap.ldap_wrapper", $server->_serviceId);
$this
->assertEqual(FALSE, $server
->isBound());
$server
->ldapBind($this->binddn, $this->bindpw);
$this
->assertEqual(TRUE, $server
->isBound());
$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']);
$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']);
$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']);
}
}