You are here

class LdapAuthorizationProviderTest in Lightweight Directory Access Protocol (LDAP) 8.4

@coversDefaultClass \Drupal\ldap_authorization\Plugin\authorization\Provider\LDAPAuthorizationProvider @group authorization

Hierarchy

Expanded class hierarchy of LdapAuthorizationProviderTest

File

ldap_authorization/tests/src/Unit/LdapAuthorizationProviderTest.php, line 16

Namespace

Drupal\Tests\ldap_authorization\Unit
View source
class LdapAuthorizationProviderTest extends UnitTestCase {

  /**
   * Provider plugin.
   *
   * @var \Drupal\ldap_authorization\Plugin\authorization\Provider\LDAPAuthorizationProvider
   */
  protected $providerPlugin;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->providerPlugin = $this
      ->getMockBuilder(LDAPAuthorizationProvider::class)
      ->setMethods(NULL)
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Test regex validation().
   */
  public function testValidateRowForm() : void {
    $form_state = new FormState();
    $mappings = [
      0 => [
        'provider_mappings' => [
          'is_regex' => 1,
          'query' => 'example',
        ],
      ],
      1 => [
        'provider_mappings' => [
          'is_regex' => 1,
          'query' => '/.*/',
        ],
      ],
      2 => [
        'provider_mappings' => [
          'is_regex' => 0,
          'query' => '/.*/',
        ],
      ],
    ];
    $form_state
      ->setUserInput($mappings);
    $sub_form_state = new SubFormState($form_state, [
      'provider_config',
    ]);
    $form = [];
    $this->providerPlugin
      ->validateRowForm($form, $sub_form_state);
    self::assertEquals([], $sub_form_state
      ->getErrors());

    // @todo Still needs more useful assertions here.
  }

  /**
   * Test the filter proposal.
   */
  public function testFilterProposal() : void {

    // Example of groups defined in.
    $input = [
      'cn=students',
    ];
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'cn=students',
      'is_regex' => FALSE,
    ]));
    $input = [
      'cn=students,ou=groups,dc=hogwarts,dc=edu',
      'cn=gryffindor,ou=groups,dc=hogwarts,dc=edu',
      'cn=users,ou=groups,dc=hogwarts,dc=edu',
    ];
    self::assertCount(0, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'cn=students',
      'is_regex' => FALSE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'cn=students,ou=groups,dc=hogwarts,dc=edu',
      'is_regex' => FALSE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'CN=students,ou=groups,dc=hogwarts,dc=edu',
      'is_regex' => FALSE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/cn=students/i',
      'is_regex' => TRUE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/CN=students/i',
      'is_regex' => TRUE,
    ]));

    // Get only one of the values.
    $input = [
      'cn=users,ou=gryffindor,ou=slytherin,ou=ravenclaw,dc=hogwarts,dc=edu',
    ];
    $output = $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/(?<=ou=).+?[^\\,]*/i',
      'is_regex' => TRUE,
    ]);
    self::assertEquals('gryffindor', $output[0]);
    self::assertCount(1, $output);
    $input = [
      'memberOf=students,ou=groups,dc=hogwarts,dc=edu',
    ];
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'memberOf=students,ou=groups,dc=hogwarts,dc=edu',
      'is_regex' => FALSE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => 'memberof=students,ou=groups,dc=hogwarts,dc=edu',
      'is_regex' => FALSE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/^memberof=students/i',
      'is_regex' => TRUE,
    ]));
    self::assertCount(1, $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/^memberOf=students/i',
      'is_regex' => TRUE,
    ]));
    self::assertCount(0, $this->providerPlugin
      ->filterProposals($input, [
      'query' => '/^emberOf=students/i',
      'is_regex' => TRUE,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LdapAuthorizationProviderTest::$providerPlugin protected property Provider plugin.
LdapAuthorizationProviderTest::setUp public function Overrides UnitTestCase::setUp
LdapAuthorizationProviderTest::testFilterProposal public function Test the filter proposal.
LdapAuthorizationProviderTest::testValidateRowForm public function Test regex validation().
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.
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.