Skip to content
Snippets Groups Projects
Commit 5b649f51 authored by Boris Doesborg's avatar Boris Doesborg
Browse files

Issue #3451521: Fix phpunit tests

parent 7c64c580
Branches
Tags
1 merge request!49[#3451521] Fix phpunit tests
Pipeline #187270 passed with warnings
......@@ -20,20 +20,6 @@ class FloodControlBasicTest extends BrowserTestBase {
'flood_control',
];
/**
* A user with authenticated permissions.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;
/**
* A user with admin permissions.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
......@@ -46,11 +32,8 @@ class FloodControlBasicTest extends BrowserTestBase {
parent::setUp();
$this->config('system.site')->set('page.front', '/test-page')->save();
$this->user = $this->drupalCreateUser([]);
$this->adminUser = $this->drupalCreateUser([]);
$this->adminUser->addRole($this->createAdminRole('admin', 'admin'));
$this->adminUser->save();
$this->drupalLogin($this->adminUser);
$this->drupalLogin($this->rootUser);
}
/**
......
......@@ -22,49 +22,29 @@ class FloodControlLoginTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
/**
* User with the permission to access the flood unblock settings.
* The database connection.
*
* @var \Drupal\user\Entity\User
* @var \Drupal\Core\Database\Connection
*/
protected $adminUser;
/**
* A regular user.
*
* @var \Drupal\user\Entity\User
*/
protected $webUser;
/**
* The module installer.
*
* @var \Drupal\Core\Extension\ModuleInstallerInterface
*/
protected $moduleInstaller;
protected $database;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->moduleInstaller = $this->container->get('module_installer');
// Defines a user that can configure the module.
$this->adminUser = $this->createUser(['administer site configuration']);
// Defines a user that can unblock.
$this->unblockUser = $this->drupalCreateUser(['access flood unblock']);
$this->database = $this->container->get('database');
}
/**
* Test blockin ip addresses after multiple login attempts.
* Test blocking ip addresses after multiple login attempts.
*/
public function testIpLoginLimit() {
$database = \Drupal::database();
// Removes all flood entries for a fresh start.
if ($database->schema()->tableExists('flood')) {
$database->truncate('flood')->execute();
if ($this->database->schema()->tableExists('flood')) {
$this->database->truncate('flood')->execute();
}
// Sets the maximum number of ip address login attempts to 3.
......@@ -90,20 +70,26 @@ class FloodControlLoginTest extends BrowserTestBase {
'name' => 'wrong-name',
'pass' => 'wrong-pass',
], 'Log in');
$this->assertSession()->pageTextNotContains('Unrecognized username or password');
$this->assertSession()
->pageTextNotContains('Unrecognized username or password');
$this->assertSession()
->pageTextContains('Login failed');
$this->assertSession()
->pageTextContains('Too many failed login attempts from your IP address');
// Checks if IP address has been added to the flood table.
$blocked_addresses = $database->select('flood', 'f')
// Check that events have been added to the flood table. It was truncated
// at the start of this test so any entry present is success. The IP used
// is not guaranteed to be 127.0.0.1.
$blocked_addresses = $this->database->select('flood', 'f')
->fields('f', ['fid'])
->condition('identifier', '127.0.0.1')
->condition('event', 'user.failed_login_ip')
->execute()
->fetchAll();
$this->assertGreaterThanOrEqual(1, count($blocked_addresses), 'Blocked IP address not found in flood table');
$this->assertGreaterThanOrEqual(
1,
count($blocked_addresses),
'Blocked IP address not found in flood table'
);
}
}
......@@ -28,13 +28,6 @@ class FloodControlSettingsFormTest extends BrowserTestBase {
*/
protected $adminUser;
/**
* A regular user.
*
* @var \Drupal\user\Entity\User
*/
protected $webUser;
/**
* The module installer.
*
......@@ -48,36 +41,11 @@ class FloodControlSettingsFormTest extends BrowserTestBase {
protected function setUp(): void {
parent::setUp();
$this->webUser = $this->createUser();
$this->adminUser = $this->drupalCreateUser([]);
$this->adminUser->addRole($this->createAdminRole('admin', 'admin'));
$this->adminUser->save();
$this->adminUser = $this->drupalCreateUser(['administer flood unblock']);
$this->moduleInstaller = $this->container->get('module_installer');
}
/**
* Test access to the settings form.
*/
public function testSettingsFormAccess() {
// Anonymous users are not allowed to access the settings form.
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(403);
// Logged in users without the 'administer site configuration' permission
// are are not allowed to access the settings form.
$this->drupalLogin($this->webUser);
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(403);
// Users with the 'administer site configuration' permission can access the
// settings form.
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(200);
}
/**
* Test the settings form.
*/
......@@ -98,6 +66,7 @@ class FloodControlSettingsFormTest extends BrowserTestBase {
'user_limit' => 10,
'user_window' => 60,
], 'Save configuration');
$this->assertSession()->statusCodeEquals(200);
// Check that the success message is shown.
$this->assertSession()->pageTextContains('The configuration options have been saved.');
......@@ -109,8 +78,9 @@ class FloodControlSettingsFormTest extends BrowserTestBase {
$this->assertEquals(10, $user_flood->get('user_limit'));
$this->assertEquals(60, $user_flood->get('user_window'));
// Enable the contact module.
$this->moduleInstaller->install(['contact']);
// Enable the contact module and confirm that it installed correctly.
$installed = $this->moduleInstaller->install(['contact']);
$this->assertTrue($installed);
// Go to the settings form.
$this->drupalGet('/admin/config/people/flood-control');
......@@ -125,14 +95,14 @@ class FloodControlSettingsFormTest extends BrowserTestBase {
'contact_threshold_limit' => 125,
'contact_threshold_window' => 1800,
], 'Save configuration');
$this->assertSession()->statusCodeEquals(200);
// Check that the success message is shown.
$this->assertSession()->pageTextContains('The configuration options have been saved.');
// Check that the configured values are correctly saved in the config.
$user_flood = $this->config('contact.settings');
$this->assertEquals(125, $user_flood->get('flood.limit'));
$this->assertEquals(1800, $user_flood->get('flood.interval'));
// Check that the correct values are displayed in the form.
$this->assertSession()->fieldValueEquals('contact_threshold_limit', 125);
$this->assertSession()->fieldValueEquals('contact_threshold_window', 1800);
}
}
......@@ -22,7 +22,7 @@ class FloodControlUiPageTest extends BrowserTestBase {
*
* @var array
*/
protected static $modules = ['flood_control', 'contact'];
protected static $modules = ['flood_control'];
/**
* The admin user that can access the admin page.
......@@ -70,6 +70,16 @@ class FloodControlUiPageTest extends BrowserTestBase {
// to the container.
$request = Request::createFromGlobals();
$this->container->get('request_stack')->push($request);
// The flood table is opportunistically created during first use. In these
// tests, that does not happen all the time in Drupal 9 but the table must
// exist for the Database backend to work.
$database = $this->container->get('database');
if (!$database->schema()->tableExists('flood')) {
$flood = $this->container->get('flood');
$flood->register('flood_control.test', 9675309, 'jenny-');
$flood->clear('flood_control.test', 'jenny-');
}
}
/**
......@@ -78,19 +88,17 @@ class FloodControlUiPageTest extends BrowserTestBase {
public function testAccessAdmin() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/people/flood-unblock');
$this->drupalGet('/admin/people/flood-unblock');
$this->assertSession()->statusCodeEquals(200);
// Test that there is an empty flood list.
$this->assertSession()
->pageTextContains('There are no failed logins at this time.');
$this->drupalGet('/admin/people/flood-unblock');
$this->assertSession()->statusCodeEquals(200);
// Check if link to control settings appears, as the user has the
// permission to see it:
$this->assertSession()->pageTextContains('You can configure the login attempt limits and time windows on the Flood Control settings page');
$this->assertSession()
->pageTextContains('You can configure the login attempt limits and time windows on the Flood Control settings page');
}
/**
......@@ -101,7 +109,7 @@ class FloodControlUiPageTest extends BrowserTestBase {
// Check access on both pages with the "administer flood unblock"
// permission:
$this->drupalGet('admin/config/people/flood-control');
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet('/admin/people/flood-unblock');
......@@ -116,7 +124,7 @@ class FloodControlUiPageTest extends BrowserTestBase {
// Check access on both pages with the "flood unblock ips"
// permission:
$this->drupalGet('admin/config/people/flood-control');
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet('/admin/people/flood-unblock');
......@@ -124,7 +132,8 @@ class FloodControlUiPageTest extends BrowserTestBase {
// Check if link to control settings does not appear, as the user does not
// have the permission to see it:
$this->assertSession()->pageTextNotContains('You can configure the login attempt limits and time windows on the Flood Control settings page');
$this->assertSession()
->pageTextNotContains('You can configure the login attempt limits and time windows on the Flood Control settings page');
}
/**
......@@ -132,7 +141,7 @@ class FloodControlUiPageTest extends BrowserTestBase {
*/
public function testAccessNoPermission() {
$this->drupalLogin($this->simpleUser);
$this->drupalGet('admin/config/people/flood-control');
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet('/admin/people/flood-unblock');
......@@ -144,7 +153,7 @@ class FloodControlUiPageTest extends BrowserTestBase {
*/
public function testAccessAnonymous() {
$this->drupalGet('admin/config/people/flood-control');
$this->drupalGet('/admin/config/people/flood-control');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet('/admin/people/flood-unblock');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment