You are here

public function UpdateNonExistentTest::testMultithreadImport in Feeds 8.3

Tests if the right items get cleaned with running multithreaded imports.

File

tests/src/Functional/UpdateNonExistentTest.php, line 268

Class

UpdateNonExistentTest
Tests the feature of updating items that are no longer available in the feed.

Namespace

Drupal\Tests\feeds\Functional

Code

public function testMultithreadImport() {

  // Set 'update_non_existent' setting to 'delete'.
  $config = $this->feedType
    ->getProcessor()
    ->getConfiguration();
  $config['update_non_existent'] = ProcessorInterface::DELETE_NON_EXISTENT;
  $this->feedType
    ->getProcessor()
    ->setConfiguration($config);

  // Set the import period to run as often as possible.
  $this->feedType
    ->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
  $this->feedType
    ->save();

  // Create a feed and import first file.
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/rss/googlenewstz.rss2',
  ]);

  // Run cron to import.
  $this
    ->cronRun();

  // Assert that 6 nodes have been created.
  $feed = $this
    ->reloadFeed($feed);
  static::assertEquals(6, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(6);

  // Import an "updated" version of the file from which one item is removed.
  $feed
    ->setSource($this
    ->resourcesPath() . '/rss/googlenewstz_missing.rss2');
  $feed
    ->save();
  $feed
    ->startCronImport();

  // Create queue.
  $queue_name = 'feeds_feed_refresh:' . $this->feedType
    ->id();
  $queue = $this->container
    ->get('queue')
    ->get($queue_name);
  $queue
    ->createQueue();
  $queue_worker = $this->container
    ->get('plugin.manager.queue_worker')
    ->createInstance($queue_name);

  // Process first three queue items. The first item is expected to be the
  // "fetch" item, the second a "parse" item.
  for ($i = 0; $i < 3; $i++) {
    $item = $queue
      ->claimItem();
    $queue_worker
      ->processItem($item->data);
    $queue
      ->deleteItem($item);
  }

  // Listen to process event.
  $this->container
    ->get('event_dispatcher')
    ->addListener(FeedsEvents::PROCESS, [
    $this,
    'onProcess',
  ], FeedsEvents::AFTER);

  // Process another item.
  $item = $queue
    ->claimItem();
  $queue_worker
    ->processItem($item->data);
  $queue
    ->deleteItem($item);

  // Process remaining items.
  while ($item = $queue
    ->claimItem()) {
    $queue_worker
      ->processItem($item->data);
    $queue
      ->deleteItem($item);
  }

  // Assert that only one node is removed.
  $feed = $this
    ->reloadFeed($feed);
  static::assertEquals(5, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(5);
}