c6970f4d9502c9e4cd9a858e3d25d798fc77fb99
[yaffs-website] / web / core / modules / text / tests / src / Unit / Plugin / migrate / field / d6 / TextFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Unit\Plugin\migrate\field\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  */
15 class TextFieldTest extends UnitTestCase {
16
17   /**
18    * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
19    */
20   protected $plugin;
21
22   /**
23    * @var \Drupal\migrate\Plugin\MigrationInterface
24    */
25   protected $migration;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     $this->plugin = new TextField([], 'text', []);
32
33     $migration = $this->prophesize(MigrationInterface::class);
34
35     // The plugin's defineValueProcessPipeline() method will call
36     // setProcessOfProperty() and return nothing. So, in order to examine the
37     // process pipeline created by the plugin, we need to ensure that
38     // getProcess() always returns the last input to setProcessOfProperty().
39     $migration->setProcessOfProperty(Argument::type('string'), Argument::type('array'))
40       ->will(function ($arguments) use ($migration) {
41         $migration->getProcess()->willReturn($arguments[1]);
42       });
43
44     $this->migration = $migration->reveal();
45   }
46
47   /**
48    * @covers ::defineValueProcessPipeline
49    */
50   public function testProcessFilteredTextFieldValues($method = 'defineValueProcessPipeline') {
51     $field_info = [
52       'widget_type' => 'text_textfield',
53     ];
54     $this->plugin->$method($this->migration, 'body', $field_info);
55
56     $process = $this->migration->getProcess();
57     $this->assertSame('sub_process', $process['plugin']);
58     $this->assertSame('body', $process['source']);
59     $this->assertSame('value', $process['process']['value']);
60
61     // Ensure that filter format IDs will be looked up in the filter format
62     // migrations.
63     $lookup = $process['process']['format'][2];
64     $this->assertSame('migration', $lookup['plugin']);
65     $this->assertContains('d6_filter_format', $lookup['migration']);
66     $this->assertContains('d7_filter_format', $lookup['migration']);
67     $this->assertSame('format', $lookup['source']);
68   }
69
70   /**
71    * @covers ::defineValueProcessPipeline
72    */
73   public function testProcessBooleanTextImplicitValues($method = 'defineValueProcessPipeline') {
74     $info = [
75       'widget_type' => 'optionwidgets_onoff',
76       'global_settings' => [
77         'allowed_values' => "foo\nbar",
78       ],
79     ];
80     $this->plugin->$method($this->migration, 'field', $info);
81
82     $expected = [
83       'value' => [
84         'plugin' => 'static_map',
85         'source' => 'value',
86         'default_value' => 0,
87         'map' => [
88           'bar' => 1,
89         ],
90       ],
91     ];
92     $this->assertSame($expected, $this->migration->getProcess()['process']);
93   }
94
95   /**
96    * @covers ::defineValueProcessPipeline
97    */
98   public function testProcessBooleanTextExplicitValues($method = 'defineValueProcessPipeline') {
99     $info = [
100       'widget_type' => 'optionwidgets_onoff',
101       'global_settings' => [
102         'allowed_values' => "foo|Foo\nbaz|Baz",
103       ],
104     ];
105     $this->plugin->$method($this->migration, 'field', $info);
106
107     $expected = [
108       'value' => [
109         'plugin' => 'static_map',
110         'source' => 'value',
111         'default_value' => 0,
112         'map' => [
113           'baz' => 1,
114         ],
115       ],
116     ];
117     $this->assertSame($expected, $this->migration->getProcess()['process']);
118   }
119
120   /**
121    * Data provider for testGetFieldType().
122    */
123   public function getFieldTypeProvider() {
124     return [
125       ['string_long', 'text_textfield', ['text_processing' => FALSE]],
126       ['string', 'text_textfield', [
127           'text_processing' => FALSE,
128           'max_length' => 128,
129         ],
130       ],
131       ['string_long', 'text_textfield', [
132           'text_processing' => FALSE,
133           'max_length' => 4096,
134         ],
135       ],
136       ['text_long', 'text_textfield', ['text_processing' => TRUE]],
137       ['text', 'text_textfield', [
138           'text_processing' => TRUE,
139           'max_length' => 128,
140         ],
141       ],
142       ['text_long', 'text_textfield', [
143           'text_processing' => TRUE,
144           'max_length' => 4096,
145         ],
146       ],
147       ['list_string', 'optionwidgets_buttons'],
148       ['list_string', 'optionwidgets_select'],
149       ['boolean', 'optionwidgets_onoff'],
150       ['text_long', 'text_textarea', ['text_processing' => TRUE]],
151       ['string_long', 'text_textarea', ['text_processing' => FALSE]],
152       [NULL, 'undefined'],
153     ];
154   }
155
156   /**
157    * @covers ::getFieldType
158    * @dataProvider getFieldTypeProvider
159    */
160   public function testGetFieldType($expected_type, $widget_type, array $settings = []) {
161     $row = new Row();
162     $row->setSourceProperty('widget_type', $widget_type);
163     $row->setSourceProperty('global_settings', $settings);
164     $this->assertSame($expected_type, $this->plugin->getFieldType($row));
165   }
166
167 }