You are here

public function EntityReferenceTest::testUpdatingMissingParentTerms in Feeds 8.3

Tests if terms get their parent on a second import.

If parent terms appear later in the feed, earlier imported terms won't get that parent. This test ensures that these terms get the parent after all on a second import.

File

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

Class

EntityReferenceTest
Tests for the entityreference target.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testUpdatingMissingParentTerms() {
  $vocabulary = $this
    ->installTaxonomyModuleWithVocabulary();

  // Create feed type for terms.
  $feed_type = $this
    ->createFeedType([
    'fetcher' => 'directory',
    'fetcher_configuration' => [
      'allowed_extensions' => 'csv',
    ],
    'parser' => 'csv',
    'processor' => 'entity:taxonomy_term',
    'processor_configuration' => [
      'authorize' => FALSE,
      'update_existing' => ProcessorInterface::UPDATE_EXISTING,
      'values' => [
        'vid' => 'tags',
      ],
    ],
    'custom_sources' => [
      'name' => [
        'label' => 'name',
        'value' => 'name',
        'machine_name' => 'name',
      ],
      'parent' => [
        'label' => 'parent',
        'value' => 'parent',
        'machine_name' => 'parent',
      ],
    ],
    'mappings' => [
      [
        'target' => 'name',
        'map' => [
          'value' => 'name',
        ],
        'unique' => [
          'value' => 1,
        ],
      ],
      [
        'target' => 'description',
        'map' => [
          'value' => 'name',
        ],
        'settings' => [
          [
            'format' => 'plain_text',
          ],
        ],
      ],
      [
        'target' => 'parent',
        'map' => [
          'target_id' => 'parent',
        ],
        'settings' => [
          'reference_by' => 'name',
          'autocreate' => 0,
        ],
      ],
    ],
  ]);

  // First import.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/terms-with-parent-later-in-file.csv',
  ]);
  $feed
    ->import();

  // Assert that all terms got imported.
  $terms = Term::loadMultiple();
  $expected_term_names = [
    1 => 'Belgium',
    2 => 'Europe',
    3 => 'Netherlands',
  ];
  foreach ($expected_term_names as $term_id => $expected_term_name) {

    // Check term name and description.
    $this
      ->assertEquals($expected_term_name, $terms[$term_id]->name->value);
    $this
      ->assertEquals($expected_term_name, $terms[$term_id]->description->value);
  }

  // Assert that "Belgium" did not get a parent assigned, but "Netherlands"
  // did, since the latter appeared later in the file.
  $this
    ->assertEquals([], $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadParents(1));
  $this
    ->assertEquals([
    2,
  ], array_keys($this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadParents(3)));

  // Second import. Now Belgium should have a parent term.
  $feed
    ->import();
  $this
    ->assertEquals([
    2,
  ], array_keys($this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadParents(1)));

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

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

  // Ensure that the terms were not updated.
  for ($i = 1; $i <= 3; $i++) {
    $term = $this
      ->reloadEntity($terms[$i]);
    $this
      ->assertEquals('Description of term ' . $i, $term->description->value);
  }
}