diff --git a/tests/feeds/content_date.csv b/tests/feeds/content_date.csv new file mode 100644 index 0000000..68a59cf --- /dev/null +++ b/tests/feeds/content_date.csv @@ -0,0 +1,3 @@ +"guid","title","created","end" +1,"Lorem ipsum",1251936720,1351936720 +2,"Ut wisi enim ad minim veniam",1251932360,1351932360 diff --git a/tests/feeds/content_empty.csv b/tests/feeds/content_empty.csv new file mode 100644 index 0000000..fac66a0 --- /dev/null +++ b/tests/feeds/content_empty.csv @@ -0,0 +1,3 @@ +"guid","title","created","end","alpha","beta","gamma","delta","body" +1,"Lorem ipsum",,,,,,, +2,"Ut wisi enim ad minim veniam",0,0,"0",0,0,0,0 diff --git a/tests/feeds/content_non_existent.csv b/tests/feeds/content_non_existent.csv new file mode 100644 index 0000000..a614ac3 --- /dev/null +++ b/tests/feeds/content_non_existent.csv @@ -0,0 +1,3 @@ +"guid","title" +1,"Lorem ipsum" +2,"Ut wisi enim ad minim veniam" diff --git a/tests/feeds_mapper.test b/tests/feeds_mapper.test index 492af89..f9951a1 100644 --- a/tests/feeds_mapper.test +++ b/tests/feeds_mapper.test @@ -56,6 +56,29 @@ class FeedsMapperTestCase extends FeedsWebTestCase { } /** + * Assert that a form field for the given field with the given value + * does not exist in the current form. + * + * @param $field_name + * The name of the field. + * @param $value + * The (raw) value of the field. + * @param $index + * The index of the field (for q multi-valued field). + * + * @see FeedsMapperTestCase::getFormFieldsNames() + * @see FeedsMapperTestCase::getFormFieldsValues() + */ + protected function assertNoNodeFieldValue($field_name, $value, $index = 0) { + $names = $this->getFormFieldsNames($field_name, $index); + $values = $this->getFormFieldsValues($field_name, $value); + foreach ($names as $k => $name) { + $value = $values[$k]; + $this->assertNoFieldByName($name, $value, t('Did not find form field %name for %field_name with the value %value.', array('%name' => $name, '%field_name' => $field_name, '%value' => $value))); + } + } + + /** * Returns the form fields names for a given CCK field. Default implementation * provides support for a single form field with the following name pattern * "field_{$field_name}[{$index}][value]" diff --git a/tests/feeds_mapper_date.test b/tests/feeds_mapper_date.test index c3cb067..eb72e95 100644 --- a/tests/feeds_mapper_date.test +++ b/tests/feeds_mapper_date.test @@ -118,4 +118,152 @@ class FeedsMapperDateTestCase extends FeedsMapperTestCase { return parent::getFormFieldsNames($field_name, $index); } } + + /** + * Tests if values are cleared out when an empty value or no 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, + ); + $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 2 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->assertNodeFieldValue('date', $date_values[$i]['created']); + $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNodeFieldValue('datestamp', $date_values[$i]['created']); + $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNodeFieldValue('datetime', $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->assertNoNodeFieldValue('date', $date_values[$i]['created']); + $this->assertNoFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNoNodeFieldValue('datestamp', $date_values[$i]['created']); + $this->assertNoFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNoNodeFieldValue('datetime', $date_values[$i]['created']); + $this->assertNoFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']); + $this->drupalGet("node/$i"); + $this->assertNoText('date_label'); + $this->assertNoText('datestamp_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->assertNodeFieldValue('date', $date_values[$i]['created']); + $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNodeFieldValue('datestamp', $date_values[$i]['created']); + $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']); + $this->assertNodeFieldValue('datetime', $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->assertNoNodeFieldValue('date', $date_values[1]['created']); + $this->assertNoFieldByName('field_date[und][0][value2][date]', $date_values[1]['end']); + $this->assertNoNodeFieldValue('datestamp', $date_values[1]['created']); + $this->assertNoFieldByName('field_datestamp[und][0][value2][date]', $date_values[1]['end']); + $this->assertNoNodeFieldValue('datetime', $date_values[1]['created']); + $this->assertNoFieldByName('field_datetime[und][0][value2][date]', $date_values[1]['end']); + // 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'); + } } diff --git a/tests/feeds_mapper_field.test b/tests/feeds_mapper_field.test index 2506a29..ae1d9ef 100644 --- a/tests/feeds_mapper_field.test +++ b/tests/feeds_mapper_field.test @@ -87,4 +87,146 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase { $this->assertNodeFieldValue('gamma', '1.20'); $this->assertNodeFieldValue('delta', '5.62951'); } + + /** + * Tests if values are cleared out when an empty value or no value + * is provided. + */ + public function testClearOutValues() { + // Create content type. + $typename = $this->createContentType(array(), array( + 'alpha' => 'text', + 'beta' => 'number_integer', + 'gamma' => 'number_decimal', + 'delta' => 'number_float', + )); + + // 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( + array( + 'source' => 'guid', + 'target' => 'guid', + 'unique' => TRUE, + ), + array( + 'source' => 'title', + 'target' => 'title', + ), + array( + 'source' => 'created', + 'target' => 'created', + ), + array( + 'source' => 'body', + 'target' => 'body', + ), + array( + 'source' => 'alpha', + 'target' => 'field_alpha', + ), + array( + 'source' => 'beta', + 'target' => 'field_beta', + ), + array( + 'source' => 'gamma', + 'target' => 'field_gamma', + ), + array( + 'source' => 'delta', + 'target' => 'field_delta', + ), + )); + + // Import CSV file. + $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Created 2 nodes'); + + // Check the two imported nodes. + $this->drupalGet('node/1/edit'); + $this->assertNodeFieldValue('alpha', 'Lorem'); + $this->assertNodeFieldValue('beta', '42'); + $this->assertNodeFieldValue('gamma', '4.20'); + $this->assertNodeFieldValue('delta', '3.14159'); + $this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'); + $this->drupalGet('node/2/edit'); + $this->assertNodeFieldValue('alpha', 'Ut wisi'); + $this->assertNodeFieldValue('beta', '32'); + $this->assertNodeFieldValue('gamma', '1.20'); + $this->assertNodeFieldValue('delta', '5.62951'); + $this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.'); + + // 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 node 1. + $this->drupalGet('node/1/edit'); + $this->assertNoNodeFieldValue('alpha', 'Lorem'); + $this->assertNoNodeFieldValue('beta', '42'); + $this->assertNoNodeFieldValue('gamma', '4.20'); + $this->assertNoNodeFieldValue('delta', '3.14159'); + $this->assertFieldByName('body[und][0][value]', ''); + // Check if labels for fields that should be cleared out are not shown. + $this->drupalGet('node/1'); + $this->assertNoText('alpha_text_label'); + $this->assertNoText('beta_number_integer_label'); + $this->assertNoText('gamma_number_decimal_label'); + $this->assertNoText('delta_number_float_label'); + + // Check if zero's didn't cleared out values for node 2. + $this->drupalGet('node/2/edit'); + $this->assertNodeFieldValue('alpha', 0); + $this->assertNodeFieldValue('beta', 0); + $this->assertNodeFieldValue('gamma', 0); + $this->assertNodeFieldValue('delta', 0); + $this->assertFieldByName('body[und][0][value]', 0); + // Check if labels for fields of node 2 are still shown. + $this->drupalGet('node/2'); + $this->assertText('alpha_text_label'); + $this->assertText('beta_number_integer_label'); + $this->assertText('gamma_number_decimal_label'); + $this->assertText('delta_number_float_label'); + + // Re-import the first file again. + $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Updated 2 nodes'); + + // Check if the two imported nodes have content again. + $this->drupalGet('node/1/edit'); + $this->assertNodeFieldValue('alpha', 'Lorem'); + $this->assertNodeFieldValue('beta', '42'); + $this->assertNodeFieldValue('gamma', '4.20'); + $this->assertNodeFieldValue('delta', '3.14159'); + $this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'); + $this->drupalGet('node/2/edit'); + $this->assertNodeFieldValue('alpha', 'Ut wisi'); + $this->assertNodeFieldValue('beta', '32'); + $this->assertNodeFieldValue('gamma', '1.20'); + $this->assertNodeFieldValue('delta', '5.62951'); + $this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.'); + + // 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->assertNoNodeFieldValue('alpha', 'Lorem'); + $this->assertNoNodeFieldValue('beta', '42'); + $this->assertNoNodeFieldValue('gamma', '4.20'); + $this->assertNoNodeFieldValue('delta', '3.14159'); + $this->assertFieldByName('body[und][0][value]', ''); + // Check if labels for fields that should be cleared out are not shown. + $this->drupalGet('node/1'); + $this->assertNoText('alpha_text_label'); + $this->assertNoText('beta_number_integer_label'); + $this->assertNoText('gamma_number_decimal_label'); + $this->assertNoText('delta_number_float_label'); + } } diff --git a/tests/feeds_mapper_taxonomy.test b/tests/feeds_mapper_taxonomy.test index 2951b58..bc9e5d1 100644 --- a/tests/feeds_mapper_taxonomy.test +++ b/tests/feeds_mapper_taxonomy.test @@ -345,6 +345,95 @@ class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase { } /** + * Tests if values are cleared out when an empty value or no value + * is provided. + */ + public function testClearOutValues() { + // Create a CSV importer configuration. + $this->createImporterConfiguration('Node import from CSV', 'node'); + $this->setSettings('node', NULL, array( + 'content_type' => '', + )); + $this->setPlugin('node', 'FeedsFileFetcher'); + $this->setPlugin('node', 'FeedsCSVParser'); + $this->setSettings('node', 'FeedsNodeProcessor', array( + 'bundle' => 'article', + 'update_existing' => 1, + )); + $this->addMappings('node', array( + 0 => array( + 'source' => 'title', + 'target' => 'title', + ), + 1 => array( + 'source' => 'alpha', + 'target' => 'field_tags', + 'term_search' => 0, + 'autocreate' => 1, + ), + 2 => array( + 'source' => 'guid', + 'target' => 'guid', + 'unique' => TRUE, + ), + )); + + $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Created 2 nodes'); + + // Check the imported nodes. + $terms1 = taxonomy_get_term_by_name('Lorem'); + $term1 = reset($terms1); + $terms2 = taxonomy_get_term_by_name('Ut wisi'); + $term2 = reset($terms2); + $taxonomy_values = array( + 1 => $term1->tid, + 2 => $term2->tid, + ); + for ($i = 1; $i <= 2; $i++) { + $this->drupalGet("node/$i/edit"); + $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]); + } + + // Import CSV file with empty values. + $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_empty.csv'); + $this->assertText('Updated 2 nodes'); + + // Check if the taxonomy reference field was cleared out for node 1. + $this->drupalGet('node/1/edit'); + $this->assertFieldByName('field_tags[und][]', '_none'); + $this->drupalGet('node/1'); + $this->assertNoText('field_tags'); + + // Check if zero's didn't cleared out the taxonomy reference field for + // node 2. + $terms0 = taxonomy_get_term_by_name('0'); + $term0 = reset($terms0); + $this->drupalGet('node/2/edit'); + $this->assertFieldByName('field_tags[und][]', $term0->tid); + $this->drupalGet('node/2'); + $this->assertText('field_tags'); + + // Re-import the first file again and check if the values returned. + $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Updated 2 nodes'); + for ($i = 1; $i <= 2; $i++) { + $this->drupalGet("node/$i/edit"); + $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]); + } + + // Import CSV file with non-existent values. + $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_non_existent.csv'); + $this->assertText('Updated 2 nodes'); + + // Check if the taxonomy reference field was cleared out for node 1. + $this->drupalGet('node/1/edit'); + $this->assertFieldByName('field_tags[und][]', '_none'); + $this->drupalGet('node/1'); + $this->assertNoText('field_tags'); + } + + /** * Finds node style taxonomy term markup in DOM. */ public function assertTaxonomyTerm($term) {