public function testResolveLinkMessageToModel()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $account = AccountTestHelper::createAccountByNameForOwner('account', Yii::app()->user->userModel);
     $content = ImportUtil::resolveLinkMessageToModel($account);
     $this->assertContains('accounts/default/details?id', $content);
     $contact = ContactTestHelper::createContactByNameForOwner('contact', Yii::app()->user->userModel);
     $content = ImportUtil::resolveLinkMessageToModel($contact);
     $this->assertContains('contacts/default/details?id', $content);
     $lead = LeadTestHelper::createLeadByNameForOwner('lead', Yii::app()->user->userModel);
     $content = ImportUtil::resolveLinkMessageToModel($lead);
     $this->assertContains('leads/default/details?id', $content);
 }
 /**
  * Given a value that is either a zurmo id or an external system id, resolve that the
  * value is valid.  The value presented can also be a 'name' value.  If the name is not found as a model
  * in the system, then a new related model will be created using this name.
  * NOTE - If the related model has other required attributes that have no default values,
  * then there will be a problem saving this new model. This is too be resolved at some point.
  * If the value is not valid then an InvalidValueToSanitizeException is thrown.
  * @param mixed $value
  * @return sanitized value
  * @throws InvalidValueToSanitizeException
  * @throws NotFoundException
  * @throws NotSupportedException
  */
 public function sanitizeValue($value)
 {
     assert('is_string($this->attributeName) && $this->attributeName != "id"');
     if ($value == null) {
         return $value;
     }
     $modelClassName = $this->modelClassName;
     $relationModelClassName = $modelClassName::getRelationModelClassName($this->attributeName);
     if ($this->mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID) {
         try {
             if ((int) $value <= 0) {
                 throw new NotFoundException();
             }
             return $relationModelClassName::getById((int) $value);
         } catch (NotFoundException $e) {
             throw new InvalidValueToSanitizeException(Zurmo::t('ImportModule', 'ID specified did not match any existing records.'));
         }
     } elseif ($this->mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID) {
         try {
             return static::getModelByExternalSystemIdAndModelClassName($value, $relationModelClassName);
         } catch (NotFoundException $e) {
             throw new InvalidValueToSanitizeException(Zurmo::t('ImportModule', 'Other ID specified did not match any existing records.'));
         }
     } else {
         if (!method_exists($relationModelClassName, 'getByName')) {
             throw new NotSupportedException();
         }
         try {
             $modelsFound = $relationModelClassName::getByName($value);
             if (!is_array($modelsFound)) {
                 $modelsFound = array($modelsFound);
             }
         } catch (NotFoundException $e) {
             $modelsFound = array();
         }
         if (count($modelsFound) == 0) {
             $newRelatedModel = new $relationModelClassName();
             $newRelatedModel->name = $value;
             $saved = $newRelatedModel->save();
             //Todo: need to handle this more gracefully. The use case where a related model is needed to be made
             //but there are some required attributes that do not have defaults. As a result, since those extra
             //defaults cannot be specified at this time, an error must be thrown.
             if (!$saved) {
                 throw new InvalidValueToSanitizeException(Zurmo::t('ImportModule', 'A new related model could not be created because there are unspecified required attributes on that related model.'));
             } else {
                 $this->importSanitizeResultsUtil->addRelatedModelMessage(Zurmo::t('ImportModule', '{modelLabel} saved correctly: {linkToModel}', array('{modelLabel}' => $newRelatedModel->getModelLabelByTypeAndLanguage('Singular'), '{linkToModel}' => ImportUtil::resolveLinkMessageToModel($newRelatedModel))));
                 if ($newRelatedModel instanceof SecurableItem && $this->explicitReadWriteModelPermissions instanceof ExplicitReadWriteModelPermissions) {
                     $resolved = ExplicitReadWriteModelPermissionsUtil::resolveExplicitReadWriteModelPermissions($newRelatedModel, $this->explicitReadWriteModelPermissions);
                     if (!$resolved) {
                         $this->importSanitizeResultsUtil->addRelatedModelMessage('The record saved, but there was a problem ' . 'setting the security permissions. It will at least be viewable by the owner.');
                     }
                 }
             }
             return $newRelatedModel;
         } else {
             return $modelsFound[0];
         }
     }
     exit;
 }