View source  
  <?php
namespace Drupal\Tests\feeds\Kernel;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
class EntityIdTest extends FeedsKernelTestBase {
  
  public function testInsertNodeId() {
    $feed_type = $this
      ->createFeedTypeForCsv([
      'title' => 'title',
      'beta' => 'beta',
    ], [
      'mappings' => [
        [
          'target' => 'title',
          'map' => [
            'value' => 'title',
          ],
        ],
        [
          'target' => 'nid',
          'map' => [
            'value' => 'beta',
          ],
        ],
      ],
    ]);
    
    $feed = $this
      ->createFeed($feed_type
      ->id(), [
      'source' => $this
        ->resourcesPath() . '/csv/content.csv',
    ]);
    $feed
      ->import();
    $this
      ->assertNodeCount(2);
    
    $node = Node::load(42);
    $this
      ->assertEquals('Lorem ipsum', $node->title->value);
    $node = Node::load(32);
    $this
      ->assertEquals('Ut wisi enim ad minim veniam', $node->title->value);
    
    $feed
      ->import();
    
    $messages = \Drupal::messenger()
      ->all();
    foreach ($messages['warning'] as $warning) {
      $this
        ->assertStringNotContainsString('SQLSTATE', $warning);
    }
  }
  
  public function testUpdateByNodeId() {
    $feed_type = $this
      ->createFeedTypeForCsv([
      'title' => 'title',
      'beta' => 'beta',
    ], [
      'processor_configuration' => [
        'update_existing' => ProcessorInterface::UPDATE_EXISTING,
        'values' => [
          'type' => 'article',
        ],
      ],
      'mappings' => [
        [
          'target' => 'title',
          'map' => [
            'value' => 'title',
          ],
        ],
        [
          'target' => 'nid',
          'map' => [
            'value' => 'beta',
          ],
          'unique' => [
            'value' => TRUE,
          ],
        ],
      ],
    ]);
    
    $node = Node::create([
      'nid' => 42,
      'title' => 'Foo',
      'type' => 'article',
    ]);
    $node
      ->save();
    
    $feed = $this
      ->createFeed($feed_type
      ->id(), [
      'source' => $this
        ->resourcesPath() . '/csv/content.csv',
    ]);
    $feed
      ->import();
    $this
      ->assertNodeCount(2);
    
    $node = $this
      ->reloadEntity($node);
    $this
      ->assertEquals('Lorem ipsum', $node->title->value);
  }
  
  public function testNoNodeIdChange() {
    
    $node1 = Node::create([
      'title' => 'Lorem ipsum',
      'type' => 'article',
    ]);
    $node1
      ->save();
    
    $node32 = Node::create([
      'nid' => 32,
      'title' => 'Foo',
      'type' => 'article',
    ]);
    $node32
      ->save();
    
    $feed_type = $this
      ->createFeedTypeForCsv([
      'title' => 'title',
      'beta' => 'beta',
    ], [
      'processor_configuration' => [
        'update_existing' => ProcessorInterface::UPDATE_EXISTING,
        'values' => [
          'type' => 'article',
        ],
      ],
      'mappings' => [
        [
          'target' => 'title',
          'map' => [
            'value' => 'title',
          ],
          'unique' => [
            'value' => TRUE,
          ],
        ],
        [
          'target' => 'nid',
          'map' => [
            'value' => 'beta',
          ],
        ],
      ],
    ]);
    
    $feed = $this
      ->createFeed($feed_type
      ->id(), [
      'source' => $this
        ->resourcesPath() . '/csv/content.csv',
    ]);
    $feed
      ->import();
    $this
      ->assertNodeCount(2);
    
    $this
      ->assertNull(Node::load(42));
    
    $node32 = $this
      ->reloadEntity($node32);
    $this
      ->assertEquals('Foo', $node32->title->value);
    
    $messages = \Drupal::messenger()
      ->all();
    foreach ($messages['warning'] as $warning) {
      $this
        ->assertStringNotContainsString('SQLSTATE', $warning);
    }
  }
}