Skip to content
Snippets Groups Projects
feeds_defaults.test 15.2 KiB
Newer Older
<?php
// $Id$

/**
 * @file
 * Tests for default configurations.
 */

// Require FeedsWebTestCase class definition.
require_once(dirname(__FILE__) .'/../../tests/feeds.test.inc');

/**
 * Base class for default tests.
 */
class FeedsDefaultsTestCase extends FeedsWebTestCase {

  /**
   * Enable a default configuration and verify it.
   */
  public function enable($ids) {
    if (is_string($ids)) {
      $ids = array($ids);
    }
    $edit = array();
    foreach ($ids as $id) {
      $edit[$id] = TRUE;
    }
    $this->drupalPost('admin/build/feeds', $edit, 'Save');
    foreach ($ids as $id) {
      $this->assertRaw('admin/build/feeds/edit/'. $id .'">Override', 'Enabled '. $id);
    }
  }

  /**
   * Disable a default configuration and verify it.
   */
  public function disable($ids) {
    if (is_string($ids)) {
      $ids = array($ids);
    }
    $edit = array();
    foreach ($ids as $id) {
      $edit[$id] = FALSE;
    }
    $this->drupalPost('admin/build/feeds', $edit, 'Save');
    foreach ($ids as $id) {
      $this->assertNoRaw('admin/build/feeds/edit/'. $id .'">Override', 'Disabled '. $id);
    }
  }
}

/**
 * Test Feed configuration.
 */
class FeedsDefaultsFeedTestCase extends FeedsDefaultsTestCase {

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'feeds_defaults');

    $this->drupalLogin(
      $this->drupalCreateUser(
        array(
          'administer feeds', 'administer nodes',
        )
      )
    );
  }

  /**
   * Describe this test.
   */
  public function getInfo() {
    return array(
      'name' => t('Defaults: Feed'),
      'description' => t('Test "Feed" default configuration.'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Run tests.
   */
  public function test() {
    $this->enable('feed');
    $nid = $this->createFeedNode('feed', NULL, '', 'feed');

    // Assert results.
    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE type = "feed_item"'));
    $this->assertEqual($count, 10, 'Found the correct number of feed item nodes in database.');

    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item}'));
    $this->assertEqual($count, 10, 'Found the correct number of records in feeds_node_item.');

    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE title = "Open Atrium Translation Workflow: Two Way Translation Updates"'));
    $this->assertEqual($count, 1, 'Found title.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE title = "Week in DC Tech: October 5th Edition"'));
    $this->assertEqual($count, 1, 'Found title.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE title = "Integrating the Siteminder Access System in an Open Atrium-based Intranet"'));
    $this->assertEqual($count, 1, 'Found title.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE title = "Scaling the Open Atrium UI"'));
    $this->assertEqual($count, 1, 'Found title.');

    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item} WHERE url = "http://developmentseed.org/blog/2009/oct/06/open-atrium-translation-workflow-two-way-updating"'));
    $this->assertEqual($count, 1, 'Found feed_node_item record.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item} WHERE url = "http://developmentseed.org/blog/2009/oct/05/week-dc-tech-october-5th-edition"'));
    $this->assertEqual($count, 1, 'Found feed_node_item record.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item} WHERE guid = "974 at http://developmentseed.org"'));
    $this->assertEqual($count, 1, 'Found feed_node_item record.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item} WHERE guid = "970 at http://developmentseed.org"'));
    $this->assertEqual($count, 1, 'Found feed_node_item record.');

    // Remove all items
    $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
    $this->assertText('Deleted 10 nodes.');

    // Import again.
    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('Created 10 Feed item nodes.');

    // Delete and assert all items gone.
    $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');

    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE type = "feed_item"'));
    $this->assertEqual($count, 0, 'Found the correct number of feed item nodes in database.');

    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item}'));
    $this->assertEqual($count, 0, 'Found the correct number of records in feeds_node_item.');

    // Create a batch of nodes.
    $this->createFeedNodes('feed', 10, 'feed');
    $count = db_result(db_query('SELECT COUNT(*) from {node} WHERE type = "feed_item"'));
    $this->assertEqual($count, 100, 'Imported 100 nodes.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item}'));
    $this->assertEqual($count, 100, 'Found 100 records in feeds_node_item.');

    // Disable the configuration.
    $this->disable('feed');
    $this->drupalGet('node/add');
    $this->assertNoRaw('node/add/feed');
    $this->assertNoText('node/add/feed-item');
  }
}

/**
 * Test Feed fast configuration.
 */
class FeedsDefaultsFastFeedTestCase extends FeedsDefaultsTestCase {

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'feeds_defaults', 'data', 'data_ui', 'views', 'views_ui');

    $this->drupalLogin(
      $this->drupalCreateUser(
        array(
          'administer feeds', 'administer nodes', 'administer data tables',
        )
      )
    );
  }

  /**
   * Describe this test.
   */
  public function getInfo() {
    return array(
      'name' => t('Defaults: Fast feed'),
      'description' => t('Test "Fast feed" default configuration <strong>Requires Data and Views.</strong>'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Run tests.
   */
  public function test() {

    // Enable configuration and assert status.
    $this->enable('feed_fast');
    $this->drupalGet('admin/build/data');
    $this->assertText('feeds_data_feed_fast');
    $this->drupalGet('admin/content/data/view/feeds_data_feed_fast');
    $this->assertText('Fast feed');
    $this->assertText('There is no data in this table.');

    // Create feed node.
    $nid = $this->createFeedNode('feed_fast', NULL, '', 'feed_fast');
    $this->assertText('Created 10 items.');

    // Verify presence of aggregated items.
    $this->drupalGet('admin/content/data/view/feeds_data_feed_fast');
    $this->assertText('Open Atrium Translation Workflow: Two Way Translation Updates');
    $this->assertText('n a word, nothing. There has been a major improvement on this front. Now your translation');
    $this->assertLink('http://developmentseed.org/blog/2009/oct/06/open-atrium-translation-workflow-two-way-updating');

    // Delete and re import.
    $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
    $this->assertText('Deleted 10 items.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_data_feed_fast}'));
    $this->assertEqual($count, 0, 'Found correct number of items.');

    $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
    $this->assertText('Deleted 0 items.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_data_feed_fast}'));
    $this->assertEqual($count, 0, 'Found correct number of items.');

    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('Created 10 items.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_data_feed_fast}'));
    $this->assertEqual($count, 10, 'Found correct number of items.');

    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('There are no new items.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_data_feed_fast}'));
    $this->assertEqual($count, 10, 'Found correct number of items.');

    // Disable.
    $this->disable('feed_fast');
    $this->drupalGet('node/add');
    $this->assertNoRaw('node/add/feed-fast');
  }
}

/**
 * Test Node import configuration.
 */
class FeedsDefaultsNodeTestCase extends FeedsDefaultsTestCase {

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'feeds_defaults');

    $this->drupalLogin(
      $this->drupalCreateUser(
        array(
          'administer feeds', 'administer nodes',
        )
      )
    );
  }

  /**
   * Describe this test.
   */
  public function getInfo() {
    return array(
      'name' => t('Defaults: Node import'),
      'description' => t('Test "Node import" default configuration.'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Run tests.
   */
  public function test() {
    $this->enable('node');

    // Import file.
    $this->importFile('node', $this->absolutePath() .'/tests/feeds/nodes.csv');

    // Assert returning page.
    $this->assertText('Created 8 Story nodes.');
    $this->assertText('Import CSV files with one or more of these columns: title, body, published, guid.');
    $this->assertText('Column guid is mandatory and considered unique: only one item per guid value will be created.');
    $this->assertRaw('feeds/nodes.csv');

    // Assert created nodes.
    $this->drupalGet('node');
    $this->assertText('Typi non habent');
    $this->assertText('Eodem modo typi');
    $this->assertText('Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.');
    $this->assertText('Lorem ipsum');
    $this->assertText('Ut wisi enim ad minim veniam');
    $this->assertText('1976');
    // Nam liber tempor has the same GUID as Lorem ipsum.
    $this->assertNoText('Nam liber tempor');

    // Click through to one node.
    $this->clickLink('Lorem ipsum');
    $this->assertText('Lorem ipsum');
    $this->assertText('Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.');
    $this->assertText('Anonymous');

    // Assert DB status as is and again after an additional import.
    for ($i = 0; $i < 2; $i++) {
      $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item}'));
      $this->assertEqual($count, 8, 'Found correct number of items.');
      $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE type = "story" AND status = 1 AND uid = 0'));
      $this->assertEqual($count, 8, 'Found correct number of items.');
      // Do not filter on type intentionally. There shouldn't be more than 8 nodes total.
      $count = db_result(db_query('SELECT COUNT(*) FROM {node_revisions}'));
      $this->assertEqual($count, 8, 'Found correct number of items.');

      // Import again. Feeds only updates items that haven't changed. However,
      // there are 2 different items with the same GUID in nodes.csv.
      // Therefore, feeds will show updates to 2 nodes.
      $this->drupalPost('import/node/import', array(), 'Import');
      $this->assertText('Updated 2 Story nodes.');
    }

    // Remove all nodes.
    $this->drupalPost('import/node/delete-items', array(), 'Delete');
    $this->assertText('Deleted 8 nodes.');

    // Import once again.
    $this->drupalPost('import/node/import', array(), 'Import');
    $this->assertText('Created 8 Story nodes.');

    // Import a similar file with changes in 4 records. Feeds should report 6
    // Updated story nodes (4 changed records, 2 records sharing a GUID
    // subsequently being updated).
    $this->importFile('node', $this->absolutePath() .'/tests/feeds/nodes_changes.csv');
    $this->assertText('Updated 6 Story nodes.');

Alex Barth's avatar
Alex Barth committed
    // Import a larger file with more records.
    $this->importFile('node', $this->absolutePath() .'/tests/feeds/many_nodes.csv');
    $this->assertText('Created 71 Story nodes.');

    // Remove all nodes.
    $this->drupalPost('import/node/delete-items', array(), 'Delete');
    $this->assertText('Deleted 79 nodes.');

    // Import once again.
    $this->drupalPost('import/node/import', array(), 'Import');
    $this->assertText('Created 79 Story nodes.');

    // Disable.
    $this->disable('node');

    // Nodes should be still present.
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_node_item}'));
Alex Barth's avatar
Alex Barth committed
    $this->assertEqual($count, 79, 'Found correct number of items.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE type = "story" AND status = 1 AND uid = 0'));
Alex Barth's avatar
Alex Barth committed
    $this->assertEqual($count, 79, 'Found correct number of items.');
    // Do not filter on type intentionally. There shouldn't be more than 8 nodes total.
    $count = db_result(db_query('SELECT COUNT(*) FROM {node_revisions}'));
Alex Barth's avatar
Alex Barth committed
    $this->assertEqual($count, 79, 'Found correct number of items.');
  }
}

/**
 * Test OPML import configuration.
 */
class FeedsDefaultsOPMLTestCase extends FeedsDefaultsTestCase {

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'feeds_defaults');

    $this->drupalLogin(
      $this->drupalCreateUser(
        array(
          'administer feeds', 'administer nodes',
        )
      )
    );
  }

  /**
   * Describe this test.
   */
  public function getInfo() {
    return array(
      'name' => t('Defaults: OPML import'),
      'description' => t('Test "OPML import" default configuration.'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Run tests.
   */
  public function test() {
    $this->enable('feed');
    $this->enable('opml');

    // Import OPML and assert.
    $file = $this->generateOPML();
    $this->importFile('opml', $file);
    $this->assertText('Created 3 feed nodes.');
    $count = db_result(db_query('SELECT COUNT(*) FROM {feeds_source}'));
    $this->assertEqual($count, 4, 'Found correct number of items.');

    // Import a feed and then delete all items from it.
    $this->drupalPost('node/1/import', array(), 'Import');
    $this->assertText('Created 10 Feed item nodes.');
    $this->drupalPost('node/1/delete-items', array(), 'Delete');
    $this->assertText('Deleted 10 nodes.');

    // Disable.
    $this->disable('feed');
    $this->disable('opml');
  }
}

/**
 * Test User import configuration.
 */
class FeedsDefaultsUserTestCase extends FeedsDefaultsTestCase {

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'feeds_defaults');

    $this->drupalLogin(
      $this->drupalCreateUser(
        array(
          'administer feeds', 'administer users',
        )
      )
    );
  }

  /**
   * Describe this test.
   */
  public function getInfo() {
    return array(
      'name' => t('Defaults: User import'),
      'description' => t('Test "User import" default configuration.'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Run tests.
   */
  public function test() {
    $this->enable('user');

    // Import CSV file.
    $this->importFile('user', $this->absolutePath() .'/tests/feeds/users.csv');

    // Assert result.
    $this->assertText('Created 4 users.');
    // 1 user has an invalid email address.
    $this->assertText('There were 1 users that could not be imported because either their name or their email was empty or not valid. Check import data and mapping settings on User processor.');
    $this->drupalGet('admin/user/user');
    $this->assertText('Morticia');
    $this->assertText('Fester');
    $this->assertText('Gomez');
    $this->assertText('Pugsley');

    $this->disable('user');
  }