Skip to content
Snippets Groups Projects
Commit 2dc64777 authored by megachriz's avatar megachriz Committed by MegaChriz
Browse files

Issue #2918840 by MegaChriz, Toki, dat deaf drupaler, markusd1984, Jason Dean:...

Issue #2918840 by MegaChriz, Toki, dat deaf drupaler, markusd1984, Jason Dean: Only validate mapped fields.
parent 04978714
No related merge requests found
......@@ -179,7 +179,7 @@ abstract class FeedsProcessor extends FeedsPlugin {
// Perform field validation if entity is fieldable.
if (!empty($info['fieldable'])) {
try {
field_attach_validate($this->entityType(), $entity);
$this->validateFields($entity);
}
catch (FieldValidationException $e) {
$errors = array();
......@@ -211,6 +211,66 @@ abstract class FeedsProcessor extends FeedsPlugin {
}
}
/**
* Validates fields of an entity.
*
* This is mostly a copy of the field_attach_validate() function. The
* difference is that field_attach_validate() validates *all* fields on an
* entity, while Feeds only validates the fields where the user mapped to.
*
* @param object $entity
* The entity for which the field to validate.
*
* @throws FieldValidationException
* If validation errors are found, a FieldValidationException is thrown. The
* 'errors' property contains the array of errors, keyed by field name,
* language and delta.
*/
protected function validateFields($entity) {
$entity_type = $this->entityType();
// Get fields for the entity type we are mapping to.
$fields = field_info_instances($entity_type, $this->bundle());
// Get targets.
$targets = $this->getCachedTargets();
$errors = array();
$null = NULL;
// Validate all fields that we are mapping to.
foreach ($this->getMappings() as $mapping) {
// Get real target name.
if (isset($targets[$mapping['target']]['real_target'])) {
$target_name = $targets[$mapping['target']]['real_target'];
}
else {
$target_name = $mapping['target'];
}
if (isset($fields[$target_name])) {
// Validate this field.
_field_invoke_default('validate', $entity_type, $entity, $errors, $null, array(
'field_name' => $target_name,
));
_field_invoke('validate', $entity_type, $entity, $errors, $null, array(
'field_name' => $target_name,
));
}
}
// Let other modules validate the entity.
// Avoid module_invoke_all() to let $errors be taken by reference.
foreach (module_implements('field_attach_validate') as $module) {
$function = $module . '_field_attach_validate';
$function($entity_type, $entity, $errors);
}
if ($errors) {
throw new FieldValidationException($errors);
}
}
/**
* Helper function to unravel error messages hidden in a FieldValidationException.
*
......
......@@ -273,6 +273,49 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase {
$this->assertText('alpha_text_label: the text may not be longer than 5 characters.');
}
/**
* Tests integer field validation.
*/
public function testIntegerFieldValidation() {
// Create content type with a field with settings to validate.
$typename = $this->createContentType(array(), array(
'beta' => array(
'type' => 'number_integer',
'instance_settings' => array(
'instance[settings][min]' => 30,
'instance[settings][max]' => 40,
),
),
));
// Create and configure importer.
$this->createImporterConfiguration('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',
),
1 => array(
'source' => 'beta',
'target' => 'field_beta',
),
));
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Created 1 node');
$this->assertText('Failed importing 1 node.');
$this->assertText("Field validation errors in item 'Lorem ipsum'");
$this->assertText('beta_number_integer_label: the value may be no greater than 40.');
}
/**
* Tests text field validation for nodes without a title.
*
......@@ -479,4 +522,64 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase {
$this->assertText('alpha_text_label: this field cannot hold more than 6 values.');
}
/**
* Tests that Feeds ignores validation of fields not mapped to.
*/
public function testIgnoreUnmappedFieldsValidation() {
// Include FeedsProcessor.inc so processor related constants are available.
module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
// Create content type with a field with settings to validate.
$typename = $this->createContentType(array(), array(
'alpha' => 'text',
'beta' => array(
'type' => 'number_integer',
'instance_settings' => array(
'instance[settings][min]' => 30,
'instance[settings][max]' => 50,
),
),
));
// Create a node of this type with an out of range integer value.
$this->drupalCreateNode(array(
'type' => $typename,
'title' => 'Lorem ipsum',
'field_beta' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 25,
),
),
),
));
// Create and configure importer.
$this->createImporterConfiguration('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' => FEEDS_UPDATE_EXISTING,
));
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'title',
'unique' => TRUE,
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha',
),
));
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Updated 1 node');
$this->assertNoText('Field validation errors');
}
}
......@@ -316,6 +316,41 @@ class FeedsMapperLinkTestCase extends FeedsMapperTestCase {
$this->assertNoText($this->staticTitle, 'Static title link not found.');
}
/**
* Tests link field validation.
*/
public function testLinkFieldValidation() {
// Create and configure importer.
$this->createImporterConfiguration('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' => $this->contentType,
));
$this->addMappings('csv', array(
0 => array(
'source' => 'title',
'target' => 'title',
),
1 => array(
'source' => 'alpha',
'target' => 'field_alpha:title'
),
2 => array(
'source' => 'alpha',
'target' => 'field_alpha:url'
),
));
$this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
$this->assertText('Failed importing 2 nodes.');
$this->assertText("Field validation errors in item 'Lorem ipsum'");
$this->assertText('The value Lorem provided for alpha_link_field_label is not a valid URL.');
}
/**
* Override parent::getFormFieldsNames().
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment