comparison core/modules/migrate/tests/src/Kernel/MigrateBundleTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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', 'user'];
22
23 /**
24 * {@inheritdoc}
25 */
26 protected function setUp() {
27 parent::setUp();
28 $this->installEntitySchema('user');
29 $this->installEntitySchema('taxonomy_vocabulary');
30 $this->installEntitySchema('taxonomy_term');
31 $this->installConfig(['taxonomy']);
32 // Set up two vocabularies (taxonomy bundles).
33 Vocabulary::create(['vid' => 'tags', 'name' => 'Tags']);
34 Vocabulary::create(['vid' => 'categories', 'name' => 'Categories']);
35 }
36
37 /**
38 * Tests setting the bundle in the destination.
39 */
40 public function testDestinationBundle() {
41 $term_data_rows = [
42 ['id' => 1, 'name' => 'Category 1'],
43 ];
44 $ids = ['id' => ['type' => 'integer']];
45 $definition = [
46 'id' => 'terms',
47 'migration_tags' => ['Bundle test'],
48 'source' => [
49 'plugin' => 'embedded_data',
50 'data_rows' => $term_data_rows,
51 'ids' => $ids,
52 ],
53 'process' => [
54 'tid' => 'id',
55 'name' => 'name',
56 ],
57 'destination' => [
58 'plugin' => 'entity:taxonomy_term',
59 'default_bundle' => 'categories',
60 ],
61 'migration_dependencies' => [],
62 ];
63
64 $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
65
66 // Import and validate the term entity was created with the correct bundle.
67 $term_executable = new MigrateExecutable($term_migration, $this);
68 $term_executable->import();
69 /** @var Term $term */
70 $term = Term::load(1);
71 $this->assertEquals($term->bundle(), 'categories');
72 }
73
74 /**
75 * Tests setting the bundle in the process pipeline.
76 */
77 public function testProcessBundle() {
78 $term_data_rows = [
79 ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'],
80 ['id' => 2, 'vocab' => 'tags', 'name' => 'Tag 1'],
81 ];
82 $ids = ['id' => ['type' => 'integer']];
83 $definition = [
84 'id' => 'terms',
85 'migration_tags' => ['Bundle test'],
86 'source' => [
87 'plugin' => 'embedded_data',
88 'data_rows' => $term_data_rows,
89 'ids' => $ids,
90 ],
91 'process' => [
92 'tid' => 'id',
93 'vid' => 'vocab',
94 'name' => 'name',
95 ],
96 'destination' => [
97 'plugin' => 'entity:taxonomy_term',
98 ],
99 'migration_dependencies' => [],
100 ];
101
102 $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
103
104 // Import and validate the term entities were created with the correct bundle.
105 $term_executable = new MigrateExecutable($term_migration, $this);
106 $term_executable->import();
107 /** @var Term $term */
108 $term = Term::load(1);
109 $this->assertEquals($term->bundle(), 'categories');
110 $term = Term::load(2);
111 $this->assertEquals($term->bundle(), 'tags');
112 }
113
114 /**
115 * Tests setting bundles both in process and destination.
116 */
117 public function testMixedBundles() {
118 $term_data_rows = [
119 ['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'],
120 ['id' => 2, 'name' => 'Tag 1'],
121 ];
122 $ids = ['id' => ['type' => 'integer']];
123 $definition = [
124 'id' => 'terms',
125 'migration_tags' => ['Bundle test'],
126 'source' => [
127 'plugin' => 'embedded_data',
128 'data_rows' => $term_data_rows,
129 'ids' => $ids,
130 ],
131 'process' => [
132 'tid' => 'id',
133 'vid' => 'vocab',
134 'name' => 'name',
135 ],
136 'destination' => [
137 'plugin' => 'entity:taxonomy_term',
138 // When no vocab is provided, the destination bundle is applied.
139 'default_bundle' => 'tags',
140 ],
141 'migration_dependencies' => [],
142 ];
143
144 $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
145
146 // Import and validate the term entities were created with the correct bundle.
147 $term_executable = new MigrateExecutable($term_migration, $this);
148 $term_executable->import();
149 /** @var Term $term */
150 $term = Term::load(1);
151 $this->assertEquals($term->bundle(), 'categories');
152 $term = Term::load(2);
153 $this->assertEquals($term->bundle(), 'tags');
154 }
155
156 }