ConfigImporterTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Render\FormattableMarkup;
  5. use Drupal\Core\Config\ConfigImporter;
  6. use Drupal\Core\Config\ConfigImporterException;
  7. use Drupal\Core\Config\StorageComparer;
  8. use Drupal\KernelTests\KernelTestBase;
  9. /**
  10. * Tests importing configuration from files into active configuration.
  11. *
  12. * @group config
  13. */
  14. class ConfigImporterTest extends KernelTestBase {
  15. /**
  16. * The beginning of an import validation error.
  17. */
  18. const FAIL_MESSAGE = 'There were errors validating the config synchronization.';
  19. /**
  20. * Config Importer object used for testing.
  21. *
  22. * @var \Drupal\Core\Config\ConfigImporter
  23. */
  24. protected $configImporter;
  25. /**
  26. * Modules to enable.
  27. *
  28. * @var array
  29. */
  30. public static $modules = ['config_test', 'system', 'config_import_test'];
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->installConfig(['system', 'config_test']);
  34. // Installing config_test's default configuration pollutes the global
  35. // variable being used for recording hook invocations by this test already,
  36. // so it has to be cleared out manually.
  37. unset($GLOBALS['hook_config_test']);
  38. $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
  39. // Set up the ConfigImporter object for testing.
  40. $storage_comparer = new StorageComparer(
  41. $this->container->get('config.storage.sync'),
  42. $this->container->get('config.storage')
  43. );
  44. $this->configImporter = new ConfigImporter(
  45. $storage_comparer->createChangelist(),
  46. $this->container->get('event_dispatcher'),
  47. $this->container->get('config.manager'),
  48. $this->container->get('lock'),
  49. $this->container->get('config.typed'),
  50. $this->container->get('module_handler'),
  51. $this->container->get('module_installer'),
  52. $this->container->get('theme_handler'),
  53. $this->container->get('string_translation'),
  54. $this->container->get('extension.list.module')
  55. );
  56. }
  57. /**
  58. * Tests omission of module APIs for bare configuration operations.
  59. */
  60. public function testNoImport() {
  61. $dynamic_name = 'config_test.dynamic.dotted.default';
  62. // Verify the default configuration values exist.
  63. $config = $this->config($dynamic_name);
  64. $this->assertIdentical($config->get('id'), 'dotted.default');
  65. // Verify that a bare $this->config() does not involve module APIs.
  66. $this->assertFalse(isset($GLOBALS['hook_config_test']));
  67. }
  68. /**
  69. * Tests that trying to import from an empty sync configuration directory
  70. * fails.
  71. */
  72. public function testEmptyImportFails() {
  73. $this->expectException(ConfigImporterException::class);
  74. $this->container->get('config.storage.sync')->deleteAll();
  75. $this->configImporter->reset()->import();
  76. }
  77. /**
  78. * Tests verification of site UUID before importing configuration.
  79. */
  80. public function testSiteUuidValidate() {
  81. $sync = \Drupal::service('config.storage.sync');
  82. // Create updated configuration object.
  83. $config_data = $this->config('system.site')->get();
  84. // Generate a new site UUID.
  85. $config_data['uuid'] = \Drupal::service('uuid')->generate();
  86. $sync->write('system.site', $config_data);
  87. try {
  88. $this->configImporter->reset()->import();
  89. $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to mis-matching site UUID.');
  90. }
  91. catch (ConfigImporterException $e) {
  92. $actual_message = $e->getMessage();
  93. $actual_error_log = $this->configImporter->getErrors();
  94. $expected_error_log = ['Site UUID in source storage does not match the target storage.'];
  95. $this->assertEqual($actual_error_log, $expected_error_log);
  96. $expected = static::FAIL_MESSAGE . PHP_EOL . 'Site UUID in source storage does not match the target storage.';
  97. $this->assertEquals($expected, $actual_message);
  98. foreach ($expected_error_log as $log_row) {
  99. $this->assertRegExp("/$log_row/", $actual_message);
  100. }
  101. }
  102. }
  103. /**
  104. * Tests deletion of configuration during import.
  105. */
  106. public function testDeleted() {
  107. $dynamic_name = 'config_test.dynamic.dotted.default';
  108. $storage = $this->container->get('config.storage');
  109. $sync = $this->container->get('config.storage.sync');
  110. // Verify the default configuration values exist.
  111. $config = $this->config($dynamic_name);
  112. $this->assertIdentical($config->get('id'), 'dotted.default');
  113. // Delete the file from the sync directory.
  114. $sync->delete($dynamic_name);
  115. // Import.
  116. $this->configImporter->reset()->import();
  117. // Verify the file has been removed.
  118. $this->assertIdentical($storage->read($dynamic_name), FALSE);
  119. $config = $this->config($dynamic_name);
  120. $this->assertIdentical($config->get('id'), NULL);
  121. // Verify that appropriate module API hooks have been invoked.
  122. $this->assertTrue(isset($GLOBALS['hook_config_test']['load']));
  123. $this->assertFalse(isset($GLOBALS['hook_config_test']['presave']));
  124. $this->assertFalse(isset($GLOBALS['hook_config_test']['insert']));
  125. $this->assertFalse(isset($GLOBALS['hook_config_test']['update']));
  126. $this->assertTrue(isset($GLOBALS['hook_config_test']['predelete']));
  127. $this->assertTrue(isset($GLOBALS['hook_config_test']['delete']));
  128. $this->assertFalse($this->configImporter->hasUnprocessedConfigurationChanges());
  129. $logs = $this->configImporter->getErrors();
  130. $this->assertCount(0, $logs);
  131. }
  132. /**
  133. * Tests creation of configuration during import.
  134. */
  135. public function testNew() {
  136. $dynamic_name = 'config_test.dynamic.new';
  137. $storage = $this->container->get('config.storage');
  138. $sync = $this->container->get('config.storage.sync');
  139. // Verify the configuration to create does not exist yet.
  140. $this->assertIdentical($storage->exists($dynamic_name), FALSE, $dynamic_name . ' not found.');
  141. // Create new config entity.
  142. $original_dynamic_data = [
  143. 'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
  144. 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
  145. 'status' => TRUE,
  146. 'dependencies' => [],
  147. 'id' => 'new',
  148. 'label' => 'New',
  149. 'weight' => 0,
  150. 'style' => '',
  151. 'size' => '',
  152. 'size_value' => '',
  153. 'protected_property' => '',
  154. ];
  155. $sync->write($dynamic_name, $original_dynamic_data);
  156. $this->assertIdentical($sync->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
  157. // Import.
  158. $this->configImporter->reset()->import();
  159. // Verify the values appeared.
  160. $config = $this->config($dynamic_name);
  161. $this->assertIdentical($config->get('label'), $original_dynamic_data['label']);
  162. // Verify that appropriate module API hooks have been invoked.
  163. $this->assertFalse(isset($GLOBALS['hook_config_test']['load']));
  164. $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
  165. $this->assertTrue(isset($GLOBALS['hook_config_test']['insert']));
  166. $this->assertFalse(isset($GLOBALS['hook_config_test']['update']));
  167. $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
  168. $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
  169. // Verify that hook_config_import_steps_alter() can add steps to
  170. // configuration synchronization.
  171. $this->assertTrue(isset($GLOBALS['hook_config_test']['config_import_steps_alter']));
  172. // Verify that there is nothing more to import.
  173. $this->assertFalse($this->configImporter->hasUnprocessedConfigurationChanges());
  174. $logs = $this->configImporter->getErrors();
  175. $this->assertCount(0, $logs);
  176. }
  177. /**
  178. * Tests that secondary writes are overwritten.
  179. */
  180. public function testSecondaryWritePrimaryFirst() {
  181. $name_primary = 'config_test.dynamic.primary';
  182. $name_secondary = 'config_test.dynamic.secondary';
  183. $sync = $this->container->get('config.storage.sync');
  184. $uuid = $this->container->get('uuid');
  185. $values_primary = [
  186. 'id' => 'primary',
  187. 'label' => 'Primary',
  188. 'weight' => 0,
  189. 'uuid' => $uuid->generate(),
  190. ];
  191. $sync->write($name_primary, $values_primary);
  192. $values_secondary = [
  193. 'id' => 'secondary',
  194. 'label' => 'Secondary Sync',
  195. 'weight' => 0,
  196. 'uuid' => $uuid->generate(),
  197. // Add a dependency on primary, to ensure that is synced first.
  198. 'dependencies' => [
  199. 'config' => [$name_primary],
  200. ],
  201. ];
  202. $sync->write($name_secondary, $values_secondary);
  203. // Import.
  204. $this->configImporter->reset()->import();
  205. $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
  206. $primary = $entity_storage->load('primary');
  207. $this->assertEqual($primary->id(), 'primary');
  208. $this->assertEqual($primary->uuid(), $values_primary['uuid']);
  209. $this->assertEqual($primary->label(), $values_primary['label']);
  210. $secondary = $entity_storage->load('secondary');
  211. $this->assertEqual($secondary->id(), 'secondary');
  212. $this->assertEqual($secondary->uuid(), $values_secondary['uuid']);
  213. $this->assertEqual($secondary->label(), $values_secondary['label']);
  214. $logs = $this->configImporter->getErrors();
  215. $this->assertCount(1, $logs);
  216. $this->assertEqual($logs[0], new FormattableMarkup('Deleted and replaced configuration entity "@name"', ['@name' => $name_secondary]));
  217. }
  218. /**
  219. * Tests that secondary writes are overwritten.
  220. */
  221. public function testSecondaryWriteSecondaryFirst() {
  222. $name_primary = 'config_test.dynamic.primary';
  223. $name_secondary = 'config_test.dynamic.secondary';
  224. $sync = $this->container->get('config.storage.sync');
  225. $uuid = $this->container->get('uuid');
  226. $values_primary = [
  227. 'id' => 'primary',
  228. 'label' => 'Primary',
  229. 'weight' => 0,
  230. 'uuid' => $uuid->generate(),
  231. // Add a dependency on secondary, so that is synced first.
  232. 'dependencies' => [
  233. 'config' => [$name_secondary],
  234. ],
  235. ];
  236. $sync->write($name_primary, $values_primary);
  237. $values_secondary = [
  238. 'id' => 'secondary',
  239. 'label' => 'Secondary Sync',
  240. 'weight' => 0,
  241. 'uuid' => $uuid->generate(),
  242. ];
  243. $sync->write($name_secondary, $values_secondary);
  244. // Import.
  245. $this->configImporter->reset()->import();
  246. $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
  247. $primary = $entity_storage->load('primary');
  248. $this->assertEqual($primary->id(), 'primary');
  249. $this->assertEqual($primary->uuid(), $values_primary['uuid']);
  250. $this->assertEqual($primary->label(), $values_primary['label']);
  251. $secondary = $entity_storage->load('secondary');
  252. $this->assertEqual($secondary->id(), 'secondary');
  253. $this->assertEqual($secondary->uuid(), $values_secondary['uuid']);
  254. $this->assertEqual($secondary->label(), $values_secondary['label']);
  255. $logs = $this->configImporter->getErrors();
  256. $this->assertCount(1, $logs);
  257. $this->assertEqual($logs[0], Html::escape("Unexpected error during import with operation create for $name_primary: 'config_test' entity with ID 'secondary' already exists."));
  258. }
  259. /**
  260. * Tests that secondary updates for deleted files work as expected.
  261. */
  262. public function testSecondaryUpdateDeletedDeleterFirst() {
  263. $name_deleter = 'config_test.dynamic.deleter';
  264. $name_deletee = 'config_test.dynamic.deletee';
  265. $name_other = 'config_test.dynamic.other';
  266. $storage = $this->container->get('config.storage');
  267. $sync = $this->container->get('config.storage.sync');
  268. $uuid = $this->container->get('uuid');
  269. $values_deleter = [
  270. 'id' => 'deleter',
  271. 'label' => 'Deleter',
  272. 'weight' => 0,
  273. 'uuid' => $uuid->generate(),
  274. ];
  275. $storage->write($name_deleter, $values_deleter);
  276. $values_deleter['label'] = 'Updated Deleter';
  277. $sync->write($name_deleter, $values_deleter);
  278. $values_deletee = [
  279. 'id' => 'deletee',
  280. 'label' => 'Deletee',
  281. 'weight' => 0,
  282. 'uuid' => $uuid->generate(),
  283. // Add a dependency on deleter, to make sure that is synced first.
  284. 'dependencies' => [
  285. 'config' => [$name_deleter],
  286. ],
  287. ];
  288. $storage->write($name_deletee, $values_deletee);
  289. $values_deletee['label'] = 'Updated Deletee';
  290. $sync->write($name_deletee, $values_deletee);
  291. // Ensure that import will continue after the error.
  292. $values_other = [
  293. 'id' => 'other',
  294. 'label' => 'Other',
  295. 'weight' => 0,
  296. 'uuid' => $uuid->generate(),
  297. // Add a dependency on deleter, to make sure that is synced first. This
  298. // will also be synced after the deletee due to alphabetical ordering.
  299. 'dependencies' => [
  300. 'config' => [$name_deleter],
  301. ],
  302. ];
  303. $storage->write($name_other, $values_other);
  304. $values_other['label'] = 'Updated other';
  305. $sync->write($name_other, $values_other);
  306. // Check update changelist order.
  307. $updates = $this->configImporter->reset()->getStorageComparer()->getChangelist('update');
  308. $expected = [
  309. $name_deleter,
  310. $name_deletee,
  311. $name_other,
  312. ];
  313. $this->assertSame($expected, $updates);
  314. // Import.
  315. $this->configImporter->import();
  316. $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
  317. $deleter = $entity_storage->load('deleter');
  318. $this->assertEqual($deleter->id(), 'deleter');
  319. $this->assertEqual($deleter->uuid(), $values_deleter['uuid']);
  320. $this->assertEqual($deleter->label(), $values_deleter['label']);
  321. // The deletee was deleted in
  322. // \Drupal\config_test\Entity\ConfigTest::postSave().
  323. $this->assertNull($entity_storage->load('deletee'));
  324. $other = $entity_storage->load('other');
  325. $this->assertEqual($other->id(), 'other');
  326. $this->assertEqual($other->uuid(), $values_other['uuid']);
  327. $this->assertEqual($other->label(), $values_other['label']);
  328. $logs = $this->configImporter->getErrors();
  329. $this->assertCount(1, $logs);
  330. $this->assertEqual($logs[0], new FormattableMarkup('Update target "@name" is missing.', ['@name' => $name_deletee]));
  331. }
  332. /**
  333. * Tests that secondary updates for deleted files work as expected.
  334. *
  335. * This test is completely hypothetical since we only support full
  336. * configuration tree imports. Therefore, any configuration updates that cause
  337. * secondary deletes should be reflected already in the staged configuration.
  338. */
  339. public function testSecondaryUpdateDeletedDeleteeFirst() {
  340. $name_deleter = 'config_test.dynamic.deleter';
  341. $name_deletee = 'config_test.dynamic.deletee';
  342. $storage = $this->container->get('config.storage');
  343. $sync = $this->container->get('config.storage.sync');
  344. $uuid = $this->container->get('uuid');
  345. $values_deleter = [
  346. 'id' => 'deleter',
  347. 'label' => 'Deleter',
  348. 'weight' => 0,
  349. 'uuid' => $uuid->generate(),
  350. // Add a dependency on deletee, to make sure that is synced first.
  351. 'dependencies' => [
  352. 'config' => [$name_deletee],
  353. ],
  354. ];
  355. $storage->write($name_deleter, $values_deleter);
  356. $values_deleter['label'] = 'Updated Deleter';
  357. $sync->write($name_deleter, $values_deleter);
  358. $values_deletee = [
  359. 'id' => 'deletee',
  360. 'label' => 'Deletee',
  361. 'weight' => 0,
  362. 'uuid' => $uuid->generate(),
  363. ];
  364. $storage->write($name_deletee, $values_deletee);
  365. $values_deletee['label'] = 'Updated Deletee';
  366. $sync->write($name_deletee, $values_deletee);
  367. // Import.
  368. $this->configImporter->reset()->import();
  369. $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
  370. // Both entities are deleted. ConfigTest::postSave() causes updates of the
  371. // deleter entity to delete the deletee entity. Since the deleter depends on
  372. // the deletee, removing the deletee causes the deleter to be removed.
  373. $this->assertNull($entity_storage->load('deleter'));
  374. $this->assertNull($entity_storage->load('deletee'));
  375. $logs = $this->configImporter->getErrors();
  376. $this->assertCount(0, $logs);
  377. }
  378. /**
  379. * Tests that secondary deletes for deleted files work as expected.
  380. */
  381. public function testSecondaryDeletedDeleteeSecond() {
  382. $name_deleter = 'config_test.dynamic.deleter';
  383. $name_deletee = 'config_test.dynamic.deletee';
  384. $storage = $this->container->get('config.storage');
  385. $uuid = $this->container->get('uuid');
  386. $values_deleter = [
  387. 'id' => 'deleter',
  388. 'label' => 'Deleter',
  389. 'weight' => 0,
  390. 'uuid' => $uuid->generate(),
  391. // Add a dependency on deletee, to make sure this delete is synced first.
  392. 'dependencies' => [
  393. 'config' => [$name_deletee],
  394. ],
  395. ];
  396. $storage->write($name_deleter, $values_deleter);
  397. $values_deletee = [
  398. 'id' => 'deletee',
  399. 'label' => 'Deletee',
  400. 'weight' => 0,
  401. 'uuid' => $uuid->generate(),
  402. ];
  403. $storage->write($name_deletee, $values_deletee);
  404. // Import.
  405. $this->configImporter->reset()->import();
  406. $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
  407. $this->assertNull($entity_storage->load('deleter'));
  408. $this->assertNull($entity_storage->load('deletee'));
  409. // The deletee entity does not exist as the delete worked and although the
  410. // delete occurred in \Drupal\config_test\Entity\ConfigTest::postDelete()
  411. // this does not matter.
  412. $logs = $this->configImporter->getErrors();
  413. $this->assertCount(0, $logs);
  414. }
  415. /**
  416. * Tests updating of configuration during import.
  417. */
  418. public function testUpdated() {
  419. $name = 'config_test.system';
  420. $dynamic_name = 'config_test.dynamic.dotted.default';
  421. $storage = $this->container->get('config.storage');
  422. $sync = $this->container->get('config.storage.sync');
  423. // Verify that the configuration objects to import exist.
  424. $this->assertIdentical($storage->exists($name), TRUE, $name . ' found.');
  425. $this->assertIdentical($storage->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
  426. // Replace the file content of the existing configuration objects in the
  427. // sync directory.
  428. $original_name_data = [
  429. 'foo' => 'beer',
  430. ];
  431. $sync->write($name, $original_name_data);
  432. $original_dynamic_data = $storage->read($dynamic_name);
  433. $original_dynamic_data['label'] = 'Updated';
  434. $sync->write($dynamic_name, $original_dynamic_data);
  435. // Verify the active configuration still returns the default values.
  436. $config = $this->config($name);
  437. $this->assertIdentical($config->get('foo'), 'bar');
  438. $config = $this->config($dynamic_name);
  439. $this->assertIdentical($config->get('label'), 'Default');
  440. // Import.
  441. $this->configImporter->reset()->import();
  442. // Verify the values were updated.
  443. \Drupal::configFactory()->reset($name);
  444. $config = $this->config($name);
  445. $this->assertIdentical($config->get('foo'), 'beer');
  446. $config = $this->config($dynamic_name);
  447. $this->assertIdentical($config->get('label'), 'Updated');
  448. // Verify that the original file content is still the same.
  449. $this->assertIdentical($sync->read($name), $original_name_data);
  450. $this->assertIdentical($sync->read($dynamic_name), $original_dynamic_data);
  451. // Verify that appropriate module API hooks have been invoked.
  452. $this->assertTrue(isset($GLOBALS['hook_config_test']['load']));
  453. $this->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
  454. $this->assertFalse(isset($GLOBALS['hook_config_test']['insert']));
  455. $this->assertTrue(isset($GLOBALS['hook_config_test']['update']));
  456. $this->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
  457. $this->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
  458. // Verify that there is nothing more to import.
  459. $this->assertFalse($this->configImporter->hasUnprocessedConfigurationChanges());
  460. $logs = $this->configImporter->getErrors();
  461. $this->assertCount(0, $logs);
  462. }
  463. /**
  464. * Tests the isInstallable method()
  465. */
  466. public function testIsInstallable() {
  467. $config_name = 'config_test.dynamic.isinstallable';
  468. $this->assertFalse($this->container->get('config.storage')->exists($config_name));
  469. \Drupal::state()->set('config_test.isinstallable', TRUE);
  470. $this->installConfig(['config_test']);
  471. $this->assertTrue($this->container->get('config.storage')->exists($config_name));
  472. }
  473. /**
  474. * Tests dependency validation during configuration import.
  475. *
  476. * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  477. * @see \Drupal\Core\Config\ConfigImporter::createExtensionChangelist()
  478. */
  479. public function testUnmetDependency() {
  480. $storage = $this->container->get('config.storage');
  481. $sync = $this->container->get('config.storage.sync');
  482. // Test an unknown configuration owner.
  483. $sync->write('unknown.config', ['test' => 'test']);
  484. // Make a config entity have unmet dependencies.
  485. $config_entity_data = $sync->read('config_test.dynamic.dotted.default');
  486. $config_entity_data['dependencies'] = ['module' => ['unknown']];
  487. $sync->write('config_test.dynamic.dotted.module', $config_entity_data);
  488. $config_entity_data['dependencies'] = ['theme' => ['unknown']];
  489. $sync->write('config_test.dynamic.dotted.theme', $config_entity_data);
  490. $config_entity_data['dependencies'] = ['config' => ['unknown']];
  491. $sync->write('config_test.dynamic.dotted.config', $config_entity_data);
  492. // Make an active config depend on something that is missing in sync.
  493. // The whole configuration needs to be consistent, not only the updated one.
  494. $config_entity_data['dependencies'] = [];
  495. $storage->write('config_test.dynamic.dotted.deleted', $config_entity_data);
  496. $config_entity_data['dependencies'] = ['config' => ['config_test.dynamic.dotted.deleted']];
  497. $storage->write('config_test.dynamic.dotted.existing', $config_entity_data);
  498. $sync->write('config_test.dynamic.dotted.existing', $config_entity_data);
  499. $extensions = $sync->read('core.extension');
  500. // Add a module and a theme that do not exist.
  501. $extensions['module']['unknown_module'] = 0;
  502. $extensions['theme']['unknown_theme'] = 0;
  503. // Add a module and a theme that depend on uninstalled extensions.
  504. $extensions['module']['book'] = 0;
  505. $extensions['theme']['bartik'] = 0;
  506. $sync->write('core.extension', $extensions);
  507. try {
  508. $this->configImporter->reset()->import();
  509. $this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
  510. }
  511. catch (ConfigImporterException $e) {
  512. $expected = [
  513. static::FAIL_MESSAGE,
  514. 'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
  515. 'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
  516. 'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
  517. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
  518. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
  519. 'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on the <em class="placeholder">unknown</em> configuration that will not exist after import.',
  520. 'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
  521. 'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on the <em class="placeholder">unknown</em> module that will not be installed after import.',
  522. 'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on the <em class="placeholder">unknown</em> theme that will not be installed after import.',
  523. 'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
  524. ];
  525. $this->assertEquals(implode(PHP_EOL, $expected), $e->getMessage());
  526. $error_log = $this->configImporter->getErrors();
  527. $expected = [
  528. 'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
  529. 'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
  530. 'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
  531. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
  532. 'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on the <em class="placeholder">unknown</em> configuration that will not exist after import.',
  533. 'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
  534. 'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on the <em class="placeholder">unknown</em> module that will not be installed after import.',
  535. 'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on the <em class="placeholder">unknown</em> theme that will not be installed after import.',
  536. 'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
  537. ];
  538. foreach ($expected as $expected_message) {
  539. $this->assertContains($expected_message, $error_log, $expected_message);
  540. }
  541. }
  542. // Make a config entity have multiple unmet dependencies.
  543. $config_entity_data = $sync->read('config_test.dynamic.dotted.default');
  544. $config_entity_data['dependencies'] = ['module' => ['unknown', 'dblog']];
  545. $sync->write('config_test.dynamic.dotted.module', $config_entity_data);
  546. $config_entity_data['dependencies'] = ['theme' => ['unknown', 'seven']];
  547. $sync->write('config_test.dynamic.dotted.theme', $config_entity_data);
  548. $config_entity_data['dependencies'] = ['config' => ['unknown', 'unknown2']];
  549. $sync->write('config_test.dynamic.dotted.config', $config_entity_data);
  550. try {
  551. $this->configImporter->reset()->import();
  552. $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to missing dependencies.');
  553. }
  554. catch (ConfigImporterException $e) {
  555. $expected = [
  556. static::FAIL_MESSAGE,
  557. 'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
  558. 'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
  559. 'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
  560. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
  561. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
  562. 'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on the <em class="placeholder">unknown</em> configuration that will not exist after import.',
  563. 'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
  564. 'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on the <em class="placeholder">unknown</em> module that will not be installed after import.',
  565. 'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on the <em class="placeholder">unknown</em> theme that will not be installed after import.',
  566. 'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
  567. 'Unable to install the <em class="placeholder">unknown_module</em> module since it does not exist.',
  568. 'Unable to install the <em class="placeholder">Book</em> module since it requires the <em class="placeholder">Node, Text, Field, Filter, User</em> modules.',
  569. 'Unable to install the <em class="placeholder">unknown_theme</em> theme since it does not exist.',
  570. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Classy</em> theme.',
  571. 'Unable to install the <em class="placeholder">Bartik</em> theme since it requires the <em class="placeholder">Stable</em> theme.',
  572. 'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on configuration (<em class="placeholder">unknown, unknown2</em>) that will not exist after import.',
  573. 'Configuration <em class="placeholder">config_test.dynamic.dotted.existing</em> depends on the <em class="placeholder">config_test.dynamic.dotted.deleted</em> configuration that will not exist after import.',
  574. 'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on modules (<em class="placeholder">unknown, Database Logging</em>) that will not be installed after import.',
  575. 'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on themes (<em class="placeholder">unknown, Seven</em>) that will not be installed after import.',
  576. 'Configuration <em class="placeholder">unknown.config</em> depends on the <em class="placeholder">unknown</em> extension that will not be installed after import.',
  577. ];
  578. $this->assertEquals(implode(PHP_EOL, $expected), $e->getMessage());
  579. $error_log = $this->configImporter->getErrors();
  580. $expected = [
  581. 'Configuration <em class="placeholder">config_test.dynamic.dotted.config</em> depends on configuration (<em class="placeholder">unknown, unknown2</em>) that will not exist after import.',
  582. 'Configuration <em class="placeholder">config_test.dynamic.dotted.module</em> depends on modules (<em class="placeholder">unknown, Database Logging</em>) that will not be installed after import.',
  583. 'Configuration <em class="placeholder">config_test.dynamic.dotted.theme</em> depends on themes (<em class="placeholder">unknown, Seven</em>) that will not be installed after import.',
  584. ];
  585. foreach ($expected as $expected_message) {
  586. $this->assertContains($expected_message, $error_log, $expected_message);
  587. }
  588. }
  589. }
  590. /**
  591. * Tests missing core.extension during configuration import.
  592. *
  593. * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  594. */
  595. public function testMissingCoreExtension() {
  596. $sync = $this->container->get('config.storage.sync');
  597. $sync->delete('core.extension');
  598. try {
  599. $this->configImporter->reset()->import();
  600. $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to missing dependencies.');
  601. }
  602. catch (ConfigImporterException $e) {
  603. $expected = static::FAIL_MESSAGE . PHP_EOL . 'The core.extension configuration does not exist.';
  604. $this->assertEquals($expected, $e->getMessage());
  605. $error_log = $this->configImporter->getErrors();
  606. $this->assertEqual(['The core.extension configuration does not exist.'], $error_log);
  607. }
  608. }
  609. /**
  610. * Tests install profile validation during configuration import.
  611. *
  612. * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  613. */
  614. public function testInstallProfile() {
  615. $sync = $this->container->get('config.storage.sync');
  616. $extensions = $sync->read('core.extension');
  617. // Add an install profile.
  618. $extensions['module']['standard'] = 0;
  619. $sync->write('core.extension', $extensions);
  620. try {
  621. $this->configImporter->reset()->import();
  622. $this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
  623. }
  624. catch (ConfigImporterException $e) {
  625. $expected = static::FAIL_MESSAGE . PHP_EOL . 'Unable to install the <em class="placeholder">standard</em> module since it does not exist.';
  626. $this->assertEquals($expected, $e->getMessage(), 'There were errors validating the config synchronization.');
  627. $error_log = $this->configImporter->getErrors();
  628. // Install profiles should not even be scanned at this point.
  629. $this->assertEqual(['Unable to install the <em class="placeholder">standard</em> module since it does not exist.'], $error_log);
  630. }
  631. }
  632. /**
  633. * Tests install profile validation during configuration import.
  634. *
  635. * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
  636. */
  637. public function testInstallProfileMisMatch() {
  638. $sync = $this->container->get('config.storage.sync');
  639. $extensions = $sync->read('core.extension');
  640. // Change the install profile.
  641. $extensions['profile'] = 'this_will_not_work';
  642. $sync->write('core.extension', $extensions);
  643. try {
  644. $this->configImporter->reset()->import();
  645. $this->fail('ConfigImporterException not thrown; an invalid import was not stopped due to missing dependencies.');
  646. }
  647. catch (ConfigImporterException $e) {
  648. $expected = static::FAIL_MESSAGE . PHP_EOL . 'Cannot change the install profile from <em class="placeholder"></em> to <em class="placeholder">this_will_not_work</em> once Drupal is installed.';
  649. $this->assertEquals($expected, $e->getMessage(), 'There were errors validating the config synchronization.');
  650. $error_log = $this->configImporter->getErrors();
  651. // Install profiles can not be changed. Note that KernelTestBase currently
  652. // does not use an install profile. This situation should be impossible
  653. // to get in but site's can removed the install profile setting from
  654. // settings.php so the test is valid.
  655. $this->assertEqual(['Cannot change the install profile from <em class="placeholder"></em> to <em class="placeholder">this_will_not_work</em> once Drupal is installed.'], $error_log);
  656. }
  657. }
  658. /**
  659. * Tests config_get_config_directory().
  660. *
  661. * @group legacy
  662. * @expectedDeprecation config_get_config_directory() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Site\Settings::get('config_sync_directory') instead. See https://www.drupal.org/node/3018145
  663. */
  664. public function testConfigGetConfigDirectory() {
  665. global $config_directories;
  666. // Ensure the global and the setting matches.
  667. $this->assertSame(config_get_config_directory(CONFIG_SYNC_DIRECTORY), $config_directories[CONFIG_SYNC_DIRECTORY]);
  668. $message = 'Calling config_get_config_directory() with an invalid key results in an exception.';
  669. try {
  670. config_get_config_directory('does_not_exist');
  671. $this->fail($message);
  672. }
  673. catch (\Exception $e) {
  674. $this->pass($message);
  675. }
  676. }
  677. /**
  678. * Tests the isSyncing flags.
  679. */
  680. public function testIsSyncingInHooks() {
  681. $dynamic_name = 'config_test.dynamic.dotted.default';
  682. $storage = $this->container->get('config.storage');
  683. // Verify the default configuration values exist.
  684. $config = $this->config($dynamic_name);
  685. $this->assertSame('dotted.default', $config->get('id'));
  686. // Delete the config so that create hooks will fire.
  687. $storage->delete($dynamic_name);
  688. \Drupal::state()->set('config_test.store_isSyncing', []);
  689. $this->configImporter->reset()->import();
  690. // The values of the syncing values should be stored in state by
  691. // config_test_config_test_create().
  692. $state = \Drupal::state()->get('config_test.store_isSyncing');
  693. $this->assertTrue($state['global_state::create'], '\Drupal::isConfigSyncing() returns TRUE');
  694. $this->assertTrue($state['entity_state::create'], 'ConfigEntity::isSyncing() returns TRUE');
  695. $this->assertTrue($state['global_state::presave'], '\Drupal::isConfigSyncing() returns TRUE');
  696. $this->assertTrue($state['entity_state::presave'], 'ConfigEntity::isSyncing() returns TRUE');
  697. $this->assertTrue($state['global_state::insert'], '\Drupal::isConfigSyncing() returns TRUE');
  698. $this->assertTrue($state['entity_state::insert'], 'ConfigEntity::isSyncing() returns TRUE');
  699. // Cause a config update so update hooks will fire.
  700. $config = $this->config($dynamic_name);
  701. $config->set('label', 'A new name')->save();
  702. \Drupal::state()->set('config_test.store_isSyncing', []);
  703. $this->configImporter->reset()->import();
  704. // The values of the syncing values should be stored in state by
  705. // config_test_config_test_create().
  706. $state = \Drupal::state()->get('config_test.store_isSyncing');
  707. $this->assertTrue($state['global_state::presave'], '\Drupal::isConfigSyncing() returns TRUE');
  708. $this->assertTrue($state['entity_state::presave'], 'ConfigEntity::isSyncing() returns TRUE');
  709. $this->assertTrue($state['global_state::update'], '\Drupal::isConfigSyncing() returns TRUE');
  710. $this->assertTrue($state['entity_state::update'], 'ConfigEntity::isSyncing() returns TRUE');
  711. // Cause a config delete so delete hooks will fire.
  712. $sync = $this->container->get('config.storage.sync');
  713. $sync->delete($dynamic_name);
  714. \Drupal::state()->set('config_test.store_isSyncing', []);
  715. $this->configImporter->reset()->import();
  716. // The values of the syncing values should be stored in state by
  717. // config_test_config_test_create().
  718. $state = \Drupal::state()->get('config_test.store_isSyncing');
  719. $this->assertTrue($state['global_state::predelete'], '\Drupal::isConfigSyncing() returns TRUE');
  720. $this->assertTrue($state['entity_state::predelete'], 'ConfigEntity::isSyncing() returns TRUE');
  721. $this->assertTrue($state['global_state::delete'], '\Drupal::isConfigSyncing() returns TRUE');
  722. $this->assertTrue($state['entity_state::delete'], 'ConfigEntity::isSyncing() returns TRUE');
  723. }
  724. /**
  725. * Tests that the isConfigSyncing flag is cleanup after an invalid step.
  726. */
  727. public function testInvalidStep() {
  728. $this->assertFalse(\Drupal::isConfigSyncing(), 'Before an import \Drupal::isConfigSyncing() returns FALSE');
  729. $context = [];
  730. try {
  731. $this->configImporter->doSyncStep('a_non_existent_step', $context);
  732. $this->fail('Expected \InvalidArgumentException thrown');
  733. }
  734. catch (\InvalidArgumentException $e) {
  735. // Expected exception; just continue testing.
  736. }
  737. $this->assertFalse(\Drupal::isConfigSyncing(), 'After an invalid step \Drupal::isConfigSyncing() returns FALSE');
  738. }
  739. /**
  740. * Tests that the isConfigSyncing flag is set correctly during a custom step.
  741. */
  742. public function testCustomStep() {
  743. $this->assertFalse(\Drupal::isConfigSyncing(), 'Before an import \Drupal::isConfigSyncing() returns FALSE');
  744. $context = [];
  745. $this->configImporter->doSyncStep([self::class, 'customStep'], $context);
  746. $this->assertTrue($context['is_syncing'], 'Inside a custom step \Drupal::isConfigSyncing() returns TRUE');
  747. $this->assertFalse(\Drupal::isConfigSyncing(), 'After an valid custom step \Drupal::isConfigSyncing() returns FALSE');
  748. }
  749. /**
  750. * Helper method to test custom config installer steps.
  751. *
  752. * @param array $context
  753. * Batch context.
  754. * @param \Drupal\Core\Config\ConfigImporter $importer
  755. * The config importer.
  756. */
  757. public static function customStep(array &$context, ConfigImporter $importer) {
  758. $context['is_syncing'] = \Drupal::isConfigSyncing();
  759. }
  760. }