db backup prior to drupal security update
[yaffs-website] / web / core / modules / text / tests / src / Unit / Migrate / TextFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\text\Unit\Migrate;
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  */
15 class TextFieldTest extends UnitTestCase {
16
17   /**
18    * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface
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 processCckFieldValues() 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 ::processCckFieldValues
49    */
50   public function testProcessFilteredTextFieldValues() {
51     $field_info = [
52       'widget_type' => 'text_textfield',
53     ];
54     $this->plugin->processCckFieldValues($this->migration, 'body', $field_info);
55
56     $process = $this->migration->getProcess();
57     $this->assertSame('iterator', $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 ::processCckFieldValues
72    */
73   public function testProcessBooleanTextImplicitValues() {
74     $info = [
75       'widget_type' => 'optionwidgets_onoff',
76       'global_settings' => [
77         'allowed_values' => "foo\nbar",
78       ]
79     ];
80     $this->plugin->processCckFieldValues($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 ::processCckFieldValues
97    */
98   public function testProcessBooleanTextExplicitValues() {
99     $info = [
100       'widget_type' => 'optionwidgets_onoff',
101       'global_settings' => [
102         'allowed_values' => "foo|Foo\nbaz|Baz",
103       ]
104     ];
105     $this->plugin->processCckFieldValues($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', [
126         'text_processing' => FALSE,
127       ]],
128       ['string', 'text_textfield', [
129         'text_processing' => FALSE,
130         'max_length' => 128,
131       ]],
132       ['string_long', 'text_textfield', [
133         'text_processing' => FALSE,
134         'max_length' => 4096,
135       ]],
136       ['text_long', 'text_textfield', [
137         'text_processing' => TRUE,
138       ]],
139       ['text', 'text_textfield', [
140         'text_processing' => TRUE,
141         'max_length' => 128,
142       ]],
143       ['text_long', 'text_textfield', [
144         'text_processing' => TRUE,
145         'max_length' => 4096,
146       ]],
147       ['list_string', 'optionwidgets_buttons'],
148       ['list_string', 'optionwidgets_select'],
149       ['boolean', 'optionwidgets_onoff'],
150       ['text_long', 'text_textarea', [
151         'text_processing' => TRUE,
152       ]],
153       ['string_long', 'text_textarea', [
154         'text_processing' => FALSE,
155       ]],
156       [NULL, 'undefined'],
157     ];
158   }
159
160   /**
161    * @covers ::getFieldType
162    * @dataProvider getFieldTypeProvider
163    */
164   public function testGetFieldType($expected_type, $widget_type, array $settings = []) {
165     $row = new Row();
166     $row->setSourceProperty('widget_type', $widget_type);
167     $row->setSourceProperty('global_settings', $settings);
168     $this->assertSame($expected_type, $this->plugin->getFieldType($row));
169   }
170
171 }