You are here

class SimpleLdapUserManagerTest in Simple LDAP 8

@coversDefaultClass \Drupal\simple_ldap_user\SimpleLdapUserManager @group simple_ldap

Hierarchy

Expanded class hierarchy of SimpleLdapUserManagerTest

File

modules/simple_ldap_user/tests/src/Unit/SimpleLdapUserManagerTest.php, line 27
Contains \Drupal\Tests\simple_ldap\Unit\SimpleLdapUserManagerTest

Namespace

Drupal\Tests\simple_ldap\Unit
View source
class SimpleLdapUserManagerTest extends SimpleLdapTestBase {

  /**
   * @var SimpleLdapServer
   */
  protected $server;

  /**
   * @var QueryFactory
   */
  protected $query_factory;

  /**
   * @var EntityTypeManager
   */
  protected $entity_manager;

  /**
   * @var string
   */
  protected $config_name = 'simple_ldap.user';

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->server = $this
      ->getMockBuilder('\\Drupal\\simple_ldap\\SimpleLdapServer')
      ->disableOriginalConstructor()
      ->getMock();
    $this->query_factory = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryFactory')
      ->disableOriginalConstructor()
      ->getMock();
    $this->entity_manager = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * @covers ::getLdapUser
   */
  public function testGetLdapUser() {
    $this
      ->setUpGetLdapUserTests();
    $this->server
      ->expects($this
      ->exactly(2))
      ->method('search')
      ->will($this
      ->onConsecutiveCalls(array(
      'dn="johnsmith"' => array(
        'cn' => array(
          'johnsmith',
        ),
        'mail' => array(
          'john@example.com',
        ),
      ),
      'dn="johnAsmith"' => array(
        'cn' => array(
          'johnAsmith',
        ),
        'mail' => array(
          'johnA@example.com',
        ),
      ),
    ), array(
      'dn="johnsmith"' => array(
        'cn' => array(
          'johnsmith',
        ),
        'mail' => array(
          'john@example.com',
        ),
      ),
    )));
    $user_manager = new SimpleLdapUserManager($this->server, $this->config_factory, $this->query_factory, $this->entity_manager);
    $user = $user_manager
      ->getLdapUser('johnsmith');
    $this
      ->assertInstanceOf(SimpleLdapUser::class, $user);
    return $user;
  }

  /**
   * @covers ::getLdapUser
   */
  public function testGetLdapUserNotFound() {
    $this
      ->setUpGetLdapUserTests();
    $this->server
      ->expects($this
      ->exactly(2))
      ->method('search')
      ->willReturn(array());
    $user_manager = new SimpleLdapUserManager($this->server, $this->config_factory, $this->query_factory, $this->entity_manager);
    $user = $user_manager
      ->getLdapUser('johnsmith');
    $this
      ->assertFalse($user);
  }

  /**
   * @covers ::loadDrupalUser
   * @depends testGetLdapUser
   */
  public function testLoadDrupalUser(SimpleLdapUser $ldap_user) {
    $attributes = $ldap_user
      ->getAttributes();
    $this
      ->setUpDrupalUserTests();
    $query = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $query
      ->expects($this
      ->exactly(2))
      ->method('condition')
      ->willReturn($query);
    $query
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn(array(
      5 => 5,
    ));
    $this->query_factory
      ->expects($this
      ->once())
      ->method('get')
      ->willReturn($query);
    $user_storage = $this
      ->getMockBuilder('\\Drupal\\user\\UserStorage')
      ->disableOriginalConstructor()
      ->getMock();
    $user_storage
      ->expects($this
      ->once())
      ->method('load')
      ->with(5)
      ->willReturn(TRUE);
    $this->entity_manager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('user')
      ->willReturn($user_storage);
    $user_manager = new SimpleLdapUserManager($this->server, $this->config_factory, $this->query_factory, $this->entity_manager);
    $user = $user_manager
      ->loadDrupalUser($ldap_user);
    $this
      ->assertTrue(TRUE);
  }

