You are here

public function CasValidatorTest::testPostValidateEvent in CAS 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Service/CasValidatorTest.php \Drupal\Tests\cas\Unit\Service\CasValidatorTest::testPostValidateEvent()

Tests the post validation event dispatched by the listener.

@covers ::validateTicket

File

tests/src/Unit/Service/CasValidatorTest.php, line 582

Class

CasValidatorTest
CasValidator unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testPostValidateEvent() {

  // Mock up listener on dispatched event.
  $this->eventDispatcher
    ->method('dispatch')
    ->willReturnCallback([
    $this,
    'dispatchEvent',
  ]);
  $this->events = [];
  $ticket = $this
    ->randomMachineName(8);
  $service_params = [];
  $response = "<cas:serviceResponse xmlns:cas='http://example.com/cas'>\n    <cas:authenticationSuccess>\n    <cas:user>username</cas:user>\n    <cas:attributes>\n    <cas:email>foo@example.com</cas:email>\n    <cas:memberof>cn=foo,o=example</cas:memberof>\n    <cas:memberof>cn=bar,o=example</cas:memberof>\n    </cas:attributes>\n    </cas:authenticationSuccess>\n    </cas:serviceResponse>";
  $mock = new MockHandler([
    new Response(200, [], $response),
  ]);
  $handler = HandlerStack::create($mock);
  $httpClient = new Client([
    'handler' => $handler,
  ]);
  $configFactory = $this
    ->getConfigFactoryStub([
    'cas.settings' => [
      'server.hostname' => 'example.com',
      'server.version' => '2.0',
    ],
  ]);
  $casHelper = $this
    ->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')
    ->disableOriginalConstructor()
    ->getMock();
  $urlGenerator = $this
    ->createMock('\\Drupal\\Core\\Routing\\UrlGeneratorInterface');
  $casValidator = new CasValidator($httpClient, $casHelper, $configFactory, $urlGenerator, $this->eventDispatcher);
  $expected_bag = new CasPropertyBag('username');
  $expected_bag
    ->setAttributes([
    'email' => [
      'modified@example.com',
    ],
    'memberof' => [
      'cn=foo,o=example',
      'cn=bar,o=example',
    ],
  ]);
  $actual_bag = $casValidator
    ->validateTicket($ticket, $service_params);
  $this
    ->assertEquals($expected_bag, $actual_bag);
}