Automated tests of farmOS Update module.

This commit is contained in:
Michael Stenta 2021-09-16 19:07:56 -04:00
parent f575b0039f
commit d51176aef1
4 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,2 @@
exclude_config:
- system.theme

View File

@ -0,0 +1,7 @@
name: farmOS Update Tests
description: 'Support module for farmOS Update testing.'
type: module
package: Testing
core_version_requirement: ^9
dependencies:
- farm:farm_update

View File

@ -0,0 +1,13 @@
<?php
/**
* @file
* Contains farm_update_test.module.
*/
/**
* Implements hook_farm_update_exclude_config().
*/
function farm_update_test_farm_update_exclude_config() {
return ['system.file'];
}

View File

@ -0,0 +1,92 @@
<?php
namespace Drupal\Tests\farm_update\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests for farmOS Update module.
*
* @group farm
*/
class FarmUpdateTest extends KernelTestBase {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The farm update service.
*
* @var \Drupal\farm_update\FarmUpdateInterface
*/
protected $farmUpdate;
/**
* {@inheritdoc}
*/
protected static $modules = [
'config_update',
'farm_update',
'farm_update_test',
'system',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->configFactory = \Drupal::service('config.factory');
$this->farmUpdate = \Drupal::service('farm.update');
$this->installConfig([
'farm_update_test',
'system',
]);
}
/**
* Test farmOS Update module.
*/
public function testFarmUpdate() {
// Confirm that overridden config gets reverted.
$this->farmUpdateTestRevertSetting('system.logging', 'error_level', 'all');
// Confirm that config excluded via hook_farm_update_exclude_config() does
// not get reverted.
$this->farmUpdateTestRevertSetting('system.file', 'default_scheme', 'public', TRUE);
// Confirm that config excluded via farm_update.settings does not get
// reverted.
$this->farmUpdateTestRevertSetting('system.theme', 'default', 'claro', TRUE);
}
/**
* Helper method to test reverting a setting.
*
* @param string $config
* Configuration name.
* @param string $setting
* Setting name within the configuration.
* @param string $override
* Value to use for override.
* @param bool $excluded
* Whether or not we expect this config to be excluded. Defaults to FALSE.
* If set to TRUE, then we expect that the config will still be overridden
* after rebuild.
*/
protected function farmUpdateTestRevertSetting(string $config, string $setting, string $override, bool $excluded = FALSE) {
$original = \Drupal::config($config)->get($setting);
$this->configFactory->getEditable($config)->set($setting, $override)->save();
$this->assertEquals($override, \Drupal::config($config)->get($setting), 'Setting is overridden before rebuild.');
$this->farmUpdate->rebuild();
$expected_value = $excluded ? $override : $original;
$expected_message = $excluded ? 'Setting is overridden after rebuild.' : 'Setting is reverted after rebuild.';
$this->assertEquals($expected_value, \Drupal::config($config)->get($setting), $expected_message);
}
}