View source
<?php
namespace Drupal\Tests\update_helper\Kernel;
use Drupal\KernelTests\KernelTestBase;
class ReversibleConfigDifferTest extends KernelTestBase {
protected static $modules = [
'config_update',
'update_helper',
'user',
];
public function testSame(array $configOne, array $configTwo, $expected) {
$configDiffer = \Drupal::service('update_helper.config_differ');
$result = $configDiffer
->same($configOne, $configTwo);
$this
->assertEquals($expected, $result);
}
public function sameDataProvider() {
return [
[
[
'uuid' => '1234-5678-90',
'_core' => 'core_id_1',
'id' => 'test.config.id',
'short_text' => 'en',
'long_text' => 'Automated tests for the ConfigDiffTransformer service.',
'true_value' => TRUE,
'false_value' => FALSE,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
'1234-5678-90' => [
'uuid' => '1234-5678-90',
],
'empty_array' => [],
'empty_string' => '',
'null_value' => NULL,
],
[
'uuid' => '09-8765-4321',
'_core' => 'core_id_2',
'id' => 'test.config.id',
'short_text' => 'en',
'long_text' => 'Automated tests for the ConfigDiffTransformer service.',
'true_value' => TRUE,
'false_value' => FALSE,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
'1234-5678-90' => [
'uuid' => '1234-5678-90',
],
'empty_array' => [],
'empty_string' => '',
'null_value' => NULL,
],
TRUE,
],
[
[
'uuid' => '1234-5678-90',
'_core' => 'core_id_1',
'id' => 'test.config.id',
'short_text' => 'en',
'long_text' => 'Automated tests for the ConfigDiffTransformer service.',
'true_value' => TRUE,
'false_value' => FALSE,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
'1234-5678-90' => [
'uuid' => '1234-5678-90',
],
'empty_array' => [],
'empty_string' => '',
'null_value' => NULL,
],
[
'uuid' => '09-8765-4321',
'_core' => 'core_id_2',
'id' => 'test.config.id',
'short_text' => 'en',
'long_text' => 'Automated tests for the ConfigDiffTransformer service.',
'true_value' => TRUE,
'false_value' => FALSE,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
'1234-5678-90' => [
'uuid' => '09-8765-4321',
],
'empty_array' => [],
'empty_string' => '',
'null_value' => NULL,
],
FALSE,
],
];
}
public function testDiff() {
$configDiffer = \Drupal::service('update_helper.config_differ');
$configOne = [
'uuid' => '1234-5678-90',
'_core' => 'core_id_1',
'id' => 'test.config.id',
'short_text' => 'en',
'true_value' => TRUE,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
'main_data' => [
'uuid' => '1234-5678-90',
],
];
$configTwo = [
'uuid' => '09-8765-4321',
'_core' => 'core_id_2',
'id' => 'test.config.id',
'short_text' => 'en',
'true_value' => TRUE,
'nested_array' => [
'flat_array' => [
'value2',
'value3',
],
'custom_key' => 'value',
'new_custom_key' => 'value',
],
'main_data' => [
'uuid' => '09-8765-4321',
],
];
$edits = $configDiffer
->diff($configOne, $configTwo)
->getEdits();
$expectedEdits = [
[
'copy' => [
'orig' => [
'id : s:14:"test.config.id";',
'main_data',
],
'closing' => [
'id : s:14:"test.config.id";',
'main_data',
],
],
],
[
'change' => [
'orig' => [
'main_data::uuid : s:12:"1234-5678-90";',
],
'closing' => [
'main_data::uuid : s:12:"09-8765-4321";',
],
],
],
[
'copy' => [
'orig' => [
'nested_array',
'nested_array::custom_key : s:5:"value";',
'nested_array::flat_array',
'nested_array::flat_array::- : s:6:"value2";',
],
'closing' => [
'nested_array',
'nested_array::custom_key : s:5:"value";',
'nested_array::flat_array',
'nested_array::flat_array::- : s:6:"value2";',
],
],
],
[
'delete' => [
'orig' => [
'nested_array::flat_array::- : s:6:"value1";',
],
'closing' => FALSE,
],
],
[
'copy' => [
'orig' => [
'nested_array::flat_array::- : s:6:"value3";',
],
'closing' => [
'nested_array::flat_array::- : s:6:"value3";',
],
],
],
[
'add' => [
'orig' => FALSE,
'closing' => [
'nested_array::new_custom_key : s:5:"value";',
],
],
],
[
'copy' => [
'orig' => [
'short_text : s:2:"en";',
'true_value : b:1;',
],
'closing' => [
'short_text : s:2:"en";',
'true_value : b:1;',
],
],
],
];
$this
->assertEquals(count($expectedEdits), count($edits));
foreach ($edits as $index => $diffOp) {
$this
->assertEquals($expectedEdits[$index][$diffOp->type]['orig'], $diffOp->orig);
$this
->assertEquals($expectedEdits[$index][$diffOp->type]['closing'], $diffOp->closing);
}
}
}