You are here

public function SkipNewTest::testSkipNewAndUpdateExisting in Feeds 8.3

Tests skip new items with update existing.

File

tests/src/Kernel/SkipNewTest.php, line 167

Class

SkipNewTest
Tests the feature of creating/skipping new items.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testSkipNewAndUpdateExisting() {

  // Configure that new items should not be imported and that existing items
  // may be updated.
  $feed_type = $this
    ->createFeedTypeForThisTest([
    'insert_new' => ProcessorInterface::SKIP_NEW,
    'update_existing' => ProcessorInterface::UPDATE_EXISTING,
  ]);

  // Create two nodes whose title is in the feed.
  $node1 = Node::create([
    'type' => 'article',
    'title' => 'Dries Buytaert: Eén using Drupal',
    'body' => 'Foo',
  ]);
  $node1
    ->save();
  $node2 = Node::create([
    'type' => 'article',
    'title' => 'NodeOne: The new Feeds module',
    'body' => 'Feeds exists for more than a decade now.',
  ]);
  $node2
    ->save();

  // Create a feed and import.
  // No nodes should be created nor updated.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/rss/drupalplanet.rss2',
  ]);
  $feed
    ->import();

  // Two nodes should be updated, but no items should get created.
  $this
    ->assertEquals(2, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(2);
  $this
    ->assertEquals(0, $this->processState->created);
  $this
    ->assertEquals(2, $this->processState->updated);

  // Assert that the existing nodes changed.
  $node1 = $this
    ->reloadEntity($node1);
  $this
    ->assertStringContainsString('a public TV station reaching millions of people in Belgium', $node1->body->value);
  $this
    ->assertNotEmpty($node1->feeds_item->imported);
  $node2 = $this
    ->reloadEntity($node2);
  $this
    ->assertStringContainsString('FeedAPI has for long been the mainstream solution for this kind of problems.', $node2->body->value);
  $this
    ->assertNotEmpty($node2->feeds_item->imported);

  // Change "insert_new" setting to insert new items to verify if changing the
  // setting later has the effect that new items will be imported as yet.
  $config = $feed_type
    ->getProcessor()
    ->getConfiguration();
  $config['insert_new'] = ProcessorInterface::INSERT_NEW;
  $feed_type
    ->getProcessor()
    ->setConfiguration($config);
  $feed_type
    ->save();

  // Import. 23 nodes should get created. No nodes should be updated, because
  // these already got updated during the previous import.
  $feed = $this
    ->reloadEntity($feed);
  $feed
    ->import();
  $this
    ->assertEquals(25, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(25);
  $this
    ->assertEquals(23, $this->processState->created);
  $this
    ->assertEquals(0, $this->processState->updated);
  $this
    ->assertEquals(2, $this->processState->skipped);
}