You are here

function FeedsSchedulerTest::testBatching in Feeds 8.2

Test batching on cron.

@todo Figure out why cron needs to be run once before.

File

lib/Drupal/feeds/Tests/FeedsSchedulerTest.php, line 203
Feeds tests.

Class

FeedsSchedulerTest
Test cron scheduling.

Namespace

Drupal\feeds\Tests

Code

function testBatching() {
  $this
    ->cronRun();

  // Set up an importer.
  $this
    ->createImporterConfiguration('Node import', 'node');

  // Set and configure plugins and mappings.
  $this
    ->setSettings('node', '', array(
    'content_type' => '',
  ));
  $this
    ->setPlugin('node', 'file');
  $this
    ->setPlugin('node', 'csv');
  $this
    ->addMappings('node', array(
    0 => array(
      'source' => 'title',
      'target' => 'title',
    ),
  ));

  // Verify that there are 86 nodes total.
  $this
    ->importFile('node', $this
    ->absolutePath() . '/tests/feeds/many_nodes.csv');
  $this
    ->assertText('Created 86 nodes');

  // Run batch twice with two different process limits.
  // 50 = FEEDS_PROCESS_LIMIT.
  foreach (array(
    10,
    50,
  ) as $limit) {
    variable_set('feeds_process_limit', $limit);
    db_query("UPDATE {job_schedule} SET next = 0");
    $this
      ->drupalPost('import/node/delete-items', array(), 'Delete');
    $this
      ->assertEqual(0, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")
      ->fetchField());

    // Hit cron (item count / limit) times, assert correct number of articles.
    for ($i = 0; $i < ceil(86 / $limit); $i++) {
      $this
        ->cronRun();
      sleep(1);
      if ($limit * ($i + 1) < 86) {
        $count = $limit * ($i + 1);
        $period = 0;

        // Import should be rescheduled for ASAP.
      }
      else {
        $count = 86;

        // We've reached our total of 86.
        $period = 1800;

        // Hence we should find the Source's default period.
      }
      $this
        ->assertEqual($count, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")
        ->fetchField());
      $this
        ->assertEqual($period, db_query("SELECT period FROM {job_schedule} WHERE type = 'node' AND id = 0")
        ->fetchField());
    }
  }

  // Delete a couple of nodes, then hit cron again. They should not be replaced
  // as the minimum update time is 30 minutes.
  $nodes = db_query_range("SELECT nid FROM {node} WHERE type = 'article'", 0, 2);
  foreach ($nodes as $node) {
    $this
      ->drupalPost("node/{$node->nid}/delete", array(), 'Delete');
  }
  $this
    ->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")
    ->fetchField());
  $this
    ->cronRun();
  $this
    ->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")
    ->fetchField());
}