You are here

public function UpdateExistingTest::testUpdateNonTranslatableEntity in Feeds 8.3

Tests that non-translatable entities can get updated.

File

tests/src/Kernel/UpdateExistingTest.php, line 119

Class

UpdateExistingTest
Tests the feature of updating items.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testUpdateNonTranslatableEntity() {
  $this
    ->installConfig([
    'field',
    'filter',
  ]);
  $this
    ->installEntitySchema('entity_test_bundle');
  $this
    ->installEntitySchema('entity_test_string_id');
  $this
    ->installEntitySchema('user');

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $entity_storage */
  $entity_storage = $this->entityTypeManager
    ->getStorage('entity_test_string_id');

  // Create a user.
  $this
    ->createUser();

  // Add a field to the entity type.
  $this
    ->createFieldWithStorage('field_alpha', [
    'entity_type' => 'entity_test_string_id',
    'bundle' => 'entity_test_string_id',
  ]);

  // Create an entity to update.
  $entity = $entity_storage
    ->create([
    'id' => 'LRM',
    'name' => 'Lorem ipsum',
    'type' => 'entity_test_string_id',
  ]);
  $entity
    ->save();

  // Create a feed type to update the entity.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'title' => 'title',
    'alpha' => 'alpha',
  ], [
    'processor' => 'entity:entity_test_string_id',
    'processor_configuration' => [
      'update_existing' => ProcessorInterface::UPDATE_EXISTING,
      'values' => [
        'type' => 'entity_test_string_id',
      ],
    ],
    'mappings' => [
      [
        'target' => 'name',
        'map' => [
          'value' => 'title',
        ],
        'unique' => [
          'value' => TRUE,
        ],
      ],
      [
        'target' => 'field_alpha',
        'map' => [
          'value' => 'alpha',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
    ],
  ]);

  // Import.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content.csv',
  ]);
  $feed
    ->import();

  // Assert that the entity was updated.
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals('Lorem', $entity->field_alpha->value);
}