You are here

class SimpleLdapServerTest in Simple LDAP 8

@coversDefaultClass \Drupal\simple_ldap\SimpleLdapServer @group simple_ldap

Hierarchy

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\Unit
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SimpleLdapServerTest::$config_name protected property Overrides SimpleLdapTestBase::$config_name
SimpleLdapServerTest::$ldap protected property
SimpleLdapServerTest::searchDataProvider public function
SimpleLdapServerTest::serverTypeDataProvider public function
SimpleLdapServerTest::setUp public function Sets up common service mocks for SimpleLdap unit tests. Overrides SimpleLdapTestBase::setUp
SimpleLdapServerTest::setUpSearchTestMocks protected function Most of the Search tests expect the same things.
SimpleLdapServerTest::testBind public function Test a default call of bind().
SimpleLdapServerTest::testBindProvided public function Test when credentials are provided in the bind() call.
SimpleLdapServerTest::testConnect public function @covers ::connect
SimpleLdapServerTest::testConnectFail public function @covers ::connect
SimpleLdapServerTest::testGetServerType public function @dataProvider serverTypeDataProvider
SimpleLdapServerTest::testSearch public function @covers ::search @depends testBind @dataProvider searchDataProvider
SimpleLdapServerTest::testSetPageSize public function @covers ::getPageSize @covers ::setRootDse @depends testSearch
SimpleLdapServerTest::testSetPageSizeUnavailable public function @covers ::getPageSize @covers ::setRootDse @depends testSearch
SimpleLdapTestBase::$config protected property
SimpleLdapTestBase::$config_factory protected property
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.