Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / text / tests / src / Unit / Migrate / d6 / TextFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Unit\Migrate\d6;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6 use Drupal\migrate\Row;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\text\Plugin\migrate\field\d6\TextField;
9 use Prophecy\Argument;
10
11 /**
12  * @coversDefaultClass \Drupal\text\Plugin\migrate\field\d6\TextField
13  * @group text
14  * @group legacy
15  */
16 class TextFieldTest extends UnitTestCase {
17
18   /**
19    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
20    */
21   protected $plugin;
22
23   /**
24    * @var \Drupal\migrate\Plugin\MigrationInterface
25    */
26   protected $migration;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     $this->plugin = new TextField([], 'text', []);
33
34     $migration = $this->prophesize(MigrationInterface::class);
35
36     // The plugin's defineValueProcessPipeline() method will call
37     // setProcessOfProperty() and return nothing. So, in order to examine the
38     // process pipeline created by the plugin, we need to ensure that
39     // getProcess() always returns the last input to setProcessOfProperty().
40     $migration->setProcessOfProperty(Argument::type('string'), Argument::type('array'))
41       ->will(function ($arguments) use ($migration) {
42         $migration->getProcess()->willReturn($arguments[1]);
43       });
44
45     $this->migration = $migration->reveal();
46   }
47
48   /**
49    * Calls the deprecated processFieldValues() method to test BC.
50    *
51    * @covers ::processFieldValues
52    *
53    * @depends testFilteredTextValueProcessPipeline
54    */
55   public function testProcessFilteredTextFieldValues() {
56     $field_info = [
57       'widget_type' => 'text_textfield',
58     ];
59     $this->plugin->processFieldValues($this->migration, 'body', $field_info);
60
61     $process = $this->migration->getProcess();
62     $this->assertSame('sub_process', $process['plugin']);
63     $this->assertSame('body', $process['source']);
64     $this->assertSame('value', $process['process']['value']);
65
66     // Ensure that filter format IDs will be looked up in the filter format
67     // migrations.
68     $lookup = $process['process']['format'][2];
69     $this->assertSame('migration', $lookup['plugin']);
70     $this->assertContains('d6_filter_format', $lookup['migration']);
71     $this->assertContains('d7_filter_format', $lookup['migration']);
72     $this->assertSame('format', $lookup['source']);
73   }
74
75   /**
76    * @covers ::defineValueProcessPipeline
77    */
78   public function testFilteredTextValueProcessPipeline() {
79     $field_info = [
80       'widget_type' => 'text_textfield',
81     ];
82     $this->plugin->defineValueProcessPipeline($this->migration, 'body', $field_info);
83
84     $process = $this->migration->getProcess();
85     $this->assertSame('sub_process', $process['plugin']);
86     $this->assertSame('body', $process['source']);
87     $this->assertSame('value', $process['process']['value']);
88
89     // Ensure that filter format IDs will be looked up in the filter format
90     // migrations.
91     $lookup = $process['process']['format'][2];
92     $this->assertSame('migration', $lookup['plugin']);
93     $this->assertContains('d6_filter_format', $lookup['migration']);
94     $this->assertContains('d7_filter_format', $lookup['migration']);
95     $this->assertSame('format', $lookup['source']);
96   }
97
98   /**
99    * Calls the deprecated processFieldValues() method to test BC.
100    *
101    * @covers ::processFieldValues
102    *
103    * @depends testBooleanTextImplicitValueProcessPipeline
104    */
105   public function testProcessBooleanTextImplicitValues() {
106     $info = [
107       'widget_type' => 'optionwidgets_onoff',
108       'global_settings' => [
109         'allowed_values' => "foo\nbar",
110       ],
111     ];
112     $this->plugin->processFieldValues($this->migration, 'field', $info);
113
114     $expected = [
115       'value' => [
116         'plugin' => 'static_map',
117         'source' => 'value',
118         'default_value' => 0,
119         'map' => [
120           'bar' => 1,
121         ],
122       ],
123     ];
124     $this->assertSame($expected, $this->migration->getProcess()['process']);
125   }
126
127   /**
128    * @covers ::defineValueProcessPipeline
129    */
130   public function testBooleanTextImplicitValueProcessPipeline() {
131     $info = [
132       'widget_type' => 'optionwidgets_onoff',
133       'global_settings' => [
134         'allowed_values' => "foo\nbar",
135       ],
136     ];
137     $this->plugin->defineValueProcessPipeline($this->migration, 'field', $info);
138
139     $expected = [
140       'value' => [
141         'plugin' => 'static_map',
142         'source' => 'value',
143         'default_value' => 0,
144         'map' => [
145           'bar' => 1,
146         ],
147       ],
148     ];
149     $this->assertSame($expected, $this->migration->getProcess()['process']);
150   }
151
152   /**
153    * Calls the deprecated processFieldValues() method to test BC.
154    *
155    * @covers ::processFieldValues
156    *
157    * @depends testBooleanTextExplicitValueProcessPipeline
158    */
159   public function testProcessBooleanTextExplicitValues() {
160     $info = [
161       'widget_type' => 'optionwidgets_onoff',
162       'global_settings' => [
163         'allowed_values' => "foo|Foo\nbaz|Baz",
164       ],
165     ];
166     $this->plugin->processFieldValues($this->migration, 'field', $info);
167
168     $expected = [
169       'value' => [
170         'plugin' => 'static_map',
171         'source' => 'value',
172         'default_value' => 0,
173         'map' => [
174           'baz' => 1,
175         ],
176       ],
177     ];
178     $this->assertSame($expected, $this->migration->getProcess()['process']);
179   }
180
181   /**
182    * @covers ::defineValueProcessPipeline
183    */
184   public function testBooleanTextExplicitValueProcessPipeline() {
185     $info = [
186       'widget_type' => 'optionwidgets_onoff',
187       'global_settings' => [
188         'allowed_values' => "foo|Foo\nbaz|Baz",
189       ],
190     ];
191     $this->plugin->defineValueProcessPipeline($this->migration, 'field', $info);
192
193     $expected = [
194       'value' => [
195         'plugin' => 'static_map',
196         'source' => 'value',
197         'default_value' => 0,
198         'map' => [
199           'baz' => 1,
200         ],
201       ],
202     ];
203     $this->assertSame($expected, $this->migration->getProcess()['process']);
204   }
205
206   /**
207    * Data provider for testGetFieldType().
208    */
209   public function getFieldTypeProvider() {
210     return [
211       ['string_long', 'text_textfield', ['text_processing' => FALSE]],
212       ['string', 'text_textfield', [
213           'text_processing' => FALSE,
214           'max_length' => 128,
215         ],
216       ],
217       ['string_long', 'text_textfield', [
218           'text_processing' => FALSE,
219           'max_length' => 4096,
220         ],
221       ],
222       ['text_long', 'text_textfield', ['text_processing' => TRUE]],
223       ['text', 'text_textfield', [
224           'text_processing' => TRUE,
225           'max_length' => 128,
226         ],
227       ],
228       ['text_long', 'text_textfield', [
229           'text_processing' => TRUE,
230           'max_length' => 4096,
231         ],
232       ],
233       ['list_string', 'optionwidgets_buttons'],
234       ['list_string', 'optionwidgets_select'],
235       ['boolean', 'optionwidgets_onoff'],
236       ['text_long', 'text_textarea', ['text_processing' => TRUE]],
237       ['string_long', 'text_textarea', ['text_processing' => FALSE]],
238       [NULL, 'undefined'],
239     ];
240   }
241
242   /**
243    * @covers ::getFieldType
244    * @dataProvider getFieldTypeProvider
245    */
246   public function testGetFieldType($expected_type, $widget_type, array $settings = []) {
247     $row = new Row();
248     $row->setSourceProperty('widget_type', $widget_type);
249     $row->setSourceProperty('global_settings', $settings);
250     $this->assertSame($expected_type, $this->plugin->getFieldType($row));
251   }
252
253 }