RedirectTest.php in Restrict Login or Role Access by IP Address 8.4
File
src/Tests/RedirectTest.php
View source
<?php
namespace Drupal\restrict_by_ip\Tests;
class RedirectTest extends RestrictByIPWebTestBase {
private $loginDeniedNode;
public static $modules = [
'restrict_by_ip',
'node',
];
public function setUp() {
parent::setUp();
$type = $this
->drupalCreateContentType();
$this->loginDeniedNode = $this
->drupalCreateNode([
'type' => $type
->id(),
]);
$this->conf
->set('error_page', 'node/' . $this->loginDeniedNode
->id())
->save();
}
public function testIpDifferGlobalNoDestination() {
$this->conf
->set('login_range', $this->outOfRangeCIDR)
->save();
$this
->assertRedirected();
}
public function testIpDifferUserNoDestination() {
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR)
->save();
$this
->assertRedirected();
}
public function testIpDifferGlobalWithDestination() {
$this->conf
->set('login_range', $this->outOfRangeCIDR)
->save();
$this
->assertRedirected('node');
}
public function testIpDifferUserWithDestination() {
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR)
->save();
$this
->assertRedirected('node');
}
private function assertRedirected($destination = NULL) {
$edit = [
'name' => $this->regularUser
->label(),
'pass' => $this->regularUser->pass_raw,
];
$options = [
'external' => FALSE,
];
if (isset($destination)) {
$options['query'] = [
'destination' => $destination,
];
}
$this
->drupalPostForm('user/login', $edit, t('Log in'), $options);
$this
->assertFalse($this
->drupalUserIsLoggedIn($this->regularUser), t('User %name unsuccessfully logged in.', [
'%name' => $this->regularUser
->label(),
]));
$this
->assertText($this->loginDeniedNode
->label(), 'Title of login denied page found.');
}
}