You are here

CasForcedLoginControllerTest.php in CAS 2.x

Same filename and directory in other branches
  1. 8 tests/src/Functional/CasForcedLoginControllerTest.php

File

tests/src/Functional/CasForcedLoginControllerTest.php
View source
<?php

namespace Drupal\Tests\cas\Functional;

use Drupal\Component\Utility\UrlHelper;

/**
 * Tests the CAS forced login controller.
 *
 * @group cas
 */
class CasForcedLoginControllerTest extends CasBrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'cas',
    'page_cache',
    'dynamic_page_cache',
  ];

  /**
   * Tests the the forced login route that redirects users authenticate.
   */
  public function testForcedLoginRoute() {
    $admin = $this
      ->drupalCreateUser([
      'administer account settings',
    ]);
    $this
      ->drupalLogin($admin);
    $edit = [
      'server[hostname]' => 'fakecasserver.localhost',
      'server[path]' => '/auth',
    ];
    $this
      ->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
    $this
      ->drupalLogout();
    $this
      ->disableRedirects();
    $this
      ->prepareRequest();
    $session = $this
      ->getSession();

    // We want to test that query string parameters that are present on the
    // request to the forced login route are passed along to the service
    // URL as well, so test each of these cases individually.
    $params_to_test = [
      [],
      [
        'returnto' => 'node/1',
      ],
      [
        'foo' => 'bar',
        'buzz' => 'baz',
      ],
    ];
    foreach ($params_to_test as $params) {
      $path = $this
        ->buildUrl('cas', [
        'query' => $params,
        'absolute' => TRUE,
      ]);
      $session
        ->visit($path);
      $this
        ->assertEquals(302, $session
        ->getStatusCode());
      $expected_redirect_location = 'https://fakecasserver.localhost/auth/login?' . UrlHelper::buildQuery([
        'service' => $this
          ->buildServiceUrlWithParams($params),
      ]);
      $this
        ->assertEquals($expected_redirect_location, $session
        ->getResponseHeader('Location'));
    }
  }

}

Classes

Namesort descending Description
CasForcedLoginControllerTest Tests the CAS forced login controller.