View source
<?php
class FeedAPI_Node_Tests extends DrupalTestCase {
var $info;
function get_info() {
return array(
'name' => 'FeedAPI Node basic functions',
'desc' => "Refresh a feed and find out if it's okay. Uses SimplePie parser for parsing and downloading.",
'group' => 'FeedAPI',
);
}
function feedapi_user() {
$user = $this
->drupalCreateUserRolePerm(array(
'administer feedapi',
'advanced feedapi options',
'administer nodes',
"create " . $this->info->type . " content",
));
$this
->drupalLoginUser($user);
}
function create_type() {
$this->info->type = 'feedapi_node_' . $this
->randomName();
$this->info->name = 'Feed' . str_replace('_', ' ', $this
->randomName());
$this->info->description = t('Aggregates RSS or Atom feeds. Items from these feeds will be turned into nodes.');
$this->info->module = 'node';
$this->info->has_title = TRUE;
$this->info->title_label = t('Title');
$this->info->has_body = TRUE;
$this->info->body_label = t('Body');
$this->info->min_word_count = 0;
$this->info->custom = TRUE;
node_type_save($this->info);
variable_set('feedapi_settings_' . $this->info->type, unserialize('a:3:{s:7:"enabled";s:1:"1";s:7:"parsers";a:2:{s:16:"parser_simplepie";a:3:{s:7:"enabled";s:1:"1";s:6:"weight";s:1:"0";s:4:"test";s:1:"3";}s:25:"parser_common_syndication";a:3:{s:7:"enabled";s:1:"0";s:6:"weight";s:2:"-2";s:6:"tester";s:1:"3";}}s:10:"processors";a:2:{s:12:"feedapi_node";a:5:{s:6:"weight";s:1:"0";s:12:"content_type";s:5:"story";s:9:"node_date";s:4:"feed";s:7:"promote";s:1:"3";s:7:"enabled";s:1:"1";}s:18:"feedapi_aggregator";a:2:{s:6:"weight";s:1:"0";s:5:"block";s:1:"3";}}}'));
}
function testFeedAPI_Node_Refresh_Feed() {
$this
->create_type();
$this
->feedapi_user();
$feed_url = "http://novaak.net/test_feed.rss?" . $this
->randomName();
$edit = array(
'feedapi[feedapi_url]' => $feed_url,
);
$this
->drupalPostRequest('node/add/' . $this->info->type, $edit, 'Submit');
$this
->assertText(t('Link to site'), 'The node is created.');
$nid = db_result(db_query("SELECT nid FROM {feedapi} WHERE url = '%s'", $feed_url));
$this
->assertEqual(is_numeric($nid), TRUE, 'The feed node is in the database');
$feed_node = node_load(array(
'nid' => $nid,
));
$this
->assertEqual(is_object($feed_node->feed), TRUE, 'The feed can be loaded.');
$settings = feedapi_get_settings($this->info->type, $nid);
$settings['items_delete'] = FEEDAPI_NEVER_DELETE_OLD;
_feedapi_store_settings(array(
'nid' => $nid,
), $settings);
$this
->drupalGet(url("node/{$nid}/refresh", NULL, NULL, TRUE));
$this
->assertText("10 new item(s) were saved. 0 existing item(s) were updated", 'The proper number of items were created');
$this
->drupalGet(url("admin/content/feed", NULL, NULL, TRUE));
$this
->assertText('Radiovaticana.org', 'The admin overview page contains the feed title.');
$result = db_query("SELECT fi.nid FROM {feedapi_node_item} fi JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fi.nid WHERE ff.feed_nid = %d", $nid);
$types = array();
$author_check = TRUE;
$item_nids = array();
while ($node = db_fetch_array($result)) {
$item_nids[] = $node['nid'];
$node = node_load(array(
'nid' => $node['nid'],
));
$types[] = $node->type;
$title_size[] = strlen($node->title);
$body_size[] = strlen($node->body);
$author_check = $feed_node->uid == $node->uid && $author_check;
}
$types = array_unique($types);
$this
->assertEqual($types[0], 'story', 'The first news item is a story.');
$this
->assertEqual(count($types), 1, 'All news items have the same type.');
sort($title_size);
sort($body_size);
$this
->assertNotEqual($title_size[0], 0, 'All news item titles are longer than 0 character.');
$this
->assertNotEqual($body_size[0], 0, 'All news item bodies are longer than 0 character.');
$this
->assertTrue($author_check, 'All news items has the proper author.');
$this
->drupalPostRequest("node/{$nid}/purge", array(), "Yes");
node_delete($nid);
$item_remain = db_result(db_query("SELECT COUNT(*) FROM {feedapi_node_item} fi JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fi.nid WHERE ff.feed_nid = %d", $nid));
$this
->assertEqual($item_remain, 0, 'All news item database entries are deleted.');
$node_remain = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE nid IN (%s)", implode(',', $item_nids)));
$this
->assertEqual($node_remain, 0, 'All nodes belonging to a news item are deleted.');
node_get_types('types', NULL, TRUE);
$this
->drupalGet(url("admin/build/block"));
_block_rehash();
$result = db_query("SELECT module, delta FROM {blocks} WHERE status = 1");
$to_disable_blocks = array();
while ($row = db_fetch_array($result)) {
$to_disable_blocks[] = $row;
}
db_query("UPDATE {blocks} SET status = 0 WHERE status = 1");
$region = array_pop(array_keys(system_region_list(variable_get('theme_default', FALSE))));
db_query("UPDATE {blocks} SET status = 1, region='%s' WHERE module = 'feedapi' AND delta = '%s'", $region, $this->info->type);
$this
->drupalGet(url('node'));
$this
->assertText(t('Feed URL'), 'The block is showing up');
$feed_url = "http://novaak.net/test_feed.rss?" . $this
->randomName();
$edit = array(
'url' => $feed_url,
);
$this
->drupalPostRequest('node', $edit, 'Add');
$this
->assertText(t('Link to site'), 'The node is created via the simplified form block.');
$nid = db_result(db_query("SELECT nid FROM {feedapi} WHERE url = '%s'", $feed_url));
$this
->assertEqual(is_numeric($nid), TRUE, 'The feed node is in the database');
if (is_numeric($nid)) {
$values = db_fetch_array(db_query("SELECT settings, feed_type, checked, half_done FROM {feedapi} WHERE nid = %d", $nid));
$sane_default = TRUE;
$sane_default = $sane_default || is_array($values['settings']) && count($values['settings']) > 1;
$sane_default = $sane_default || $values['feed_type'] == 'XML feed';
$sane_default = $sane_default || $values['checked'] == 0;
$sane_default = $sane_default || $values['half_done'] == 0;
$this
->assertIdentical($sane_default, TRUE, "The feed has sane default values in the database table");
node_delete($nid);
}
db_query("UPDATE {blocks} SET status = 0 WHERE module = 'feedapi' AND delta = '%s'", $this->info->type);
foreach ($to_disable_blocks as $to_enable_block) {
db_query("UPDATE {blocks} SET status = 1 WHERE module = '%s' AND delta ='%s'", $to_enable_block['module'], $to_enable_block['delta']);
}
node_types_rebuild();
node_type_delete($this->info->type);
$this
->assertIdentical(FALSE, variable_get('feedapi_settings_' . $this->info->type, FALSE), 'The content-type\'s setting variable is successfully deleted.');
variable_init();
_block_rehash();
$this
->drupalGet(url("admin/build/block"));
}
function testFeedAPI_Node_Export_OPML() {
$this
->feedapi_user();
$this
->drupalGet('admin/content/feed/export_opml');
$this
->assertResponse(200, 'The export OPML path returns good status code.');
}
function testFeedAPI_Node_Import_Form() {
$this
->feedapi_user();
$this
->drupalGet('admin/content/feed/import_opml');
$this
->assertResponse(200, 'The OPML import form is accessible.');
$this
->assertText(t('OPML File'), 'The retrieved page contains the OPML form.');
$this
->drupalPostRequest('admin/content/feed/import_opml', array(), 'Submit');
$this
->assertText(t('Data could not be retrieved, invalid or empty file.'), 'The error message appears when the empty OPML import form is submitted.');
}
}