View source
<?php
namespace Drupal\Tests\farm_settings\Functional;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class ModulesFormTest extends WebDriverTestBase {
protected $profile = 'farm';
protected $defaultTheme = 'stark';
protected static $modules = [
'farm_settings',
'farm_land',
'farm_observation',
];
protected function setUp() : void {
$GLOBALS['farm_test'] = TRUE;
parent::setUp();
$user = $this
->createUser([
'administer farm settings',
]);
$this
->drupalLogin($user);
}
public function testInstallFunctionality() {
$this
->drupalGet('farm/settings/modules');
$this
->assertInstallButtonState(TRUE);
foreach ([
'farm_land',
'farm_observation',
] as $module) {
$this
->assertModuleCheckboxState('core', $module, TRUE, TRUE);
}
foreach ([
'farm_plant',
'farm_maintenance',
] as $module) {
$this
->assertModuleCheckboxState('core', $module, FALSE, FALSE);
}
$this
->assertModuleCheckboxState('contrib', 'farm_settings_test', FALSE, FALSE);
$this
->installModules([
'core' => [
'farm_plant',
],
'contrib' => [
'farm_settings_test',
],
]);
$this
->assertModuleCheckboxState('core', 'farm_maintenance', TRUE, TRUE);
}
protected function assertModuleCheckboxState(string $type, string $module, bool $checked = FALSE, bool $disabled = FALSE) {
$page = $this
->getSession()
->getPage();
$field_name = $type . "[modules][{$module}]";
$checkbox = $page
->findField($field_name);
$this
->assertNotEmpty($checkbox, "The checkbox for {$module} exists.");
$this
->assertEquals($checked, $checkbox
->isChecked(), "The {$module} checkbox is " . $checked ? '' : 'not ' . 'checked.');
$this
->assertEquals($disabled, $checkbox
->hasAttribute('disabled'), "The {$module} checkbox is disabled: {$disabled}");
}
protected function assertInstallButtonState(bool $disabled = FALSE) {
$page = $this
->getSession()
->getPage();
$button = $page
->findButton('install-modules');
$this
->assertNotEmpty($button, "The install modules button exists.");
$this
->assertEquals($disabled, $button
->hasAttribute('disabled'));
}
protected function installModules(array $modules) {
$module_names = [];
$this
->assertNotEmpty($modules, 'Modules array is not empty.');
foreach ($modules as $type => $module_list) {
$this
->assertNotEmpty($module_list, 'Modules of the specified type are provided.');
foreach ($module_list as $module_name) {
$module_names[] = $module_name;
$page = $this
->getSession()
->getPage();
$page
->checkField($type . "[modules][{$module_name}]");
$this
->assertModuleCheckboxState($type, $module_name, TRUE, FALSE);
}
}
$this
->assertInstallButtonState(FALSE);
$page
->pressButton('install-modules');
$this
->assertSession()
->waitForText('Install modules', 30000);
$this
->rebuildContainer();
$module_extension_list = \Drupal::service('extension.list.module');
$module_extension_list
->reset();
foreach ($modules as $type => $module_list) {
foreach ($module_list as $module_name) {
$module_info = $module_extension_list
->getExtensionInfo($module_name);
$this
->assertNotEmpty($module_info);
$this
->assertModuleCheckboxState($type, $module_name, TRUE, TRUE);
}
}
}
}