You are here

public function CasValidatorTest::testParseAttributes 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::testParseAttributes()

Test parsing out CAS attributes from response.

@covers ::validateVersion2 @covers ::parseAttributes

File

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

Class

CasValidatorTest
CasValidator unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testParseAttributes() {
  $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' => [
      'foo@example.com',
    ],
    'memberof' => [
      'cn=foo,o=example',
      'cn=bar,o=example',
    ],
  ]);
  $actual_bag = $casValidator
    ->validateTicket($ticket, $service_params);
  $this
    ->assertEquals($expected_bag, $actual_bag);
}