3 namespace Drupal\Tests\field\Unit\Plugin\migrate\process;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\field\Plugin\migrate\process\ProcessField;
7 use Drupal\migrate\MigrateException;
8 use Drupal\migrate\MigrateExecutable;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\Row;
11 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
12 use Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
13 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
14 use Drupal\Tests\migrate\Unit\MigrateTestCase;
17 * Tests the ProcessField migrate process plugin.
19 * @coversDefaultClass \Drupal\field\Plugin\migrate\process\ProcessField
23 class ProcessFieldTest extends MigrateTestCase {
28 protected function setUp() {
29 $this->cckFieldManager = $this->prophesize(MigrateCckFieldPluginManagerInterface::class);
30 $this->fieldManager = $this->prophesize(MigrateFieldPluginManagerInterface::class);
31 $this->fieldPlugin = $this->prophesize(MigrateFieldInterface::class);
32 $this->migrateExecutable = $this->prophesize(MigrateExecutable::class);
33 $this->migration = $this->prophesize(MigrationInterface::class);
34 $this->row = $this->prophesize(Row::class);
36 $this->fieldManager->getPluginIdFromFieldType('foo', [], $this->migration->reveal())->willReturn('foo');
37 $this->fieldManager->createInstance('foo', [], $this->migration->reveal())->willReturn($this->fieldPlugin);
43 * Tests the transform method.
45 * @param string $method
47 * @param string $value
48 * The value to process.
49 * @param mixed $expected_value
50 * The expected transformed value.
51 * @param string $migrate_exception
52 * The MigrateException message to expect.
53 * @param bool $plugin_not_found
54 * Whether the field plugin is not found.
57 * @dataProvider providerTestTransform
59 public function testTransform($method, $value, $expected_value, $migrate_exception = '', $plugin_not_found = FALSE) {
61 $this->fieldPlugin->$method($this->row->reveal())->willReturn($expected_value);
63 $this->plugin = new ProcessField(['method' => $method], $value, [], $this->cckFieldManager->reveal(), $this->fieldManager->reveal(), $this->migration->reveal());
65 if ($migrate_exception) {
66 $this->setExpectedException(MigrateException::class, $migrate_exception);
69 if ($plugin_not_found) {
70 $exception = new PluginNotFoundException('foo');
71 $this->cckFieldManager->getPluginIdFromFieldType()->willThrow($exception);
72 $this->fieldManager->getPluginIdFromFieldType()->willThrow($exception);
75 $transformed_value = $this->plugin->transform($value, $this->migrateExecutable->reveal(), $this->row->reveal(), 'foo');
76 $this->assertSame($transformed_value, $expected_value);
80 * Provides data for the transform method test.
83 * - The method to call.
84 * - The value to process.
85 * - The expected transformed value.
86 * - The MigrateException message to expect.
87 * - Whether the field plugin is not found.
89 public function providerTestTransform() {
91 // Tests the getFieldType() method.
93 'method' => 'getFieldType',
95 'expected_value' => 'bar',
97 // Tests the getFieldFormatterMap() method.
99 'method' => 'getFieldFormatterMap',
101 'expected_value' => ['foo' => 'bar'],
103 // Tests the getFieldWidgetMap() method.
105 'method' => 'getFieldWidgetMap',
107 'expected_value' => ['foo' => 'bar'],
109 // Tests that an exception is thrown if the value is not a string.
111 'method' => 'getFieldType',
113 'expected_value' => '',
114 'migrate_exception' => 'The input value must be a string.',
116 // Tests that an exception is thrown if no method name is provided.
120 'expected_value' => '',
121 'migrate_exception' => 'You need to specify the name of a method to be called on the Field plugin.',
123 // Tests that NULL is returned if no field plugin is found.
125 'method' => 'getFieldType',
127 'expected_value' => NULL,
128 'migrate_exception' => '',
129 'plugin_not_found' => TRUE,