You are here

public function EntityReferenceTest::testUpdatingMissingNodeAuthors in Feeds 8.3

Tests if articles get an author later.

If articles are imported before their authors, the articles won't have an author yet on the first import. When the articles get imported again after the authors are imported, the articles should get an author after all.

File

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

Class

EntityReferenceTest
Tests for the entityreference target.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testUpdatingMissingNodeAuthors() {

  // Create feed type for importing articles.
  $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',
      ],
      'author' => [
        'label' => 'author',
        'value' => 'author',
        'machine_name' => 'author',
      ],
    ],
    'mappings' => array_merge($this
      ->getDefaultMappings(), [
      [
        'target' => 'uid',
        'map' => [
          'target_id' => 'author',
        ],
        'settings' => [
          'reference_by' => 'name',
          'autocreate' => 0,
        ],
      ],
    ]),
  ]);

  // Create feed type for users.
  $this
    ->createFeedType([
    'id' => 'authors',
    'label' => 'Authors',
    'fetcher' => 'directory',
    'fetcher_configuration' => [
      'allowed_extensions' => 'csv',
    ],
    'parser' => 'csv',
    'processor' => 'entity:user',
    'processor_configuration' => [
      'authorize' => FALSE,
      'values' => [],
    ],
    'custom_sources' => [
      'name' => [
        'label' => 'name',
        'value' => 'name',
        'machine_name' => 'name',
      ],
      'mail' => [
        'label' => 'mail',
        'value' => 'mail',
        'machine_name' => 'mail',
      ],
      'status' => [
        'label' => 'status',
        'value' => 'status',
        'machine_name' => 'status',
      ],
    ],
    'mappings' => [
      [
        'target' => 'name',
        'map' => [
          'value' => 'name',
        ],
        'unique' => [
          'value' => 1,
        ],
      ],
      [
        'target' => 'mail',
        'map' => [
          'value' => 'mail',
        ],
      ],
      [
        'target' => 'status',
        'map' => [
          'value' => 'status',
        ],
      ],
    ],
  ]);

  // Import articles.
  $article_feed = $this
    ->createFeed('article_feed_type', [
    'source' => $this
      ->resourcesPath() . '/csv/content-with-author.csv',
  ]);
  $article_feed
    ->import();

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

  // Assert that the first node doesn't currently have an author.
  $this
    ->assertEquals(0, $node->uid->target_id);

  // Import authors.
  $author_feed = $this
    ->createFeed('authors', [
    'source' => $this
      ->resourcesPath() . '/csv/users.csv',
  ]);
  $author_feed
    ->import();

  // And re-import first feed. Previously imported articles now should get an
  // author.
  $article_feed
    ->import();

  // Reload node 1 and check if it got an author now.
  $nodes[1] = $this
    ->reloadEntity($node);
  $this
    ->assertEquals(1, $nodes[1]->uid->target_id);

  // Check nodes 2 and 3 too.
  $nodes[2] = Node::load(2);
  $this
    ->assertEquals(1, $nodes[2]->uid->target_id);
  $nodes[3] = Node::load(3);
  $this
    ->assertEquals(2, $nodes[3]->uid->target_id);

  // Ensure that the nodes aren't updated again. Change the titles of all
  // articles, so we can check that these won't be updated by Feeds.
  for ($i = 1; $i <= 3; $i++) {
    $nodes[$i]->title->value = 'Article ' . $i;
    $nodes[$i]
      ->save();
  }

  // And re-import first feed again. No nodes should get updated.
  $article_feed
    ->import();

  // Ensure that the nodes were not updated.
  for ($i = 1; $i <= 3; $i++) {
    $node = $this
      ->reloadEntity($nodes[$i]);
    $this
      ->assertEquals('Article ' . $i, $node->title->value);
  }
}