View source
<?php
namespace Drupal\Tests\login_destination\Functional;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\login_destination\Entity\LoginDestination;
use Drupal\Tests\login_destination\Traits\LoginDestinationCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
class RedirectTest extends BrowserTestBase {
use LoginDestinationCreationTrait;
public static $modules = [
'login_destination',
'node',
];
protected $account;
public function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
]);
$this
->drupalCreateNode();
$this->account = $this
->drupalCreateUser([
'access content',
]);
}
protected function createLoginDestinationToNode1(array $triggers = []) {
$this
->createLoginDestinationRule([
'triggers' => $triggers,
'destination_path' => 'internal:/node/1',
]);
}
protected function drupalLogout() {
$assert_session = $this
->assertSession();
$this
->drupalGet('user/logout');
unset($this->loggedInUser->sessionId);
$this->loggedInUser = FALSE;
\Drupal::currentUser()
->setAccount(new AnonymousUserSession());
}
protected function register($password = NULL) {
$name = $this
->randomMachineName();
$mail = $name . '@example.com';
$edit = [
'name' => $name,
'mail' => $mail,
];
if ($password) {
$edit += [
'pass[pass1]' => $password,
'pass[pass2]' => $password,
];
}
$this
->drupalPostForm('user/register', $edit, 'Create new account');
$storage = $this->container
->get('entity_type.manager')
->getStorage('user');
$storage
->resetCache();
$accounts = $storage
->loadByProperties([
'name' => $name,
'mail' => $mail,
]);
$new_user = reset($accounts);
return $new_user;
}
public function testRedirectAfterLogin() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGIN,
]);
$this
->drupalLogin($this->account);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/node/1');
}
public function testNoRedirectAfterLogin() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_REGISTRATION,
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
LoginDestination::TRIGGER_LOGOUT,
]);
$this
->drupalLogin($this->account);
$this
->assertSession()
->addressEquals('/user/2');
}
public function testRedirectAfterRegistering() {
$this
->config('user.settings')
->set('verify_mail', FALSE)
->set('register', UserInterface::REGISTER_VISITORS)
->save();
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_REGISTRATION,
]);
$this
->register(user_password());
$this
->assertSession()
->pageTextContains('Registration successful. You are now logged in.');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/node/1');
}
public function testNoRedirectAfterRegistering() {
$this
->config('user.settings')
->set('verify_mail', FALSE)
->set('register', UserInterface::REGISTER_VISITORS)
->save();
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGIN,
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
LoginDestination::TRIGGER_LOGOUT,
]);
$this
->register(user_password());
$this
->assertSession()
->pageTextContains('Registration successful. You are now logged in.');
$this
->assertSession()
->addressEquals('/user/3');
}
public function testRedirectAfterOneTimeLoginAndSettingPassword() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
]);
$url = user_pass_reset_url($this->account);
$this
->drupalPostForm($url, NULL, 'Log in');
$this
->assertSession()
->pageTextContains('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');
$this
->assertSession()
->titleEquals(strtr('@name | @site', [
'@name' => $this->account
->getAccountName(),
'@site' => $this
->config('system.site')
->get('name'),
]));
$password = user_password();
$edit = [
'pass[pass1]' => $password,
'pass[pass2]' => $password,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/node/1');
}
public function testNoRedirectAfterOneTimeLoginAndSettingPassword() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGIN,
LoginDestination::TRIGGER_REGISTRATION,
LoginDestination::TRIGGER_LOGOUT,
]);
$url = user_pass_reset_url($this->account);
$this
->drupalPostForm($url, NULL, 'Log in');
$this
->assertSession()
->pageTextContains('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');
$this
->assertSession()
->titleEquals(strtr('@name | @site', [
'@name' => $this->account
->getAccountName(),
'@site' => $this
->config('system.site')
->get('name'),
]));
$password = user_password();
$edit = [
'pass[pass1]' => $password,
'pass[pass2]' => $password,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->assertSession()
->addressEquals('/user/2/edit');
}
public function testRedirectImmediatelyAfterOneTimeLogin() {
$this
->config('login_destination.settings')
->set('immediate_redirect', TRUE)
->save();
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
]);
$url = user_pass_reset_url($this->account);
$this
->drupalPostForm($url, NULL, 'Log in');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/node/1');
}
public function testNoRedirectImmediatelyAfterOneTimeLogin() {
$this
->config('login_destination.settings')
->set('immediate_redirect', TRUE)
->save();
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGIN,
LoginDestination::TRIGGER_REGISTRATION,
LoginDestination::TRIGGER_LOGOUT,
]);
$url = user_pass_reset_url($this->account);
$this
->drupalPostForm($url, NULL, 'Log in');
$this
->assertSession()
->addressEquals('/user/2/edit');
}
public function testNoRedirectAfterUpdatingAccountWithoutLoginLink() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
]);
$this
->drupalLogin($this->account);
$password = user_password();
$edit = [
'current_pass' => $this->account->passRaw,
'pass[pass1]' => $password,
'pass[pass2]' => $password,
];
$this
->drupalPostForm('user/2/edit', $edit, 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->assertSession()
->addressEquals('/user/2/edit');
}
public function testRedirectAfterLogout() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGOUT,
]);
$this
->drupalLogin($this->account);
$this
->assertSession()
->addressEquals('/user/2');
$this
->drupalLogout();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/node/1');
}
public function testNoRedirectAfterLogout() {
$this
->createLoginDestinationToNode1([
LoginDestination::TRIGGER_LOGIN,
LoginDestination::TRIGGER_REGISTRATION,
LoginDestination::TRIGGER_ONE_TIME_LOGIN,
]);
$this
->drupalLogin($this->account);
$this
->drupalGet('/user/2');
$this
->drupalLogout();
$this
->assertSession()
->addressEquals('/');
}
}