You are here

function FeedAPINodeTestsCase::testFeedAPI_Node in FeedAPI 6

Add a content-type, create a feed and refresh it. Check if everything seems ok Delete the feed Check if the rubbish is purged as well. Use simplified form to create a feed

File

feedapi_node/tests/feedapi_node.test, line 29

Class

FeedAPINodeTestsCase
Class for testing feedapi_node. It also tests FeedAPI and SimplePie parsers.

Code

function testFeedAPI_Node() {
  $parsers_ok = $this
    ->get_parsers();
  $this
    ->assertEqual(count($parsers_ok) > 0, TRUE, 'FeedAPI has at least one parser.');
  foreach ($parsers_ok as $parser) {

    // Create a new content-type for creating the feed node
    $this
      ->create_type($parser);
    $settings = feedapi_get_settings($this->info->type);
    $this
      ->assertEqual($settings['parsers'][$parser]['enabled'], TRUE, $parser . ' parser is enabled for the content-type ' . $this->info->type);

    // Login with a user who has FeedAPI rights
    $this
      ->feedapi_user();

    // Create the feed node
    // Make the URL unique. It's not impossible that someone add this feed URL to the DB prior.
    $feed_url = $this
      ->testFileURL('test_feed.rss');
    $edit = array(
      'feedapi[feedapi_url]' => $feed_url,
    );
    $this
      ->drupalPost('node/add/' . $this->info->type, $edit, 'Save');
    $this
      ->assertText(t('Link to site'), 'The node is created.');

    // Check if the entry is in the DB
    $nid = db_result(db_query("SELECT nid FROM {feedapi} WHERE url = '%s'", $feed_url));
    $vid = db_result(db_query_range("SELECT vid FROM {feedapi} WHERE nid = %d ORDER by vid DESC", $nid, 1, 1));
    $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.');

    // Admin overview page loads and the feed can be found there with the correct number of items
    $this
      ->drupalGet('admin/content/feed');
    $this
      ->assertText('Radiovaticana.org', 'The admin overview page contains the feed title.');

    // Disable feed item expiring
    $settings = feedapi_get_settings($this->info->type, $nid);
    $settings['items_delete'] = FEEDAPI_NEVER_DELETE_OLD;
    _feedapi_store_settings(array(
      'vid' => $vid,
    ), $settings);

    // Refresh the feed
    $this
      ->drupalGet("node/{$nid}/refresh");
    $notification = t("%new new item(s) were saved. %updated existing item(s) were updated.", array(
      "%new" => 10,
      "%updated" => 0,
    ));
    $notification = str_replace('<em>', '', $notification);
    $notification = str_replace('</em>', '', $notification);
    $this
      ->assertText($notification, 'The proper number of items were created');

    // Refresh fact at the admin page
    $this
      ->drupalGet('admin/content/feed');
    $this
      ->assertText('10');

    // Check the feed items
    $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;

      // Check the length of the nodes
      $title_size[] = strlen($node->title);
      $body_size[] = strlen($node->body);

      // Check the author of the nodes
      $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
      ->drupalPost("node/{$nid}/purge", array(), "Yes");

    // Remove the unwanted rubbish
    node_delete($nid);

    // Check if the news items are deleted as well
    $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 because of feed deletion.');

    // Check if the nodes belong to the news items are really deleted
    $node_remain = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE nid IN (" . db_placeholders($item_nids, 'int') . ")", $item_nids));
    $this
      ->assertEqual($node_remain, 0, 'All nodes belonging to a news item are deleted.');

    // Reset node types so we get all blocks
    node_get_types('types', NULL, TRUE);

    // Generate blocks to get a simplified feed adding block for the new content-type
    $this
      ->drupalGet("admin/build/block");
    _block_rehash();

    // Disable all blocks, but remember enabled ones
    $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;
    }

    // Enable simplified form block
    db_query("UPDATE {blocks} SET status = 0 WHERE status = 1");
    $region = array_pop(array_keys(system_region_list(variable_get('theme_default', FALSE))));

    // First region which is ok for the current theme
    db_query("UPDATE {blocks} SET status = 1, region='%s' WHERE module = 'feedapi' AND delta = '%s'", $region, $this->info->type);

    // Check for existing block showing up
    $this
      ->drupalGet('node');
    $this
      ->assertText(t('Feed URL'), 'The block is showing up');

    // Submit feed via simplified block
    $edit = array(
      'url' => $feed_url,
    );
    $this
      ->drupalPost('node', $edit, 'Add');
    $this
      ->assertText(t('Link to site'), 'The node is created via the simplified form block.');

    // Check if the entry is in the DB
    $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, next_refresh_time, 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['next_refresh_time'] == 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);
    }

    // Restore blocks to the user's previous settings
    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']);
    }

    // Test refresh on feed creation
    $edit = array(
      'feedapi[feedapi_url]' => $feed_url,
      'feedapi[refresh_on_create]' => TRUE,
    );
    $this
      ->drupalPost('node/add/' . $this->info->type, $edit, 'Save');

    // Check if the entry is in the DB
    $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');
    $this
      ->drupalPost("node/{$nid}/purge", array(), "Yes");

    // Remove the unwanted rubbish
    node_delete($nid);
    $this
      ->drupalGet('admin/build/block');
  }
  menu_rebuild();
}