ConfigReverterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace Drupal\Tests\config_update\Unit;
  3. use Drupal\config_update\ConfigReverter;
  4. use Drupal\config_update\ConfigRevertInterface;
  5. use Drupal\config_update\ConfigDeleteInterface;
  6. /**
  7. * Tests the \Drupal\config_update\ConfigReverter class.
  8. *
  9. * @group config_update
  10. *
  11. * @coversDefaultClass \Drupal\config_update\ConfigReverter
  12. */
  13. class ConfigReverterTest extends ConfigUpdateUnitTestBase {
  14. /**
  15. * The config reverter to test.
  16. *
  17. * @var \Drupal\config_update\ConfigReverter
  18. */
  19. protected $configReverter;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function setUp() {
  24. $this->configReverter = new ConfigReverter(
  25. $this->getEntityManagerMock(),
  26. $this->getConfigStorageMock('active'),
  27. $this->getConfigStorageMock('extension'),
  28. $this->getConfigStorageMock('optional'),
  29. $this->getConfigFactoryMock(),
  30. $this->getEventDispatcherMock());
  31. }
  32. /**
  33. * @covers \Drupal\config_update\ConfigReverter::getFromActive
  34. * @dataProvider getFromActiveProvider
  35. */
  36. public function testGetFromActive($a, $b, $expected) {
  37. $this->assertEquals($expected, $this->configReverter->getFromActive($a, $b));
  38. }
  39. /**
  40. * Data provider for self:testGetFromActive().
  41. */
  42. public function getFromActiveProvider() {
  43. return [
  44. // Arguments are $type, $name, and return value is the config.
  45. // Some config items that are already prefixed.
  46. ['', 'foo.bar.one', ['foo.bar.one' => 'active', 'id' => 'one']],
  47. ['system.simple', 'foo.bar.one',
  48. ['foo.bar.one' => 'active', 'id' => 'one'],
  49. ],
  50. // Config item with a defined entity definition prefix. Entity type 'foo'
  51. // has prefix 'foo.bar'.
  52. ['foo', 'one', ['foo.bar.one' => 'active', 'id' => 'one']],
  53. // Unknown type. This should not generate a call into the config read,
  54. // so should not return the known value.
  55. ['unknown', 'foo.bar.one', FALSE],
  56. // Missing configuration. Config mock is configured to return FALSE for
  57. // this particular config name.
  58. ['system.simple', 'missing', FALSE],
  59. ];
  60. }
  61. /**
  62. * @covers \Drupal\config_update\ConfigReverter::getFromExtension
  63. * @dataProvider getFromExtensionProvider
  64. */
  65. public function testGetFromExtension($a, $b, $expected) {
  66. $this->assertEquals($expected, $this->configReverter->getFromExtension($a, $b));
  67. }
  68. /**
  69. * Data provider for self:testGetFromExtension().
  70. */
  71. public function getFromExtensionProvider() {
  72. return [
  73. // Arguments are $type, $name, and return value is the config.
  74. // Some config items that are already prefixed, and exist in the mock
  75. // extension storage.
  76. ['', 'in.extension', ['in.extension' => 'extension']],
  77. ['system.simple', 'in.extension', ['in.extension' => 'extension']],
  78. // Config item with a defined entity definition prefix. Entity type 'foo'
  79. // has prefix 'foo.bar'.
  80. ['foo', 'one', ['foo.bar.one' => 'extension', 'id' => 'one']],
  81. // One that exists in both extension and optional storage.
  82. ['system.simple', 'in.both', ['in.both' => 'extension']],
  83. // One that exists only in optional storage.
  84. ['system.simple', 'in.optional', ['in.optional' => 'optional']],
  85. // Unknown type. This should not generate a call into the config read,
  86. // so should not return the known value.
  87. ['unknown', 'in.extension', FALSE],
  88. // Missing configuration. Storage mock is configured to return FALSE for
  89. // this particular config name.
  90. ['system.simple', 'missing2', FALSE],
  91. ];
  92. }
  93. /**
  94. * @covers \Drupal\config_update\ConfigReverter::import
  95. * @dataProvider importProvider
  96. */
  97. public function testImport($type, $name, $config_name, $expected, $config_before, $config_after) {
  98. // Clear dispatch log and set pre-config.
  99. $this->dispatchedEvents = [];
  100. if ($config_name) {
  101. $this->configStorage[$config_name] = $config_before;
  102. }
  103. $save_config = $this->configStorage;
  104. // Call the importer and test the Boolean result.
  105. $result = $this->configReverter->import($type, $name);
  106. $this->assertEquals($result, $expected);
  107. if ($result) {
  108. // Verify that the config is correct after import, and logging worked.
  109. $this->assertEquals($this->configStorage[$config_name], $config_after);
  110. $this->assertEquals(count($this->dispatchedEvents), 1);
  111. $this->assertEquals($this->dispatchedEvents[0][0], ConfigRevertInterface::IMPORT);
  112. }
  113. else {
  114. // Verify that the config didn't change and no events were logged.
  115. $this->assertEquals($this->configStorage, $save_config);
  116. $this->assertEquals(count($this->dispatchedEvents), 0);
  117. }
  118. }
  119. /**
  120. * Data provider for self:testImport().
  121. */
  122. public function importProvider() {
  123. return [
  124. // Elements: type, name, config name, return value,
  125. // config to set up before, config expected after. See also
  126. // getFromExtensionProvider().
  127. [
  128. 'system.simple',
  129. 'in.extension',
  130. 'in.extension',
  131. TRUE,
  132. ['in.extension' => 'before'],
  133. ['in.extension' => 'extension', '_core' => 'core_for_in.extension'],
  134. ],
  135. [
  136. 'foo',
  137. 'one',
  138. 'foo.bar.one',
  139. TRUE,
  140. ['foo.bar.one' => 'before', 'id' => 'one'],
  141. [
  142. 'foo.bar.one' => 'extension',
  143. 'id' => 'one',
  144. '_core' => 'core_for_foo.bar.one',
  145. ],
  146. ],
  147. [
  148. 'system.simple',
  149. 'in.both',
  150. 'in.both',
  151. TRUE,
  152. ['in.both' => 'before'],
  153. ['in.both' => 'extension', '_core' => 'core_for_in.both'],
  154. ],
  155. [
  156. 'system.simple',
  157. 'in.optional',
  158. 'in.optional',
  159. TRUE,
  160. ['in.optional' => 'before'],
  161. ['in.optional' => 'optional', '_core' => 'core_for_in.optional'],
  162. ],
  163. [
  164. 'unknown',
  165. 'in.extension',
  166. FALSE,
  167. FALSE,
  168. FALSE,
  169. FALSE,
  170. ],
  171. [
  172. 'system.simple',
  173. 'missing2',
  174. 'missing2',
  175. FALSE,
  176. FALSE,
  177. FALSE,
  178. ],
  179. ];
  180. }
  181. /**
  182. * @covers \Drupal\config_update\ConfigReverter::revert
  183. * @dataProvider revertProvider
  184. */
  185. public function testRevert($type, $name, $config_name, $expected, $config_before, $config_after) {
  186. // Clear dispatch log and set pre-config.
  187. $this->dispatchedEvents = [];
  188. if ($config_name) {
  189. $this->configStorage[$config_name] = $config_before;
  190. }
  191. $save_config = $this->configStorage;
  192. // Call the reverter and test the Boolean result.
  193. $result = $this->configReverter->revert($type, $name);
  194. $this->assertEquals($result, $expected);
  195. if ($result) {
  196. // Verify that the config is correct after revert, and logging worked.
  197. $this->assertEquals($this->configStorage[$config_name], $config_after);
  198. $this->assertEquals(count($this->dispatchedEvents), 1);
  199. $this->assertEquals($this->dispatchedEvents[0][0], ConfigRevertInterface::REVERT);
  200. }
  201. else {
  202. // Verify that the config didn't change and no events were logged.
  203. $this->assertEquals($this->configStorage, $save_config);
  204. $this->assertEquals(count($this->dispatchedEvents), 0);
  205. }
  206. }
  207. /**
  208. * Data provider for self:testRevert().
  209. */
  210. public function revertProvider() {
  211. return [
  212. // Elements: type, name, config name, return value,
  213. // config to set up before, config expected after. See also
  214. // getFromExtensionProvider().
  215. [
  216. 'system.simple',
  217. 'in.extension',
  218. 'in.extension',
  219. TRUE,
  220. ['in.extension' => 'active'],
  221. ['in.extension' => 'extension', '_core' => 'core_for_in.extension'],
  222. ],
  223. [
  224. 'foo',
  225. 'one',
  226. 'foo.bar.one',
  227. TRUE,
  228. ['foo.bar.one' => 'active', 'id' => 'one'],
  229. [
  230. 'foo.bar.one' => 'extension',
  231. 'id' => 'one',
  232. '_core' => 'core_for_foo.bar.one',
  233. ],
  234. ],
  235. [
  236. 'system.simple',
  237. 'in.both',
  238. 'in.both',
  239. TRUE,
  240. ['in.both' => 'active'],
  241. ['in.both' => 'extension', '_core' => 'core_for_in.both'],
  242. ],
  243. [
  244. 'system.simple',
  245. 'in.optional',
  246. 'in.optional',
  247. TRUE,
  248. ['in.optional' => 'active'],
  249. ['in.optional' => 'optional', '_core' => 'core_for_in.optional'],
  250. ],
  251. [
  252. 'unknown',
  253. 'in.extension',
  254. FALSE,
  255. FALSE,
  256. FALSE,
  257. FALSE,
  258. ],
  259. // Missing from extension storage.
  260. [
  261. 'system.simple',
  262. 'missing2',
  263. 'missing2',
  264. FALSE,
  265. FALSE,
  266. FALSE,
  267. ],
  268. // Present in extension storage but missing from active storage.
  269. [
  270. 'system.simple',
  271. 'another',
  272. 'another',
  273. FALSE,
  274. FALSE,
  275. FALSE,
  276. ],
  277. ];
  278. }
  279. /**
  280. * @covers \Drupal\config_update\ConfigReverter::delete
  281. * @dataProvider deleteProvider
  282. */
  283. public function testDelete($type, $name, $config_name, $expected) {
  284. // Clear dispatch log.
  285. $this->dispatchedEvents = [];
  286. $save_config = $this->configStorage;
  287. // Call the deleteer and test the Boolean result.
  288. $result = $this->configReverter->delete($type, $name);
  289. $this->assertEquals($result, $expected);
  290. if ($result) {
  291. // Verify that the config is missing after delete, and logging worked.
  292. $this->assertTrue(!isset($this->configStorage[$config_name]));
  293. $this->assertEquals(count($this->dispatchedEvents), 1);
  294. $this->assertEquals($this->dispatchedEvents[0][0], ConfigDeleteInterface::DELETE);
  295. }
  296. else {
  297. // Verify that the config didn't change and no events were logged.
  298. $this->assertEquals($this->configStorage, $save_config);
  299. $this->assertEquals(count($this->dispatchedEvents), 0);
  300. }
  301. }
  302. /**
  303. * Data provider for self:testDelete().
  304. */
  305. public function deleteProvider() {
  306. return [
  307. // Elements: type, name, config name, return value.
  308. [
  309. 'system.simple',
  310. 'in.extension',
  311. 'in.extension',
  312. TRUE,
  313. ],
  314. [
  315. 'foo',
  316. 'one',
  317. 'foo.bar.one',
  318. TRUE,
  319. ],
  320. [
  321. 'unknown',
  322. 'in.extension',
  323. FALSE,
  324. FALSE,
  325. ],
  326. [
  327. 'system.simple',
  328. 'missing2',
  329. 'missing2',
  330. FALSE,
  331. ],
  332. ];
  333. }
  334. }