You are here

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

Test validation of Cas tickets.

@covers ::__construct @covers ::validateTicket @covers ::validateVersion1 @covers ::validateVersion2 @covers ::verifyProxyChain @covers ::parseAllowedProxyChains @covers ::parseServerProxyChain

@dataProvider validateTicketDataProvider

File

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

Class

CasValidatorTest
CasValidator unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testValidateTicket($ticket, array $service_params, $username, $response, $validationRequestUrl, $version, $ssl_verification, $is_proxy, $can_be_proxied, $proxy_chains) {

  // Setup Guzzle to return a mock response.
  $mock = new MockHandler([
    new Response(200, [], $response),
  ]);
  $handler = HandlerStack::create($mock);
  $guzzleTransactions = [];
  $history = Middleware::history($guzzleTransactions);
  $handler
    ->push($history);
  $httpClient = new Client([
    'handler' => $handler,
  ]);
  $configFactory = $this
    ->getConfigFactoryStub([
    'cas.settings' => [
      'server.hostname' => 'example-server.com',
      'server.port' => 443,
      'server.protocol' => 'https',
      'server.path' => '/cas',
      'server.version' => $version,
      'server.verify' => $ssl_verification,
      'server.cert' => 'foo',
      'proxy.initialize' => $is_proxy,
      'proxy.can_be_proxied' => $can_be_proxied,
      'proxy.proxy_chains' => $proxy_chains,
    ],
  ]);

  // Need to mock the URL generator so it returns the correct URL based
  // on the service params that will be fed into it.
  if (!empty($service_params)) {
    $params = '';
    foreach ($service_params as $key => $value) {
      $params .= '&' . $key . '=' . urlencode($value);
    }
    $params = '?' . substr($params, 1);
    $return_value = 'https://example.com/client' . $params;
  }
  else {
    $return_value = 'https://example.com/client';
  }
  $urlGenerator = $this
    ->createMock('\\Drupal\\Core\\Routing\\UrlGeneratorInterface');
  $urlGenerator
    ->expects($this
    ->once())
    ->method('generate')
    ->will($this
    ->returnValue($return_value));
  $urlGenerator
    ->expects($this
    ->any())
    ->method('generateFromRoute')
    ->will($this
    ->returnValue('https://example.com/casproxycallback'));
  $casHelper = $this
    ->getMockBuilder('\\Drupal\\cas\\Service\\CasHelper')
    ->disableOriginalConstructor()
    ->getMock();
  $casValidator = new CasValidator($httpClient, $casHelper, $configFactory, $urlGenerator, $this->eventDispatcher);
  $property_bag = $casValidator
    ->validateTicket($ticket, $service_params);

  // Verify the username is what we expect after parsing the response.
  $this
    ->assertEquals($username, $property_bag
    ->getUsername());

  // Make sure the request made to the server to validate ticket is what
  // we expect.
  $validationTransaction = array_shift($guzzleTransactions);
  $this
    ->assertEquals((string) $validationTransaction['request']
    ->getUri(), $validationRequestUrl);
}