Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / MigrateConfigRollbackTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 use Drupal\migrate\MigrateExecutable;
6
7 /**
8  * Tests rolling back of configuration objects.
9  *
10  * @group migrate
11  */
12 class MigrateConfigRollbackTest extends MigrateTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system', 'language', 'config_translation'];
20
21   /**
22    * Tests rolling back configuration.
23    */
24   public function testConfigRollback() {
25     // Use system.site configuration to demonstrate importing and rolling back
26     // configuration.
27     $variable = [
28       [
29         'id' => 'site_name',
30         'site_name' => 'Some site',
31         'site_slogan' => 'Awesome slogan',
32       ],
33     ];
34     $ids = [
35       'id' =>
36         [
37           'type' => 'string',
38         ],
39     ];
40     $definition = [
41       'id' => 'config',
42       'migration_tags' => ['Import and rollback test'],
43       'source' => [
44         'plugin' => 'embedded_data',
45         'data_rows' => $variable,
46         'ids' => $ids,
47       ],
48       'process' => [
49         'name' => 'site_name',
50         'slogan' => 'site_slogan',
51       ],
52       'destination' => [
53         'plugin' => 'config',
54         'config_name' => 'system.site',
55       ],
56     ];
57
58     /** @var \Drupal\migrate\Plugin\Migration $config_migration */
59     $config_migration = \Drupal::service('plugin.manager.migration')
60       ->createStubMigration($definition);
61     $config_id_map = $config_migration->getIdMap();
62
63     // Rollback is not enabled for configuration translations.
64     $this->assertFalse($config_migration->getDestinationPlugin()->supportsRollback());
65
66     // Import and validate config entities were created.
67     $config_executable = new MigrateExecutable($config_migration, $this);
68     $config_executable->import();
69     $config = $this->config('system.site');
70     $this->assertSame('Some site', $config->get('name'));
71     $this->assertSame('Awesome slogan', $config->get('slogan'));
72     $map_row = $config_id_map->getRowBySource(['id' => $variable[0]['id']]);
73     $this->assertNotNull($map_row['destid1']);
74
75     // Rollback and verify the configuration changes are still there.
76     $config_executable->rollback();
77     $config = $this->config('system.site');
78     $this->assertSame('Some site', $config->get('name'));
79     $this->assertSame('Awesome slogan', $config->get('slogan'));
80     // Confirm the map row is deleted.
81     $map_row = $config_id_map->getRowBySource(['id' => $variable[0]['id']]);
82     $this->assertNull($map_row['destid1']);
83
84     // We use system configuration to demonstrate importing and rolling back
85     // configuration translations.
86     $i18n_variable = [
87       [
88         'id' => 'site_name',
89         'language' => 'fr',
90         'site_name' => 'fr - Some site',
91         'site_slogan' => 'fr - Awesome slogan',
92       ],
93       [
94         'id' => 'site_name',
95         'language' => 'is',
96         'site_name' => 'is - Some site',
97         'site_slogan' => 'is - Awesome slogan',
98       ],
99     ];
100     $ids = [
101       'id' =>
102         [
103           'type' => 'string',
104         ],
105       'language' =>
106         [
107           'type' => 'string',
108         ],
109     ];
110     $definition = [
111       'id' => 'i18n_config',
112       'migration_tags' => ['Import and rollback test'],
113       'source' => [
114         'plugin' => 'embedded_data',
115         'data_rows' => $i18n_variable,
116         'ids' => $ids,
117       ],
118       'process' => [
119         'langcode' => 'language',
120         'name' => 'site_name',
121         'slogan' => 'site_slogan',
122       ],
123       'destination' => [
124         'plugin' => 'config',
125         'config_name' => 'system.site',
126         'translations' => 'true',
127       ],
128     ];
129
130     $config_migration = \Drupal::service('plugin.manager.migration')
131       ->createStubMigration($definition);
132     $config_id_map = $config_migration->getIdMap();
133
134     // Rollback is enabled for configuration translations.
135     $this->assertTrue($config_migration->getDestinationPlugin()->supportsRollback());
136
137     // Import and validate config entities were created.
138     $config_executable = new MigrateExecutable($config_migration, $this);
139     $config_executable->import();
140
141     $language_manager = \Drupal::service('language_manager');
142     foreach ($i18n_variable as $row) {
143       $langcode = $row['language'];
144       /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
145       $config_translation = $language_manager->getLanguageConfigOverride($langcode, 'system.site');
146       $this->assertSame($row['site_name'], $config_translation->get('name'));
147       $this->assertSame($row['site_slogan'], $config_translation->get('slogan'));
148       $map_row = $config_id_map->getRowBySource(['id' => $row['id'], 'language' => $row['language']]);
149       $this->assertNotNull($map_row['destid1']);
150     }
151
152     // Rollback and verify the translation have been removed.
153     $config_executable->rollback();
154     foreach ($i18n_variable as $row) {
155       $langcode = $row['language'];
156       $config_translation = $language_manager->getLanguageConfigOverride($langcode, 'system.site');
157       $this->assertNull($config_translation->get('name'));
158       $this->assertNull($config_translation->get('slogan'));
159       // Confirm the map row is deleted.
160       $map_row = $config_id_map->getRowBySource(['id' => $row['id'], 'language' => $langcode]);
161       $this->assertFalse($map_row);
162     }
163
164     // Test that the configuration is still present.
165     $config = $this->config('system.site');
166     $this->assertSame('Some site', $config->get('name'));
167     $this->assertSame('Awesome slogan', $config->get('slogan'));
168   }
169
170 }