class TokenTests in Lightweight Directory Access Protocol (LDAP) 8.3
@coversDefaultClass \Drupal\ldap_servers\Processor\TokenProcessor @group ldap @runTestsInSeparateProcesses @preserveGlobalState disabled
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\ldap_servers\Unit\TokenTests
Expanded class hierarchy of TokenTests
File
- ldap_servers/
tests/ src/ Unit/ TokenTests.php, line 41
Namespace
Drupal\Tests\ldap_servers\UnitView source
class TokenTests extends UnitTestCase {
public $serverFactory;
public $config;
public $container;
private $ldapEntry;
protected $detailLog;
protected $fileSystem;
/**
* Test setup.
*/
protected function setUp() {
parent::setUp();
class_alias('\\Drupal\\Tests\\ldap_servers\\Unit\\LdapExplodeDnMock', '\\Drupal\\ldap_servers\\Entity\\Server', TRUE);
$this->detailLog = $this
->getMockBuilder('\\Drupal\\ldap_servers\\Logger\\LdapDetailLog')
->disableOriginalConstructor()
->getMock();
$this->fileSystem = $this
->getMockBuilder('\\Drupal\\Core\\File\\FileSystem')
->disableOriginalConstructor()
->getMock();
/* Mocks the Server due to wrapper for ldap_explode_dn(). */
$this->serverFactory = $this
->getMockBuilder('\\Drupal\\ldap_servers\\Entity\\Server')
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('ldap.servers', $this->serverFactory);
$this->container
->set('ldap.detail_log', $this->detailLog);
$this->container
->set('file_system', $this->fileSystem);
\Drupal::setContainer($this->container);
$this->ldapEntry = [
'dn' => 'cn=hpotter,ou=Gryffindor,ou=student,ou=people,dc=hogwarts,dc=edu',
'mail' => [
0 => 'hpotter@hogwarts.edu',
'count' => 1,
],
'sAMAccountName' => [
0 => 'hpotter',
'count' => 1,
],
'house' => [
0 => 'Gryffindor',
1 => 'Privet Drive',
'count' => 2,
],
'guid' => [
0 => 'sdafsdfsdf',
'count' => 1,
],
'count' => 3,
];
}
/**
* Test the replacement of tokens.
*
* See http://drupal.org/node/1245736 for test tokens.
*/
public function testTokenReplacement() {
$tokenHelper = new TokenProcessor($this->detailLog, $this->fileSystem);
$dn = $tokenHelper
->tokenReplace($this->ldapEntry, '[dn]');
$this
->assertEquals($this->ldapEntry['dn'], $dn);
$house0 = $tokenHelper
->tokenReplace($this->ldapEntry, '[house:0]');
$this
->assertEquals($this->ldapEntry['house'][0], $house0);
$mixed = $tokenHelper
->tokenReplace($this->ldapEntry, 'thisold[house:0]');
$this
->assertEquals('thisold' . $this->ldapEntry['house'][0], $mixed);
$compound = $tokenHelper
->tokenReplace($this->ldapEntry, '[samaccountname:0][house:0]');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0] . $this->ldapEntry['house'][0], $compound);
$literalValue = $tokenHelper
->tokenReplace($this->ldapEntry, 'literalvalue');
$this
->assertEquals('literalvalue', $literalValue);
$house0 = $tokenHelper
->tokenReplace($this->ldapEntry, '[house]');
$this
->assertEquals($this->ldapEntry['house'][0], $house0);
$houseLast = $tokenHelper
->tokenReplace($this->ldapEntry, '[house:last]');
$this
->assertEquals($this->ldapEntry['house'][1], $houseLast);
$sAMAccountName = $tokenHelper
->tokenReplace($this->ldapEntry, '[samaccountname:0]');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0], $sAMAccountName);
$sAMAccountNameMixedCase = $tokenHelper
->tokenReplace($this->ldapEntry, '[sAMAccountName:0]');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0], $sAMAccountNameMixedCase);
$sAMAccountName2 = $tokenHelper
->tokenReplace($this->ldapEntry, '[samaccountname]');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0], $sAMAccountName2);
$sAMAccountName3 = $tokenHelper
->tokenReplace($this->ldapEntry, '[sAMAccountName]');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0], $sAMAccountName3);
$base64encode = $tokenHelper
->tokenReplace($this->ldapEntry, '[guid;base64_encode]');
$this
->assertEquals(base64_encode($this->ldapEntry['guid'][0]), $base64encode);
$bin2hex = $tokenHelper
->tokenReplace($this->ldapEntry, '[guid;bin2hex]');
$this
->assertEquals(bin2hex($this->ldapEntry['guid'][0]), $bin2hex);
$msguid = $tokenHelper
->tokenReplace($this->ldapEntry, '[guid;msguid]');
$this
->assertEquals(ConversionHelper::convertMsguidToString($this->ldapEntry['guid'][0]), $msguid);
$binary = $tokenHelper
->tokenReplace($this->ldapEntry, '[guid;binary]');
$this
->assertEquals(ConversionHelper::binaryConversionToString($this->ldapEntry['guid'][0]), $binary);
$account = $this
->prophesize('\\Drupal\\user\\Entity\\User');
$value = new \stdClass();
$value->value = $this->ldapEntry['sAMAccountName'][0];
$account
->get('name')
->willReturn($value);
$nameReplacement = $tokenHelper
->tokenReplace($account
->reveal(), '[property.name]', 'user_account');
$this
->assertEquals($this->ldapEntry['sAMAccountName'][0], $nameReplacement);
}
/**
* Additional token tests for the reverse behaviour for DN derivatives.
*/
public function testTokensReverse() {
$tokenHelper = new TokenProcessor($this->detailLog, $this->fileSystem);
// Test regular reversal (2 elements) at beginning.
$dc = $tokenHelper
->tokenReplace($this->ldapEntry, '[dc:reverse:0]');
$this
->assertEquals('edu', $dc);
// Test single element reversion.
$ou = $tokenHelper
->tokenReplace($this->ldapEntry, '[cn:reverse:0]');
$this
->assertEquals('hpotter', $ou);
// Test 3 element reversion at end.
$ou2 = $tokenHelper
->tokenReplace($this->ldapEntry, '[ou:reverse:2]');
$this
->assertEquals('Gryffindor', $ou2);
}
}
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. | |
TokenTests:: |
public | property | ||
TokenTests:: |
public | property | ||
TokenTests:: |
protected | property | ||
TokenTests:: |
protected | property | ||
TokenTests:: |
private | property | ||
TokenTests:: |
public | property | ||
TokenTests:: |
protected | function |
Test setup. Overrides UnitTestCase:: |
|
TokenTests:: |
public | function | Test the replacement of tokens. | |
TokenTests:: |
public | function | Additional token tests for the reverse behaviour for DN derivatives. | |
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. |