View source
<?php
namespace Drupal\Tests\range\Kernel\Migrate\d7;
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
class MigrateRangeFieldTest extends MigrateDrupal7TestBase {
protected static $modules = [
'node',
'range',
'text',
'system',
];
protected function setUp() : void {
parent::setUp();
$this
->migrateUsers(FALSE);
$this
->migrateContentTypes();
$this
->executeMigrations([
'd7_field',
'd7_field_instance',
'd7_field_instance_widget_settings',
'd7_view_modes',
'd7_field_formatter_settings',
'd7_node',
]);
}
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal7.php';
}
public function testFieldMigration($id, $type, $settings) {
$field = FieldStorageConfig::load($id);
$this
->assertInstanceOf(FieldStorageConfigInterface::class, $field);
$this
->assertSame($type, $field
->getType());
$this
->assertSame($settings, $field
->getSettings());
}
public function fieldMigrationDataProvider() {
return [
'range_decimal' => [
'node.field_decimal',
'range_decimal',
[
'precision' => 12,
'scale' => 4,
],
],
'range_float' => [
'node.field_float',
'range_float',
[],
],
'range_integer' => [
'node.field_integer',
'range_integer',
[],
],
];
}
public function testFieldInstanceMigration($id, $default_value, $settings) {
$field = FieldConfig::load($id);
$this
->assertSame($default_value, $field
->getDefaultValueLiteral());
$this
->assertSame($settings, $field
->getSettings());
}
public function fieldInstanceMigrationDataProvider() {
return [
'range_decimal' => [
'node.page.field_decimal',
[],
[
'min' => NULL,
'max' => NULL,
'from' => [
'prefix' => '',
'suffix' => '',
],
'to' => [
'prefix' => '',
'suffix' => '',
],
'field' => [
'prefix' => '',
'suffix' => '',
],
'combined' => [
'prefix' => '',
'suffix' => '',
],
'precision' => 12,
'scale' => 4,
],
],
'range_float' => [
'node.page.field_float',
[
[
'from' => 2.5,
'to' => 4.5,
],
],
[
'min' => -10.5,
'max' => 10.5,
'from' => [
'prefix' => '',
'suffix' => '',
],
'to' => [
'prefix' => '',
'suffix' => '',
],
'field' => [
'prefix' => '',
'suffix' => '',
],
'combined' => [
'prefix' => '',
'suffix' => '',
],
],
],
'range_integer' => [
'node.page.field_integer',
[],
[
'min' => NULL,
'max' => NULL,
'from' => [
'prefix' => 'FROM Prefix',
'suffix' => 'FROM Suffix',
],
'to' => [
'prefix' => 'TO PREFIX',
'suffix' => 'TO SUFFIX',
],
'field' => [
'prefix' => 'FIELD PREFIX',
'suffix' => 'FIELD SUFFIX',
],
'combined' => [
'prefix' => 'COMBINED PREFIX',
'suffix' => 'COMBINED SUFFIX',
],
],
],
];
}
public function testFieldWidgetMigration($display_id, $component_id, $type, $settings) {
$component = EntityFormDisplay::load($display_id)
->getComponent($component_id);
$this
->assertSame($type, $component['type']);
$this
->assertSame($settings, $component['settings']);
}
public function fieldWidgetMigrationDataProvider() {
return [
'range_decimal' => [
'node.page.default',
'field_decimal',
'range',
[
'label' => [
'from' => 'From',
'to' => 'to',
],
'placeholder' => [
'from' => '',
'to' => '',
],
],
],
'range_float' => [
'node.page.default',
'field_float',
'range',
[
'label' => [
'from' => 'FROM',
'to' => 'TO',
],
'placeholder' => [
'from' => '',
'to' => '',
],
],
],
'range_integer' => [
'node.page.default',
'field_integer',
'range',
[
'label' => [
'from' => 'From',
'to' => 'to',
],
'placeholder' => [
'from' => '',
'to' => '',
],
],
],
];
}
public function testFieldFormatterMigration($display_id, $component_id, $type, $settings) {
$component = EntityViewDisplay::load($display_id)
->getComponent($component_id);
$this
->assertIsArray($component);
$this
->assertSame($type, $component['type']);
$this
->assertSame($settings, $component['settings']);
}
public function fieldFormatterMigrationDataProvider() {
return [
'range_decimal' => [
'node.page.default',
'field_decimal',
'range_decimal',
[
'thousand_separator' => '.',
'decimal_separator' => ', ',
'scale' => 3,
'range_separator' => ' - ',
'range_combine' => TRUE,
'from_prefix_suffix' => TRUE,
'to_prefix_suffix' => TRUE,
'field_prefix_suffix' => TRUE,
'combined_prefix_suffix' => TRUE,
],
],
'range_decimal_sprintf' => [
'node.page.teaser',
'field_decimal',
'range_decimal_sprintf',
[
'format_string' => '%.0f',
'range_separator' => '--',
'range_combine' => FALSE,
'from_prefix_suffix' => FALSE,
'to_prefix_suffix' => FALSE,
'field_prefix_suffix' => FALSE,
'combined_prefix_suffix' => FALSE,
],
],
'range_integer' => [
'node.page.default',
'field_integer',
'range_integer',
[
'range_separator' => '-',
'range_combine' => TRUE,
'from_prefix_suffix' => FALSE,
'to_prefix_suffix' => TRUE,
'field_prefix_suffix' => FALSE,
'combined_prefix_suffix' => FALSE,
'thousand_separator' => ' ',
],
],
'range_integer_sprintf' => [
'node.page.teaser',
'field_integer',
'range_integer_sprintf',
[
'format_string' => '%x',
'range_separator' => '|',
'range_combine' => TRUE,
'from_prefix_suffix' => FALSE,
'to_prefix_suffix' => FALSE,
'field_prefix_suffix' => FALSE,
'combined_prefix_suffix' => FALSE,
],
],
'range_unformatted' => [
'node.page.default',
'field_float',
'range_unformatted',
[
'range_separator' => '-',
'range_combine' => TRUE,
'from_prefix_suffix' => FALSE,
'to_prefix_suffix' => FALSE,
'field_prefix_suffix' => FALSE,
'combined_prefix_suffix' => TRUE,
],
],
];
}
public function testFieldDataMigration($field_name, $expected) {
$node = Node::load(1);
$this
->assertSame($expected, $node->{$field_name}
->getValue());
}
public function fieldDataMigrationDataProvider() {
return [
'range_decimal' => [
'field_decimal',
[
[
'from' => '12.0000',
'to' => '18.0000',
],
[
'from' => '-44.3300',
'to' => '66.7700',
],
],
],
'range_float' => [
'field_float',
[
[
'from' => '2.5',
'to' => '4.5',
],
],
],
'range_integer' => [
'field_integer',
[
[
'from' => '8',
'to' => '111111',
],
],
],
];
}
}