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