public function testMakeCollection() { Yii::app()->user->userModel = User::getByUsername('super'); $collection = AttributeImportRulesFactory::makeCollection('ImportModelTestItem', array()); $this->assertEquals(array(), $collection); $collection = AttributeImportRulesFactory::makeCollection('ImportModelTestItem', array('string', 'boolean', 'date')); $this->assertEquals(3, count($collection)); $this->assertTrue($collection['string'] instanceof TextAttributeImportRules); $this->assertTrue($collection['boolean'] instanceof CheckBoxAttributeImportRules); $this->assertTrue($collection['date'] instanceof DateAttributeImportRules); }
/** * Based on the column type, filter down (sanitize) the array of $mappableAttributeIndicesAndDerivedTypes * based on which attribute indices and derived types are available. * @param array $mappableAttributeIndicesAndDerivedTypes * @param string $columnType * @return a sanitized array of $mappableAttributeIndicesAndDerivedTypes for the column type. */ protected static function resolveMappableAttributeIndicesAndDerivedTypesByColumnType($mappableAttributeIndicesAndDerivedTypes, $columnType, $importRulesType) { assert('is_array($mappableAttributeIndicesAndDerivedTypes)'); assert('$columnType == "importColumn" || $columnType == "extraColumn"'); assert('is_string($importRulesType)'); if ($columnType == 'importColumn') { return $mappableAttributeIndicesAndDerivedTypes; } $attributeImportRules = AttributeImportRulesFactory::makeCollection($importRulesType, array_keys($mappableAttributeIndicesAndDerivedTypes)); $sanitizedMappableAttributeIndicesAndDerivedTypes = array(); foreach ($mappableAttributeIndicesAndDerivedTypes as $attributeIndicesAndDerivedType => $label) { if ($attributeImportRules[$attributeIndicesAndDerivedType]->getExtraColumnUsableCountOfModelAttributeMappingRuleFormTypesAndElementTypes() > 0) { $sanitizedMappableAttributeIndicesAndDerivedTypes[$attributeIndicesAndDerivedType] = $label; } } return $sanitizedMappableAttributeIndicesAndDerivedTypes; }
/** * Validation used in the saveMappingData scenario to make sure the mapping data is correct based on * user input. Runs several different validations on the data. This does not validate the validity of the * mapping rules data itself. That is done seperately. * @see MappingRuleFormAndElementTypeUtil::validateMappingRuleForms * @param string $attribute * @param array $params */ public function validateMappingData($attribute, $params) { assert('$this->importRulesType != null'); assert('$this->mappingData != null'); $atLeastOneAttributeMappedOrHasRules = false; $attributeMappedOrHasRulesMoreThanOnce = false; $mappedAttributes = array(); $importRulesClassName = ImportRulesUtil::getImportRulesClassNameByType($this->importRulesType); foreach ($this->mappingData as $columnName => $data) { if ($data['attributeIndexOrDerivedType'] != null) { $atLeastOneAttributeMappedOrHasRules = true; if (in_array($data['attributeIndexOrDerivedType'], $mappedAttributes)) { $attributeMappedOrHasRulesMoreThanOnce = true; } else { $mappedAttributes[] = $data['attributeIndexOrDerivedType']; } } } if ($attributeMappedOrHasRulesMoreThanOnce) { $this->addError('mappingData', Zurmo::t('ImportModule', 'You can only map each field once.')); } if (!$atLeastOneAttributeMappedOrHasRules) { $this->addError('mappingData', Zurmo::t('ImportModule', 'You must map at least one of your import columns.')); } $mappedAttributeIndicesOrDerivedAttributeTypes = ImportMappingUtil::getMappedAttributeIndicesOrDerivedAttributeTypesByMappingData($this->mappingData); $requiredAttributeCollection = $importRulesClassName::getRequiredAttributesCollectionNotIncludingReadOnly(); $mappedAttributeImportRulesCollection = AttributeImportRulesFactory::makeCollection($this->importRulesType, $mappedAttributeIndicesOrDerivedAttributeTypes); if (!ImportRulesUtil::areAllRequiredAttributesMappedOrHaveRules($requiredAttributeCollection, $mappedAttributeImportRulesCollection)) { $attributesLabelContent = null; foreach ($requiredAttributeCollection as $noteUsed => $attributeData) { if ($attributesLabelContent != null) { $attributesLabelContent .= ', '; } $attributesLabelContent .= $attributeData['attributeLabel']; } $this->addError('mappingData', Zurmo::t('ImportModule', 'All required fields must be mapped or added: {attributesLabelContent}', array('{attributesLabelContent}' => $attributesLabelContent))); } try { ImportRulesUtil::checkIfAnyAttributesAreDoubleMapped($mappedAttributeImportRulesCollection); } catch (ImportAttributeMappedMoreThanOnceException $e) { $this->addError('mappingData', Zurmo::t('ImportModule', 'The following field is mapped more than once. {message}', array('{message}' => $e->getMessage()))); } }
public function testCheckIfAnyAttributesAreDoubleMappedWhenTheyAreDobuleMapped() { Yii::app()->user->userModel = User::getByUsername('super'); $mappedAttributeImportRulesCollection = AttributeImportRulesFactory::makeCollection('ImportModelTestItem', array('boolean', 'string', 'FullName')); ImportRulesUtil::checkIfAnyAttributesAreDoubleMapped($mappedAttributeImportRulesCollection); //Now it should fail, because lastName is mapped both as a non-derived and within FullName $mappedAttributeImportRulesCollection = AttributeImportRulesFactory::makeCollection('ImportModelTestItem', array('boolean', 'lastName', 'FullName')); try { ImportRulesUtil::checkIfAnyAttributesAreDoubleMapped($mappedAttributeImportRulesCollection); $this->fail(); } catch (ImportAttributeMappedMoreThanOnceException $e) { } //This should not fail because $mappedAttributeImportRulesCollection = AttributeImportRulesFactory::makeCollection('ImportModelTestItem', array('primaryEmail__emailAddress', 'secondaryEmail__emailAddress')); ImportRulesUtil::checkIfAnyAttributesAreDoubleMapped($mappedAttributeImportRulesCollection); }