Index: tests/feeds.test =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/tests/feeds.test,v retrieving revision 1.18 diff -u -p -r1.18 feeds.test --- tests/feeds.test 20 Jun 2010 16:46:12 -0000 1.18 +++ tests/feeds.test 28 Jun 2010 13:43:53 -0000 @@ -421,7 +421,9 @@ class FeedsRSStoDataTest extends FeedsWe 'unique' => FALSE, ), ); - $this->addMappings('rss', $mappings); + // $test_mappings is FALSE because FeedsDataProcessor overrides + // addMapping() so we have to test the mappings ourselves. + $this->addMappings('rss', $mappings, FALSE); // Verify the mapping configuration. $config = unserialize(db_result(db_query("SELECT config FROM {feeds_importer} WHERE id = 'rss'"))); Index: tests/feeds.test.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/tests/feeds.test.inc,v retrieving revision 1.11 diff -u -p -r1.11 feeds.test.inc --- tests/feeds.test.inc 27 Jun 2010 17:24:51 -0000 1.11 +++ tests/feeds.test.inc 28 Jun 2010 13:43:53 -0000 @@ -283,21 +283,57 @@ class FeedsWebTestCase extends DrupalWeb } /** + * Get an array of current mappings from the feeds_importer config. + */ + protected function getCurrentMappings($id) { + $config = db_result(db_query("SELECT config FROM {feeds_importer} WHERE id='%s'", $id)); + $config = unserialize($config); + return empty($config['processor']['config']['mappings']) ? array() : $config['processor']['config']['mappings']; + } + + /** + * Get an array of current mappings from the feeds_importer config. + * + * @param $id + * ID of the importer we are looking at + * + * @return + * -1 if the mapping doesn't exist, the key of the mapping otherwise. + */ + protected function mappingExists($id, $source, $target) { + $current_mappings = $this->getCurrentMappings($id); + foreach ($current_mappings as $key => $mapping) { + if ($mapping['source'] == $source && $mapping['target'] == $target) { + return $key; + } + } + return -1; + } + + /** * Add mappings to a given configuration. * * @param $mappings * An array of mapping arrays. Each mapping array must have a source and * an target key and can have a unique key. + * @param $test_mappings + * TRUE if you want this function to automatically test mapping configs, + * FALSE if you would prefer to not test them or test them yourself. * * @see FeedsRSStoDataTest class. */ - public function addMappings($id, $mappings) { + public function addMappings($id, $mappings, $test_mappings = TRUE) { $path = 'admin/build/feeds/edit/'. $id .'/mapping'; // Iterate through all mappings and add the via the form. foreach ($mappings as $i => $mapping) { + if ($test_mappings) { + $current_mapping_key = $this->mappingExists($id, $mapping['source'], $mapping['target']); + $this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.'); + } + // Get unique flag and unset it - otherwise drupalPost will complain that // there is no form element named "unique". $unique = !empty($mapping['unique']); @@ -311,6 +347,47 @@ class FeedsWebTestCase extends DrupalWeb ); $this->drupalPost($path, $edit, t('Save')); } + + if ($test_mappings) { + $current_mapping_key = $this->mappingExists($id, $mapping['source'], $mapping['target']); + $this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition'); + } + } + } + + /** + * Remove mappings from a given configuration. This function is mimicking + * the Javascript behavior in feeds_ui.js + * + * @param $mappings + * An array of mapping arrays. Each mapping array must have a source and + * an target key and can have a unique key. + * @param $test_mappings + * TRUE if you want this function to automatically test mapping configs, + * FALSE if you would prefer to not test them or test them yourself. + */ + public function removeMappings($id, $mappings, $test_mappings = TRUE) { + $path = 'admin/build/feeds/edit/'. $id .'/mapping'; + + $current_mappings = $this->getCurrentMappings($id); + + // Iterate through all mappings and add the via the form. + foreach ($mappings as $i => $mapping) { + $current_mapping_key = $this->mappingExists($id, $mapping['source'], $mapping['target']); + if ($test_mappings) { + $this->assertTrue($current_mapping_key >= 0, 'Mapping exists before removal'); + } + + $remove_mapping = array( + "remove_flags[{$current_mapping_key}]" => 1, + ); + $this->drupalPost($path, $remove_mapping, t('Save')); + $this->assertText('Your changes have been saved.'); + + if ($test_mappings) { + $current_mapping_key = $this->mappingExists($id, $mapping['source'], $mapping['target']); + $this->assertEqual($current_mapping_key, -1, 'Mapping does not exist after removal'); + } } } }