  /**
   * @covers ::createDrupalUser
   * @depends testGetLdapUser
   */
  public function testCreateDrupalUser(SimpleLdapUser $ldap_user) {
    $attributes = $ldap_user
      ->getAttributes();
    $this->config
      ->expects($this
      ->exactly(2))
      ->method('get')
      ->withConsecutive([
      'name_attribute',
    ], [
      'mail_attribute',
    ])
      ->will($this
      ->onConsecutiveCalls('cn', 'mail'));
    $user_storage = $this
      ->getMockBuilder('\\Drupal\\user\\UserStorage')
      ->disableOriginalConstructor()
      ->getMock();
    $user = $this
      ->getMockBuilder('\\Drupal\\user\\User')
      ->disableOriginalConstructor()
      ->setMethods(array(
      'enforceIsNew',
      'activate',
      'save',
    ))
      ->getMock();
    $user
      ->expects($this
      ->once())
      ->method('enforceIsNew');
    $user
      ->expects($this
      ->once())
      ->method('activate');
    $user
      ->expects($this
      ->once())
      ->method('save');
    $user_storage
      ->expects($this
      ->once())
      ->method('create')
      ->with(array(
      'name' => $attributes['cn'][0],
      'mail' => $attributes['mail'][0],
    ))
      ->willReturn($user);
    $this->entity_manager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('user')
      ->willReturn($user_storage);
    $user_manager = new SimpleLdapUserManager($this->server, $this->config_factory, $this->query_factory, $this->entity_manager);
    $user = $user_manager
      ->createDrupalUser($ldap_user);
    $this
      ->assertNotEmpty($user);
  }

  /**
   * Helper method for testCreateDrupalUser() and testLoadDrupalUser().
   */
  protected function setUpDrupalUserTests() {
    $this->config
      ->expects($this
      ->exactly(2))
      ->method('get')
      ->withConsecutive([
      'name_attribute',
    ], [
      'mail_attribute',
    ])
      ->will($this
      ->onConsecutiveCalls('cn', 'mail'));
  }

  /**
   * Helper method for getLdapUser tests.
   */
  protected function setUpGetLdapUserTests() {
    $this->config
      ->expects($this
      ->exactly(5))
      ->method('get')
      ->withConsecutive([
      'name_attribute',
    ], [
      'mail_attribute',
    ], [
      'basedn',
    ], [
      'user_scope',
    ], [
      'object_class',
    ])
      ->will($this
      ->onConsecutiveCalls('cn', 'mail', 'dc=local', 'sub', array(
      'inetOrgPerson',
      'person',
    )));
    $this->server
      ->expects($this
      ->once())
      ->method('bind')
      ->willReturn(TRUE);
  }

}

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.
SimpleLdapTestBase::$config protected property
SimpleLdapTestBase::$config_factory protected property
SimpleLdapUserManagerTest::$config_name protected property Overrides SimpleLdapTestBase::$config_name
SimpleLdapUserManagerTest::$entity_manager protected property
SimpleLdapUserManagerTest::$query_factory protected property
SimpleLdapUserManagerTest::$server protected property
SimpleLdapUserManagerTest::setUp public function Sets up common service mocks for SimpleLdap unit tests. Overrides SimpleLdapTestBase::setUp
SimpleLdapUserManagerTest::setUpDrupalUserTests protected function Helper method for testCreateDrupalUser() and testLoadDrupalUser().
SimpleLdapUserManagerTest::setUpGetLdapUserTests protected function Helper method for getLdapUser tests.
SimpleLdapUserManagerTest::testCreateDrupalUser public function @covers ::createDrupalUser @depends testGetLdapUser
SimpleLdapUserManagerTest::testGetLdapUser public function @covers ::getLdapUser
SimpleLdapUserManagerTest::testGetLdapUserNotFound public function @covers ::getLdapUser
SimpleLdapUserManagerTest::testLoadDrupalUser public function @covers ::loadDrupalUser @depends testGetLdapUser
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.