Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / field / tests / src / Unit / Plugin / migrate / process / ProcessFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Unit\Plugin\migrate\process;
4
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;
15
16 /**
17  * Tests the ProcessField migrate process plugin.
18  *
19  * @coversDefaultClass \Drupal\field\Plugin\migrate\process\ProcessField
20  * @group field
21  * @group legacy
22  */
23 class ProcessFieldTest extends MigrateTestCase {
24
25   /**
26    * {@inheritdoc}
27    */
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);
35
36     $this->fieldManager->getPluginIdFromFieldType('foo', [], $this->migration->reveal())->willReturn('foo');
37     $this->fieldManager->createInstance('foo', [], $this->migration->reveal())->willReturn($this->fieldPlugin);
38
39     parent::setUp();
40   }
41
42   /**
43    * Tests the transform method.
44    *
45    * @param string $method
46    *   The method to call.
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.
55    *
56    * @covers ::transform
57    * @dataProvider providerTestTransform
58    */
59   public function testTransform($method, $value, $expected_value, $migrate_exception = '', $plugin_not_found = FALSE) {
60     if ($method) {
61       $this->fieldPlugin->$method($this->row->reveal())->willReturn($expected_value);
62     }
63     $this->plugin = new ProcessField(['method' => $method], $value, [], $this->cckFieldManager->reveal(), $this->fieldManager->reveal(), $this->migration->reveal());
64
65     if ($migrate_exception) {
66       $this->setExpectedException(MigrateException::class, $migrate_exception);
67     }
68
69     if ($plugin_not_found) {
70       $exception = new PluginNotFoundException('foo');
71       $this->cckFieldManager->getPluginIdFromFieldType()->willThrow($exception);
72       $this->fieldManager->getPluginIdFromFieldType()->willThrow($exception);
73     }
74
75     $transformed_value = $this->plugin->transform($value, $this->migrateExecutable->reveal(), $this->row->reveal(), 'foo');
76     $this->assertSame($transformed_value, $expected_value);
77   }
78
79   /**
80    * Provides data for the transform method test.
81    *
82    * @return array
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.
88    */
89   public function providerTestTransform() {
90     return [
91       // Tests the getFieldType() method.
92       [
93         'method' => 'getFieldType',
94         'value' => 'foo',
95         'expected_value' => 'bar',
96       ],
97       // Tests the getFieldFormatterMap() method.
98       [
99         'method' => 'getFieldFormatterMap',
100         'value' => 'foo',
101         'expected_value' => ['foo' => 'bar'],
102       ],
103       // Tests the getFieldWidgetMap() method.
104       [
105         'method' => 'getFieldWidgetMap',
106         'value' => 'foo',
107         'expected_value' => ['foo' => 'bar'],
108       ],
109       // Tests that an exception is thrown if the value is not a string.
110       [
111         'method' => 'getFieldType',
112         'value' => ['foo'],
113         'expected_value' => '',
114         'migrate_exception' => 'The input value must be a string.',
115       ],
116       // Tests that an exception is thrown if no method name is provided.
117       [
118         'method' => '',
119         'value' => '',
120         'expected_value' => '',
121         'migrate_exception' => 'You need to specify the name of a method to be called on the Field plugin.',
122       ],
123       // Tests that NULL is returned if no field plugin is found.
124       [
125         'method' => 'getFieldType',
126         'value' => 'foo',
127         'expected_value' => NULL,
128         'migrate_exception' => '',
129         'plugin_not_found' => TRUE,
130       ],
131     ];
132   }
133
134 }