Skip to content
Snippets Groups Projects
feeds_mapper_taxonomy.test 6.01 KiB
<?php
// $Id$

/**
 * @file
 * Test case for taxonomy field mapper mappers/taxonomy.inc.
 */

require_once(drupal_get_path('module', 'feeds') . '/tests/feeds_mapper_test.inc');

/**
 * Class for testing Feeds <em>content</em> mapper.
 */
class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {

  public static function getInfo() {
    return array(
      'name' => t('Mapper: Taxonomy'),
      'description' => t('Test Feeds Mapper support for Taxonomy.'),
      'group' => t('Feeds'),
    );
  }

  /**
   * Set up the test.
   */
  function setUp() {
    // Call parent setup with required modules.
    parent::setUp('feeds', 'feeds_ui', 'ctools', 'taxonomy');

    // Create user and login.
    $this->drupalLogin($this->drupalCreateUser(
        array(
          'administer content types',
          'administer feeds',
          'administer nodes',
          'administer site configuration',
          'administer taxonomy',
        )
    ));
  }

  /**
   * Basic test loading an RSS feed.
   */
  function test() {

    // Add a new taxonomy vocabulary, add to story content type.
    $edit = array(
      'name' => 'Tags',
      'tags' => TRUE,
      'nodes[story]' => TRUE,
    );
    $this->drupalPost('admin/content/taxonomy/add/vocabulary', $edit, 'Save');

    // Create a feed, include mapping to taxonomy.
    $this->createFeedConfiguration('Syndication', 'syndication');
    $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,
        ),
        array(
          'source' => 'tags',
          'target' => 'taxonomy:1',
        ),
      )
    );

    // Aggregate feed node with "Tag" vocabulary.
    $nid = $this->createFeedNode();
    // Assert 10 items aggregated after creation of the node.
    $this->assertText('Created 10 Story nodes.');
    // There should be 30 terms and 44 term-node relations.
    $this->assertEqual(30, db_result(db_query("SELECT count(*) FROM {term_data}")), "Found correct number of terms.");
    $this->assertEqual(44, db_result(db_query("SELECT count(*) FROM {term_node}")), "Found correct number of term-node relations.");

    // Take a look at the actual terms on frontpage.
    $this->drupalGet('node');
    $terms = array(
      'authentication',
      'custom mapping',
      'data visualization',
      'Drupal',
      'Drupal planet',
      'faceted search',
      'GeoDC',
      'graphs',
      'interface',
      'intranet',
      'localization',
      'localization client',
      'localization server',
      'map-basec browser',
      'mapbox',
      'microfinance',
      'MIX Market',
      'open atrium',
      'open data',
      'open source',
      'Peru',
      'salesforce',
      'siteminder',
      'siteminder module',
      'software freedom day',
      'translation',
      'translation server',
      'usability',
      'Washington DC',
      'World Bank',
    );
    foreach ($terms as $term) {
      $term = check_plain($term);
      $this->assertPattern('/<a href="(.*?)\/taxonomy\/term\/([0-9]*?)"(.*)>'. $term .'<\/a>/', 'Found '. $term);
    }

    // Delete all items, all associations are gone.
    $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
    $this->assertText('Deleted 10 nodes.');
    $this->assertEqual(30, db_result(db_query("SELECT count(*) FROM {term_data}")), "Found correct number of terms.");
    $this->assertEqual(0, db_result(db_query("SELECT count(*) FROM {term_node}")), "Found correct number of term-node relations.");

    // Remove "Tag" setting, import again.
    $edit = array(
      'tags' => FALSE,
    );
    $this->drupalPost('admin/content/taxonomy/edit/vocabulary/1', $edit, 'Save');
    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('Created 10 Story nodes.');

    // We should only get one term-node association per node.
    $this->assertEqual(30, db_result(db_query("SELECT count(*) FROM {term_data}")), "Found correct number of terms.");
    $this->assertEqual(10, db_result(db_query("SELECT count(*) FROM {term_node}")), "Found correct number of term-node relations.");

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

    // Set vocabulary to multiple terms, import again.
    $edit = array(
      'multiple' => TRUE,
    );
    $this->drupalPost('admin/content/taxonomy/edit/vocabulary/1', $edit, 'Save');
    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('Created 10 Story nodes.');

    // We should get all term-node associations again.
    $this->assertEqual(30, db_result(db_query("SELECT count(*) FROM {term_data}")), "Found correct number of terms.");
    $this->assertEqual(44, db_result(db_query("SELECT count(*) FROM {term_node}")), "Found correct number of term-node relations.");

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

    // Remove a term, import again.
    $this->drupalPost('admin/content/taxonomy/edit/term/1', array(), 'Delete');
    $this->drupalPost(NULL, array(), 'Delete');
    $this->assertText('Deleted term');
    $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
    $this->assertText('Created 10 Story nodes.');

    // This term should now be missing from term-node associations.
    $this->assertEqual(29, db_result(db_query("SELECT count(*) FROM {term_data}")), "Found correct number of terms.");
    $this->assertEqual(39, db_result(db_query("SELECT count(*) FROM {term_node}")), "Found correct number of term-node relations.". db_result(db_query("SELECT count(*) FROM {term_node}")));
  }
}