View source
<?php
namespace Drupal\Tests\login_redirect_per_role\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class RedirectTest extends BrowserTestBase {
protected static $modules = [
'login_redirect_per_role_test',
];
protected $defaultTheme = 'stark';
protected $user1;
protected $user2;
protected $user3;
protected $user4;
protected $state;
protected function setUp() : void {
parent::setUp();
$role1 = $this
->drupalCreateRole([]);
$role2 = $this
->drupalCreateRole([]);
$role3 = $this
->drupalCreateRole([]);
$role4 = $this
->drupalCreateRole([]);
$this->user1 = $this
->createUser([], NULL, FALSE, [
'roles' => $role1,
]);
$this->user2 = $this
->createUser([], NULL, FALSE, [
'roles' => $role2,
]);
$this->user3 = $this
->createUser([], NULL, FALSE, [
'roles' => $role3,
]);
$this->user4 = $this
->createUser([], NULL, FALSE, [
'roles' => $role4,
]);
$this
->config('login_redirect_per_role.settings')
->set("login.{$role1}", [
'allow_destination' => TRUE,
'redirect_url' => '/login-url-role1',
'weight' => 0,
])
->set("logout.{$role1}", [
'allow_destination' => TRUE,
'redirect_url' => '/logout-url-role1',
'weight' => 0,
])
->set("login.{$role2}", [
'allow_destination' => FALSE,
'redirect_url' => '/login-url-role2',
'weight' => 0,
])
->set("logout.{$role2}", [
'allow_destination' => FALSE,
'redirect_url' => '/logout-url-role2',
'weight' => 0,
])
->set("login.{$role3}", [
'allow_destination' => FALSE,
'redirect_url' => '[site:valid-path]',
'weight' => 0,
])
->set("logout.{$role3}", [
'allow_destination' => FALSE,
'redirect_url' => '[site:invalid-path]',
'weight' => 0,
])
->set("login.{$role4}", [
'allow_destination' => FALSE,
'redirect_url' => '<front>',
'weight' => 0,
])
->set("logout.{$role4}", [
'allow_destination' => FALSE,
'redirect_url' => '<front>',
'weight' => 0,
])
->save();
$this->state = $this->container
->get('state');
}
public function testLoginAndLogoutRedirectUrl() {
$this
->drupalLogin($this->user1);
$this
->assertSession()
->addressEquals('login-url-role1');
$this
->drupalGet(Url::fromRoute('user.logout'));
$this
->assertSession()
->addressEquals('logout-url-role1');
$this
->drupalLogin($this->user2);
$this
->assertSession()
->addressEquals('login-url-role2');
$this
->drupalGet(Url::fromRoute('user.logout'));
$this
->assertSession()
->addressEquals('logout-url-role2');
$this
->drupalLogin($this->user3);
$this
->assertSession()
->addressEquals('valid-path');
$this
->drupalGet(Url::fromRoute('user.logout'));
$this
->assertSession()
->addressEquals('invalid-path');
$this
->drupalLogin($this->user4);
$this
->assertSession()
->addressEquals('user/' . $this->user4
->id());
$this
->drupalGet(Url::fromRoute('user.logout'));
$this
->assertSession()
->addressEquals('');
}
public function testDestinationUrl() {
$this
->drupalGet(Url::fromRoute('user.login', [], [
'query' => [
'destination' => 'destination-url',
],
]));
$this
->submitForm([
'name' => $this->user1
->getAccountName(),
'pass' => $this->user1->passRaw,
], 'Log in');
$this
->assertSession()
->addressEquals('destination-url');
$this
->drupalGet(Url::fromRoute('user.logout', [], [
'query' => [
'destination' => 'destination-url',
],
]));
$this
->assertSession()
->addressEquals('destination-url');
$this
->drupalGet(Url::fromRoute('user.login', [], [
'query' => [
'destination' => 'destination-url',
],
]));
$this
->submitForm([
'name' => $this->user2
->getAccountName(),
'pass' => $this->user2->passRaw,
], 'Log in');
$this
->assertSession()
->addressEquals('login-url-role2');
$this
->drupalGet(Url::fromRoute('user.logout', [], [
'query' => [
'destination' => 'destination-url',
],
]));
$this
->assertSession()
->addressEquals('logout-url-role2');
}
public function testUserLoginAndUserLogoutHookExecution() {
$this->state
->set('login_redirect_per_role_test.user_login_called', FALSE);
$this->state
->set('login_redirect_per_role_test.user_logout_called', FALSE);
$this
->drupalLogin($this->user1);
$this
->assertEquals(TRUE, $this->state
->get('login_redirect_per_role_test.user_login_called'));
$this
->drupalGet(Url::fromRoute('user.logout'));
$this
->assertEquals(TRUE, $this->state
->get('login_redirect_per_role_test.user_logout_called'));
}
}