Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / text / tests / src / Unit / Plugin / migrate / cckfield / TextCckTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Unit\Plugin\migrate\cckfield;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6 use Drupal\migrate\Row;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\text\Plugin\migrate\cckfield\TextField;
9 use Prophecy\Argument;
10
11 /**
12  * @coversDefaultClass \Drupal\text\Plugin\migrate\cckfield\TextField
13  * @group text
14  * @group legacy
15  */
16 class TextCckTest extends UnitTestCase {
17
18   /**
19    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface
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 processCckFieldValues() 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    * @covers ::processCckFieldValues
50    */
51   public function testProcessFilteredTextFieldValues() {
52     $field_info = [
53       'widget_type' => 'text_textfield',
54     ];
55     $this->plugin->processCckFieldValues($this->migration, 'body', $field_info);
56
57     $process = $this->migration->getProcess();
58     $this->assertSame('sub_process', $process['plugin']);
59     $this->assertSame('body', $process['source']);
60     $this->assertSame('value', $process['process']['value']);
61
62     // Ensure that filter format IDs will be looked up in the filter format
63     // migrations.
64     $lookup = $process['process']['format'][2];
65     $this->assertSame('migration', $lookup['plugin']);
66     $this->assertContains('d6_filter_format', $lookup['migration']);
67     $this->assertContains('d7_filter_format', $lookup['migration']);
68     $this->assertSame('format', $lookup['source']);
69   }
70
71   /**
72    * @covers ::processCckFieldValues
73    */
74   public function testProcessBooleanTextImplicitValues() {
75     $info = [
76       'widget_type' => 'optionwidgets_onoff',
77       'global_settings' => [
78         'allowed_values' => "foo\nbar",
79       ],
80     ];
81     $this->plugin->processCckFieldValues($this->migration, 'field', $info);
82
83     $expected = [
84       'value' => [
85         'plugin' => 'static_map',
86         'source' => 'value',
87         'default_value' => 0,
88         'map' => [
89           'bar' => 1,
90         ],
91       ],
92     ];
93     $this->assertSame($expected, $this->migration->getProcess()['process']);
94   }
95
96   /**
97    * @covers ::processCckFieldValues
98    */
99   public function testProcessBooleanTextExplicitValues() {
100     $info = [
101       'widget_type' => 'optionwidgets_onoff',
102       'global_settings' => [
103         'allowed_values' => "foo|Foo\nbaz|Baz",
104       ],
105     ];
106     $this->plugin->processCckFieldValues($this->migration, 'field', $info);
107
108     $expected = [
109       'value' => [
110         'plugin' => 'static_map',
111         'source' => 'value',
112         'default_value' => 0,
113         'map' => [
114           'baz' => 1,
115         ],
116       ],
117     ];
118     $this->assertSame($expected, $this->migration->getProcess()['process']);
119   }
120
121   /**
122    * Data provider for testGetFieldType().
123    */
124   public function getFieldTypeProvider() {
125     return [
126       ['string_long', 'text_textfield', ['text_processing' => FALSE]],
127       ['string', 'text_textfield', [
128           'text_processing' => FALSE,
129           'max_length' => 128,
130         ],
131       ],
132       ['string_long', 'text_textfield', [
133           'text_processing' => FALSE,
134           'max_length' => 4096,
135         ],
136       ],
137       ['text_long', 'text_textfield', ['text_processing' => TRUE]],
138       ['text', 'text_textfield', [
139           'text_processing' => TRUE,
140           'max_length' => 128,
141         ],
142       ],
143       ['text_long', 'text_textfield', [
144           'text_processing' => TRUE,
145           'max_length' => 4096,
146         ],
147       ],
148       ['list_string', 'optionwidgets_buttons'],
149       ['list_string', 'optionwidgets_select'],
150       ['boolean', 'optionwidgets_onoff'],
151       ['text_long', 'text_textarea', ['text_processing' => TRUE]],
152       ['string_long', 'text_textarea', ['text_processing' => FALSE]],
153       [NULL, 'undefined'],
154     ];
155   }
156
157   /**
158    * @covers ::getFieldType
159    * @dataProvider getFieldTypeProvider
160    */
161   public function testGetFieldType($expected_type, $widget_type, array $settings = []) {
162     $row = new Row(['widget_type' => $widget_type], ['widget_type' => []]);
163     $row->setSourceProperty('global_settings', $settings);
164     $this->assertSame($expected_type, $this->plugin->getFieldType($row));
165   }
166
167 }