You are here

public function MultiMappingTest::testImportTwoDateValues in Feeds 8.3

Tests importing two date values to the same target.

The target configuration per date target differs to ensure that this configuration is respected per value. The first date target is configured to interpret date values in the "UTC" timezone, while the second should interpret it as "Europe/Amsterdam".

File

tests/src/Kernel/MultiMappingTest.php, line 107

Class

MultiMappingTest
Tests mapping multiple times to the same target.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testImportTwoDateValues() {

  // Create a text field that can hold an unlimited amount of values.
  $this
    ->createFieldWithStorage('field_date', [
    'type' => 'datetime',
    'storage' => [
      'settings' => [
        'datetime_type' => 'datetime',
      ],
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ],
  ]);

  // Create a feed type, map two sources to the same target.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'title' => 'title',
    'datetime_start' => 'datetime_start',
    'datetime_end' => 'datetime_end',
  ], [
    'mappings' => [
      [
        'target' => 'title',
        'map' => [
          'value' => 'title',
        ],
      ],
      [
        'target' => 'field_date',
        'map' => [
          'value' => 'datetime_start',
        ],
        'settings' => [
          'timezone' => 'UTC',
        ],
      ],
      [
        'target' => 'field_date',
        'map' => [
          'value' => 'datetime_end',
        ],
        'settings' => [
          'timezone' => 'Europe/Amsterdam',
        ],
      ],
    ],
  ]);

  // Import data.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content_date.csv',
  ]);
  $feed
    ->import();
  $this
    ->assertNodeCount(4);

  // Check the values imported into field_date.
  $nodes = Node::loadMultiple();
  $expected_values_per_node = [
    1 => [
      [
        'value' => '1955-11-05T12:00:00',
      ],
      [
        'value' => '1955-11-05T14:00:00',
      ],
    ],
    2 => [
      [
        'value' => '2015-10-21T23:29:00',
      ],
      [
        'value' => '2015-10-22T00:29:00',
      ],
    ],
    3 => [
      [
        'value' => '2018-02-09T00:00:00',
      ],
      [
        'value' => '2018-02-10T22:00:00',
      ],
    ],
  ];
  foreach ($expected_values_per_node as $node_id => $expected_values) {
    $this
      ->assertEquals($expected_values, $nodes[$node_id]->field_date
      ->getValue());
  }
}