You are here

class TokenTests in Lightweight Directory Access Protocol (LDAP) 8.3

@coversDefaultClass \Drupal\ldap_servers\Processor\TokenProcessor @group ldap @runTestsInSeparateProcesses @preserveGlobalState disabled

Hierarchy

Expanded class hierarchy of TokenTests

File

ldap_servers/tests/src/Unit/TokenTests.php, line 41

Namespace

Drupal\Tests\ldap_servers\Unit
View 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

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.
TokenTests::$config public property
TokenTests::$container public property
TokenTests::$detailLog protected property
TokenTests::$fileSystem protected property
TokenTests::$ldapEntry private property
TokenTests::$serverFactory public property
TokenTests::setUp protected function Test setup. Overrides UnitTestCase::setUp
TokenTests::testTokenReplacement public function Test the replacement of tokens.
TokenTests::testTokensReverse public function Additional token tests for the reverse behaviour for DN derivatives.
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.