View source
<?php
namespace Drupal\Tests\cas\Functional;
use Drupal\cas\Service\CasHelper;
use Drupal\Component\Utility\UrlHelper;
class CasGatewayAuthSubscriberTest extends CasBrowserTestBase {
protected static $modules = [
'cas',
'path',
'filter',
'node',
'page_cache',
'dynamic_page_cache',
];
public function testGatewayPaths() {
global $base_path;
$admin = $this
->drupalCreateUser([
'administer account settings',
]);
$this
->drupalLogin($admin);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$edit = [
'server[hostname]' => 'fakecasserver.localhost',
'server[path]' => '/auth',
'gateway[enabled]' => TRUE,
'gateway[recheck_time]' => 720,
'gateway[paths][pages]' => "/node/1\n/node/2",
'gateway[method]' => CasHelper::GATEWAY_SERVER_SIDE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$config = $this
->config('cas.settings');
$this
->assertEquals(TRUE, $config
->get('gateway.enabled'));
$this
->assertEquals(720, $config
->get('gateway.recheck_time'));
$this
->assertEquals(CasHelper::GATEWAY_SERVER_SIDE, $config
->get('gateway.method'));
$this
->assertEquals("/node/1\n/node/2", $config
->get('gateway.paths')['pages']);
$session = $this
->getSession();
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$this
->assertEquals($this
->getExpectedRedirectUrl(1), $session
->getResponseHeader('Location'));
$this
->assertNotEmpty($this
->getSession()
->getCookie('cas_gateway_checked_ss'));
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$this
->assertNotEmpty($this
->getSession()
->getCookie('cas_gateway_checked_ss'));
$session
->reset();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$this
->assertEquals($this
->getExpectedRedirectUrl(1), $session
->getResponseHeader('Location'));
$this
->assertNotEmpty($this
->getSession()
->getCookie('cas_gateway_checked_ss'));
$session
->reset();
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$this
->assertEquals($this
->getExpectedRedirectUrl(2), $session
->getResponseHeader('Location'));
$session
->reset();
$this
->drupalLogin($admin);
$edit = [
'gateway[recheck_time]' => "-1",
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$this
->assertEquals($this
->getExpectedRedirectUrl(1), $session
->getResponseHeader('Location'));
$this
->assertEmpty($this
->getSession()
->getCookie('cas_gateway_checked_ss'));
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$this
->assertEquals($this
->getExpectedRedirectUrl(2), $session
->getResponseHeader('Location'));
$this
->assertEmpty($this
->getSession()
->getCookie('cas_gateway_checked_ss'));
$session
->reset();
$this
->drupalLogin($admin);
$edit = [
'gateway[paths][pages]' => '/node/1',
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$session
->reset();
$this
->drupalLogin($admin);
$edit = [
'gateway[enabled]' => FALSE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session = $this
->getSession();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$this
->drupalLogin($admin);
$edit = [
'gateway[enabled]' => TRUE,
'gateway[recheck_time]' => "-1",
'gateway[method]' => CasHelper::GATEWAY_CLIENT_SIDE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The "Every page request" recheck time is not compatible with the "Client-side" method');
$edit = [
'gateway[enabled]' => TRUE,
'gateway[recheck_time]' => 720,
'gateway[method]' => CasHelper::GATEWAY_CLIENT_SIDE,
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$drupalSettings = $this
->getDrupalSettings();
$this
->assertEquals($this
->getExpectedRedirectUrl(1), $drupalSettings['cas']['gatewayRedirectUrl']);
$this
->assertEquals(720, $drupalSettings['cas']['recheckTime']);
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$this
->assertEmpty($this
->getDrupalSettings());
}
protected function getExpectedRedirectUrl(int $nid) : string {
return 'https://fakecasserver.localhost/auth/login?' . UrlHelper::buildQuery([
'gateway' => 'true',
'service' => $this
->buildServiceUrlWithParams([
'destination' => "{$GLOBALS['base_path']}node/{$nid}",
'from_gateway' => 1,
]),
]);
}
}