View source
<?php
namespace Drupal\Tests\cas\Functional;
use Drupal\Component\Utility\UrlHelper;
class CasForcedAuthSubscriberTest extends CasBrowserTestBase {
protected static $modules = [
'cas',
'path',
'filter',
'node',
'page_cache',
'dynamic_page_cache',
];
public function testForcedLoginPaths() {
global $base_path;
$admin = $this
->drupalCreateUser([
'administer account settings',
]);
$this
->drupalLogin($admin);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this
->drupalCreateNode([
'path' => [
[
'alias' => '/my/path',
],
],
]);
$edit = [
'server[hostname]' => 'fakecasserver.localhost',
'server[path]' => '/auth',
'forced_login[enabled]' => TRUE,
'forced_login[paths][pages]' => "/node/2\n/my/path",
];
$this
->drupalPostForm('/admin/config/people/cas', $edit, 'Save configuration');
$config = $this
->config('cas.settings');
$this
->assertTrue($config
->get('forced_login.enabled'));
$this
->assertEquals("/node/2\n/my/path", $config
->get('forced_login.paths')['pages']);
$this
->drupalLogout();
$this
->disableRedirects();
$this
->prepareRequest();
$session = $this
->getSession();
$session
->visit($this
->buildUrl('node/1', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(302, $session
->getStatusCode());
$expected_redirect_url = 'https://fakecasserver.localhost/auth/login?' . UrlHelper::buildQuery([
'service' => $this
->buildServiceUrlWithParams([
'destination' => $base_path . 'node/2',
]),
]);
$this
->assertEquals($expected_redirect_url, $session
->getResponseHeader('Location'));
$session
->visit($this
->buildUrl('my/path', [
'absolute' => TRUE,
'query' => [
'foo' => 'bar',
'destination' => '/some/other/path',
],
]));
$this
->assertEquals(302, $session
->getStatusCode());
$expected_redirect_url = 'https://fakecasserver.localhost/auth/login?' . UrlHelper::buildQuery([
'service' => $this
->buildServiceUrlWithParams([
'destination' => $base_path . 'my/path?destination=%2Fsome%2Fother%2Fpath&foo=bar',
]),
]);
$this
->assertEquals($expected_redirect_url, $session
->getResponseHeader('Location'));
$this
->enabledRedirects();
$this
->drupalLogin($admin);
$session
->visit($this
->buildUrl('node/2', [
'absolute' => TRUE,
]));
$this
->assertEquals(200, $session
->getStatusCode());
}
}