You are here

class ConfigListerTest in Configuration Update Manager 8

Tests the \Drupal\config_update\ConfigListerWithProviders class.

The methods from \Drupal\config_update\ConfigLister are also tested.

@group config_update

@coversDefaultClass \Drupal\config_update\ConfigListerWithProviders

Hierarchy

Expanded class hierarchy of ConfigListerTest

File

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

Namespace

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

  /**
   * The config lister to test.
   *
   * @var \Drupal\config_update\ConfigListerWithProviders
   */
  protected $configLister;

  /**
   * List of configuration by provider in the mocks.
   *
   * This is an array whose keys are provider names, and whose values are
   * each an array containing the provider type, an array of config items
   * mocked to be in config/install, and the same for config/optional. In
   * all cases, the first item in the array of config items should be tested
   * to be provided by that provider, and any others should not be there.
   *
   * @var array
   */
  protected $configProviderList = [
    'foo_module' => [
      'module',
      [
        'foo.barbaz.one',
        'baz.bar.one',
      ],
      [
        'foo.barbaz.two',
      ],
    ],
    'foo_theme' => [
      'theme',
      [
        'foo.bar.one',
      ],
      [
        'foo.bar.two',
      ],
    ],
    'standard' => [
      'profile',
      [
        'baz.bar.one',
      ],
      [
        'baz.bar.two',
      ],
    ],
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $lister = $this
      ->getMockBuilder('Drupal\\config_update\\ConfigListerWithProviders')
      ->setConstructorArgs([
      $this
        ->getEntityManagerMock(),
      $this
        ->getConfigStorageMock('active'),
      $this
        ->getConfigStorageMock('extension'),
      $this
        ->getConfigStorageMock('optional'),
      $this
        ->getModuleHandlerMock(),
      $this
        ->getThemeHandlerMock(),
    ])
      ->setMethods([
      'listProvidedItems',
      'getProfileName',
    ])
      ->getMock();
    $lister
      ->method('getProfileName')
      ->willReturn('standard');
    $map = [];
    foreach ($this->configProviderList as $provider => $info) {

      // Info has: [type, install storage items, optional storage items].
      // Map needs: [type, provider name, isOptional, [config items]].
      $map[] = [
        $info[0],
        $provider,
        FALSE,
        $info[1],
      ];
      $map[] = [
        $info[0],
        $provider,
        TRUE,
        $info[2],
      ];
    }
    $lister
      ->method('listProvidedItems')
      ->will($this
      ->returnValueMap($map));
    $this->configLister = $lister;
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::listConfig
   * @dataProvider listConfigProvider
   */
  public function testListConfig($a, $b, $expected) {
    $this
      ->assertEquals($expected, $this->configLister
      ->listConfig($a, $b));
  }

  /**
   * Data provider for self:testListConfig().
   */
  public function listConfigProvider() {
    return [
      // Arguments are $list_type, $name, and return value is that list of
      // configuration in active, extension, and optional storage.
      [
        'type',
        'system.all',
        [
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.three',
            'foo.barbaz.four',
            'foo.barbaz.five',
            'foo.barbaz.six',
            'something.else',
            'another.one',
          ],
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.seven',
            'foo.barbaz.four',
            'foo.barnot.three',
            'something.else',
          ],
          [
            'foo.barbaz.four',
          ],
        ],
      ],
      [
        'type',
        'system.simple',
        [
          [
            'something.else',
            'another.one',
          ],
          [
            'foo.barnot.three',
            'something.else',
          ],
          [],
        ],
      ],
      [
        'type',
        'foo',
        [
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.three',
          ],
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.seven',
          ],
          [],
        ],
      ],
      [
        'type',
        'unknown.type',
        [
          [],
          [],
          [],
        ],
      ],
      [
        'profile',
        'dummy',
        [
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.three',
            'foo.barbaz.four',
            'foo.barbaz.five',
            'foo.barbaz.six',
            'something.else',
            'another.one',
          ],
          [
            'baz.bar.one',
          ],
          [
            'baz.bar.two',
          ],
        ],
      ],
      [
        'module',
        'foo_module',
        [
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.three',
            'foo.barbaz.four',
            'foo.barbaz.five',
            'foo.barbaz.six',
            'something.else',
            'another.one',
          ],
          [
            'foo.barbaz.one',
            'baz.bar.one',
          ],
          [
            'foo.barbaz.two',
          ],
        ],
      ],
      [
        'theme',
        'foo_theme',
        [
          [
            'foo.bar.one',
            'foo.bar.two',
            'foo.bar.three',
            'foo.barbaz.four',
            'foo.barbaz.five',
            'foo.barbaz.six',
            'something.else',
            'another.one',
          ],
          [
            'foo.bar.one',
          ],
          [
            'foo.bar.two',
          ],
        ],
      ],
    ];
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::getType
   */
  public function testGetType() {
    $return = $this->configLister
      ->getType('not_in_list');
    $this
      ->assertNull($return);
    foreach ($this->entityDefinitionInformation as $info) {
      $return = $this->configLister
        ->getType($info['type']);
      $this
        ->assertEquals($return
        ->getConfigPrefix(), $info['prefix']);
    }
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::getTypeByPrefix
   */
  public function testGetTypeByPrefix() {
    $return = $this->configLister
      ->getTypeByPrefix('not_in_list');
    $this
      ->assertNull($return);
    foreach ($this->entityDefinitionInformation as $info) {
      $return = $this->configLister
        ->getTypeByPrefix($info['prefix']);
      $this
        ->assertEquals($return
        ->getConfigPrefix(), $info['prefix']);
    }
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::getTypeNameByConfigName
   */
  public function testGetTypeNameByConfigName() {
    $return = $this->configLister
      ->getTypeNameByConfigName('not_in_list');
    $this
      ->assertNull($return);
    foreach ($this->entityDefinitionInformation as $info) {
      $return = $this->configLister
        ->getTypeNameByConfigName($info['prefix'] . '.something');
      $this
        ->assertEquals($return, $info['type']);
    }
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::listTypes
   */
  public function testListTypes() {
    $return = $this->configLister
      ->listTypes();

    // Should return an array in sorted order, of just the config entities
    // that $this->getEntityManagerMock() set up.
    $expected = [
      'bar' => 'foo.barbaz',
      'baz' => 'baz.foo',
      'foo' => 'foo.bar',
    ];
    $this
      ->assertEquals(array_keys($return), array_keys($expected));
    foreach ($return as $key => $definition) {
      $this
        ->assertTrue($definition
        ->entityClassImplements('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface'));
      $this
        ->assertEquals($definition
        ->getConfigPrefix(), $expected[$key]);
    }
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::listProviders
   */
  public function testListProviders() {

    // This method's return value is not sorted in any particular way.
    $return = $this->configLister
      ->listProviders();
    $expected = [];
    foreach ($this->configProviderList as $provider => $info) {

      // Info has: [type, install storage items, optional storage items], with
      // only the first item in each list that should be present in
      // listProviders().
      // Expected needs: key is item name, value is [type, provider name].
      $expected[$info[1][0]] = [
        $info[0],
        $provider,
      ];
      $expected[$info[2][0]] = [
        $info[0],
        $provider,
      ];
    }
    ksort($return);
    ksort($expected);
    $this
      ->assertEquals($return, $expected);
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::getConfigProvider
   * @dataProvider getConfigProviderProvider
   */
  public function testGetConfigProvider($a, $expected) {
    $this
      ->assertEquals($expected, $this->configLister
      ->getConfigProvider($a));
  }

  /**
   * Data provider for self:testGetConfigProvider().
   */
  public function getConfigProviderProvider() {
    $values = [];
    foreach ($this->configProviderList as $provider => $info) {

      // Info has: [type, install storage items, optional storage items], with
      // the first item in each list that should be OK to test with
      // getConfigProvider().
      // Values needs: [item, [type, provider name]].
      $values[] = [
        $info[1][0],
        [
          $info[0],
          $provider,
        ],
      ];
      $values[] = [
        $info[2][0],
        [
          $info[0],
          $provider,
        ],
      ];
    }
    $values[] = [
      'not.a.config.item',
      NULL,
    ];
    return $values;
  }

  /**
   * @covers \Drupal\config_update\ConfigListerWithProviders::providerHasConfig
   * @dataProvider providerHasConfigProvider
   */
  public function testProviderHasConfig($a, $b, $expected) {
    $this
      ->assertEquals($expected, $this->configLister
      ->providerHasConfig($a, $b));
  }

  /**
   * Data provider for self:testProviderHasConfig().
   */
  public function providerHasConfigProvider() {
    $values = [];
    foreach ($this->configProviderList as $provider => $info) {

      // Info has: [type, install storage items, optional storage items].
      // Values needs: [type, provider name, TRUE] for valid providers,
      // change the last to FALSE for invalid providers.
      $values[] = [
        $info[0],
        $provider,
        TRUE,
      ];
      $values[] = [
        $info[0],
        $provider . '_suffix',
        FALSE,
      ];
    }
    $values[] = [
      'invalid_type',
      'foo_module',
      FALSE,
    ];
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigListerTest::$configLister protected property The config lister to test.
ConfigListerTest::$configProviderList protected property List of configuration by provider in the mocks.
ConfigListerTest::getConfigProviderProvider public function Data provider for self:testGetConfigProvider().
ConfigListerTest::listConfigProvider public function Data provider for self:testListConfig().
ConfigListerTest::providerHasConfigProvider public function Data provider for self:testProviderHasConfig().
ConfigListerTest::setUp protected function Overrides UnitTestCase::setUp
ConfigListerTest::testGetConfigProvider public function @covers \Drupal\config_update\ConfigListerWithProviders::getConfigProvider @dataProvider getConfigProviderProvider
ConfigListerTest::testGetType public function @covers \Drupal\config_update\ConfigListerWithProviders::getType
ConfigListerTest::testGetTypeByPrefix public function @covers \Drupal\config_update\ConfigListerWithProviders::getTypeByPrefix
ConfigListerTest::testGetTypeNameByConfigName public function @covers \Drupal\config_update\ConfigListerWithProviders::getTypeNameByConfigName
ConfigListerTest::testListConfig public function @covers \Drupal\config_update\ConfigListerWithProviders::listConfig @dataProvider listConfigProvider
ConfigListerTest::testListProviders public function @covers \Drupal\config_update\ConfigListerWithProviders::listProviders
ConfigListerTest::testListTypes public function @covers \Drupal\config_update\ConfigListerWithProviders::listTypes
ConfigListerTest::testProviderHasConfig public function @covers \Drupal\config_update\ConfigListerWithProviders::providerHasConfig @dataProvider providerHasConfigProvider
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.