You are here

public function CasRedirectorTest::testBuildRedirectResponse in CAS 2.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Service/CasRedirectorTest.php \Drupal\Tests\cas\Unit\Service\CasRedirectorTest::testBuildRedirectResponse()

Test buildRedirectResponse.

File

tests/src/Unit/Service/CasRedirectorTest.php, line 142

Class

CasRedirectorTest
Cas Redirector Unit Tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testBuildRedirectResponse() {
  $cas_redirector = new CasRedirector($this->casHelper, $this->eventDispatcher, $this->urlGenerator, $this->configFactory);
  $cas_data = new CasRedirectData();
  $cas_data
    ->forceRedirection();
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data, TRUE);

  // Make sure that are url begins with the login redirection.
  $this
    ->assertEquals('https://example-server.com/cas/login?service=http%3A//example.com/casservice', $response
    ->getTargetUrl());
  $this
    ->assertInstanceOf('\\Drupal\\core\\Routing\\TrustedRedirectResponse', $response);

  // Validate redirection control.
  $cas_data
    ->preventRedirection();
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertNull($response, 'Return null for no response');
  $cas_data
    ->forceRedirection();
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertNotNull($response, 'Found response for redirect data');

  // Make sure setting of normal parameters works.
  $cas_data
    ->setParameter('strong_auth', 'true');
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertEquals('https://example-server.com/cas/login?strong_auth=true&service=http%3A//example.com/casservice', $response
    ->getTargetUrl(), 'Target URL with parameters');
  $cas_data
    ->setParameter('strong_auth', NULL);

  // Verfiy setting of gateway parameters.
  $cas_data
    ->setServiceParameter('returnto', 'node/1');
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertEquals('https://example-server.com/cas/login?service=http%3A//example.com/casservice%3Freturnto%3Dnode/1', $response
    ->getTargetUrl(), 'Service parameters present');

  // Verify proper redirector type.
  $cas_data
    ->setIsCacheable(TRUE);

  /** @var \Drupal\Core\Routing\TrustedRedirectResponse $response */
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertInstanceOf('\\Drupal\\core\\Routing\\TrustedRedirectResponse', $response);
  $data = $response
    ->getCacheableMetadata();
  $tags = $data
    ->getCacheTags();
  $this
    ->assertContains('config:cas.settings', $tags, 'Cache Tags set');

  // Verify proper redirector type.
  $cas_data
    ->setIsCacheable(FALSE);
  $response = $cas_redirector
    ->buildRedirectResponse($cas_data);
  $this
    ->assertInstanceOf('\\Drupal\\cas\\CasRedirectResponse', $response, 'Non-cacheable response');
}