You are here

public function ConfigEntityReferenceTest::testImportByUuid in Feeds 8.3

Tests importing config entity references by UUID.

File

tests/src/Kernel/Feeds/Target/ConfigEntityReferenceTest.php, line 137

Class

ConfigEntityReferenceTest
Tests for the config entity reference target.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testImportByUuid() {

  // Because it's unpredictable which uuids a config entity gets, let's add an
  // event subscriber that sets these values for the 'type' column.
  $this->container
    ->get('event_dispatcher')
    ->addListener(FeedsEvents::PARSE, function (ParseEvent $event) {

    // Set UUID on items.
    $counter = 0;
    $config_entities = [
      'test',
      'test2',
    ];
    foreach ($event
      ->getParserResult() as $item) {
      $uuid = $this->entityTypeManager
        ->getStorage('entity_test_bundle')
        ->load($config_entities[$counter])
        ->uuid();
      $item
        ->set('type', $uuid);
      $counter++;
    }
  }, FeedsEvents::AFTER);

  // Create a feed type, map to created field.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'guid' => 'guid',
    'title' => 'title',
    'type' => 'type',
  ], [
    'mappings' => array_merge($this
      ->getDefaultMappings(), [
      [
        'target' => 'field_entity_test_type',
        'map' => [
          'target_id' => 'type',
        ],
        'settings' => [
          'reference_by' => 'uuid',
        ],
      ],
    ]),
  ]);

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

  // Assert two created nodes.
  $this
    ->assertNodeCount(2);

  // Test target id values of these nodes.
  $expected_values = [
    1 => 'test',
    2 => 'test2',
  ];
  foreach ($expected_values as $nid => $expected_value) {
    $node = Node::load($nid);
    $this
      ->assertEquals($expected_value, $node->field_entity_test_type->target_id);
  }
}