You are here

public function FieldTest::testClearOutValues in Feeds 8.3

Tests if values are cleared out when an empty value is provided.

File

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

Class

FieldTest
Tests for mapping to text and numeric fields.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testClearOutValues() {
  $this
    ->setUpFieldDisplay();

  // Add mapping to GUID and set that column as unique.
  $this->feedType
    ->addCustomSource('guid', [
    'label' => 'GUID',
    'value' => 'guid',
  ]);
  $this->feedType
    ->addMapping([
    'target' => 'feeds_item',
    'map' => [
      'guid' => 'guid',
    ],
    'unique' => [
      'guid' => TRUE,
    ],
  ]);
  $this->feedType
    ->save();

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

  // Check the two imported nodes.
  $expected_values_per_node = [
    1 => [
      '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',
    ],
    2 => [
      'body' => 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.',
      'field_alpha' => 'Ut wisi',
      'field_beta' => '32',
      'field_gamma' => '1.20',
      'field_delta' => '5.62951',
    ],
  ];
  $this
    ->checkValues($expected_values_per_node);

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

  // Import CSV file with empty values.
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content_empty.csv',
  ]);
  $feed
    ->import();
  $this
    ->assertNodeCount(2);

  // Check if all values were cleared out for node 1 and
  // that for node 2 all values were set to '0'.
  $expected_values_per_node_empty = [
    1 => [
      'body' => '',
      'field_alpha' => '',
      'field_beta' => '',
      'field_gamma' => '',
      'field_delta' => '',
    ],
    2 => [
      'body' => 0,
      'field_alpha' => 0,
      'field_beta' => 0,
      'field_gamma' => 0,
      'field_delta' => 0,
    ],
  ];
  $this
    ->checkValues($expected_values_per_node_empty);
  $field_labels = [
    'field_alpha label',
    'field_beta label',
    'field_gamma label',
    'field_delta label',
  ];

  // Check for node 1 if labels are no longer shown.
  $rendered_content = $this
    ->renderNode(Node::load(1));
  foreach ($field_labels as $label) {
    $this
      ->assertStringNotContainsString($label, $rendered_content);
  }

  // Check for node 2 if labels are still shown.
  $rendered_content = $this
    ->renderNode(Node::load(2));
  foreach ($field_labels as $label) {
    $this
      ->assertStringContainsString($label, $rendered_content);
  }

  // Re-import the first file again.
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content.csv',
  ]);
  $feed
    ->import();

  // Check if the two imported nodes have content again.
  $this
    ->checkValues($expected_values_per_node);

  // Import CSV file with non-existent values.
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content_non_existent.csv',
  ]);
  $feed
    ->import();

  // Check if all values were cleared out for node 1.
  $expected_values_per_node_non_existent = [
    1 => [
      'body' => '',
      'field_alpha' => '',
      'field_beta' => '',
      'field_gamma' => '',
      'field_delta' => '',
    ],
  ];
  $this
    ->checkValues($expected_values_per_node_non_existent);

  // Check if labels for fields that should be cleared out are not shown.
  $rendered_content = $this
    ->renderNode(Node::load(1));
  foreach ($field_labels as $label) {
    $this
      ->assertStringNotContainsString($label, $rendered_content);
  }
}