You are here

feeds_mapper_date.test in Feeds 7.2

Same filename and directory in other branches
  1. 6 tests/feeds_mapper_date.test
  2. 7 tests/feeds_mapper_date.test

Contains FeedsMapperDateTestCase.

File

tests/feeds_mapper_date.test
View source
<?php

/**
 * @file
 * Contains FeedsMapperDateTestCase.
 */

/**
 * Test case for CCK date field mapper mappers/date.inc.
 *
 * @todo: Add test method iCal
 * @todo: Add test method for end date
 */
class FeedsMapperDateTestCase extends FeedsMapperTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Mapper: Date',
      'description' => 'Test Feeds Mapper support for CCK Date fields.',
      'group' => 'Feeds',
      'dependencies' => array(
        'date',
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'date_api',
      'date',
    ));
    variable_set('date_default_timezone', 'UTC');
  }

  /**
   * Basic test loading a single entry CSV file.
   */
  public function test() {
    $this
      ->drupalGet('admin/config/regional/settings');

    // Create content type.
    $typename = $this
      ->createContentType(array(), array(
      'date' => 'date',
      'datestamp' => 'datestamp',
      'datetime' => 'datetime',
    ));

    // Hack to get date fields to not round to every 15 minutes.
    foreach (array(
      'date',
      'datestamp',
      'datetime',
    ) as $field) {
      $field = 'field_' . $field;
      $edit = array(
        'widget_type' => 'date_select',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
      $edit = array(
        'instance[widget][settings][increment]' => 1,
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
      $edit = array(
        'widget_type' => 'date_text',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
    }

    // Create and configure importer.
    $this
      ->createImporterConfiguration('Date RSS', 'daterss');
    $this
      ->setSettings('daterss', NULL, array(
      'content_type' => '',
      'import_period' => FEEDS_SCHEDULE_NEVER,
    ));
    $this
      ->setPlugin('daterss', 'FeedsFileFetcher');
    $this
      ->setSettings('daterss', 'FeedsNodeProcessor', array(
      'bundle' => $typename,
    ));
    $this
      ->addMappings('daterss', array(
      0 => array(
        'source' => 'title',
        'target' => 'title',
      ),
      1 => array(
        'source' => 'description',
        'target' => 'body',
      ),
      2 => array(
        'source' => 'timestamp',
        'target' => 'field_date:start',
      ),
      3 => array(
        'source' => 'timestamp',
        'target' => 'field_datestamp:start',
      ),
      4 => array(
        'source' => 'timestamp',
        'target' => 'field_datetime:start',
      ),
    ));
    $edit = array(
      'allowed_extensions' => 'rss2',
    );
    $this
      ->drupalPost('admin/structure/feeds/daterss/settings/FeedsFileFetcher', $edit, 'Save');

    // Import CSV file.
    $this
      ->importFile('daterss', $this
      ->absolutePath() . '/tests/feeds/googlenewstz.rss2');
    $this
      ->assertText('Created 6 nodes');

    // Check the imported nodes.
    $values = array(
      '01/06/2010 - 19:26',
      '01/06/2010 - 10:21',
      '01/06/2010 - 13:42',
      '01/06/2010 - 06:05',
      '01/06/2010 - 11:26',
      '01/07/2010 - 00:26',
    );
    for ($i = 1; $i <= 6; $i++) {
      $this
        ->drupalGet("node/{$i}/edit");
      $this
        ->assertNodeFieldValue('date', $values[$i - 1]);
      $this
        ->assertNodeFieldValue('datestamp', $values[$i - 1]);
      $this
        ->assertNodeFieldValue('datetime', $values[$i - 1]);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getFormFieldsNames($field_name, $index) {
    if (in_array($field_name, array(
      'date',
      'datetime',
      'datestamp',
    ))) {
      return array(
        "field_{$field_name}[und][{$index}][value][date]",
      );
    }
    else {
      return parent::getFormFieldsNames($field_name, $index);
    }
  }

  /**
   * Tests importing dates using the timezone mapping option.
   */
  public function testTimezoneMappingOption() {

    // Create content type.
    $typename = $this
      ->createContentType(array(), array(
      'date' => 'date',
      'datestamp' => 'datestamp',
      'datetime' => 'datetime',
    ));

    // Hack to get date fields to not round to every 15 minutes.
    foreach (array(
      'date',
      'datestamp',
      'datetime',
    ) as $field) {
      $field = 'field_' . $field;
      $edit = array(
        'widget_type' => 'date_select',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
      $edit = array(
        'instance[widget][settings][increment]' => 1,
        'field[settings][enddate_get]' => 1,
        'instance[settings][default_value]' => 'blank',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
      $edit = array(
        'widget_type' => 'date_text',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
    }

    // Create and configure importer.
    $this
      ->createImporterConfiguration('Content CSV', 'csv');
    $this
      ->setSettings('csv', NULL, array(
      'content_type' => '',
      'import_period' => FEEDS_SCHEDULE_NEVER,
    ));
    $this
      ->setPlugin('csv', 'FeedsFileFetcher');
    $this
      ->setPlugin('csv', 'FeedsCSVParser');
    $this
      ->setSettings('csv', 'FeedsNodeProcessor', array(
      'bundle' => $typename,
    ));
    $this
      ->addMappings('csv', array(
      0 => array(
        'source' => 'title',
        'target' => 'title',
        'unique' => TRUE,
      ),
      // Los Angeles == UTC-08:00.
      1 => array(
        'source' => 'datetime_start',
        'target' => 'field_date:start',
        'timezone' => 'America/Los_Angeles',
      ),
      2 => array(
        'source' => 'datetime_end',
        'target' => 'field_date:end',
        'timezone' => 'America/Los_Angeles',
      ),
      // Amsterdam == UTC+01:00.
      3 => array(
        'source' => 'datetime_start',
        'target' => 'field_datestamp:start',
        'timezone' => 'Europe/Amsterdam',
      ),
      4 => array(
        'source' => 'datetime_end',
        'target' => 'field_datestamp:end',
        'timezone' => 'Europe/Amsterdam',
      ),
      // Sydney == UTC+10:00.
      5 => array(
        'source' => 'datetime_start',
        'target' => 'field_datetime:start',
        'timezone' => 'Australia/Sydney',
      ),
      6 => array(
        'source' => 'datetime_end',
        'target' => 'field_datetime:end',
        'timezone' => 'Australia/Sydney',
      ),
    ));

    // Import CSV file.
    $this
      ->importFile('csv', $this
      ->absolutePath() . '/tests/feeds/content_date.csv');
    $this
      ->assertText('Created 3 nodes');

    // Check the imported nodes.
    $date_values = array(
      // Wintertime.
      // (Hear me calling).
      1 => array(
        'field_date_start' => '11/05/1955 - 20:00',
        'field_date_end' => '11/05/1955 - 23:00',
        'field_datestamp_start' => '11/05/1955 - 11:00',
        'field_datestamp_end' => '11/05/1955 - 14:00',
        'field_datetime_start' => '11/05/1955 - 02:00',
        'field_datetime_end' => '11/05/1955 - 05:00',
      ),
      // Summertime =+0100.
      // (Dee dee dee).
      2 => array(
        'field_date_start' => '10/22/2015 - 06:29',
        'field_date_end' => '10/22/2015 - 09:29',
        'field_datestamp_start' => '10/21/2015 - 21:29',
        'field_datestamp_end' => '10/22/2015 - 00:29',
        'field_datetime_start' => '10/21/2015 - 12:29',
        'field_datetime_end' => '10/21/2015 - 15:29',
      ),
      // Timezone is specified in string, all UTC time.
      3 => array(
        'field_date_start' => '02/09/2018 - 00:00',
        'field_date_end' => '02/10/2018 - 22:00',
        'field_datestamp_start' => '02/09/2018 - 00:00',
        'field_datestamp_end' => '02/10/2018 - 22:00',
        'field_datetime_start' => '02/09/2018 - 00:00',
        'field_datetime_end' => '02/10/2018 - 22:00',
      ),
    );
    for ($i = 1; $i <= 3; $i++) {
      $this
        ->drupalGet("node/{$i}/edit");
      $this
        ->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['field_date_start']);
      $this
        ->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['field_date_end']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['field_datestamp_start']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['field_datestamp_end']);
      $this
        ->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['field_datetime_start']);
      $this
        ->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['field_datetime_end']);
    }
  }

  /**
   * Tests if values are cleared out when an empty value is provided.
   */
  public function testClearOutValues() {

    // Create content type.
    $typename = $this
      ->createContentType(array(), array(
      'date' => 'date',
      'datestamp' => 'datestamp',
      'datetime' => 'datetime',
    ));

    // Hack to get date fields to not round to every 15 minutes.
    foreach (array(
      'date',
      'datestamp',
      'datetime',
    ) as $field) {
      $field = 'field_' . $field;
      $edit = array(
        'widget_type' => 'date_select',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
      $edit = array(
        'instance[widget][settings][increment]' => 1,
        'field[settings][enddate_get]' => 1,
        'instance[settings][default_value]' => 'blank',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
      $edit = array(
        'widget_type' => 'date_text',
      );
      $this
        ->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
    }

    // Create and configure importer.
    $this
      ->createImporterConfiguration('Content CSV', 'csv');
    $this
      ->setSettings('csv', NULL, array(
      'content_type' => '',
      'import_period' => FEEDS_SCHEDULE_NEVER,
    ));
    $this
      ->setPlugin('csv', 'FeedsFileFetcher');
    $this
      ->setPlugin('csv', 'FeedsCSVParser');
    $this
      ->setSettings('csv', 'FeedsNodeProcessor', array(
      'bundle' => $typename,
      'update_existing' => 1,
    ));
    $this
      ->addMappings('csv', array(
      0 => array(
        'source' => 'title',
        'target' => 'title',
        'unique' => TRUE,
      ),
      1 => array(
        'source' => 'created',
        'target' => 'field_date:start',
      ),
      2 => array(
        'source' => 'end',
        'target' => 'field_date:end',
      ),
      3 => array(
        'source' => 'created',
        'target' => 'field_datestamp:start',
      ),
      4 => array(
        'source' => 'end',
        'target' => 'field_datestamp:end',
      ),
      5 => array(
        'source' => 'created',
        'target' => 'field_datetime:start',
      ),
      6 => array(
        'source' => 'end',
        'target' => 'field_datetime:end',
      ),
    ));

    // Import CSV file.
    $this
      ->importFile('csv', $this
      ->absolutePath() . '/tests/feeds/content_date.csv');
    $this
      ->assertText('Created 3 nodes');

    // Check the imported nodes.
    $date_values = array(
      1 => array(
        'created' => '09/03/2009 - 00:12',
        'end' => '11/03/2012 - 09:58',
      ),
      2 => array(
        'created' => '09/02/2009 - 22:59',
        'end' => '11/03/2012 - 08:46',
      ),
    );
    for ($i = 1; $i <= 2; $i++) {
      $this
        ->drupalGet("node/{$i}/edit");
      $this
        ->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
      $this
        ->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
    }

    // Import CSV file with empty values.
    $this
      ->importFile('csv', $this
      ->absolutePath() . '/tests/feeds/content_empty.csv');
    $this
      ->assertText('Updated 2 nodes');

    // Check if all values were cleared out for both nodes.
    for ($i = 1; $i <= 2; $i++) {
      $this
        ->drupalGet("node/{$i}/edit");
      $this
        ->assertFieldByName('field_date[und][0][value][date]', '');
      $this
        ->assertFieldByName('field_date[und][0][value2][date]', '');
      $this
        ->assertFieldByName('field_datestamp[und][0][value][date]', '');
      $this
        ->assertFieldByName('field_datestamp[und][0][value2][date]', '');
      $this
        ->assertFieldByName('field_datetime[und][0][value][date]', '');
      $this
        ->assertFieldByName('field_datetime[und][0][value2][date]', '');
      $this
        ->drupalGet("node/{$i}");
      $this
        ->assertNoText('date_label');
      $this
        ->assertNoText('datestamp_label');
      $this
        ->assertNoText('datetime_label');
    }

    // Re-import the first file again and check if the values returned.
    $this
      ->importFile('csv', $this
      ->absolutePath() . '/tests/feeds/content_date.csv');
    $this
      ->assertText('Updated 2 nodes');
    for ($i = 1; $i <= 2; $i++) {
      $this
        ->drupalGet("node/{$i}/edit");
      $this
        ->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
      $this
        ->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
      $this
        ->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
    }

    // Import CSV file with non-existent values.
    $this
      ->importFile('csv', $this
      ->absolutePath() . '/tests/feeds/content_non_existent.csv');
    $this
      ->assertText('Updated 2 nodes');

    // Check if all values were cleared out for node 1.
    $this
      ->drupalGet('node/1/edit');
    $this
      ->assertFieldByName('field_date[und][0][value2][date]', '');
    $this
      ->assertFieldByName('field_date[und][0][value2][date]', '');
    $this
      ->assertFieldByName('field_datestamp[und][0][value2][date]', '');
    $this
      ->assertFieldByName('field_datestamp[und][0][value2][date]', '');
    $this
      ->assertFieldByName('field_datetime[und][0][value2][date]', '');
    $this
      ->assertFieldByName('field_datetime[und][0][value2][date]', '');

    // Check if labels for fields that should be cleared out are not shown.
    $this
      ->drupalGet('node/1');
    $this
      ->assertNoText('date_label');
    $this
      ->assertNoText('datestamp_label');
    $this
      ->assertNoText('datetime_label');
  }

}

Classes

Namesort descending Description
FeedsMapperDateTestCase Test case for CCK date field mapper mappers/date.inc.