View source
<?php
namespace Drupal\Tests\update_helper\Kernel;
use Drupal\KernelTests\KernelTestBase;
class UpdaterTest extends KernelTestBase {
protected $configDir = '';
protected $moduleHandler = NULL;
protected static $configSchemaCheckerExclusions = [
'field.storage.node.body',
];
protected static $modules = [
'config_update',
'update_helper',
'system',
'user',
'text',
'field',
'node',
'tour',
];
protected function getUpdateDefinition() {
return [
'__global_actions' => [
'install_modules' => [
'help',
],
'import_configs' => [
'tour.tour.tour-update-helper-test',
],
],
'field.storage.node.body' => [
'expected_config' => [
'lost_config' => 'text',
'settings' => [
'max_length' => 123,
],
'status' => FALSE,
'type' => 'text',
],
'update_actions' => [
'add' => [
'cardinality' => 1,
],
'change' => [
'settings' => [],
'status' => TRUE,
'type' => 'text_with_summary',
],
'delete' => [
'lost_config' => 'text',
'settings' => [
'max_length' => '123',
],
],
],
],
];
}
protected function setUp() {
parent::setUp();
$this->moduleHandler = \Drupal::moduleHandler();
$this->configDir = $this->moduleHandler
->getModule('update_helper')
->getPath() . '/config';
mkdir($this->configDir . '/install', 0755, TRUE);
$tour_config = [
'id' => 'tour-update-helper-test',
'module' => 'update_helper',
'label' => 'Tour test Update Helper config import',
'langcode' => 'en',
'routes' => [
[
'route_name' => 'update_helper.1',
],
],
'tips' => [
'tour-update-helper-test-1' => [
'id' => 'update-helper-test-1',
'plugin' => 'text',
'label' => 'Update Helper',
'body' => 'Update helper test tour.',
'weight' => 1,
],
],
];
$yml_serializer = \Drupal::service('serialization.yaml');
file_put_contents($this->configDir . '/install/tour.tour.tour-update-helper-test.yml', $yml_serializer::encode($tour_config));
$config_handler = \Drupal::service('update_helper.config_handler');
$patch_file_path = $config_handler
->getPatchFile('update_helper', 'test_updater', TRUE);
file_put_contents($patch_file_path, $yml_serializer::encode($this
->getUpdateDefinition()));
$patch_file_path = $config_handler
->getPatchFile('update_helper', 'test_updater_only_delete', TRUE);
file_put_contents($patch_file_path, $yml_serializer::encode([
'field.storage.node.body' => [
'expected_config' => [],
'update_actions' => [
'delete' => [
'lost_config' => 'text',
],
],
],
]));
}
protected function tearDown() {
$config_dir = $this->moduleHandler
->getModule('update_helper')
->getPath() . '/config';
unlink($config_dir . '/install/tour.tour.tour-update-helper-test.yml');
rmdir($config_dir . '/install');
unlink($config_dir . '/update/test_updater.yml');
unlink($config_dir . '/update/test_updater_only_delete.yml');
rmdir($config_dir . '/update');
rmdir($config_dir);
parent::tearDown();
}
public function testExecuteUpdate() {
$config_reverter = \Drupal::service('config_update.config_update');
$config_reverter
->import('field_storage_config', 'node.body');
$config_factory = \Drupal::service('config.factory');
$config = $config_factory
->getEditable('field.storage.node.body');
$expected_config_data = $config
->get();
$config_data = $config
->get();
$config_data['status'] = FALSE;
$config_data['type'] = 'text';
unset($config_data['cardinality']);
$config_data['settings'] = [
'max_length' => 123,
];
$config_data['lost_config'] = 'text';
$config
->setData($config_data)
->save(TRUE);
$update_helper = \Drupal::service('update_helper.updater');
$this
->assertFalse($this->moduleHandler
->moduleExists('help'), 'Module "help" should not be installed.');
$this
->assertEquals(NULL, $config_factory
->get('tour.tour.tour-update-helper-test')
->get('id'), 'Tour configuration should not exist.');
$this
->assertEquals('text', $config_factory
->get('field.storage.node.body')
->get('lost_config'));
$update_helper
->executeUpdate('update_helper', 'test_updater');
$this
->assertEquals($expected_config_data, $config_factory
->get('field.storage.node.body')
->get());
$this
->assertTrue($this->moduleHandler
->moduleExists('help'), 'Module "help" should be installed.');
$this
->assertEquals('tour-update-helper-test', $this->container
->get('config.factory')
->get('tour.tour.tour-update-helper-test')
->get('id'), 'Tour configuration should exist.');
}
public function testOnlyDeleteUpdate() {
$config_reverter = \Drupal::service('config_update.config_update');
$config_reverter
->import('field_storage_config', 'node.body');
$config = $this
->config('field.storage.node.body');
$expected_config_data = $config
->get();
$config_data = $expected_config_data;
$config_data['lost_config'] = 'text';
$config
->setData($config_data)
->save(TRUE);
$update_helper = \Drupal::service('update_helper.updater');
$this
->assertEquals('text', $this
->config('field.storage.node.body')
->get('lost_config'));
$update_helper
->executeUpdate('update_helper', 'test_updater_only_delete');
$this
->assertEquals($expected_config_data, $this
->config('field.storage.node.body')
->get());
}
}