ConfigListerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace Drupal\Tests\config_update\Unit;
  3. /**
  4. * Tests the \Drupal\config_update\ConfigListerWithProviders class.
  5. *
  6. * The methods from \Drupal\config_update\ConfigLister are also tested.
  7. *
  8. * @group config_update
  9. *
  10. * @coversDefaultClass \Drupal\config_update\ConfigListerWithProviders
  11. */
  12. class ConfigListerTest extends ConfigUpdateUnitTestBase {
  13. /**
  14. * The config lister to test.
  15. *
  16. * @var \Drupal\config_update\ConfigListerWithProviders
  17. */
  18. protected $configLister;
  19. /**
  20. * List of configuration by provider in the mocks.
  21. *
  22. * This is an array whose keys are provider names, and whose values are
  23. * each an array containing the provider type, the name of one config item
  24. * mocked to be in config/install, and one in config/optional.
  25. *
  26. * @var array
  27. */
  28. protected $configProviderList = [
  29. 'foo_module' => ['module', 'foo.barbaz.one', 'foo.barbaz.two'],
  30. 'foo_theme' => ['theme', 'foo.bar.one', 'foo.bar.two'],
  31. 'standard' => ['profile', 'baz.bar.one', 'baz.bar.two'],
  32. ];
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function setUp() {
  37. $lister = $this->getMockBuilder('Drupal\config_update\ConfigListerWithProviders')
  38. ->setConstructorArgs([
  39. $this->getEntityManagerMock(),
  40. $this->getConfigStorageMock('active'),
  41. $this->getConfigStorageMock('extension'),
  42. $this->getConfigStorageMock('optional'),
  43. $this->getModuleHandlerMock(),
  44. $this->getThemeHandlerMock(),
  45. ])
  46. ->setMethods(['listProvidedItems', 'getProfileName'])
  47. ->getMock();
  48. $lister->method('getProfileName')
  49. ->willReturn('standard');
  50. $map = [];
  51. foreach ($this->configProviderList as $provider => $info) {
  52. // Info has: [type, install storage item, optional storage item].
  53. // Map needs: [type, provider name, isOptional, [config items]].
  54. $map[] = [$info[0], $provider, FALSE, [$info[1]]];
  55. $map[] = [$info[0], $provider, TRUE, [$info[2]]];
  56. }
  57. $lister->method('listProvidedItems')
  58. ->will($this->returnValueMap($map));
  59. $this->configLister = $lister;
  60. }
  61. /**
  62. * @covers \Drupal\config_update\ConfigListerWithProviders::listConfig
  63. * @dataProvider listConfigProvider
  64. */
  65. public function testListConfig($a, $b, $expected) {
  66. $this->assertEquals($expected, $this->configLister->listConfig($a, $b));
  67. }
  68. /**
  69. * Data provider for self:testListConfig().
  70. */
  71. public function listConfigProvider() {
  72. return [
  73. // Arguments are $list_type, $name, and return value is that list of
  74. // configuration in active, extension, and optional storage.
  75. ['type', 'system.all',
  76. [
  77. [
  78. 'foo.bar.one',
  79. 'foo.bar.two',
  80. 'foo.bar.three',
  81. 'foo.barbaz.four',
  82. 'foo.barbaz.five',
  83. 'foo.barbaz.six',
  84. 'something.else',
  85. 'another.one',
  86. ],
  87. [
  88. 'foo.bar.one',
  89. 'foo.bar.two',
  90. 'foo.bar.seven',
  91. 'foo.barbaz.four',
  92. 'foo.barnot.three',
  93. 'something.else',
  94. ],
  95. ['foo.barbaz.four'],
  96. ],
  97. ],
  98. ['type', 'system.simple',
  99. [
  100. ['something.else', 'another.one'],
  101. ['foo.barnot.three', 'something.else'],
  102. [],
  103. ],
  104. ],
  105. ['type', 'foo',
  106. [
  107. ['foo.bar.one', 'foo.bar.two', 'foo.bar.three'],
  108. ['foo.bar.one', 'foo.bar.two', 'foo.bar.seven'],
  109. [],
  110. ],
  111. ],
  112. ['type', 'unknown.type', [[], [], []]],
  113. ['profile', 'dummy',
  114. [
  115. [
  116. 'foo.bar.one',
  117. 'foo.bar.two',
  118. 'foo.bar.three',
  119. 'foo.barbaz.four',
  120. 'foo.barbaz.five',
  121. 'foo.barbaz.six',
  122. 'something.else',
  123. 'another.one',
  124. ],
  125. ['baz.bar.one'],
  126. ['baz.bar.two'],
  127. ],
  128. ],
  129. ['module', 'foo_module',
  130. [
  131. [
  132. 'foo.bar.one',
  133. 'foo.bar.two',
  134. 'foo.bar.three',
  135. 'foo.barbaz.four',
  136. 'foo.barbaz.five',
  137. 'foo.barbaz.six',
  138. 'something.else',
  139. 'another.one',
  140. ],
  141. ['foo.barbaz.one'],
  142. ['foo.barbaz.two'],
  143. ],
  144. ],
  145. ['theme', 'foo_theme',
  146. [
  147. [
  148. 'foo.bar.one',
  149. 'foo.bar.two',
  150. 'foo.bar.three',
  151. 'foo.barbaz.four',
  152. 'foo.barbaz.five',
  153. 'foo.barbaz.six',
  154. 'something.else',
  155. 'another.one',
  156. ],
  157. ['foo.bar.one'],
  158. ['foo.bar.two'],
  159. ],
  160. ],
  161. ];
  162. }
  163. /**
  164. * @covers \Drupal\config_update\ConfigListerWithProviders::getType
  165. */
  166. public function testGetType() {
  167. $return = $this->configLister->getType('not_in_list');
  168. $this->assertNull($return);
  169. foreach ($this->entityDefinitionInformation as $info) {
  170. $return = $this->configLister->getType($info['type']);
  171. $this->assertEquals($return->getConfigPrefix(), $info['prefix']);
  172. }
  173. }
  174. /**
  175. * @covers \Drupal\config_update\ConfigListerWithProviders::getTypeByPrefix
  176. */
  177. public function testGetTypeByPrefix() {
  178. $return = $this->configLister->getTypeByPrefix('not_in_list');
  179. $this->assertNull($return);
  180. foreach ($this->entityDefinitionInformation as $info) {
  181. $return = $this->configLister->getTypeByPrefix($info['prefix']);
  182. $this->assertEquals($return->getConfigPrefix(), $info['prefix']);
  183. }
  184. }
  185. /**
  186. * @covers \Drupal\config_update\ConfigListerWithProviders::getTypeNameByConfigName
  187. */
  188. public function testGetTypeNameByConfigName() {
  189. $return = $this->configLister->getTypeNameByConfigName('not_in_list');
  190. $this->assertNull($return);
  191. foreach ($this->entityDefinitionInformation as $info) {
  192. $return = $this->configLister->getTypeNameByConfigName($info['prefix'] . '.something');
  193. $this->assertEquals($return, $info['type']);
  194. }
  195. }
  196. /**
  197. * @covers \Drupal\config_update\ConfigListerWithProviders::listTypes
  198. */
  199. public function testListTypes() {
  200. $return = $this->configLister->listTypes();
  201. // Should return an array in sorted order, of just the config entities
  202. // that $this->getEntityManagerMock() set up.
  203. $expected = ['bar' => 'foo.barbaz', 'baz' => 'baz.foo', 'foo' => 'foo.bar'];
  204. $this->assertEquals(array_keys($return), array_keys($expected));
  205. foreach ($return as $key => $definition) {
  206. $this->assertTrue($definition->entityClassImplements('Drupal\Core\Config\Entity\ConfigEntityInterface'));
  207. $this->assertEquals($definition->getConfigPrefix(), $expected[$key]);
  208. }
  209. }
  210. /**
  211. * @covers \Drupal\config_update\ConfigListerWithProviders::listProviders
  212. */
  213. public function testListProviders() {
  214. // This method's return value is not sorted in any particular way.
  215. $return = $this->configLister->listProviders();
  216. $expected = [];
  217. foreach ($this->configProviderList as $provider => $info) {
  218. // Info has: [type, install storage item, optional storage item].
  219. // Expected needs: key is item name, value is [type, provider name].
  220. $expected[$info[1]] = [$info[0], $provider];
  221. $expected[$info[2]] = [$info[0], $provider];
  222. }
  223. ksort($return);
  224. ksort($expected);
  225. $this->assertEquals($return, $expected);
  226. }
  227. /**
  228. * @covers \Drupal\config_update\ConfigListerWithProviders::getConfigProvider
  229. * @dataProvider getConfigProviderProvider
  230. */
  231. public function testGetConfigProvider($a, $expected) {
  232. $this->assertEquals($expected, $this->configLister->getConfigProvider($a));
  233. }
  234. /**
  235. * Data provider for self:testGetConfigProvider().
  236. */
  237. public function getConfigProviderProvider() {
  238. $values = [];
  239. foreach ($this->configProviderList as $provider => $info) {
  240. // Info has: [type, install storage item, optional storage item].
  241. // Values needs: [item, [type, provider name]].
  242. $values[] = [$info[1], [$info[0], $provider]];
  243. $values[] = [$info[2], [$info[0], $provider]];
  244. }
  245. $values[] = ['not.a.config.item', NULL];
  246. return $values;
  247. }
  248. /**
  249. * @covers \Drupal\config_update\ConfigListerWithProviders::providerHasConfig
  250. * @dataProvider providerHasConfigProvider
  251. */
  252. public function testProviderHasConfig($a, $b, $expected) {
  253. $this->assertEquals($expected, $this->configLister->providerHasConfig($a, $b));
  254. }
  255. /**
  256. * Data provider for self:testProviderHasConfig().
  257. */
  258. public function providerHasConfigProvider() {
  259. $values = [];
  260. foreach ($this->configProviderList as $provider => $info) {
  261. // Info has: [type, install storage item, optional storage item].
  262. // Values needs: [type, provider name, TRUE] for valid providers,
  263. // change the last to FALSE for invalid providers.
  264. $values[] = [$info[0], $provider, TRUE];
  265. $values[] = [$info[0], $provider . '_suffix', FALSE];
  266. }
  267. $values[] = ['invalid_type', 'foo_module', FALSE];
  268. return $values;
  269. }
  270. }