class SimpleLdapUserManagerTest in Simple LDAP 8
@coversDefaultClass \Drupal\simple_ldap_user\SimpleLdapUserManager @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\SimpleLdapUserManagerTest
- class \Drupal\Tests\simple_ldap\Unit\SimpleLdapTestBase
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\UnitView 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
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. | |
SimpleLdapTestBase:: |
protected | property | ||
SimpleLdapTestBase:: |
protected | property | ||
SimpleLdapUserManagerTest:: |
protected | property |
Overrides SimpleLdapTestBase:: |
|
SimpleLdapUserManagerTest:: |
protected | property | ||
SimpleLdapUserManagerTest:: |
protected | property | ||
SimpleLdapUserManagerTest:: |
protected | property | ||
SimpleLdapUserManagerTest:: |
public | function |
Sets up common service mocks for SimpleLdap unit tests. Overrides SimpleLdapTestBase:: |
|
SimpleLdapUserManagerTest:: |
protected | function | Helper method for testCreateDrupalUser() and testLoadDrupalUser(). | |
SimpleLdapUserManagerTest:: |
protected | function | Helper method for getLdapUser tests. | |
SimpleLdapUserManagerTest:: |
public | function | @covers ::createDrupalUser @depends testGetLdapUser | |
SimpleLdapUserManagerTest:: |
public | function | @covers ::getLdapUser | |
SimpleLdapUserManagerTest:: |
public | function | @covers ::getLdapUser | |
SimpleLdapUserManagerTest:: |
public | function | @covers ::loadDrupalUser @depends testGetLdapUser | |
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. |