You are here

parser_ical_feeds.test in iCal feed parser 6.2

Same filename and directory in other branches
  1. 7 tests/parser_ical_feeds.test

File

tests/parser_ical_feeds.test
View source
<?php

/**
 * @file
 * Test case for CCK ical date field mapper mappers/content.inc.
 */
require_once drupal_get_path('module', 'feeds') . '/tests/feeds_mapper_test.inc';

/**
 * Class for testing Feeds <em>content</em> mapper.
 *
 * @todo: Add test method iCal
 * @todo: Add common feeds tests to mapping test
 *   Apple (School.ics), Google, Upcoming, include one with only feed timezone, not per item
 * @todo: Repeating rules 
 */
class ParserIcalFeedsTestCase extends FeedsMapperTestCase {
  public function absolutePath() {
    return $this
      ->absolute() . '/' . drupal_get_path('module', 'parser_ical');
  }

  /**
   * Set up the test.
   */
  public function setUp() {
    $modules = array_merge(array(
      'feeds',
      'feeds_ui',
      'ctools',
      'content',
      'date_api',
      'date',
      'parser_ical',
    ), func_get_args());

    // Call parent setup with the required modules
    if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
      call_user_func_array('parent::setUp', $modules);
    }
    else {
      call_user_func_array(array(
        'parent',
        'setUp',
      ), $modules);
    }

    // date module : consistent site timezone to against
    variable_set('date_default_timezone_name', 'Pacific/Apia');

    // and just because I'll make mistakes working with tests with the US date format
    variable_set('date_format_short', 'Y-m-d H:i');

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

  /**
   * Basic test configured feed item, to import feed into.
   */
  public function createConfiguredFeed($fields = array()) {

    // Create content type.
    $fields = array_merge($fields, array(
      'datefield' => array(
        'type' => 'date',
        'settings' => array(
          'todate' => 'optional',
          'tz_handling' => 'date',
        ),
      ),
      'datesite' => array(
        'type' => 'date',
        'settings' => array(
          'todate' => 'optional',
          'tz_handling' => 'site',
        ),
      ),
    ));
    $typename = $this
      ->createContentType(NULL, $fields);

    // Create and configure importer.
    $this
      ->createFeedConfiguration('iCal Feed', 'ical');
    $this
      ->setSettings('ical', NULL, array(
      'content_type' => '',
      'import_period' => FEEDS_SCHEDULE_NEVER,
    ));
    $this
      ->setPlugin('ical', 'FeedsFileFetcher');
    $this
      ->setPlugin('ical', 'ParserIcalFeedsParser');
    $this
      ->setSettings('ical', 'FeedsNodeProcessor', array(
      'content_type' => $typename,
      'update_existing' => 1,
    ));
    $this
      ->addMappings('ical', array(
      array(
        'source' => 'title',
        'target' => 'title',
      ),
      array(
        'source' => 'description',
        'target' => 'body',
      ),
      array(
        'source' => 'ical_date',
        'target' => 'field_datefield:start',
      ),
      array(
        'source' => 'ical_date',
        'target' => 'field_datesite:start',
      ),
      array(
        'source' => 'guid',
        'target' => 'guid',
        'unique' => '1',
      ),
    ));
    return $typename;
  }

  /**
   * assertCCKFieldValue uses an XPath on the CCK fields on the form,
   * the method looks in 'value' and for plain text the end date is a 'value2'
   *
   * Can't quickly see a nice extend on it so this _yet_ but sure it's there
   * so this is a simple version of the function making assumtions that we
   * know of the date field.
   *
   * @todo this properly.
   */
  protected function assertCCKFieldValue2($field_name, $value) {
    $xpath = '//input[@name="field_' . $field_name . '[0][value2][date]"]';
    $message = t('Found form %field_name with the expected value.', array(
      '%field_name' => $field_name,
    ));
    $this
      ->assertFieldByXPath($xpath, $value, $message);
  }
  protected function assertCCKFieldTimezone($field_name, $value) {
    $xpath = '//select[@name="field_' . $field_name . '[0][timezone][timezone]"]';
    $message = t('Found form %field_name timezone with the expected value.', array(
      '%field_name' => $field_name,
    ));
    $this
      ->assertFieldByXPath($xpath, $value, $message);
  }

  /**
   * Return the form field name for a given CCK field. Special handling for date
   * fields.
   */
  protected function getFormFieldsNames($field_name, $index) {
    if (in_array($field_name, array(
      'date',
      'datetime',
      'datestamp',
      'datefield',
      'datesite',
    ))) {
      return array(
        "field_{$field_name}[{$index}][value][date]",
      );
    }
    else {
      return parent::getFormFieldsNames($field_name, $index);
    }
  }

}
class ParserIcalFeedsDateTestCase extends ParserIcalFeedsTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Basic feeds integration'),
      'description' => t('Test Feeds iCal parser support and date mapping. <strong>Requires Feeds and Date module.</strong>'),
      'group' => t('iCal Parser'),
    );
  }

  /**
   * Basic test using Basic.ics
   */
  public function testBasicFeed() {
    $typename = $this
      ->createConfiguredFeed();

    // Import iCal file.
    $this
      ->importFile('ical', $this
      ->absolutePath() . '/tests/feeds/Basic.ics');
    $this
      ->assertText('Created 7 ' . $typename . ' nodes.');

    // Check the imported nodes.
    // Timezones
    $data = array(
      array(
        'title' => 'Simple start and end',
        'field' => '2009-01-31 19:30',
        'site' => '2009-01-31 19:30',
        'field2' => '2009-01-31 20:30',
        'site2' => '2009-01-31 20:30',
        'timezone' => 'Pacific/Apia',
      ),
      array(
        'title' => 'Different start and end',
        'field' => '2009-01-01 19:00',
        'site' => '2009-01-01 07:00',
        'field2' => '2009-01-01 20:00',
        'site2' => '2009-01-01 08:00',
        'timezone' => 'Europe/Paris',
      ),
      array(
        'title' => 'No timezone this time',
        'field' => '2009-09-01 19:00',
        'site' => '2009-09-01 19:00',
        'field2' => '2009-09-01 20:00',
        'site2' => '2009-09-01 20:00',
        'timezone' => 'Pacific/Apia',
      ),
      array(
        'title' => 'UTC demarked with Z, over midnight.',
        'field' => '2009-09-30 19:00',
        'site' => '2009-09-30 08:00',
        'field2' => '2009-10-01 20:00',
        'site2' => '2009-10-01 09:00',
        'timezone' => 'UTC',
      ),
    );
    foreach ($data as $d) {
      $n = node_load(array(
        'title' => $d['title'],
      ), NULL, TRUE);
      $this
        ->drupalGet('node/' . $n->nid . '/edit');
      $this
        ->pass('Checking : ' . $d['title']);
      $this
        ->assertCCKFieldValue('datefield', $d['field']);
      $this
        ->assertCCKFieldValue('datesite', $d['site']);
      $this
        ->assertCCKFieldValue2('datefield', $d['field2']);
      $this
        ->assertCCKFieldValue2('datesite', $d['site2']);
      $this
        ->assertCCKFieldTimezone('datefield', $d['timezone']);
    }

    // test if it refreshes items correctly
    $this
      ->importFile('ical', $this
      ->absolutePath() . '/tests/feeds/Basic1.ics');
    $this
      ->assertText('Updated 1 ' . $typename . ' node.');
  }

}
class ParserIcalFeedsLocationTestCase extends ParserIcalFeedsTestCase {

  /**
   * Set up the test.
   */
  public function setUp() {
    parent::setup('text');
  }

  // Call parent setup with the required module.
  public static function getInfo() {
    return array(
      'name' => t('Location'),
      'description' => t('Location parsing and mapping. <strong>Requires Feeds, Date, CCK Text (and in the future geo) module.</strong>'),
      'group' => t('iCal Parser'),
    );
  }
  public function createConfiguredFeed() {
    $typename = parent::createConfiguredFeed(array(
      'location' => array(
        'type' => 'text',
      ),
    ));
    $this
      ->addMappings('ical', array(
      array(
        'source' => 'ical_location',
        'target' => 'field_location',
      ),
    ));
    return $typename;
  }

  /**
   * Basic test using Location.ics
   */
  public function testLocationFeed() {
    $typename = $this
      ->createConfiguredFeed();

    // Import iCal file.
    $this
      ->importFile('ical', $this
      ->absolutePath() . '/tests/feeds/Location.ics');
    $this
      ->assertText('Created 3 ' . $typename . ' nodes.');

    // Check the imported nodes.
    // Timezones
    $data = array(
      array(
        'title' => 'Basic location',
        'location' => 'Text location',
      ),
    );
    foreach ($data as $d) {
      $n = node_load(array(
        'title' => $d['title'],
      ), NULL, TRUE);
      $this
        ->drupalGet('node/' . $n->nid . '/edit');
      $this
        ->assertCCKFieldValue('location', $d['location']);
    }
  }

}

Classes

Namesort descending Description
ParserIcalFeedsDateTestCase
ParserIcalFeedsLocationTestCase
ParserIcalFeedsTestCase Class for testing Feeds <em>content</em> mapper.