View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\migrate\process\Get;
use Drupal\migrate\Plugin\migrate\process\SubProcess;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SubProcessTest extends MigrateTestCase {
protected $plugin;
protected $migrationConfiguration = [
'id' => 'test',
];
public function testSubProcess($process_configuration, $source_values = []) {
$migration = $this
->getMigration($process_configuration);
$plugin = new SubProcess($process_configuration, 'sub_process', []);
foreach ($process_configuration['process'] as $destination => $source) {
$sub_process_plugins[$destination][] = new Get([
'source' => $source,
], 'get', []);
}
$migration
->expects($this
->at(1))
->method('getProcessPlugins')
->willReturn($sub_process_plugins);
$key_plugin['key'][] = new Get([
'source' => '@id',
], 'get', []);
$migration
->expects($this
->at(2))
->method('getProcessPlugins')
->will($this
->returnValue($key_plugin));
$event_dispatcher = $this
->createMock(EventDispatcherInterface::class);
$migrate_executable = new MigrateExecutable($migration, $this
->createMock(MigrateMessageInterface::class), $event_dispatcher);
$current_value = [
[
'source_foo' => 'test',
'source_id' => 42,
] + $source_values,
];
$row = new Row($source_values);
$new_value = $plugin
->transform($current_value, $migrate_executable, $row, 'test');
$this
->assertCount(1, $new_value);
$this
->assertCount(count($process_configuration['process']), $new_value[42]);
$this
->assertSame('test', $new_value[42]['foo']);
if ($source_values) {
$this
->assertSame('source_baz', $new_value[42]['baaa']);
}
$this
->assertSame(42, $new_value[42]['id']);
}
public function providerTestSubProcess() {
return [
'no source context' => [
'process configuration' => [
'process' => [
'foo' => 'source_foo',
'id' => 'source_id',
],
'key' => '@id',
],
],
'default source key' => [
'process configuration' => [
'process' => [
'foo' => 'source_foo',
'id' => 'source_id',
'baaa' => 'source/baf',
],
'key' => '@id',
'include_source' => TRUE,
],
'source values' => [
'baf' => 'source_baz',
],
],
'renamed source key' => [
'process configuration' => [
'process' => [
'foo' => 'source_foo',
'id' => 'source_id',
'baaa' => 'my_source/baf',
],
'key' => '@id',
'include_source' => TRUE,
'source_key' => 'my_source',
],
'source values' => [
'baf' => 'source_baz',
],
],
];
}
public function testNotFoundSubProcess($process_configuration, $source_values = []) {
$migration = $this
->getMigration();
$plugin = new SubProcess($process_configuration, 'sub_process', []);
foreach ($process_configuration['process'] as $destination => $source) {
$sub_process_plugins[$destination][] = new Get([
'source' => $source,
], 'get', []);
}
$migration
->expects($this
->at(1))
->method('getProcessPlugins')
->willReturn($sub_process_plugins);
if (array_key_exists('key', $process_configuration)) {
$key_plugin['key'][] = new Get([
'source' => '@id',
], 'get', []);
$migration
->expects($this
->at(2))
->method('getProcessPlugins')
->will($this
->returnValue($key_plugin));
}
$event_dispatcher = $this
->createMock(EventDispatcherInterface::class);
$migrate_executable = new MigrateExecutable($migration, $this
->createMock(MigrateMessageInterface::class), $event_dispatcher);
$current_value = [
[
'source_foo' => 'test',
'source_id' => NULL,
] + $source_values,
];
$row = new Row($source_values);
$new_value = $plugin
->transform($current_value, $migrate_executable, $row, 'test');
$this
->assertArrayEquals([], $new_value);
}
public function providerTestNotFoundSubProcess() {
return [
'no key' => [
'process configuration' => [
'process' => [
'foo' => 'source_foo',
],
'key' => '@id',
],
],
'lookup returns NULL' => [
'process configuration' => [
'process' => [
'foo' => 'source_foo',
'id' => 'source_id',
],
'key' => '@id',
],
],
];
}
}