You are here

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

Tests the pre validation event dispatched by the listener.

@covers ::validateTicket

File

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

Class

CasValidatorTest
CasValidator unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testPreValidateEvent() {

  // Mock up event dispatcher so we can return a fake subscriber that
  // subscribes to the pre validate event to change the request path.
  $this->eventDispatcher
    ->method('dispatch')
    ->willReturnCallback([
    $this,
    'dispatchEvent',
  ]);
  $this->events = [];

  // Setup Guzzle to return a mock response.
  $mock = new MockHandler([
    new Response(200, [], "yes\nfoobar\n"),
  ]);
  $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' => '1.0',
      'server.verify' => CasHelper::CA_DEFAULT,
    ],
  ]);
  $ticket = $this
    ->randomMachineName(8);
  $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);
  $casValidator
    ->validateTicket($ticket);

  // The 'fake' subscriber we created alters the path on the server to
  // "customPath", test that that actually occured.
  $expected_url = "https://example-server.com/cas/customPath?service&ticket=" . $ticket . '&foo=bar';
  $actual_url = (string) $guzzleTransactions[0]['request']
    ->getUri();
  $this
    ->assertEquals($expected_url, $actual_url);
}