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