LoginTest.php in Restrict Login or Role Access by IP Address 8.4
File
src/Tests/LoginTest.php
View source
<?php
namespace Drupal\restrict_by_ip\Tests;
class LoginTest extends RestrictByIPWebTestBase {
public function testIpMatchGlobal() {
$this->conf
->set('login_range', $this->currentIPCIDR)
->save();
$this
->drupalLogin($this->regularUser);
}
public function testIpDifferGlobal() {
$this->conf
->set('login_range', $this->outOfRangeCIDR)
->save();
$this
->assertNoLogin();
}
public function testIpMatchUser() {
$this->conf
->set('user.' . $this->regularUser
->id(), $this->currentIPCIDR)
->save();
$this
->drupalLogin($this->regularUser);
}
public function testIpDifferUser() {
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR)
->save();
$this
->assertNoLogin();
}
public function testIpDifferGlobalMatchUser() {
$this->conf
->set('login_range', $this->outOfRangeCIDR);
$this->conf
->set('user.' . $this->regularUser
->id(), $this->currentIPCIDR);
$this->conf
->save();
$this
->drupalLogin($this->regularUser);
}
public function testIpMatchGlobalDifferUser() {
$this->conf
->set('login_range', $this->currentIPCIDR);
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR);
$this->conf
->save();
$this
->drupalLogin($this->regularUser);
}
public function testIpDifferGlobalDiffUser() {
$this->conf
->set('login_range', $this->outOfRangeCIDR);
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR);
$this->conf
->save();
$this
->assertNoLogin();
}
public function testIpMatchGlobalMatchUser() {
$this->conf
->set('login_range', $this->currentIPCIDR);
$this->conf
->set('user.' . $this->regularUser
->id(), $this->currentIPCIDR);
$this->conf
->save();
$this
->drupalLogin($this->regularUser);
}
public function testForceLogout() {
$this
->drupalLogin($this->regularUser);
$this->conf
->set('login_range', $this->outOfRangeCIDR)
->save();
$this->dumpHeaders = TRUE;
$this
->drupalGet('user');
$this
->assertFalse($this
->drupalUserIsLoggedIn($this->regularUser), t('User logged out.'));
}
public function testUserDelete() {
$this->conf
->set('user.' . $this->regularUser
->id(), $this->outOfRangeCIDR)
->save();
$this->regularUser
->delete();
$updatedConf = $this
->config('restrict_by_ip.settings');
$result = $updatedConf
->get('user.' . $this->regularUser
->id());
$this
->assertNull($result);
}
private function assertNoLogin() {
$edit = [
'name' => $this->regularUser
->label(),
'pass' => $this->regularUser->pass_raw,
];
$this
->drupalPostForm('user', $edit, t('Log in'));
$this
->assertNoText('Member for', t('User %name unsuccessfully logged in.', [
'%name' => $this->regularUser
->label(),
]));
}
}