diff --git a/feeds.info b/feeds.info index ec2ffda..eb7387b 100644 --- a/feeds.info +++ b/feeds.info @@ -41,6 +41,7 @@ files[] = tests/feeds_mapper_profile.test files[] = tests/feeds_mapper.test files[] = tests/feeds_mapper_config.test files[] = tests/feeds_fetcher_file.test +files[] = tests/feeds_mapper_format_config.test files[] = tests/feeds_fetcher_http.test files[] = tests/feeds_processor_entity.test files[] = tests/feeds_processor_node.test diff --git a/mappers/text.inc b/mappers/text.inc index 029c82e..6fb50bf 100644 --- a/mappers/text.inc +++ b/mappers/text.inc @@ -40,15 +40,6 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam if (!empty($instance['settings']['text_processing'])) { $targets[$name]['summary_callback'] = 'text_feeds_summary_callback'; $targets[$name]['form_callback'] = 'text_feeds_form_callback'; - - $targets[$name . ':format'] = array( - 'name' => check_plain($instance['label']) . ': Format', - 'callback' => 'text_feeds_set_target', - 'description' => t('The @label field\'s format of the entity. This will overwrite the default or configured format fo the field.', array('@label' => $instance['label'])), - 'real_target' => $name, - 'summary_callback' => 'text_feeds_format_summary_callback', - 'form_callback' => 'text_feeds_format_form_callback', - ); } } } @@ -57,22 +48,15 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * Callback for mapping text fields. */ function text_feeds_set_target($source, $entity, $target, array $values, $mapping = array()) { - list($field_name, $column) = explode(':', $target . ':value'); - // Logic for backward compatibilty. if ($column === 'value' && isset($source->importer->processor->config['input_format'])) { - $default_format = $source->importer->processor->config['input_format']; + $format = $source->importer->processor->config['input_format']; + // Add in default values. + $mapping += array( + 'format' => $format, + ); } - else { - $default_format = filter_fallback_format(); - } - - // Add in default values. - $mapping += array( - 'input_format' => $default_format, - 'allowed_input_formats' => array($default_format), - ); $field = isset($entity->$field_name) ? $entity->$field_name : array('und' => array()); @@ -85,19 +69,11 @@ function text_feeds_set_target($source, $entity, $target, array $values, $mappin } if (is_scalar($value) && strlen($value)) { - // Only set format on value, other columns, such as summary and format - // will handled independently. - if ($column === 'value') { - $field['und'][$delta]['format'] = $mapping['input_format']; - } - if ($column === 'format') { - if (in_array($value, array_filter($mapping['allowed_input_formats']))) { - $field['und'][$delta]['format'] = (string) $value; - } - } - else { - $field['und'][$delta][$column] = (string) $value; + $field['und'][$delta][$column] = (string) $value; + + if (isset($mapping['format'])) { + $field['und'][$delta]['format'] = $mapping['format']; } } @@ -108,13 +84,12 @@ function text_feeds_set_target($source, $entity, $target, array $values, $mappin } /** - * Summary of input formats. + * Summary callback for text field targets. * - * @return - * Returns, as a string that may contain HTML, the summary to display while - * the full form isn't visible. - * If the return value is empty, no summary and no option to view the form - * will be displayed. + * Displays which text format will be used for the text field target. + * + * @see text_feeds_processor_targets_alter() + * @see text_feeds_form_callback() */ function text_feeds_summary_callback($mapping, $target, $form, $form_state) { global $user; @@ -124,21 +99,19 @@ function text_feeds_summary_callback($mapping, $target, $form, $form_state) { $importer = feeds_importer_load($form['#importer']); $default_format = isset($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); $mapping += array( - 'input_format' => $default_format, + 'format' => $default_format, ); - - return t('Input format: %format', array('%format' => $formats[$mapping['input_format']]->name)); + return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name)); } /** - * Select the input format to use for the text field. + * Form callback for text field targets. * - * @return - * The per mapping configuration form. Once the form is saved, $mapping will - * be populated with the form values. + * Allows to select a text format for the text field target. * - * @see my_module_summary_callback() + * @see text_feeds_processor_targets_alter() + * @see text_feeds_summary_callback() */ function text_feeds_form_callback($mapping, $target, $form, $form_state) { global $user; @@ -148,82 +121,18 @@ function text_feeds_form_callback($mapping, $target, $form, $form_state) { $formats_options[$id] = $format->name; } - // Processor-wide input format setting. + // Processor-wide text format setting. $importer = feeds_importer_load($form['#importer']); $default_format = isset($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); $mapping += array( - 'input_format' => $default_format, + 'format' => $default_format, ); - $config['input_format'] = array( + $config['format'] = array( '#type' => 'select', - '#title' => t('Input format'), - '#options' => $formats_options, - '#default_value' => $mapping['input_format'], - ); - - return $config; -} - -/** - * Summary of allowed input formats. - * - * @return - * Returns, as a string that may contain HTML, the summary to display while - * the full form isn't visible. - * If the return value is empty, no summary and no option to view the form - * will be displayed. - */ -function text_feeds_format_summary_callback($mapping, $target, $form, $form_state) { - global $user; - $formats = filter_formats($user); - - $importer = feeds_importer_load($form['#importer']); - $default_format = isset($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); - $mapping += array( - 'allowed_input_formats' => array($default_format), - ); - - $format_names = array(); - foreach (array_filter($mapping['allowed_input_formats']) as $format) { - $format_names[] = $formats[$format]->name; - } - - - return t('Allowed input formats: %formats', array('%formats' => implode(', ', $format_names))); -} - -/** - * Select the allowed input formats to use for the text field format mapping. - * - * @return - * The per mapping configuration form. Once the form is saved, $mapping will - * be populated with the form values. - * - * @see my_module_summary_callback() - */ -function text_feeds_format_form_callback($mapping, $target, $form, $form_state) { - global $user; - $formats_options = array(); - $formats = filter_formats($user); - foreach ($formats as $id => $format) { - $formats_options[$id] = $format->name; - } - - // Processor-wide input format setting. - $importer = feeds_importer_load($form['#importer']); - $default_format = isset($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format(); - $mapping += array( - 'allowed_input_formats' => array($default_format), - ); - - $config['allowed_input_formats'] = array( - '#type' => 'checkboxes', - '#title' => t('Allowed input formats'), + '#title' => t('Text format'), '#options' => $formats_options, - '#default_value' => $mapping['allowed_input_formats'], - '#required' => TRUE, - '#description' => t('Select the allowed input formats that will processed from the feed.
Any other filter will be ignored and the default will be used.') + '#default_value' => $mapping['format'], ); return $config; diff --git a/plugins/FeedsTermProcessor.inc b/plugins/FeedsTermProcessor.inc index 1801808..a4843b8 100644 --- a/plugins/FeedsTermProcessor.inc +++ b/plugins/FeedsTermProcessor.inc @@ -126,11 +126,15 @@ class FeedsTermProcessor extends FeedsProcessor { $target_term->weight = $weight; break; case 'description': - $default_format = isset($this->config['input_format']) ? $this->config['input_format'] : filter_fallback_format(); - $mapping += array( - 'input_format' => $default_format, - ); - $target_term->format = $mapping['input_format']; + if (isset($mapping['format'])) { + $target_term->format = $mapping['format']; + } + elseif (isset($this->config['input_format'])) { + $target_term->format = $this->config['input_format']; + } + else { + $target_term->format = filter_fallback_format(); + } default: parent::setTargetElement($source, $target_term, $target_element, $value); break; diff --git a/tests/feeds_mapper.test b/tests/feeds_mapper.test index 81665b6..492af89 100644 --- a/tests/feeds_mapper.test +++ b/tests/feeds_mapper.test @@ -22,10 +22,13 @@ class FeedsMapperTestCase extends FeedsWebTestCase { 'file' => 'file_generic', 'image' => 'image_image', 'link_field' => 'link_field', + 'list_text' => 'options_select', 'number_float' => 'number', 'number_integer' => 'number', 'nodereference' => 'nodereference_select', 'text' => 'text_textfield', + 'text_long' => 'text_textarea', + 'text_with_summary' => 'text_textarea_with_summary', 'userreference' => 'userreference_select', ); diff --git a/tests/feeds_mapper_format_config.test b/tests/feeds_mapper_format_config.test new file mode 100644 index 0000000..4630293 --- /dev/null +++ b/tests/feeds_mapper_format_config.test @@ -0,0 +1,244 @@ + 'Mapper: Text format mapping configuration', + 'description' => 'Test text format mapping configuration for text fields.', + 'group' => 'Feeds', + ); + } + + public function setUp() { + parent::setUp(array('list', 'taxonomy')); + } + + /** + * Basic test for setting mapping configuration. + */ + public function test() { + // Create content type with three fields. Two that support text formats + // and one that doesn't. + $typename = $this->createContentType(array(), array( + 'alpha' => array( + 'type' => 'text', + 'instance_settings' => array( + 'instance[settings][text_processing]' => 1, + ), + ), + 'beta' => array( + 'type' => 'text_long', + 'instance_settings' => array( + 'instance[settings][text_processing]' => 1, + ), + ), + 'gamma' => array( + 'type' => 'text', + 'instance_settings' => array( + 'instance[settings][text_processing]' => 0, + ), + ), + )); + + // Create a new filter format. + $format = drupal_strtolower($this->randomName()); + $edit = array( + 'format' => $format, + 'name' => $this->randomName(), + // Authenticated users. + 'roles[2]' => TRUE, + ); + $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration')); + + // Create and configure importer. + $this->createImporterConfiguration(); + $this->setSettings('syndication', NULL, array( + 'content_type' => '', + 'import_period' => FEEDS_SCHEDULE_NEVER, + )); + $this->setPlugin('syndication', 'FeedsFileFetcher'); + $this->setPlugin('syndication', 'FeedsCSVParser'); + $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename)); + $this->addMappings('syndication', array( + 0 => array( + 'source' => 'title', + 'target' => 'title', + ), + 1 => array( + 'source' => 'created', + 'target' => 'created', + ), + 2 => array( + 'source' => 'body', + 'target' => 'body', + 'format' => $format, + ), + 3 => array( + 'source' => 'alpha', + 'target' => 'field_alpha', + 'format' => $format, + ), + 4 => array( + 'source' => 'beta', + 'target' => 'field_beta', + 'format' => $format, + ), + 5 => array( + 'source' => 'gamma', + 'target' => 'field_gamma', + ), + )); + // Assert that for the gamma field no format can be chosen. + $this->assertNoText('Text format: Plain text'); + + // Import csv file. + $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Created 2 nodes'); + + // Assert that fields body, alpha and beta got the expected text format. + $node = node_load(1); + $this->assertEqual($format, $node->body[LANGUAGE_NONE][0]['format'], 'The body field got the expected format.'); + $this->assertEqual($format, $node->field_alpha[LANGUAGE_NONE][0]['format'], 'The alpha field got the expected format.'); + $this->assertEqual($format, $node->field_beta[LANGUAGE_NONE][0]['format'], 'The beta field got the expected format.'); + } + + /** + * Tests if an user with limited privileges can only choose the text format + * it has access to. + */ + public function testWithLimitedPrivileges() { + // Create content type with a field that uses a text format. + $typename = $this->createContentType(array(), array( + 'alpha' => array( + 'type' => 'text', + 'instance_settings' => array( + 'instance[settings][text_processing]' => 1, + ), + ), + )); + + // Create a new user with limited privileges. + $account = $this->drupalCreateUser(array('administer feeds')); + + // Create filter format the user may use. + $format1 = drupal_strtolower($this->randomName()); + $edit1 = array( + 'format' => $format1, + 'name' => $this->randomName(), + // Authenticated users. + 'roles[2]' => TRUE, + ); + $this->drupalPost('admin/config/content/formats/add', $edit1, t('Save configuration')); + + // Create filter format the user may NOT use. + $rid = $this->drupalCreateRole(array()); + $format2 = drupal_strtolower($this->randomName()); + $edit2 = array( + 'format' => $format2, + 'name' => $this->randomName(), + 'roles[' . $rid . ']' => TRUE, + ); + $this->drupalPost('admin/config/content/formats/add', $edit2, t('Save configuration')); + + // Login as the user that may only use certain formats. + $this->drupalLogin($account); + + // Create importer and ensure the user can use the first format. + $this->createImporterConfiguration(); + $this->setSettings('syndication', NULL, array( + 'content_type' => '', + 'import_period' => FEEDS_SCHEDULE_NEVER, + )); + $this->setPlugin('syndication', 'FeedsFileFetcher'); + $this->setPlugin('syndication', 'FeedsCSVParser'); + $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename)); + $this->addMappings('syndication', array( + 0 => array( + 'source' => 'alpha', + 'target' => 'field_alpha', + 'format' => $format1, + ), + )); + + // Check user can choose first, but not second format as an option. + $this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_0'); + $xpath = $this->constructFieldXpath('name', 'config[0][settings][format]'); + $fields = $this->xpath($xpath); + $field = reset($fields); + $format_options = $this->getAllOptions($field); + $format1_found = FALSE; + $format2_found = FALSE; + foreach ($format_options as $option) { + if ($option['value'] == $format1) { + $format1_found = TRUE; + } + if ($option['value'] == $format2) { + $format2_found = TRUE; + } + } + // Assert first format can be chosen. + $this->assertTrue($format1_found, format_string('Text format %format can be chosen.', array('%format' => $format1))); + // Assert second format can NOT be chosen. + $this->assertFalse($format2_found, format_string('Text format %format can NOT be chosen.', array('%format' => $format2))); + } + + /** + * Tests if text format can be set for taxonomy descriptions. + */ + public function testTaxonomyDescriptionTextFormat() { + // Create a vocabulary. + $edit = array( + 'name' => 'Tags', + 'machine_name' => 'tags', + ); + $this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save'); + + // Create a new filter format. + $format = drupal_strtolower($this->randomName()); + $edit = array( + 'format' => $format, + 'name' => $this->randomName(), + // Authenticated users. + 'roles[2]' => TRUE, + ); + $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration')); + + // Create a taxonomy term importer. + $this->createImporterConfiguration(); + $this->setSettings('syndication', NULL, array( + 'content_type' => '', + 'import_period' => FEEDS_SCHEDULE_NEVER, + )); + $this->setPlugin('syndication', 'FeedsFileFetcher'); + $this->setPlugin('syndication', 'FeedsCSVParser'); + $this->setPlugin('syndication', 'FeedsTermProcessor'); + $this->setSettings('syndication', 'FeedsTermProcessor', array('bundle' => 'tags')); + $this->addMappings('syndication', array( + 0 => array( + 'source' => 'title', + 'target' => 'name', + ), + 1 => array( + 'source' => 'body', + 'target' => 'description', + 'format' => $format, + ), + )); + + // Import csv file. + $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/content.csv'); + $this->assertText('Created 2 terms'); + + // Assert that the term description got the expected text format. + $term = taxonomy_term_load(1); + $this->assertEqual($format, $term->format, 'The taxonomy term got the expected format.'); + } +}