TextFieldTest.php 4.75 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\text\Unit\Migrate;

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 10 11
use Prophecy\Argument;

/**
12
 * @coversDefaultClass \Drupal\text\Plugin\migrate\cckfield\TextField
13
 * @group text
14
 * @group legacy
15 16 17 18
 */
class TextFieldTest extends UnitTestCase {

  /**
19
   * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface
20 21 22 23
   */
  protected $plugin;

  /**
24
   * @var \Drupal\migrate\Plugin\MigrationInterface
25 26 27 28 29 30
   */
  protected $migration;

  /**
   * {@inheritdoc}
   */
31
  protected function setUp() {
32 33 34 35
    $this->plugin = new TextField([], 'text', []);

    $migration = $this->prophesize(MigrationInterface::class);

36
    // The plugin's processCckFieldValues() method will call
37 38 39 40 41 42 43 44 45 46 47 48
    // setProcessOfProperty() and return nothing. So, in order to examine the
    // process pipeline created by the plugin, we need to ensure that
    // getProcess() always returns the last input to setProcessOfProperty().
    $migration->setProcessOfProperty(Argument::type('string'), Argument::type('array'))
      ->will(function($arguments) use ($migration) {
        $migration->getProcess()->willReturn($arguments[1]);
      });

    $this->migration = $migration->reveal();
  }

  /**
49
   * @covers ::processCckFieldValues
50 51 52 53 54
   */
  public function testProcessFilteredTextFieldValues() {
    $field_info = [
      'widget_type' => 'text_textfield',
    ];
55
    $this->plugin->processCckFieldValues($this->migration, 'body', $field_info);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    $process = $this->migration->getProcess();
    $this->assertSame('iterator', $process['plugin']);
    $this->assertSame('body', $process['source']);
    $this->assertSame('value', $process['process']['value']);

    // Ensure that filter format IDs will be looked up in the filter format
    // migrations.
    $lookup = $process['process']['format'][2];
    $this->assertSame('migration', $lookup['plugin']);
    $this->assertContains('d6_filter_format', $lookup['migration']);
    $this->assertContains('d7_filter_format', $lookup['migration']);
    $this->assertSame('format', $lookup['source']);
  }

  /**
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 84 85 86 87 88 89 90 91 92 93 94 95 96

    $expected = [
      'value' => [
        'plugin' => 'static_map',
        'source' => 'value',
        'default_value' => 0,
        'map' => [
          'bar' => 1,
        ],
      ],
    ];
    $this->assertSame($expected, $this->migration->getProcess()['process']);
  }

  /**
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 109 110 111 112 113 114 115 116 117 118 119 120

    $expected = [
      'value' => [
        'plugin' => 'static_map',
        'source' => 'value',
        'default_value' => 0,
        'map' => [
          'baz' => 1,
        ],
      ],
    ];
    $this->assertSame($expected, $this->migration->getProcess()['process']);
  }

121 122 123 124
  /**
   * Data provider for testGetFieldType().
   */
  public function getFieldTypeProvider() {
125 126
    return [
      ['string_long', 'text_textfield', [
127
        'text_processing' => FALSE,
128 129
      ]],
      ['string', 'text_textfield', [
130 131
        'text_processing' => FALSE,
        'max_length' => 128,
132 133
      ]],
      ['string_long', 'text_textfield', [
134 135
        'text_processing' => FALSE,
        'max_length' => 4096,
136 137
      ]],
      ['text_long', 'text_textfield', [
138
        'text_processing' => TRUE,
139 140
      ]],
      ['text', 'text_textfield', [
141 142
        'text_processing' => TRUE,
        'max_length' => 128,
143 144
      ]],
      ['text_long', 'text_textfield', [
145 146
        'text_processing' => TRUE,
        'max_length' => 4096,
147 148 149 150
      ]],
      ['list_string', 'optionwidgets_buttons'],
      ['list_string', 'optionwidgets_select'],
      ['boolean', 'optionwidgets_onoff'],
151 152 153 154 155 156
      ['text_long', 'text_textarea', [
        'text_processing' => TRUE,
      ]],
      ['string_long', 'text_textarea', [
        'text_processing' => FALSE,
      ]],
157 158
      [NULL, 'undefined'],
    ];
159 160 161 162 163 164
  }

  /**
   * @covers ::getFieldType
   * @dataProvider getFieldTypeProvider
   */
165
  public function testGetFieldType($expected_type, $widget_type, array $settings = []) {
166 167
    $row = new Row();
    $row->setSourceProperty('widget_type', $widget_type);
168 169 170 171
    $row->setSourceProperty('global_settings', $settings);
    $this->assertSame($expected_type, $this->plugin->getFieldType($row));
  }

172
}