You are here

public function FeedsSchedulerTestCase::testScheduling in Feeds 7.2

Same name and namespace in other branches
  1. 6 tests/feeds_scheduler.test \FeedsSchedulerTestCase::testScheduling()
  2. 7 tests/feeds_scheduler.test \FeedsSchedulerTestCase::testScheduling()

Test scheduling of enabled import on cron.

File

tests/feeds_scheduler.test, line 49
Feeds tests.

Class

FeedsSchedulerTestCase
Test cron scheduling.

Code

public function testScheduling() {

  // Initialize scheduling.
  $init = $this
    ->initSyndication();
  $time = $init['time'];
  $nids = $init['nids'];
  $count = $init['count'];
  $this
    ->cronRun();
  $this
    ->cronRun();

  // There should be feeds_schedule_num X 2 (= 20) feeds updated now.
  $schedule = array();
  $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(
    ':time' => $time,
  ));
  foreach ($rows as $row) {
    $schedule[$row->id] = $row;
  }
  $this
    ->assertEqual(count($schedule), 20, format_string('20 feeds refreshed on cron (actual: @actual).', array(
    '@actual' => count($schedule),
  )));

  // There should be 200 article nodes in the database.
  $count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article' AND status = 1")
    ->fetchField();
  $this
    ->assertEqual($count, 200, format_string('There are 200 article nodes aggregated (actual: @actual).', array(
    '@actual' => $count,
  )));

  // There shouldn't be any items with scheduled = 1 now, if so, this would
  // mean they are stuck.
  $count = db_query("SELECT COUNT(*) FROM {job_schedule} WHERE scheduled = 1")
    ->fetchField();
  $this
    ->assertEqual($count, 0, format_string('All items are unscheduled (schedule flag = 0) (actual: @actual).', array(
    '@actual' => $count,
  )));

  // Hit cron again twice.
  $this
    ->cronRun();
  $this
    ->cronRun();

  // The import_period setting of the feed configuration is 1800, there
  // shouldn't be any change to the database now.
  $equal = TRUE;
  $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(
    ':time' => $time,
  ));
  foreach ($rows as $row) {
    $equal = $equal && $row->last == $schedule[$row->id]->last;
  }
  $this
    ->assertTrue($equal, 'Schedule did not change.');

  // Log back in and set refreshing to as often as possible.
  $this
    ->drupalLogin($this->admin_user);
  $edit = array(
    'import_period' => 0,
  );
  $this
    ->drupalPost('admin/structure/feeds/syndication/settings', $edit, 'Save');
  $this
    ->assertText('Periodic import: as often as possible');
  $this
    ->drupalLogout();

  // Hit cron once, this should cause Feeds to reschedule all entries.
  $this
    ->cronRun();
  $equal = FALSE;
  $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(
    ':time' => $time,
  ));
  foreach ($rows as $row) {
    $equal = $equal && $row->last == $schedule[$row->id]->last;
    $schedule[$row->id] = $row;
  }
  $this
    ->assertFalse($equal, 'Every feed schedule time changed.');

  // Hit cron again, 4 times now, every item should change again.
  for ($i = 0; $i < 4; $i++) {
    $this
      ->cronRun();
  }
  $equal = FALSE;
  $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(
    ':time' => $time,
  ));
  foreach ($rows as $row) {
    $equal = $equal && $row->last == $schedule[$row->id]->last;
  }
  $this
    ->assertFalse($equal, 'Every feed schedule time changed.');

  // There should be 200 article nodes in the database.
  $count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article' AND status = 1")
    ->fetchField();
  $this
    ->assertEqual($count, 200, 'The total of 200 article nodes has not changed.');

  // Set expire settings, check rescheduling.
  $max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")
    ->fetchField();
  $min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")
    ->fetchField();
  $this
    ->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")
    ->fetchField());
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->setSettings('syndication', 'FeedsNodeProcessor', array(
    'expire' => 86400,
  ));
  $this
    ->drupalLogout();
  sleep(1);
  $this
    ->cronRun();

  // There should be 20 feeds_source_expire jobs now, and all last fields should be reset.
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND last <> 0 AND scheduled = 0 AND period = 3600")
    ->fetchField());
  $new_max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")
    ->fetchField();
  $new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")
    ->fetchField();
  $this
    ->assertNotEqual($new_max_last, $max_last);
  $this
    ->assertNotEqual($new_min_last, $min_last);
  $this
    ->assertEqual($new_max_last, $new_min_last);
  $max_last = $new_max_last;
  $min_last = $new_min_last;

  // Set import settings, check rescheduling.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->setSettings('syndication', '', array(
    'import_period' => 3600,
  ));
  $this
    ->drupalLogout();
  sleep(1);
  $this
    ->cronRun();
  $new_max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 3600")
    ->fetchField();
  $new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 3600")
    ->fetchField();
  $this
    ->assertNotEqual($new_max_last, $max_last);
  $this
    ->assertNotEqual($new_min_last, $min_last);
  $this
    ->assertEqual($new_max_last, $new_min_last);
  $this
    ->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period <> 3600")
    ->fetchField());
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND period = 3600 AND last = :last", array(
    ':last' => $new_min_last,
  ))
    ->fetchField());

  // Delete source, delete importer, check schedule.
  $this
    ->drupalLogin($this->admin_user);
  $nid = array_shift($nids);
  $this
    ->drupalPost("node/{$nid}/delete", array(), t('Delete'));
  $this
    ->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND id = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField());
  $this
    ->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND id = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField());
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")
    ->fetchField());
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")
    ->fetchField());
  $this
    ->drupalPost('admin/structure/feeds/syndication/delete', array(), t('Delete'));
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")
    ->fetchField());
  $this
    ->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")
    ->fetchField());
}