53d3ad467d52104f626c74b256e539a0c4a5d087
[yaffs-website] / web / modules / contrib / migrate_plus / tests / src / Kernel / MigrationGroupTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_plus\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate_plus\Entity\Migration;
7 use Drupal\migrate_plus\Entity\MigrationGroup;
8
9 /**
10  * Test migration groups.
11  *
12  * @group migrate_plus
13  */
14 class MigrationGroupTest extends KernelTestBase {
15
16   public static $modules = ['migrate', 'migrate_plus'];
17
18   /**
19    * Test that group configuration is properly merged into specific migrations.
20    */
21   public function testConfigurationMerge() {
22     $group_id = 'test_group';
23
24     /** @var \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group */
25     $group_configuration = [
26       'id' => $group_id,
27       'shared_configuration' => [
28         // In migration, so will be overridden.
29         'migration_tags' => ['Drupal 6'],
30         'source' => [
31           'constants' => [
32             // Not in migration, so will be added.
33             'type' => 'image',
34             // In migration, so will be overridden.
35             'cardinality' => '1',
36           ],
37         ],
38         // Not in migration, so will be added.
39         'destination' => ['plugin' => 'field_storage_config'],
40       ],
41     ];
42     $this->container->get('entity_type.manager')->getStorage('migration_group')
43       ->create($group_configuration)->save();
44
45     /** @var \Drupal\migrate_plus\Entity\MigrationInterface $migration */
46     $migration = $this->container->get('entity_type.manager')
47       ->getStorage('migration')->create([
48         'id' => 'specific_migration',
49         'load' => [],
50         'migration_group' => $group_id,
51         'label' => 'Unaffected by the group',
52           // Overrides group.
53         'migration_tags' => ['Drupal 7'],
54         'destination' => [],
55         'source' => [],
56         'process' => [],
57         'migration_dependencies' => [],
58       ]);
59     $migration->set('source', [
60       // Not in group, persists.
61       'plugin' => 'empty',
62       'constants' => [
63         // Not in group, persists.
64         'entity_type' => 'user',
65         // Overrides group.
66         'cardinality' => '3',
67       ],
68     ]);
69     $migration->save();
70
71     $expected_config = [
72       'migration_group' => $group_id,
73       'label' => 'Unaffected by the group',
74       'migration_tags' => ['Drupal 7'],
75       'source' => [
76         'plugin' => 'empty',
77         'constants' => [
78           'entity_type' => 'user',
79           'type' => 'image',
80           'cardinality' => '3',
81         ],
82       ],
83       'destination' => ['plugin' => 'field_storage_config'],
84     ];
85     /** @var \Drupal\migrate\Plugin\MigrationInterface $loaded_migration */
86     $loaded_migration = $this->container->get('plugin.manager.migration')
87       ->createInstance('specific_migration');
88     foreach ($expected_config as $key => $expected_value) {
89       $actual_value = $loaded_migration->get($key);
90       $this->assertEquals($expected_value, $actual_value);
91     }
92   }
93
94   /**
95    * Test that deleting a group deletes its migrations.
96    */
97   public function testDelete() {
98     /** @var \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group */
99     $group_configuration = [
100       'id' => 'test_group',
101     ];
102     $migration_group = $this->container->get('entity_type.manager')
103       ->getStorage('migration_group')->create($group_configuration);
104     $migration_group->save();
105
106     /** @var \Drupal\migrate_plus\Entity\MigrationInterface $migration */
107     $migration = $this->container->get('entity_type.manager')
108       ->getStorage('migration')->create([
109         'id' => 'specific_migration',
110         'migration_group' => 'test_group',
111         'migration_tags' => [],
112         'load' => [],
113         'destination' => [],
114         'source' => [],
115         'migration_dependencies' => [],
116       ]);
117     $migration->save();
118
119     /** @var \Drupal\migrate_plus\Entity\MigrationGroupInterface $loaded_migration_group */
120     $loaded_migration_group = MigrationGroup::load('test_group');
121     $loaded_migration_group->delete();
122
123     /** @var \Drupal\migrate_plus\Entity\MigrationInterface $loaded_migration */
124     $loaded_migration = Migration::load('specific_migration');
125     $this->assertNull($loaded_migration);
126   }
127
128 }