You are here

location_feeds.cck.test in Location Feeds 7

Same filename and directory in other branches
  1. 6 tests/location_feeds.cck.test

File

tests/location_feeds.cck.test
View source
<?php

/**
 * @file
 *
 * Test case for CCK location imports.
 *
 * @author: Elliott Foster
 */
module_load_include('inc', 'location_feeds', 'tests/location_feeds.test');
class CCKFeedsMapperLocationTestCase extends FeedsMapperLocationTestCase {
  public static function getInfo() {
    return array(
      'name' => t('CCK Locations'),
      'description' => t('Test Feeds Mapper support for CCK Location.'),
      'group' => t('Location Feeds'),
    );
  }

  /**
   * Set up the test.
   */
  public function setUp() {

    // Call parent setup with required modules.
    parent::setUp(array(
      'location',
      'location_feeds',
      'cck',
      'location_cck',
    ));

    // Create user and login.
    $this
      ->drupalLogin($this
      ->drupalCreateUser(array(
      'administer content types',
      'administer feeds',
      'administer nodes',
      'administer site configuration',
    )));
    $settings = LocationTestCase::getLocationFieldDefaults();

    // unset stuff CCK doesn't need
    unset($settings['location_settings']['multiple']);
    unset($settings['location_settings']['rss']);

    // Create content type.
    $type = $this
      ->drupalCreateContentType($settings);
    $this->type = $type->type;
    $fields = array(
      'field_location' => array(
        'field_name' => 'field_location',
        'type' => 'location',
        'label' => 'field_location_location_label',
        'widget' => 'location',
        'settings' => array(
          'location_settings' => array(
            'form' => array(
              'fields' => $settings,
            ),
          ),
        ),
      ),
    );
    foreach ($fields as $field) {
      $f = array(
        'field_name' => $field['field_name'],
        'type' => $field['type'],
      );
      field_create_field($f);
      $i = array(
        'field_name' => $field['field_name'],
        'entity_type' => 'node',
        'label' => $field['label'],
        'bundle' => $this->type,
        'settings' => isset($field['settings']) ? $field['settings'] : array(),
        'widget' => array(
          'type' => $field['widget'],
        ),
      );
      field_create_instance($i);
    }
  }

  /**
   * Basic test loading an RSS feed for nodes with CCK locations.
   */
  public function test() {
    $type = $this->type;

    // Test constants
    $loc = array(
      0 => array(
        'street' => "93 Rue de Rivoli",
        'city' => "Paris",
        'province' => "",
        'country' => "fr",
        'postal_code' => 75001,
      ),
      1 => array(
        'street' => "1600 Amphitheatre Parkway",
        'city' => "Mountain View",
        'province' => "CA",
        'country' => "us",
        'postal_code' => 94043,
      ),
      2 => array(
        'street' => "93 Rue de Rivoli",
        'city' => "Paris",
        'province' => "",
        'country' => "fr",
        'postal_code' => 75001,
      ),
      3 => array(
        'street' => "1600 Amphitheatre Parkway",
        'city' => "Mountain View",
        'province' => "CA",
        'country' => "us",
        'postal_code' => 94043,
      ),
    );

    // Create and configure importer.
    // Create a feed.
    $this
      ->createImporterConfiguration('Location import', 'location_import');

    // Set and configure plugins.
    $this
      ->setSettings('location_import', NULL, array(
      'content_type' => '',
      'import_period' => FEEDS_SCHEDULE_NEVER,
    ));
    $this
      ->setPlugin('location_import', 'FeedsFileFetcher');
    $this
      ->setPlugin('location_import', 'FeedsCSVParser');
    $this
      ->setSettings('location_import', 'FeedsNodeProcessor', array(
      'content_type' => $type,
    ));

    // Go to mapping page and create a couple of mappings.
    $mappings = array(
      array(
        'source' => 'title',
        'target' => 'title',
        'unique' => FALSE,
      ),
      array(
        'source' => 'description',
        'target' => 'body',
        'unique' => FALSE,
      ),
      array(
        'source' => 'timestamp',
        'target' => 'created',
        'unique' => FALSE,
      ),
      array(
        'source' => 'guid',
        'target' => 'guid',
        'unique' => TRUE,
      ),
      array(
        'source' => 'street',
        'target' => 'field_location:street',
      ),
      array(
        'source' => 'city',
        'target' => 'field_location:city',
      ),
      array(
        'source' => 'state',
        'target' => 'field_location:province',
      ),
      array(
        'source' => 'zip',
        'target' => 'field_location:postal_code',
      ),
      array(
        'source' => 'country',
        'target' => 'field_location:country',
      ),
    );
    $this
      ->addMappings('location_import', $mappings);

    // Import CSV file.
    $this
      ->importFile('location_import', drupal_get_path('module', 'location_feeds') . '/tests/feeds/location_nodes.csv');
    $this
      ->assertText('Created 5 nodes');

    // Check the imported locations
    $x = 0;
    $nodes = db_query("SELECT nid FROM {node} WHERE type=:type", array(
      ':type' => $type,
    ));
    foreach ($nodes as $node) {
      $node = node_load($node->nid);

      // the location doesn't appear to be loaded correctly on the node object
      // but is correctly stored in the database so load it directly here
      $lid = db_query("SELECT lid FROM {location_instance} WHERE genid=:genid", array(
        ':genid' => 'cck:field_location:' . $node->vid,
      ))
        ->fetchCol();
      $location = location_load_location($lid[0]);
      if (isset($location['street'])) {
        $this
          ->assertEqual($loc[$x]['street'], $location['street'], t('Testing street import, expected: @e, found: @f', array(
          '@e' => $loc[$x]['street'],
          '@f' => $location['street'],
        )));
      }
      else {
        $this
          ->assert('fail', t('The street was not imported correctly.'));
      }
      if (isset($location['city'])) {
        $this
          ->assertEqual($loc[$x]['city'], $location['city'], t('Testing city import, expected: @e, found: @f', array(
          '@e' => $loc[$x]['city'],
          '@f' => $location['city'],
        )));
      }
      else {
        $this
          ->assert('fail', t('The city was not imported correctly.'));
      }
      if (isset($location['province'])) {
        $this
          ->assertEqual($loc[$x]['province'], $location['province'], t('Testing province import, expected: @e, found: @f', array(
          '@e' => $loc[$x]['province'],
          '@f' => $location['province'],
        )));
      }
      else {
        $this
          ->assert('fail', t('The city was not imported correctly.'));
      }
      if (isset($location['country'])) {
        $this
          ->assertEqual($loc[$x]['country'], $location['country'], t('Testing country import, expected: @e, found: @f', array(
          '@e' => $loc[$x]['country'],
          '@f' => $location['country'],
        )));
      }
      else {
        $this
          ->assert('fail', t('The country was not imported correctly.'));
      }
      if (isset($location['postal_code'])) {
        $this
          ->assertEqual($loc[$x]['postal_code'], $location['postal_code'], t('Testing country import, expected: @e, found: @f', array(
          '@e' => $loc[$x]['postal_code'],
          '@f' => $location['postal_code'],
        )));
      }
      else {
        $this
          ->assert('fail', t('The postal code was not imported correctly.'));
      }
      $x++;
    }
  }

}

Classes