class ConfigDifferTest in Configuration Update Manager 8
Tests the \Drupal\config_update\ConfigDiffer class.
@group config_update
@coversDefaultClass \Drupal\config_update\ConfigDiffer
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\config_update\Unit\ConfigUpdateUnitTestBase
- class \Drupal\Tests\config_update\Unit\ConfigDifferTest
- class \Drupal\Tests\config_update\Unit\ConfigUpdateUnitTestBase
Expanded class hierarchy of ConfigDifferTest
File
- tests/
src/ Unit/ ConfigDifferTest.php, line 14
Namespace
Drupal\Tests\config_update\UnitView source
class ConfigDifferTest extends ConfigUpdateUnitTestBase {
/**
* The config differ to test.
*
* @var \Drupal\config_update\ConfigDiffer
*/
protected $configDiffer;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->configDiffer = new ConfigDiffer($this
->getTranslationMock());
}
/**
* @covers \Drupal\config_update\ConfigDiffer::same
* @dataProvider sameProvider
*/
public function testSame($a, $b, $expected) {
$this
->assertEquals($expected, $this->configDiffer
->same($a, $b));
}
/**
* Data provider for self:testSame().
*/
public function sameProvider() {
$base = [
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
];
return [
[
$base,
$base,
TRUE,
],
// Add _core, omit uuid at top level. Should match, as both are removed
// in normalization process.
[
$base,
[
'_core' => 'foo',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
],
TRUE,
],
// Change order in top and deep level. Should match.
[
$base,
[
'uuid' => 'bar',
'b' => 0,
'a' => 'a',
'c' => [
'e' => FALSE,
'empty' => [],
'd' => TRUE,
],
],
TRUE,
],
// Add _core in deeper level. Should not match, as this is removed
// only at the top level during normalization.
[
$base,
[
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'_core' => 'do-not-use-this-key',
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
],
FALSE,
],
// Add uuid in deeper level. Should not match, as this is removed
// only at the top level during normalization.
[
$base,
[
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => FALSE,
'uuid' => 'important',
'empty' => [],
],
],
FALSE,
],
// Omit a component. Should not match.
[
$base,
[
'uuid' => 'bar',
'a' => 'a',
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
],
FALSE,
],
// Add a component. Should not match.
[
$base,
[
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
'f' => 'f',
],
FALSE,
],
// 0 should not match a string.
[
$base,
[
'_core' => 'foo',
'uuid' => 'bar',
'a' => 'a',
'b' => 'b',
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
],
FALSE,
],
// 0 should not match NULL.
[
$base,
[
'_core' => 'foo',
'uuid' => 'bar',
'a' => 'a',
'b' => NULL,
'c' => [
'd' => TRUE,
'e' => FALSE,
'empty' => [],
],
],
FALSE,
],
// FALSE should not match a string.
[
$base,
[
'_core' => 'foo',
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => 'e',
'empty' => [],
],
],
FALSE,
],
// TRUE should not match a string.
[
$base,
[
'_core' => 'foo',
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => 'd',
'e' => FALSE,
'empty' => [],
],
],
FALSE,
],
// Add an empty array at top, and remove at lower level. Should still
// match.
[
$base,
[
'_core' => 'foo',
'uuid' => 'bar',
'a' => 'a',
'b' => 0,
'c' => [
'd' => TRUE,
'e' => FALSE,
],
'empty_two' => [],
],
TRUE,
],
];
}
/**
* @covers \Drupal\config_update\ConfigDiffer::diff
*/
public function testDiff() {
$configOne = [
'uuid' => '1234-5678-90',
'id' => 'test.config.id',
'id_to_remove' => 'test.remove.id',
'type' => 'old_type',
'true_value' => TRUE,
'null_value' => NULL,
'nested_array' => [
'flat_array' => [
'value2',
'value1',
'value3',
],
'custom_key' => 'value',
],
];
$configTwo = [
'uuid' => '09-8765-4321',
'id' => 'test.config.id',
'type' => 'new_type',
'true_value' => FALSE,
'null_value' => FALSE,
'nested_array' => [
'flat_array' => [
'value2',
'value3',
],
'custom_key' => 'value',
'custom_key_2' => 'value2',
],
];
$edits = $this->configDiffer
->diff($configOne, $configTwo)
->getEdits();
$expectedEdits = [
[
'copy' => [
'orig' => [
'id : test.config.id',
],
'closing' => [
'id : test.config.id',
],
],
],
[
'delete' => [
'orig' => [
'id_to_remove : test.remove.id',
],
'closing' => FALSE,
],
],
[
'copy' => [
'orig' => [
'nested_array',
'nested_array::custom_key : value',
],
'closing' => [
'nested_array',
'nested_array::custom_key : value',
],
],
],
[
'add' => [
'orig' => FALSE,
'closing' => [
'nested_array::custom_key_2 : value2',
],
],
],
[
'copy' => [
'orig' => [
'nested_array::flat_array',
'nested_array::flat_array::0 : value2',
],
'closing' => [
'nested_array::flat_array',
'nested_array::flat_array::0 : value2',
],
],
],
[
'change' => [
'orig' => [
'nested_array::flat_array::1 : value1',
'nested_array::flat_array::2 : value3',
'null_value : null',
'true_value : true',
'type : old_type',
],
'closing' => [
'nested_array::flat_array::1 : value3',
'null_value : false',
'true_value : false',
'type : new_type',
],
],
],
];
$this
->assertEquals(count($expectedEdits), count($edits));
/** @var \Drupal\Component\Diff\Engine\DiffOp $diffOp */
foreach ($edits as $index => $diffOp) {
$this
->assertEquals($expectedEdits[$index][$diffOp->type]['orig'], $diffOp->orig);
$this
->assertEquals($expectedEdits[$index][$diffOp->type]['closing'], $diffOp->closing);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigDifferTest:: |
protected | property | The config differ to test. | |
ConfigDifferTest:: |
public | function | Data provider for self:testSame(). | |
ConfigDifferTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ConfigDifferTest:: |
public | function | @covers \Drupal\config_update\ConfigDiffer::diff | |
ConfigDifferTest:: |
public | function | @covers \Drupal\config_update\ConfigDiffer::same @dataProvider sameProvider | |
ConfigUpdateUnitTestBase:: |
protected | property | Mock config storage for the mock config factory. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of active configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of extension configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | Array of optional configuration information for mocking. | |
ConfigUpdateUnitTestBase:: |
protected | property | List of mock-dispatched events. | |
ConfigUpdateUnitTestBase:: |
protected | property | The mocked entity definition information. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock config factory class for the test. | |
ConfigUpdateUnitTestBase:: |
public | function | Gets the value of the mocked config storage. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock config storage object for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock entity manager for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Mocks the event dispatcher service. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock module handler for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock theme handler for the test. | |
ConfigUpdateUnitTestBase:: |
protected | function | Creates a mock string translation class for the test. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks event dispatch. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the getEditable() method for the mock config factory. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the getStorage() method for the entity manager. | |
ConfigUpdateUnitTestBase:: |
public | function | Mocks the translateString() method for the string translation mock object. | |
ConfigUpdateUnitTestBase:: |
public | function | Sets the value of the mocked config storage. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |