You are here

public function EntityReferenceTest::testUpdatingMissingReferences in Feeds 8.3

Tests if items are updated that previously referenced a missing item.

When importing a feed that references items that are imported by an other feed later, the referenced items do not exist yet. In this case these items should be updated on a second import, when the referenced items may exist by then.

In this test, feed types for two content types are created: one for the article content type and one for the page content type. The content type 'page' has a field called 'field_article' that references article nodes. Content for the 'page' content type is imported first, which means that the articles that the source references, do not exist yet. Articles are imported next. Finally, the source for the 'page' content type is imported again to ensure that references to the article nodes do get imported after all, even though the source did not change.

Feeds usually skips importing a source item if it did not change since the previous import, but in case of previously missing references, it should do not.

File

tests/src/Kernel/Feeds/Target/EntityReferenceTest.php, line 43

Class

EntityReferenceTest
Tests for the entityreference target.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testUpdatingMissingReferences() {

  // Create a content type.
  $type = NodeType::create([
    'type' => 'page',
    'name' => 'Page',
  ]);
  $type
    ->save();

  // Add an entityreference field to this content type.
  $this
    ->createEntityReferenceField('node', 'page', 'field_article', 'Article', 'node', 'default', [
    'target_bundles' => [
      'article',
    ],
  ]);

  // Create feed type for the article content type.
  $this
    ->createFeedType([
    'id' => 'article_feed_type',
    'label' => 'Article importer',
    'fetcher' => 'directory',
    'fetcher_configuration' => [
      'allowed_extensions' => 'csv',
    ],
    'parser' => 'csv',
    'processor_configuration' => [
      'authorize' => FALSE,
      'update_existing' => ProcessorInterface::UPDATE_EXISTING,
      'values' => [
        'type' => 'article',
      ],
    ],
    'custom_sources' => [
      'guid' => [
        'label' => 'guid',
        'value' => 'guid',
        'machine_name' => 'guid',
      ],
      'title' => [
        'label' => 'title',
        'value' => 'title',
        'machine_name' => 'title',
      ],
    ],
  ]);

  // Create feed type for the 'page' content type, with a mapping to the
  // entityreference field 'field_article'.
  $this
    ->createFeedType([
    'id' => 'page_feed_type',
    'label' => 'Page importer',
    'fetcher' => 'directory',
    'fetcher_configuration' => [
      'allowed_extensions' => 'csv',
    ],
    'parser' => 'csv',
    'processor_configuration' => [
      'authorize' => FALSE,
      'update_existing' => ProcessorInterface::UPDATE_EXISTING,
      'values' => [
        'type' => 'page',
      ],
    ],
    'custom_sources' => [
      'guid' => [
        'label' => 'guid',
        'value' => 'guid',
        'machine_name' => 'guid',
      ],
      'title' => [
        'label' => 'title',
        'value' => 'title',
        'machine_name' => 'title',
      ],
      'article' => [
        'label' => 'article',
        'value' => 'article',
        'machine_name' => 'article',
      ],
    ],
    'mappings' => array_merge($this
      ->getDefaultMappings(), [
      [
        'target' => 'field_article',
        'map' => [
          'target_id' => 'article',
        ],
        'settings' => [
          'reference_by' => 'feeds_item',
          'feeds_item' => 'guid',
          'autocreate' => 0,
        ],
      ],
    ]),
  ]);

  // Import pages.
  $feed = $this
    ->createFeed('page_feed_type', [
    'source' => $this
      ->resourcesPath() . '/csv/content-with-reference.csv',
  ]);
  $feed
    ->import();

  // Assert two created nodes.
  $this
    ->assertNodeCount(2);
  $node = Node::load(1);

  // Assert that field_article is empty at the moment.
  $this
    ->assertEquals([], $node->field_article
    ->getValue());

  // Import second feed.
  $feed2 = $this
    ->createFeed('article_feed_type', [
    'source' => $this
      ->resourcesPath() . '/csv/content.csv',
  ]);
  $feed2
    ->import();
  $this
    ->assertNodeCount(4);

  // And re-import first feed.
  $feed
    ->import();

  // Reload node.
  $node = $this
    ->reloadEntity($node);
  $this
    ->assertEquals(4, $node->field_article->target_id);

  // Check node 2 too.
  $node2 = Node::load(2);
  $this
    ->assertEquals(3, $node2->field_article->target_id);

  // Ensure that the nodes aren't updated again. Change the titles of all page
  // nodes, so we can check that these won't be updated by Feeds.
  $node->title->value = 'Page 1';
  $node
    ->save();
  $node2->title->value = 'Page 2';
  $node2
    ->save();

  // And re-import first feed again.
  $feed
    ->import();

  // Ensure that the nodes were not updated.
  $node = $this
    ->reloadEntity($node);
  $this
    ->assertEquals('Page 1', $node->title->value);
  $node2 = $this
    ->reloadEntity($node2);
  $this
    ->assertEquals('Page 2', $node2->title->value);
}