Newer
Older
<?php
declare(strict_types = 1);
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Row;

Ivan Doroshenko
committed
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
* Tests the entity_lookup plugin.
*
* @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup
* @group migrate_plus
*/
final class EntityLookupTest extends KernelTestBase {
use UserCreationTrait;

Ivan Doroshenko
committed
use NodeCreationTrait;
/**
* The migrate executable mock object.
*
* @var \Drupal\migrate\MigrateExecutable|\PHPUnit\Framework\MockObject\MockObject
*/
protected $migrateExecutable;
/**
* {@inheritdoc}
*/
protected static $modules = [
'migrate_plus',
'migrate',
'user',
'system',

Ivan Doroshenko
committed
'node',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');

Ivan Doroshenko
committed
$this->installEntitySchema('node');
$this->installConfig(['filter']);
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();

Ivan Doroshenko
committed
$test_nodes = [
['title' => 'foo 1'],
['title' => 'foo 2'],
['title' => 'bar 1'],
];
foreach ($test_nodes as $test_node) {
$this->createNode($test_node);
}
}
/**
* Lookup an entity without bundles on destination key.
*
* Using user entity as destination entity without bundles as example for
* testing.
*
* @covers ::transform
*/
public function testLookupEntityWithoutBundles(): void {

Ivan Doroshenko
committed
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration([
'id' => 'test',
'source' => [],
'process' => [],
'destination' => [
'plugin' => 'entity:user',
],
]);
// Create a user.
$known_user = $this->createUser([], 'lucuma');

Ivan Doroshenko
committed
$configuration = [
'entity_type' => 'user',
'value_key' => 'name',
];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$row = new Row();

Ivan Doroshenko
committed
// Check the known user is found.

Ivan Doroshenko
committed
$value = $plugin->transform('lucuma', $this->migrateExecutable, $row, 'name');
$this->assertSame($known_user->id(), $value);

Ivan Doroshenko
committed
// Check an unknown user is not found.

Ivan Doroshenko
committed
$value = $plugin->transform('orange', $this->migrateExecutable, $row, 'name');
$this->assertNull($value);
}
/**
* Tests a lookup of config entity.
*/
public function testConfigEntityLookup(): void {
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration([
'id' => 'test',
'source' => [],
'process' => [],
'destination' => [
'plugin' => 'entity:node',
],
]);
$configuration = [
'entity_type' => 'filter_format',
'value_key' => 'name',
];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$value = $plugin->transform('Plain text', $this->migrateExecutable, new Row(), 'destination_property');
$this->assertEquals('plain_text', $value);
}

Ivan Doroshenko
committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Tests lookup with different operators.
*
* @covers ::transform
* @dataProvider providerTestLookupOperators
*/
public function testLookupOperators($configuration, $lookup_value, $expected_value): void {
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration([
'id' => 'test',
'source' => [],
'process' => [],
'destination' => [
'plugin' => 'entity:node',
],
]);
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$value = $plugin->transform($lookup_value, $this->migrateExecutable, new Row(), 'destination_property');
$this->assertEquals($expected_value, $value);
}
/**
* Data provider for testLookupOperators test.
*
* @return array[]
* The test cases.
*/
public function providerTestLookupOperators(): array {
return [
'Default operator' => [
[
'entity_type' => 'node',
'value_key' => 'title',
],
'foo 1',
'1',
],
'Multiple values' => [
[
'entity_type' => 'node',
'value_key' => 'title',
],
['foo 1', 'foo 2'],
['2', '1'],
],
'Starts with' => [
[
'entity_type' => 'node',
'value_key' => 'title',
'operator' => 'STARTS_WITH',
],
'bar',
'3',
],
];
}

Ivan Doroshenko
committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Tests a lookup with bundle conditions.
*/
public function testEntityLookupWithBundles(): void {
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration([
'id' => 'test',
'source' => [],
'process' => [],
'destination' => [
'plugin' => 'entity:node',
],
]);
// Create a node of article content type.
$this->createNode([
'title' => 'article 1',
'type' => 'article',
]);
$configuration = [
'entity_type' => 'node',
'value_key' => 'title',
'bundle_key' => 'type',
'bundle' => 'page',
];
// The search is performed by one bundle - node is not found.
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$value = $plugin->transform('article 1', $this->migrateExecutable, new Row(), 'destination_property');
$this->assertEquals(NULL, $value);
// Now include both bundles in the search - node is found.
$configuration['bundle'] = ['page', 'article'];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $migration);
$value = $plugin->transform('article 1', $this->migrateExecutable, new Row(), 'destination_property');
$this->assertEquals('4', $value);
}