Version 1
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / MigrateBundleTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel;
4
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8
9 /**
10  * Tests setting of bundles on content entity migrations.
11  *
12  * @group migrate
13  */
14 class MigrateBundleTest extends MigrateTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['taxonomy', 'text'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28     $this->installEntitySchema('taxonomy_vocabulary');
29     $this->installEntitySchema('taxonomy_term');
30     $this->installConfig(['taxonomy']);
31     // Set up two vocabularies (taxonomy bundles).
32     Vocabulary::create(['vid' => 'tags', 'name' => 'Tags']);
33     Vocabulary::create(['vid' => 'categories', 'name' => 'Categories']);
34   }
35
36   /**
37    * Tests setting the bundle in the destination.
38    */
39   public function testDestinationBundle() {
40     $term_data_rows = [
41       ['id' => 1, 'name' => 'Category 1'],
42     ];
43     $ids = ['id' => ['type' => 'integer']];
44     $definition = [
45       'id' => 'terms',
46       'migration_tags' => ['Bundle test'],
47       'source' => [
48         'plugin' => 'embedded_data',
49         'data_rows' => $term_data_rows,
50         'ids' => $ids,
51       ],
52       'process' => [
53         'tid' => 'id',
54         'name' => 'name',
55       ],
56       'destination' => [
57         'plugin' => 'entity:taxonomy_term',
58         'default_bundle' => 'categories',
59       ],
60       'migration_dependencies' => [],
61     ];
62
63     $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
64
65     // Import and validate the term entity was created with the correct bundle.
66     $term_executable = new MigrateExecutable($term_migration, $this);
67     $term_executable->import();
68     /** @var Term $term */
69     $term = Term::load(1);
70     $this->assertEquals($term->bundle(), 'categories');
71   }
72
73   /**
74    * Tests setting the bundle in the process pipeline.
75    */
76   public function testProcessBundle() {
77     $term_data_rows = [
78       ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'],
79       ['id' => 2, 'vocab' => 'tags', 'name' => 'Tag 1'],
80     ];
81     $ids = ['id' => ['type' => 'integer']];
82     $definition = [
83       'id' => 'terms',
84       'migration_tags' => ['Bundle test'],
85       'source' => [
86         'plugin' => 'embedded_data',
87         'data_rows' => $term_data_rows,
88         'ids' => $ids,
89       ],
90       'process' => [
91         'tid' => 'id',
92         'vid' => 'vocab',
93         'name' => 'name',
94       ],
95       'destination' => [
96         'plugin' => 'entity:taxonomy_term',
97       ],
98       'migration_dependencies' => [],
99     ];
100
101     $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
102
103     // Import and validate the term entities were created with the correct bundle.
104     $term_executable = new MigrateExecutable($term_migration, $this);
105     $term_executable->import();
106     /** @var Term $term */
107     $term = Term::load(1);
108     $this->assertEquals($term->bundle(), 'categories');
109     $term = Term::load(2);
110     $this->assertEquals($term->bundle(), 'tags');
111   }
112
113   /**
114    * Tests setting bundles both in process and destination.
115    */
116   public function testMixedBundles() {
117     $term_data_rows = [
118       ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'],
119       ['id' => 2, 'name' => 'Tag 1'],
120     ];
121     $ids = ['id' => ['type' => 'integer']];
122     $definition = [
123       'id' => 'terms',
124       'migration_tags' => ['Bundle test'],
125       'source' => [
126         'plugin' => 'embedded_data',
127         'data_rows' => $term_data_rows,
128         'ids' => $ids,
129       ],
130       'process' => [
131         'tid' => 'id',
132         'vid' => 'vocab',
133         'name' => 'name',
134       ],
135       'destination' => [
136         'plugin' => 'entity:taxonomy_term',
137         // When no vocab is provided, the destination bundle is applied.
138         'default_bundle' => 'tags',
139       ],
140       'migration_dependencies' => [],
141     ];
142
143     $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
144
145     // Import and validate the term entities were created with the correct bundle.
146     $term_executable = new MigrateExecutable($term_migration, $this);
147     $term_executable->import();
148     /** @var Term $term */
149     $term = Term::load(1);
150     $this->assertEquals($term->bundle(), 'categories');
151     $term = Term::load(2);
152     $this->assertEquals($term->bundle(), 'tags');
153   }
154
155 }