You are here

public function FieldTest::testTargetUnique in Feeds 8.3

Tests if text and numeric fields can be used as unique target.

@dataProvider dataProviderTargetUnique

Parameters

string $field: The name of the field to set as unique.

string $subfield: The subfield of the field.

int $delta: The index of the target in the mapping configuration.

array $values: (optional) The list of initial values the node to create should get.

1 call to FieldTest::testTargetUnique()
FieldTest::testListIntegerTargetUnique in tests/src/Kernel/Feeds/Target/FieldTest.php
Tests if list integer fields can be used as unique target.

File

tests/src/Kernel/Feeds/Target/FieldTest.php, line 290

Class

FieldTest
Tests for mapping to text and numeric fields.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testTargetUnique($field, $subfield, $delta, array $values = []) {
  $expected_values = [
    'body' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.',
    'field_alpha' => 'Lorem',
    'field_beta' => '42',
    'field_gamma' => '4.20',
    'field_delta' => '3.14159',
  ];

  // Set mapper as unique.
  $mappings = $this->feedType
    ->getMappings();
  $mappings[$delta]['unique'] = [
    $subfield => TRUE,
  ];
  $this->feedType
    ->setMappings($mappings);

  // Configure feed type to update existing values.
  $this->feedType
    ->getProcessor()
    ->setConfiguration([
    'update_existing' => ProcessorInterface::UPDATE_EXISTING,
  ] + $this->feedType
    ->getProcessor()
    ->getConfiguration());

  // And save feed type.
  $this->feedType
    ->save();

  // Create an entity to update.
  $values += [
    'title' => $this
      ->randomMachineName(8),
    'type' => 'article',
    'uid' => 0,
    $field => isset($expected_values[$field]) ? $expected_values[$field] : NULL,
  ];
  $node = Node::create($values);
  $node
    ->save();

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

  // Check if the first node has the expected values.
  $node = $this
    ->reloadEntity($node);
  foreach ($expected_values as $field_name => $value) {
    $this
      ->assertEquals($value, $node->{$field_name}->value);
  }
}