View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\ldap_sso\Functional;
use Behat\Mink\Session;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\ldap_servers\LdapUserAttributesInterface;
use Drupal\Tests\BrowserTestBase;
class LdapSsoBrowserTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'language',
'user',
'externalauth',
'ldap_servers',
'ldap_servers_dummy',
'ldap_sso',
'ldap_authentication',
'ldap_user',
'node',
'path',
'path_alias',
'ldap_sso_dummy_ldap',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$node_front = $this
->drupalCreateNode();
$this->nodeWithAlias = $this
->drupalCreateNode([
'type' => 'page',
'status' => 1,
'path' => '/' . $this
->randomMachineName(),
]);
$this
->config('system.site')
->set('page.front', '/node/' . $node_front
->id())
->save();
$manager = $this->container
->get('entity_type.manager');
$server = $manager
->getStorage('ldap_server')
->create([
'id' => 'test',
'timeout' => 30,
'encryption' => 'none',
'address' => 'example',
'port' => 963,
'basedn' => [
'ou=people,dc=hogwarts,dc=edu',
],
'user_attr' => 'cn',
'unique_persistent_attr' => 'uid',
'status' => TRUE,
'mail_attr' => 'mail',
]);
$server
->save();
$this
->config('ldap_authentication.settings')
->set('sids', [
$server
->id(),
])
->set('excludeIfTextInDn', [])
->set('allowOnlyIfTextInDn', [])
->save();
$this
->config('ldap_user.settings')
->set('acctCreation', LdapUserAttributesInterface::ACCOUNT_CREATION_LDAP_BEHAVIOUR)
->set('drupalAcctProvisionServer', $server
->id())
->set('ldapUserSyncMappings', [
'drupal' => [],
'ldap' => [],
])
->set('drupalAcctProvisionTriggers', [
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_AUTHENTICATION,
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_ON_MANUAL_CREATION,
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_UPDATE_CREATE,
])
->save();
$this
->config('ldap_sso.settings')
->set('seamlessLogin', TRUE)
->set('ssoExcludedHosts', [])
->set('ssoExcludedPaths', [])
->set('ssoSplitUserRealm', FALSE)
->set('ssoVariable', 'REMOTE_USER')
->save();
$this->container
->get('config.factory')
->reset();
}
public function testExclusions() : void {
$url = $this
->buildUrl(Url::fromRoute('user.login'), [
'absolute' => TRUE,
]);
$session = $this
->getWithoutRedirect($url);
self::assertEquals($url, $session
->getCurrentUrl());
self::assertNull($session
->getResponseHeader('Location'));
self::assertEquals(200, $session
->getStatusCode());
$url = $this
->buildUrl(Url::fromRoute('<front>'), [
'absolute' => TRUE,
]);
$session = $this
->getSession();
$session
->getDriver()
->getClient()
->followRedirects(FALSE);
$session
->setCookie('sso_stop', 'sso_stop');
$this
->prepareRequest();
$session
->visit($url);
self::assertEquals($url, $session
->getCurrentUrl());
self::assertEquals('', (string) $session
->getResponseHeader('Location'));
self::assertEquals(200, $session
->getStatusCode());
}
public function testSeamlessDisabled() : void {
$this
->config('ldap_sso.settings')
->set('seamlessLogin', FALSE)
->save();
$destination = $this
->config('system.site')
->get('page.front');
$this
->getWithoutRedirect($destination);
$session = $this
->getSession();
self::assertStringContainsString($destination, $session
->getCurrentUrl());
self::assertEquals(200, $session
->getStatusCode());
}
public function testSeamless() : void {
$url = $this
->buildUrl(Url::fromRoute('<front>'), [
'absolute' => TRUE,
]);
$this
->drupalGet($url);
self::assertStringContainsString('You have been successfully authenticated', $this
->getSession()
->getPage()
->getContent());
$destination = $this
->config('system.site')
->get('page.front');
self::assertStringContainsString($destination, $this
->getSession()
->getCurrentUrl());
self::assertEquals(200, $this
->getSession()
->getStatusCode());
}
public function testNodeLanguagePrefix() : void {
$fr = ConfigurableLanguage::createFromLangcode('fr');
$fr
->save();
$this
->rebuildContainer();
$language_manager = $this->container
->get('language_manager');
self::assertCount(2, $language_manager
->getLanguages());
$this->container
->get('state')
->set('ldap_sso_username_test_override', 'hpotter');
$url = $this
->buildUrl(Url::fromRoute('entity.node.canonical', [
'node' => 1,
], [
'absolute' => TRUE,
'language' => $language_manager
->getLanguage('fr'),
]));
self::assertStringContainsString('/fr/node/1', $url);
$this
->drupalGet($url);
self::assertStringContainsString('You have been successfully authenticated', $this
->getSession()
->getPage()
->getContent());
self::assertEquals($url, $this
->getSession()
->getCurrentUrl());
self::assertEquals(200, $this
->getSession()
->getStatusCode());
}
private function getWithoutRedirect(string $url) : Session {
$session = $this
->getSession();
$session
->getDriver()
->getClient()
->followRedirects(FALSE);
$this
->prepareRequest();
$session
->visit($url);
return $session;
}
}