You are here

class ConfigDifferTest in Configuration Update Manager 8

Tests the \Drupal\config_update\ConfigDiffer class.

@group config_update

@coversDefaultClass \Drupal\config_update\ConfigDiffer

Hierarchy

Expanded class hierarchy of ConfigDifferTest

File

tests/src/Unit/ConfigDifferTest.php, line 14

Namespace

Drupal\Tests\config_update\Unit
View 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

Namesort descending Modifiers Type Description Overrides
ConfigDifferTest::$configDiffer protected property The config differ to test.
ConfigDifferTest::sameProvider public function Data provider for self:testSame().
ConfigDifferTest::setUp protected function Overrides UnitTestCase::setUp
ConfigDifferTest::testDiff public function @covers \Drupal\config_update\ConfigDiffer::diff
ConfigDifferTest::testSame public function @covers \Drupal\config_update\ConfigDiffer::same @dataProvider sameProvider
ConfigUpdateUnitTestBase::$configStorage protected property Mock config storage for the mock config factory.
ConfigUpdateUnitTestBase::$configStorageActiveInfo protected property Array of active configuration information for mocking.
ConfigUpdateUnitTestBase::$configStorageExtensionInfo protected property Array of extension configuration information for mocking.
ConfigUpdateUnitTestBase::$configStorageOptionalInfo protected property Array of optional configuration information for mocking.
ConfigUpdateUnitTestBase::$dispatchedEvents protected property List of mock-dispatched events.
ConfigUpdateUnitTestBase::$entityDefinitionInformation protected property The mocked entity definition information.
ConfigUpdateUnitTestBase::getConfigFactoryMock protected function Creates a mock config factory class for the test.
ConfigUpdateUnitTestBase::getConfigStorage public function Gets the value of the mocked config storage.
ConfigUpdateUnitTestBase::getConfigStorageMock protected function Creates a mock config storage object for the test.
ConfigUpdateUnitTestBase::getEntityManagerMock protected function Creates a mock entity manager for the test.
ConfigUpdateUnitTestBase::getEventDispatcherMock protected function Mocks the event dispatcher service.
ConfigUpdateUnitTestBase::getModuleHandlerMock protected function Creates a mock module handler for the test.
ConfigUpdateUnitTestBase::getThemeHandlerMock protected function Creates a mock theme handler for the test.
ConfigUpdateUnitTestBase::getTranslationMock protected function Creates a mock string translation class for the test.
ConfigUpdateUnitTestBase::mockDispatch public function Mocks event dispatch.
ConfigUpdateUnitTestBase::mockGetEditable public function Mocks the getEditable() method for the mock config factory.
ConfigUpdateUnitTestBase::mockGetStorage public function Mocks the getStorage() method for the entity manager.
ConfigUpdateUnitTestBase::mockTranslate public function Mocks the translateString() method for the string translation mock object.
ConfigUpdateUnitTestBase::setConfigStorage public function Sets the value of the mocked config storage.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.