Newer
Older
<?php
// $Id$
/**
* @file
* Feeds tests.
*/
// Require FeedsWebTestCase class definition.
require_once(dirname(__FILE__) .'/feeds.test.inc');
/**
* Test cron scheduling.
*/
class FeedsSchedulerTestCase extends FeedsWebTestCase {
/**
* Describe this test.
*/
public function getInfo() {
return array(
'name' => t('Scheduler'),
'description' => t('Tests for feeds scheduler.'),
'group' => t('Feeds'),
);
}
/**
* Set up test.
*/
public function setUp() {
Alex Barth
committed
parent::setUp('feeds', 'feeds_ui', 'ctools', 'job_scheduler');
$this->loginAdmin();
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$this->createImporterConfiguration();
$this->addMappings('syndication',
array(
array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
)
);
}
/**
* Test scheduling on cron.
*/
public function testScheduling() {
// Create 10 feed nodes. Turn off import on create before doing that.
$edit = array(
'import_on_create' => FALSE,
);
$this->drupalPost('admin/structure/feeds/edit/syndication/settings', $edit, 'Save');
$this->assertText('Do not import on create');
$nids = $this->createFeedNodes();
// This implicitly tests the import_on_create node setting being 0.
$this->assertTrue($nids[0] == 1 && $nids[1] == 2, 'Node ids sequential.');
Alex Barth
committed
// Check whether feed got properly added to scheduler.
foreach ($nids as $nid) {
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND id = :nid AND callback = 'feeds_source_import' AND last <> 0 AND scheduled = 0 AND period = 1800 AND periodic = 1", array(':nid' => $nid))->fetchField());
Alex Barth
committed
}
// Take time for comparisons.
$time = time();
sleep(1);
// Log out and run cron, no changes.
$this->drupalLogout();
Alex Barth
committed
$this->runCron();
$count = db_query("SELECT COUNT(*) FROM {job_schedule} WHERE last > :time", array(':time' => $time))->fetchField();
Alex Barth
committed
$this->assertEqual($count, 0, '0 feeds refreshed on cron.');
// Set next time to 0 to simulate updates.
// There should be 2 x job_schedule_num (= 10) feeds updated now.
db_query("UPDATE {job_schedule} SET next = 0");
$this->runCron();
$this->runCron();
$schedule = array();
$count = db_query("SELECT COUNT(*) FROM {job_schedule} WHERE last > :time", array(':time' => $time))->fetchField();
$this->assertEqual($count, 10, '10 feeds refreshed on cron.');
Alex Barth
committed
$result = db_query("SELECT * FROM {job_schedule}", $time);
// There should be 100 story nodes in the database.
$count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story'")->fetchField();
$this->assertEqual($count, 100, 'There are 100 story nodes aggregated.');
// Hit twice cron again.
Alex Barth
committed
$this->runCron();
$this->runCron();
// 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) {
Alex Barth
committed
$schedule[$row->id] = $row;
}
$this->assertEqual(count($schedule), 20, '20 feeds refreshed on cron.');
// There should be 200 story nodes in the database.
$count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story' AND status = 1")->fetchField();
$this->assertEqual($count, 200, 'There are 200 story nodes aggregated.');
// 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, 'All items are unscheduled (schedule flag = 0).');
// Hit cron again twice.
Alex Barth
committed
$this->runCron();
$this->runCron();
// 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) {
Alex Barth
committed
$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.
Alex Barth
committed
$this->loginAdmin();
$edit = array(
'import_period' => 0,
);
$this->drupalPost('admin/structure/feeds/edit/syndication/settings', $edit, 'Save');
$this->assertText('Refresh: as often as possible');
Alex Barth
committed
$this->drupalLogout();
Alex Barth
committed
// Hit cron once, this should cause Feeds to reschedule all entries.
$this->runCron();
$equal = FALSE;
$rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
foreach ($rows as $row) {
Alex Barth
committed
$equal = $equal && ($row->last == $schedule[$row->id]->last);
$schedule[$row->id] = $row;
Alex Barth
committed
$this->assertFalse($equal, 'Every feed schedule time changed.');
Alex Barth
committed
// Hit cron again, 4 times now, every item should change again.
for ($i = 0; $i < 4; $i++) {
$this->runCron();
}
$equal = FALSE;
$rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
foreach ($rows as $row) {
Alex Barth
committed
$equal = $equal && ($row->last == $schedule[$row->id]->last);
}
$this->assertFalse($equal, 'Every feed schedule time changed.');
// There should be 200 story nodes in the database.
$count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story' AND status = 1")->fetchField();
$this->assertEqual($count, 200, 'The total of 200 story nodes has not changed.');
Alex Barth
committed
// Set expire settings, check rescheduling.
$max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import' AND period = 0")->fetchField();
$min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import' AND period = 0")->fetchField();
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_importer_expire' AND last <> 0 AND scheduled = 0")->fetchField());
Alex Barth
committed
$this->loginAdmin();
$this->setSettings('syndication', 'FeedsNodeProcessor', array('expire' => 86400));
$this->drupalLogout();
sleep(1);
$this->runCron();
// There should be a feeds_importer_expire callback now, and all last fields should be reset.
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_importer_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 callback = 'feeds_source_import' AND period = 0")->fetchField();
$new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import' AND period = 0")->fetchField();
Alex Barth
committed
$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->loginAdmin();
$this->setSettings('syndication', '', array('import_period' => 3600));
$this->drupalLogout();
sleep(1);
$this->runCron();
$new_max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import' AND period = 3600")->fetchField();
$new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import' AND period = 3600")->fetchField();
Alex Barth
committed
$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 callback = 'feeds_source_import' AND period <> 3600")->fetchField());
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_importer_expire' AND period = 3600 AND last = :last", array(':last' => $new_min_last))->fetchField());
Alex Barth
committed
// Delete source, delete importer, check schedule.
$this->loginAdmin();
$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 callback = 'feeds_source_import' AND id = :nid", array(':nid' => $nid))->fetchField());
$this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import'")->fetchField());
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_importer_expire' AND id = 0")->fetchField());
Alex Barth
committed
$this->drupalPost('admin/structure/feeds/delete/syndication', array(), t('Delete'));
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_importer_expire' AND id = 0")->fetchField());
$this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND callback = 'feeds_source_import'")->fetchField());
}
/**
* Test batching on cron.
*/
function testBatching() {
// Verify that there are 150 nodes total.
$nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/many_items.rss2');
$this->assertText('Created 150 Story nodes.');
$this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
$this->assertText('Deleted 150 nodes.');
Alex Barth
committed
// Set next time to 0 to simulate updates.
db_query("UPDATE {job_schedule} SET next = 0");
// Hit cron 3 times, assert correct number of story nodes.
for ($i = 0; $i < 3; $i++) {
Alex Barth
committed
$this->runCron(1);
// 50 == FEEDS_NODE_BATCH_SIZE
$this->assertEqual(50 * ($i + 1), db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story'")->fetchField());
$this->assertEqual(0, db_query("SELECT period FROM {job_schedule} WHERE type = 'syndication' AND id = :nid", array(':nid' => $nid))->fetchField());
Alex Barth
committed
// Run one more time to cause the batch to reset, check period back to 1800.
$this->runCron();
$this->assertEqual(1800, db_query("SELECT period FROM {job_schedule} WHERE type = 'syndication' AND id = :nid", array(':nid' => $nid))->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 = 'story'", 0, 2);
foreach ($nodes as $node) {
$this->drupalPost("node/{$node->nid}/delete", array(), 'Delete');
}
$this->assertEqual(148, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story'")->fetchField());
Alex Barth
committed
$this->runCron();
$this->assertEqual(148, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'story'")->fetchField());
Alex Barth
committed
/**
* Helper, log in as an admin user.
*/
protected function loginAdmin() {
$this->drupalLogin(
$this->drupalCreateUser(
array(
'administer feeds', 'administer nodes',
)
)
);
}
/**
* Helper, run cron.
*/
protected function runCron($sleep = 0) {
$this->drupalGet($GLOBALS['base_url'] .'/cron.php');
sleep($sleep);
}