You are here

public function MultiMappingTest::testImportTwoValues in Feeds 8.3

Tests importing two values to the same target.

File

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

Class

MultiMappingTest
Tests mapping multiple times to the same target.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testImportTwoValues() {

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

  // Create a feed type, map two sources to the same target.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'title' => 'title',
    'alpha' => 'alpha',
    'beta' => 'beta',
  ], [
    'mappings' => [
      [
        'target' => 'title',
        'map' => [
          'value' => 'title',
        ],
      ],
      [
        'target' => 'field_alpha',
        'map' => [
          'value' => 'alpha',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
      [
        'target' => 'field_alpha',
        'map' => [
          'value' => 'beta',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
    ],
  ]);

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

  // Check the values imported into field_alpha.
  $nodes = Node::loadMultiple();
  $expected_values_per_node = [
    1 => [
      [
        'value' => 'Lorem',
        'format' => 'plain_text',
      ],
      [
        'value' => '42',
        'format' => 'plain_text',
      ],
    ],
    2 => [
      [
        'value' => 'Ut wisi',
        'format' => 'plain_text',
      ],
      [
        'value' => '32',
        'format' => 'plain_text',
      ],
    ],
  ];
  foreach ($expected_values_per_node as $node_id => $expected_values) {
    $this
      ->assertEquals($expected_values, $nodes[$node_id]->field_alpha
      ->getValue());
  }
}