You are here

public function FeedsMapperDateTestCase::testClearOutValues in Feeds 7.2

Tests if values are cleared out when an empty value is provided.

File

tests/feeds_mapper_date.test, line 269
Contains FeedsMapperDateTestCase.

Class

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

Code

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');
}