* Definition of Drupal\aggregator\Tests\AggregatorCronTest.
*/
namespaceDrupal\aggregator\Tests;
classAggregatorCronTestextendsAggregatorTestBase{
publicstaticfunctiongetInfo(){
returnarray(
'name'=>'Update on cron functionality',
'description'=>'Update feeds on cron.',
'group'=>'Aggregator'
);
}
/**
* Add feeds update them on cron.
*/
publicfunctiontestCron(){
// Create feed and test basic updating on cron.
global$base_url;
$key=config('system.cron')->get('cron_key');
$this->createSampleNodes();
$feed=$this->createFeed();
$this->cronRun();
$this->assertEqual(5,db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid))->fetchField(),'Expected number of items in database.');
$this->removeFeedItems($feed);
$this->assertEqual(0,db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid))->fetchField(),'Expected number of items in database.');
$this->cronRun();
$this->assertEqual(5,db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid))->fetchField(),'Expected number of items in database.');
// Test feed locking when queued for update.
$this->removeFeedItems($feed);
db_update('aggregator_feed')
->condition('fid',$feed->fid)
->fields(array(
'queued'=>REQUEST_TIME,
))
->execute();
$this->cronRun();
$this->assertEqual(0,db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid))->fetchField(),'Expected number of items in database.');
db_update('aggregator_feed')
->condition('fid',$feed->fid)
->fields(array(
'queued'=>0,
))
->execute();
$this->cronRun();
$this->assertEqual(5,db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid))->fetchField(),'Expected number of items in database.');
$this->assertRaw(t('The feed %name has been added.',array('%name'=>$edit['title'])),t('The feed !name has been added.',array('!name'=>$edit['title'])));
$feed=db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url",array(':title'=>$edit['title'],':url'=>$edit['url']))->fetch();
$this->assertTrue(!empty($feed),t('The feed found in database.'));
$this->assertRaw(t('The feed %title has been deleted.',array('%title'=>$feed->title)),t('Feed deleted successfully.'));
}
/**
* Return a randomly generated feed edit array.
*
* @param $feed_url
* If given, feed will be created with this URL, otherwise /rss.xml will be used.
* @return
* A feed array.
*/
functiongetFeedEditArray($feed_url=NULL){
$feed_name=$this->randomName(10);
if(!$feed_url){
$feed_url=url('rss.xml',array(
'query'=>array('feed'=>$feed_name),
'absolute'=>TRUE,
));
}
$edit=array(
'title'=>$feed_name,
'url'=>$feed_url,
'refresh'=>'900',
);
return$edit;
}
/**
* Return the count of the randomly created feed array.
*
* @return
* Number of feed items on default feed created by createFeed().
*/
functiongetDefaultFeedItemCount(){
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
$feed_count=db_query_range('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1',0,config('system.rss-publishing')->get('feed_default_items'))->fetchField();
return$feed_count>10?10:$feed_count;
}
/**
* Update feed items (simulate click to admin/config/services/aggregator/update/$fid).
*
* @param $feed
* Feed object representing the feed.
* @param $expected_count
* Expected number of feed items.
*/
functionupdateFeedItems(&$feed,$expected_count){
// First, let's ensure we can get to the rss xml.
$this->drupalGet($feed->url);
$this->assertResponse(200,t('!url is reachable.',array('!url'=>$feed->url)));
// Attempt to access the update link directly without an access token.
$result=db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid',array(':fid'=>$feed->fid));
$items=array();
$feed->items=array();
foreach($resultas$item){
$feed->items[]=$item->iid;
}
$feed->item_count=count($feed->items);
$this->assertEqual($expected_count,$feed->item_count,t('Total items in feed equal to the total items in database (!val1 != !val2)',array('!val1'=>$expected_count,'!val2'=>$feed->item_count)));