View source
<?php
namespace Drupal\Tests\field\Unit\Plugin\migrate\process;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\field\Plugin\migrate\process\ProcessField;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
use Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
class ProcessFieldTest extends MigrateTestCase {
protected function setUp() {
$this->cckFieldManager = $this
->prophesize(MigrateCckFieldPluginManagerInterface::class);
$this->fieldManager = $this
->prophesize(MigrateFieldPluginManagerInterface::class);
$this->fieldPlugin = $this
->prophesize(MigrateFieldInterface::class);
$this->migrateExecutable = $this
->prophesize(MigrateExecutable::class);
$this->migration = $this
->prophesize(MigrationInterface::class);
$this->row = $this
->prophesize(Row::class);
$this->fieldManager
->getPluginIdFromFieldType('foo', [], $this->migration
->reveal())
->willReturn('foo');
$this->fieldManager
->createInstance('foo', [], $this->migration
->reveal())
->willReturn($this->fieldPlugin);
parent::setUp();
}
public function testTransform($method, $value, $expected_value, $migrate_exception = '', $plugin_not_found = FALSE) {
if ($method) {
$this->fieldPlugin
->{$method}($this->row
->reveal())
->willReturn($expected_value);
}
$this->plugin = new ProcessField([
'method' => $method,
], $value, [], $this->cckFieldManager
->reveal(), $this->fieldManager
->reveal(), $this->migration
->reveal());
if ($migrate_exception) {
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage($migrate_exception);
}
if ($plugin_not_found) {
$exception = new PluginNotFoundException('foo');
$this->cckFieldManager
->getPluginIdFromFieldType()
->willThrow($exception);
$this->fieldManager
->getPluginIdFromFieldType()
->willThrow($exception);
}
$transformed_value = $this->plugin
->transform($value, $this->migrateExecutable
->reveal(), $this->row
->reveal(), 'foo');
$this
->assertSame($transformed_value, $expected_value);
}
public function providerTestTransform() {
return [
[
'method' => 'getFieldType',
'value' => 'foo',
'expected_value' => 'bar',
],
[
'method' => 'getFieldFormatterMap',
'value' => 'foo',
'expected_value' => [
'foo' => 'bar',
],
],
[
'method' => 'getFieldWidgetMap',
'value' => 'foo',
'expected_value' => [
'foo' => 'bar',
],
],
[
'method' => 'getFieldType',
'value' => [
'foo',
],
'expected_value' => '',
'migrate_exception' => 'The input value must be a string.',
],
[
'method' => '',
'value' => '',
'expected_value' => '',
'migrate_exception' => 'You need to specify the name of a method to be called on the Field plugin.',
],
[
'method' => 'getFieldType',
'value' => 'foo',
'expected_value' => NULL,
'migrate_exception' => '',
'plugin_not_found' => TRUE,
],
];
}
}