You are here

class ConfigReverterTest in Configuration Update Manager 8

Tests the \Drupal\config_update\ConfigReverter class.

@group config_update

@coversDefaultClass \Drupal\config_update\ConfigReverter

Hierarchy

Expanded class hierarchy of ConfigReverterTest

File

tests/src/Unit/ConfigReverterTest.php, line 16

Namespace

Drupal\Tests\config_update\Unit
View source
class ConfigReverterTest extends ConfigUpdateUnitTestBase {

  /**
   * The config reverter to test.
   *
   * @var \Drupal\config_update\ConfigReverter
   */
  protected $configReverter;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->configReverter = new ConfigReverter($this
      ->getEntityManagerMock(), $this
      ->getConfigStorageMock('active'), $this
      ->getConfigStorageMock('extension'), $this
      ->getConfigStorageMock('optional'), $this
      ->getConfigFactoryMock(), $this
      ->getEventDispatcherMock());
  }

  /**
   * @covers \Drupal\config_update\ConfigReverter::getFromActive
   * @dataProvider getFromActiveProvider
   */
  public function testGetFromActive($a, $b, $expected) {
    $this
      ->assertEquals($expected, $this->configReverter
      ->getFromActive($a, $b));
  }

  /**
   * Data provider for self:testGetFromActive().
   */
  public function getFromActiveProvider() {
    return [
      // Arguments are $type, $name, and return value is the config.
      // Some config items that are already prefixed.
      [
        '',
        'foo.bar.one',
        [
          'foo.bar.one' => 'active',
          'id' => 'one',
        ],
      ],
      [
        'system.simple',
        'foo.bar.one',
        [
          'foo.bar.one' => 'active',
          'id' => 'one',
        ],
      ],
      // Config item with a defined entity definition prefix. Entity type 'foo'
      // has prefix 'foo.bar'.
      [
        'foo',
        'one',
        [
          'foo.bar.one' => 'active',
          'id' => 'one',
        ],
      ],
      // Unknown type. This should not generate a call into the config read,
      // so should not return the known value.
      [
        'unknown',
        'foo.bar.one',
        FALSE,
      ],
      // Missing configuration. Config mock is configured to return FALSE for
      // this particular config name.
      [
        'system.simple',
        'missing',
        FALSE,
      ],
    ];
  }

  /**
   * @covers \Drupal\config_update\ConfigReverter::getFromExtension
   * @dataProvider getFromExtensionProvider
   */
  public function testGetFromExtension($a, $b, $expected) {
    $this
      ->assertEquals($expected, $this->configReverter
      ->getFromExtension($a, $b));
  }

  /**
   * Data provider for self:testGetFromExtension().
   */
  public function getFromExtensionProvider() {
    return [
      // Arguments are $type, $name, and return value is the config.
      // Some config items that are already prefixed, and exist in the mock
      // extension storage.
      [
        '',
        'in.extension',
        [
          'in.extension' => 'extension',
        ],
      ],
      [
        'system.simple',
        'in.extension',
        [
          'in.extension' => 'extension',
        ],
      ],
      // Config item with a defined entity definition prefix. Entity type 'foo'
      // has prefix 'foo.bar'.
      [
        'foo',
        'one',
        [
          'foo.bar.one' => 'extension',
          'id' => 'one',
        ],
      ],
      // One that exists in both extension and optional storage.
      [
        'system.simple',
        'in.both',
        [
          'in.both' => 'extension',
        ],
      ],
      // One that exists only in optional storage.
      [
        'system.simple',
        'in.optional',
        [
          'in.optional' => 'optional',
        ],
      ],
      // Unknown type. This should not generate a call into the config read,
      // so should not return the known value.
      [
        'unknown',
        'in.extension',
        FALSE,
      ],
      // Missing configuration. Storage mock is configured to return FALSE for
      // this particular config name.
      [
        'system.simple',
        'missing2',
        FALSE,
      ],
    ];
  }

  /**
   * @covers \Drupal\config_update\ConfigReverter::import
   * @dataProvider importProvider
   */
  public function testImport($type, $name, $config_name, $expected, $config_before, $config_after) {

    // Clear dispatch log and set pre-config.
    $this->dispatchedEvents = [];
    if ($config_name) {
      $this->configStorage[$config_name] = $config_before;
    }
    $save_config = $this->configStorage;

    // Call the importer and test the Boolean result.
    $result = $this->configReverter
      ->import($type, $name);
    $this
      ->assertEquals($result, $expected);
    if ($result) {

      // Verify that the config is correct after import, and logging worked.
      $this
        ->assertEquals($this->configStorage[$config_name], $config_after);
      $this
        ->assertEquals(count($this->dispatchedEvents), 1);
      $this
        ->assertEquals($this->dispatchedEvents[0][0], ConfigRevertInterface::IMPORT);
    }
    else {

      // Verify that the config didn't change and no events were logged.
      $this
        ->assertEquals($this->configStorage, $save_config);
      $this
        ->assertEquals(count($this->dispatchedEvents), 0);
    }
  }

  /**
   * Data provider for self:testImport().
   */
  public function importProvider() {
    return [
      // Elements: type, name, config name, return value,
      // config to set up before, config expected after. See also
      // getFromExtensionProvider().
      [
        'system.simple',
        'in.extension',
        'in.extension',
        TRUE,
        [
          'in.extension' => 'before',
        ],
        [
          'in.extension' => 'extension',
          '_core' => 'core_for_in.extension',
        ],
      ],
      [
        'foo',
        'one',
        'foo.bar.one',
        TRUE,
        [
          'foo.bar.one' => 'before',
          'id' => 'one',
        ],
        [
          'foo.bar.one' => 'extension',
          'id' => 'one',
          '_core' => 'core_for_foo.bar.one',
        ],
      ],
      [
        'system.simple',
        'in.both',
        'in.both',
        TRUE,
        [
          'in.both' => 'before',
        ],
        [
          'in.both' => 'extension',
          '_core' => 'core_for_in.both',
        ],
      ],
      [
        'system.simple',
        'in.optional',
        'in.optional',
        TRUE,
        [
          'in.optional' => 'before',
        ],
        [
          'in.optional' => 'optional',
          '_core' => 'core_for_in.optional',
        ],
      ],
      [
        'unknown',
        'in.extension',
        FALSE,
        FALSE,
        FALSE,
        FALSE,
      ],
      [
        'system.simple',
        'missing2',
        'missing2',
        FALSE,
        FALSE,
        FALSE,
      ],
    ];
  }

  /**
   * @covers \Drupal\config_update\ConfigReverter::revert
   * @dataProvider revertProvider
   */
  public function testRevert($type, $name, $config_name, $expected, $config_before, $config_after) {

    // Clear dispatch log and set pre-config.
    $this->dispatchedEvents = [];
    if ($config_name) {
      $this->configStorage[$config_name] = $config_before;
    }
    $save_config = $this->configStorage;

    // Call the reverter and test the Boolean result.
    $result = $this->configReverter
      ->revert($type, $name);
    $this
      ->assertEquals($result, $expected);
    if ($result) {

      // Verify that the config is correct after revert, and logging worked.
      $this
        ->assertEquals($this->configStorage[$config_name], $config_after);
      $this
        ->assertEquals(count($this->dispatchedEvents), 1);
      $this
        ->assertEquals($this->dispatchedEvents[0][0], ConfigRevertInterface::REVERT);
    }
    else {

      // Verify that the config didn't change and no events were logged.
      $this
        ->assertEquals($this->configStorage, $save_config);
      $this
        ->assertEquals(count($this->dispatchedEvents), 0);
    }
  }

  /**
   * Data provider for self:testRevert().
   */
  public function revertProvider() {
    return [
      // Elements: type, name, config name, return value,
      // config to set up before, config expected after. See also
      // getFromExtensionProvider().
      [
        'system.simple',
        'in.extension',
        'in.extension',
        TRUE,
        [
          'in.extension' => 'active',
        ],
        [
          'in.extension' => 'extension',
          '_core' => 'core_for_in.extension',
        ],
      ],
      [
        'foo',
        'one',
        'foo.bar.one',
        TRUE,
        [
          'foo.bar.one' => 'active',
          'id' => 'one',
        ],
        [
          'foo.bar.one' => 'extension',
          'id' => 'one',
          '_core' => 'core_for_foo.bar.one',
        ],
      ],
      [
        'system.simple',
        'in.both',
        'in.both',
        TRUE,
        [
          'in.both' => 'active',
        ],
        [
          'in.both' => 'extension',
          '_core' => 'core_for_in.both',
        ],
      ],
      [
        'system.simple',
        'in.optional',
        'in.optional',
        TRUE,
        [
          'in.optional' => 'active',
        ],
        [
          'in.optional' => 'optional',
          '_core' => 'core_for_in.optional',
        ],
      ],
      [
        'unknown',
        'in.extension',
        FALSE,
        FALSE,
        FALSE,
        FALSE,
      ],
      // Missing from extension storage.
      [
        'system.simple',
        'missing2',
        'missing2',
        FALSE,
        FALSE,
        FALSE,
      ],
      // Present in extension storage but missing from active storage.
      [
        'system.simple',
        'another',
        'another',
        FALSE,
        FALSE,
        FALSE,
      ],
    ];
  }

  /**
   * @covers \Drupal\config_update\ConfigReverter::delete
   * @dataProvider deleteProvider
   */
  public function testDelete($type, $name, $config_name, $expected) {

    // Clear dispatch log.
    $this->dispatchedEvents = [];
    $save_config = $this->configStorage;

    // Call the deleteer and test the Boolean result.
    $result = $this->configReverter
      ->delete($type, $name);
    $this
      ->assertEquals($result, $expected);
    if ($result) {

      // Verify that the config is missing after delete, and logging worked.
      $this
        ->assertTrue(!isset($this->configStorage[$config_name]));
      $this
        ->assertEquals(count($this->dispatchedEvents), 1);
      $this
        ->assertEquals($this->dispatchedEvents[0][0], ConfigDeleteInterface::DELETE);
    }
    else {

      // Verify that the config didn't change and no events were logged.
      $this
        ->assertEquals($this->configStorage, $save_config);
      $this
        ->assertEquals(count($this->dispatchedEvents), 0);
    }
  }

  /**
   * Data provider for self:testDelete().
   */
  public function deleteProvider() {
    return [
      // Elements: type, name, config name, return value.
      [
        'system.simple',
        'in.extension',
        'in.extension',
        TRUE,
      ],
      [
        'foo',
        'one',
        'foo.bar.one',
        TRUE,
      ],
      [
        'unknown',
        'in.extension',
        FALSE,
        FALSE,
      ],
      [
        'system.simple',
        'missing2',
        'missing2',
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigReverterTest::$configReverter protected property The config reverter to test.
ConfigReverterTest::deleteProvider public function Data provider for self:testDelete().
ConfigReverterTest::getFromActiveProvider public function Data provider for self:testGetFromActive().
ConfigReverterTest::getFromExtensionProvider public function Data provider for self:testGetFromExtension().
ConfigReverterTest::importProvider public function Data provider for self:testImport().
ConfigReverterTest::revertProvider public function Data provider for self:testRevert().
ConfigReverterTest::setUp protected function Overrides UnitTestCase::setUp
ConfigReverterTest::testDelete public function @covers \Drupal\config_update\ConfigReverter::delete @dataProvider deleteProvider
ConfigReverterTest::testGetFromActive public function @covers \Drupal\config_update\ConfigReverter::getFromActive @dataProvider getFromActiveProvider
ConfigReverterTest::testGetFromExtension public function @covers \Drupal\config_update\ConfigReverter::getFromExtension @dataProvider getFromExtensionProvider
ConfigReverterTest::testImport public function @covers \Drupal\config_update\ConfigReverter::import @dataProvider importProvider
ConfigReverterTest::testRevert public function @covers \Drupal\config_update\ConfigReverter::revert @dataProvider revertProvider
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.