class SimpleLdapServerTest in Simple LDAP 8
@coversDefaultClass \Drupal\simple_ldap\SimpleLdapServer @group simple_ldap
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\simple_ldap\Unit\SimpleLdapTestBase
- class \Drupal\Tests\simple_ldap\Unit\SimpleLdapServerTest
- class \Drupal\Tests\simple_ldap\Unit\SimpleLdapTestBase
Expanded class hierarchy of SimpleLdapServerTest
File
- tests/
src/ Unit/ SimpleLdapServerTest.php, line 19 - Contains \Drupal\Tests\simple_ldap\Unit\SimpleLdapServer
Namespace
Drupal\Tests\simple_ldap\UnitView source
class SimpleLdapServerTest extends SimpleLdapTestBase {
/**
* @var \Drupal\simple_ldap\SimpleLdap
*/
protected $ldap;
/**
* @var string
*/
protected $config_name = 'simple_ldap.server';
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->ldap = $this
->getMockBuilder('\\Drupal\\simple_ldap\\SimpleLdap')
->disableOriginalConstructor()
->getMock();
}
/**
* @covers ::connect
*/
public function testConnect() {
$this->ldap
->expects($this
->exactly(2))
->method('connect');
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$this
->assertTrue($server
->connect());
}
/**
* @covers ::connect
*/
public function testConnectFail() {
$this->ldap
->expects($this
->exactly(2))
->method('connect')
->will($this
->throwException(new SimpleLdapException('', '')));
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$this
->assertFalse($server
->connect());
}
/**
* Test a default call of bind().
*
* @covers ::bind
* @depends testConnect
*/
public function testBind() {
$this->config
->expects($this
->exactly(2))
->method('get')
->withConsecutive(array(
'binddn',
), array(
'bindpw',
))
->willReturn('test_cred');
$this->ldap
->expects($this
->exactly(2))
->method('isBound')
->will($this
->onConsecutiveCalls(FALSE, TRUE));
$this->ldap
->expects($this
->once())
->method('ldapBind')
->willReturn(TRUE);
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$this
->assertTrue($server
->bind());
}
/**
* Test when credentials are provided in the bind() call.
*
* @covers ::bind
* @depends testConnect
*/
public function testBindProvided() {
$this->ldap
->expects($this
->exactly(2))
->method('isBound')
->will($this
->onConsecutiveCalls(FALSE, TRUE));
$this->ldap
->expects($this
->once())
->method('ldapBind')
->with('testdn', 'testpw')
->willReturn(TRUE);
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$server
->connect();
$this
->assertTrue($server
->bind('testdn', 'testpw'));
}
/**
* @covers ::search
* @depends testBind
* @dataProvider searchDataProvider
*/
public function testSearch($test_results, $scope, $method) {
$this
->setUpSearchTestMocks($test_results);
$this->ldap
->expects($this
->once())
->method($method)
->willReturn('12345678');
// Arbitrary value to simulate an LDAP search identifier resource.
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$server
->connect();
$server
->bind();
$results = $server
->search('dc=local', 'cn=ldapuser', $scope);
$this
->assertArrayEquals($test_results, $results);
}
/**
* Most of the Search tests expect the same things.
*
* @param $result_array
*/
protected function setUpSearchTestMocks($result_array) {
$this->ldap
->expects($this
->any())
->method('isBound')
->willReturn(TRUE);
$this->ldap
->expects($this
->once())
->method('getEntries')
->willReturn($result_array);
$this->ldap
->expects($this
->once())
->method('freeResult')
->willReturn(TRUE);
$this->ldap
->expects($this
->once())
->method('clean')
->willReturn($result_array);
}
public function searchDataProvider() {
return array(
array(
array(
'returned_value_1',
'returned_value_2',
),
'base',
'ldapRead',
),
array(
array(
'returned_value_1',
'returned_value_2',
),
'one',
'ldapList',
),
array(
array(
'returned_value_1',
'returned_value_2',
),
'sub',
'ldapSearch',
),
);
}
/**
* @covers ::getPageSize
* @covers ::setRootDse
* @depends testSearch
*/
public function testSetPageSize() {
$test_results = array(
'' => array(
'supportedcontrol' => array(
'1.2.840.113556.1.4.319',
'returned_value_2',
),
),
'count' => 1,
0 => 'test_result',
);
$this->config
->expects($this
->once())
->method('get')
->with('pagesize')
->willReturn(10);
$this
->setUpSearchTestMocks($test_results);
$this->ldap
->expects($this
->once())
->method('ldapRead')
->willReturn('12345678');
// Arbitrary value to simulate an LDAP search identifier resource.
$this->ldap
->expects($this
->once())
->method('controlPageResultResponse')
->with('12345678', '');
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$server
->connect();
$server
->bind();
$result = $server
->getPageSize();
$this
->assertEquals(10, $result);
}
/**
* @covers ::getPageSize
* @covers ::setRootDse
* @depends testSearch
*/
public function testSetPageSizeUnavailable() {
$test_results = array(
'' => array(
'supportedcontrol' => array(
'1.2.840.113556.1.4.xxxx',
'returned_value_2',
),
),
'count' => 1,
0 => 'test_result',
);
$this->config
->expects($this
->once())
->method('get')
->with('pagesize')
->willReturn(10);
$this
->setUpSearchTestMocks($test_results);
$this->ldap
->expects($this
->once())
->method('ldapRead')
->willReturn('12345678');
// Arbitrary value to simulate an LDAP search identifier resource.
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$server
->connect();
$server
->bind();
$result = $server
->getPageSize();
$this
->assertFalse($result);
}
/**
* @dataProvider serverTypeDataProvider
*/
public function testGetServerType($expected, $rootdse_results) {
$this
->setUpSearchTestMocks($rootdse_results);
$this->ldap
->expects($this
->once())
->method('ldapRead')
->willReturn('12345678');
// Arbitrary value to simulate an LDAP search identifier resource.
$server = new SimpleLdapServer($this->config_factory, $this->ldap);
$server
->connect();
$server
->bind();
$type = $server
->getServerType();
}
public function serverTypeDataProvider() {
return array(
array(
'Active Directory',
array(
'' => array(
'rootdomainnamingcontext' => 'is_active_directory',
),
),
),
array(
'OpenLDAP',
array(
'' => array(
'objectclass' => array(
'OpenLDAProotDSE',
'Another record',
),
),
),
),
array(
'LDAP',
array(
'' => array(
'nothing' => 'is_default_ldap',
),
),
),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SimpleLdapServerTest:: |
protected | property |
Overrides SimpleLdapTestBase:: |
|
SimpleLdapServerTest:: |
protected | property | ||
SimpleLdapServerTest:: |
public | function | ||
SimpleLdapServerTest:: |
public | function | ||
SimpleLdapServerTest:: |
public | function |
Sets up common service mocks for SimpleLdap unit tests. Overrides SimpleLdapTestBase:: |
|
SimpleLdapServerTest:: |
protected | function | Most of the Search tests expect the same things. | |
SimpleLdapServerTest:: |
public | function | Test a default call of bind(). | |
SimpleLdapServerTest:: |
public | function | Test when credentials are provided in the bind() call. | |
SimpleLdapServerTest:: |
public | function | @covers ::connect | |
SimpleLdapServerTest:: |
public | function | @covers ::connect | |
SimpleLdapServerTest:: |
public | function | @dataProvider serverTypeDataProvider | |
SimpleLdapServerTest:: |
public | function | @covers ::search @depends testBind @dataProvider searchDataProvider | |
SimpleLdapServerTest:: |
public | function | @covers ::getPageSize @covers ::setRootDse @depends testSearch | |
SimpleLdapServerTest:: |
public | function | @covers ::getPageSize @covers ::setRootDse @depends testSearch | |
SimpleLdapTestBase:: |
protected | property | ||
SimpleLdapTestBase:: |
protected | property | ||
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |