ConfigDifferTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace Drupal\Tests\config_update\Unit;
  3. use Drupal\config_update\ConfigDiffer;
  4. /**
  5. * Tests the \Drupal\config_update\ConfigDiffer class.
  6. *
  7. * @group config_update
  8. *
  9. * @coversDefaultClass \Drupal\config_update\ConfigDiffer
  10. */
  11. class ConfigDifferTest extends ConfigUpdateUnitTestBase {
  12. /**
  13. * The config differ to test.
  14. *
  15. * @var \Drupal\config_update\ConfigDiffer
  16. */
  17. protected $configDiffer;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. $this->configDiffer = new ConfigDiffer($this->getTranslationMock());
  23. }
  24. /**
  25. * @covers \Drupal\config_update\ConfigDiffer::same
  26. * @dataProvider sameProvider
  27. */
  28. public function testSame($a, $b, $expected) {
  29. $this->assertEquals($expected, $this->configDiffer->same($a, $b));
  30. }
  31. /**
  32. * Data provider for self:testSame().
  33. */
  34. public function sameProvider() {
  35. $base = [
  36. 'uuid' => 'bar',
  37. 'a' => 'a',
  38. 'b' => 0,
  39. 'c' => [
  40. 'd' => TRUE,
  41. 'e' => FALSE,
  42. 'empty' => [],
  43. ],
  44. ];
  45. return [
  46. [$base, $base, TRUE],
  47. // Add _core, omit uuid at top level. Should match, as both are removed
  48. // in normalization process.
  49. [
  50. $base,
  51. [
  52. '_core' => 'foo',
  53. 'a' => 'a',
  54. 'b' => 0,
  55. 'c' => [
  56. 'd' => TRUE,
  57. 'e' => FALSE,
  58. 'empty' => [],
  59. ],
  60. ],
  61. TRUE,
  62. ],
  63. // Change order in top and deep level. Should match.
  64. [
  65. $base,
  66. [
  67. 'uuid' => 'bar',
  68. 'b' => 0,
  69. 'a' => 'a',
  70. 'c' => [
  71. 'e' => FALSE,
  72. 'empty' => [],
  73. 'd' => TRUE,
  74. ],
  75. ],
  76. TRUE,
  77. ],
  78. // Add _core in deeper level. Should not match, as this is removed
  79. // only at the top level during normalization.
  80. [
  81. $base,
  82. [
  83. 'uuid' => 'bar',
  84. 'a' => 'a',
  85. 'b' => 0,
  86. 'c' => [
  87. '_core' => 'do-not-use-this-key',
  88. 'd' => TRUE,
  89. 'e' => FALSE,
  90. 'empty' => [],
  91. ],
  92. ],
  93. FALSE,
  94. ],
  95. // Add uuid in deeper level. Should not match, as this is removed
  96. // only at the top level during normalization.
  97. [
  98. $base,
  99. [
  100. 'uuid' => 'bar',
  101. 'a' => 'a',
  102. 'b' => 0,
  103. 'c' => [
  104. 'd' => TRUE,
  105. 'e' => FALSE,
  106. 'uuid' => 'important',
  107. 'empty' => [],
  108. ],
  109. ],
  110. FALSE,
  111. ],
  112. // Omit a component. Should not match.
  113. [
  114. $base,
  115. [
  116. 'uuid' => 'bar',
  117. 'a' => 'a',
  118. 'c' => [
  119. 'd' => TRUE,
  120. 'e' => FALSE,
  121. 'empty' => [],
  122. ],
  123. ],
  124. FALSE,
  125. ],
  126. // Add a component. Should not match.
  127. [
  128. $base,
  129. [
  130. 'uuid' => 'bar',
  131. 'a' => 'a',
  132. 'b' => 0,
  133. 'c' => [
  134. 'd' => TRUE,
  135. 'e' => FALSE,
  136. 'empty' => [],
  137. ],
  138. 'f' => 'f',
  139. ],
  140. FALSE,
  141. ],
  142. // 0 should not match a string.
  143. [
  144. $base,
  145. [
  146. '_core' => 'foo',
  147. 'uuid' => 'bar',
  148. 'a' => 'a',
  149. 'b' => 'b',
  150. 'c' => [
  151. 'd' => TRUE,
  152. 'e' => FALSE,
  153. 'empty' => [],
  154. ],
  155. ],
  156. FALSE,
  157. ],
  158. // 0 should not match NULL.
  159. [
  160. $base,
  161. [
  162. '_core' => 'foo',
  163. 'uuid' => 'bar',
  164. 'a' => 'a',
  165. 'b' => NULL,
  166. 'c' => [
  167. 'd' => TRUE,
  168. 'e' => FALSE,
  169. 'empty' => [],
  170. ],
  171. ],
  172. FALSE,
  173. ],
  174. // FALSE should not match a string.
  175. [
  176. $base,
  177. [
  178. '_core' => 'foo',
  179. 'uuid' => 'bar',
  180. 'a' => 'a',
  181. 'b' => 0,
  182. 'c' => [
  183. 'd' => TRUE,
  184. 'e' => 'e',
  185. 'empty' => [],
  186. ],
  187. ],
  188. FALSE,
  189. ],
  190. // TRUE should not match a string.
  191. [
  192. $base,
  193. [
  194. '_core' => 'foo',
  195. 'uuid' => 'bar',
  196. 'a' => 'a',
  197. 'b' => 0,
  198. 'c' => [
  199. 'd' => 'd',
  200. 'e' => FALSE,
  201. 'empty' => [],
  202. ],
  203. ],
  204. FALSE,
  205. ],
  206. // Add an empty array at top, and remove at lower level. Should still
  207. // match.
  208. [
  209. $base,
  210. [
  211. '_core' => 'foo',
  212. 'uuid' => 'bar',
  213. 'a' => 'a',
  214. 'b' => 0,
  215. 'c' => [
  216. 'd' => TRUE,
  217. 'e' => FALSE,
  218. ],
  219. 'empty_two' => [],
  220. ],
  221. TRUE,
  222. ],
  223. ];
  224. }
  225. /**
  226. * @covers \Drupal\config_update\ConfigDiffer::diff
  227. */
  228. public function testDiff() {
  229. $configOne = [
  230. 'uuid' => '1234-5678-90',
  231. 'id' => 'test.config.id',
  232. 'id_to_remove' => 'test.remove.id',
  233. 'type' => 'old_type',
  234. 'true_value' => TRUE,
  235. 'null_value' => NULL,
  236. 'nested_array' => [
  237. 'flat_array' => [
  238. 'value2',
  239. 'value1',
  240. 'value3',
  241. ],
  242. 'custom_key' => 'value',
  243. ],
  244. ];
  245. $configTwo = [
  246. 'uuid' => '09-8765-4321',
  247. 'id' => 'test.config.id',
  248. 'type' => 'new_type',
  249. 'true_value' => FALSE,
  250. 'null_value' => FALSE,
  251. 'nested_array' => [
  252. 'flat_array' => [
  253. 'value2',
  254. 'value3',
  255. ],
  256. 'custom_key' => 'value',
  257. 'custom_key_2' => 'value2',
  258. ],
  259. ];
  260. $edits = $this->configDiffer->diff($configOne, $configTwo)->getEdits();
  261. $expectedEdits = [
  262. [
  263. 'copy' => [
  264. 'orig' => [
  265. 'id : test.config.id',
  266. ],
  267. 'closing' => [
  268. 'id : test.config.id',
  269. ],
  270. ],
  271. ],
  272. [
  273. 'delete' => [
  274. 'orig' => [
  275. 'id_to_remove : test.remove.id',
  276. ],
  277. 'closing' => FALSE,
  278. ],
  279. ],
  280. [
  281. 'copy' => [
  282. 'orig' => [
  283. 'nested_array',
  284. 'nested_array::custom_key : value',
  285. ],
  286. 'closing' => [
  287. 'nested_array',
  288. 'nested_array::custom_key : value',
  289. ],
  290. ],
  291. ],
  292. [
  293. 'add' => [
  294. 'orig' => FALSE,
  295. 'closing' => [
  296. 'nested_array::custom_key_2 : value2',
  297. ],
  298. ],
  299. ],
  300. [
  301. 'copy' => [
  302. 'orig' => [
  303. 'nested_array::flat_array',
  304. 'nested_array::flat_array::0 : value2',
  305. ],
  306. 'closing' => [
  307. 'nested_array::flat_array',
  308. 'nested_array::flat_array::0 : value2',
  309. ],
  310. ],
  311. ],
  312. [
  313. 'change' => [
  314. 'orig' => [
  315. 'nested_array::flat_array::1 : value1',
  316. 'nested_array::flat_array::2 : value3',
  317. 'null_value : null',
  318. 'true_value : true',
  319. 'type : old_type',
  320. ],
  321. 'closing' => [
  322. 'nested_array::flat_array::1 : value3',
  323. 'null_value : false',
  324. 'true_value : false',
  325. 'type : new_type',
  326. ],
  327. ],
  328. ],
  329. ];
  330. $this->assertEquals(count($expectedEdits), count($edits));
  331. /** @var \Drupal\Component\Diff\Engine\DiffOp $diffOp */
  332. foreach ($edits as $index => $diffOp) {
  333. $this->assertEquals($expectedEdits[$index][$diffOp->type]['orig'], $diffOp->orig);
  334. $this->assertEquals($expectedEdits[$index][$diffOp->type]['closing'], $diffOp->closing);
  335. }
  336. }
  337. }