fef464ed7f21fdca9bcc21a9271326f03b7430c1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigImportRecreateTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\Core\Config\ConfigImporter;
6 use Drupal\Core\Config\StorageComparer;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\node\Entity\NodeType;
9
10 /**
11  * Tests importing recreated configuration entities.
12  *
13  * @group config
14  */
15 class ConfigImportRecreateTest extends KernelTestBase {
16
17   /**
18    * Config Importer object used for testing.
19    *
20    * @var \Drupal\Core\Config\ConfigImporter
21    */
22   protected $configImporter;
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['system', 'field', 'text', 'user', 'node'];
30
31   protected function setUp() {
32     parent::setUp();
33
34     $this->installEntitySchema('node');
35     $this->installConfig(['system', 'field', 'node']);
36
37     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
38
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       $this->container->get('config.manager')
44     );
45     $this->configImporter = new ConfigImporter(
46       $storage_comparer->createChangelist(),
47       $this->container->get('event_dispatcher'),
48       $this->container->get('config.manager'),
49       $this->container->get('lock'),
50       $this->container->get('config.typed'),
51       $this->container->get('module_handler'),
52       $this->container->get('module_installer'),
53       $this->container->get('theme_handler'),
54       $this->container->get('string_translation')
55     );
56   }
57
58   public function testRecreateEntity() {
59     $type_name = mb_strtolower($this->randomMachineName(16));
60     $content_type = NodeType::create([
61       'type' => $type_name,
62       'name' => 'Node type one',
63     ]);
64     $content_type->save();
65     node_add_body_field($content_type);
66     /** @var \Drupal\Core\Config\StorageInterface $active */
67     $active = $this->container->get('config.storage');
68     /** @var \Drupal\Core\Config\StorageInterface $sync */
69     $sync = $this->container->get('config.storage.sync');
70
71     $config_name = $content_type->getEntityType()->getConfigPrefix() . '.' . $content_type->id();
72     $this->copyConfig($active, $sync);
73
74     // Delete the content type. This will also delete a field storage, a field,
75     // an entity view display and an entity form display.
76     $content_type->delete();
77     $this->assertFalse($active->exists($config_name), 'Content type\'s old name does not exist active store.');
78     // Recreate with the same type - this will have a different UUID.
79     $content_type = NodeType::create([
80       'type' => $type_name,
81       'name' => 'Node type two',
82     ]);
83     $content_type->save();
84     node_add_body_field($content_type);
85
86     $this->configImporter->reset();
87     // A node type, a field, an entity view display and an entity form display
88     // will be recreated.
89     $creates = $this->configImporter->getUnprocessedConfiguration('create');
90     $deletes = $this->configImporter->getUnprocessedConfiguration('delete');
91     $this->assertEqual(5, count($creates), 'There are 5 configuration items to create.');
92     $this->assertEqual(5, count($deletes), 'There are 5 configuration items to delete.');
93     $this->assertEqual(0, count($this->configImporter->getUnprocessedConfiguration('update')), 'There are no configuration items to update.');
94     $this->assertSame($creates, array_reverse($deletes), 'Deletes and creates contain the same configuration names in opposite orders due to dependencies.');
95
96     $this->configImporter->import();
97
98     // Verify that there is nothing more to import.
99     $this->assertFalse($this->configImporter->reset()->hasUnprocessedConfigurationChanges());
100     $content_type = NodeType::load($type_name);
101     $this->assertEqual('Node type one', $content_type->label());
102   }
103
104 }