/** * Test case for createProfileContact. */ public function testCreateProfileContact() { $fields = CRM_Contact_BAO_Contact::exportableFields('Individual'); //current employer field for individual $fields['organization_name'] = array('name' => 'organization_name', 'where' => 'civicrm_organization.organization_name', 'title' => 'Current Employer'); //get the common params $contactParams = $this->contactParams(); $unsetParams = array('location', 'privacy'); foreach ($unsetParams as $param) { unset($contactParams[$param]); } $profileParams = array('organization_name' => 'Yahoo', 'gender_id' => '2', 'prefix_id' => '3', 'suffix_id' => '2', 'city-Primary' => 'Newark', 'contact_type' => 'Individual', 'country-Primary' => '1228', 'do_not_email' => '1', 'do_not_mail' => '1', 'do_not_phone' => '1', 'do_not_trade' => '1', 'do_not_sms' => '1', 'email-Primary' => '*****@*****.**', 'geo_code_1-Primary' => '18.219023', 'geo_code_2-Primary' => '-105.00973', 'im-Primary-provider_id' => '1', 'im-Primary' => 'john.smith', 'on_hold' => '1', 'openid' => '*****@*****.**', 'phone-Primary-1' => '303443689', 'phone-Primary-2' => '9833910234', 'postal_code-Primary' => '01903', 'postal_code_suffix-Primary' => '12345', 'state_province-Primary' => '1029', 'street_address-Primary' => 'Saint Helier St', 'supplemental_address_1-Primary' => 'Hallmark Ct', 'supplemental_address_2-Primary' => 'Jersey Village', 'user_unique_id' => '123456789', 'is_bulkmail' => '1', 'world_region' => 'India', 'tag' => array('3' => '1', '4' => '1', '1' => '1')); $createParams = array_merge($contactParams, $profileParams); //create the contact using create profile contact. $contactId = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE); //get the parameters to compare. $params = $this->contactParams(); //check the values in DB. foreach ($params as $key => $val) { if (!is_array($params[$key])) { if ($key == 'contact_source') { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source', 'id', $params[$key], "Check for {$key} creation."); } else { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key, 'id', $params[$key], "Check for {$key} creation."); } } } //check privacy options. foreach ($params['privacy'] as $key => $value) { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key, 'id', $params['privacy'][$key], 'Check for do_not_email creation.'); } $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type', 'id', $profileParams['contact_type'], 'Check for contact type creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id', 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id', $params['birth_date'], 'Check for birth_date creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id', $params['deceased_date'], 'Check for deceased_date creation.'); $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)); $checkPrefComm = array(); foreach ($dbPrefComm as $key => $value) { if ($value) { $checkPrefComm[$value] = 1; } } $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']); //Now check DB for Address $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'is_primary' => 1); $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $profileParams), 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $profileParams), 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $profileParams), 'city' => CRM_Utils_Array::value('city-Primary', $profileParams), 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $profileParams), 'country_id' => CRM_Utils_Array::value('country-Primary', $profileParams), 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $profileParams), 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $profileParams), 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $profileParams)); $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); //Now check DB for Email $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $profileParams)); $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); //Now check DB for IM $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $profileParams), 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $profileParams)); $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); //Now check DB for Phone $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'is_primary' => 1); $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams)); $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); //Now check DB for Mobile $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2])); $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams)); $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); //get the value of relationship $values = array(); $searchParams = array('contact_id' => $contactId); $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values); //Now check values of Relationship Count. $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count'); foreach ($values['relationship']['data'] as $key => $val) { //Now check values of Relationship organization. $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization'); //Now check values of Relationship type. $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); //delete the organization. $this->contactDelete(CRM_Utils_Array::value('cid', $val)); } //Now check values of tag ids. $tags = CRM_Core_BAO_EntityTag::getTag($contactId); foreach ($tags as $key => $val) { $tagIds[$key] = 1; } $this->assertAttributesEquals($profileParams['tag'], $tagIds); //update Contact mode $updateCParams = array('first_name' => 'john', 'last_name' => 'doe', 'contact_type' => 'Individual', 'middle_name' => 'abc', 'prefix_id' => 2, 'suffix_id' => 3, 'nick_name' => 'Nick Name Updated', 'job_title' => 'software Developer', 'gender_id' => 1, 'is_deceased' => 1, 'website' => array(1 => array('website_type_id' => 1, 'url' => 'http://civicrmUpdate.org')), 'contact_source' => 'test contact', 'external_identifier' => 111222333, 'preferred_mail_format' => 'Both', 'is_opt_out' => 0, 'legal_identifier' => '123123123123', 'image_URL' => 'http://imageupdate.com', 'deceased_date' => '1981-10-10', 'birth_date' => '1951-11-11', 'privacy' => array('do_not_phone' => 1, 'do_not_email' => 1), 'preferred_communication_method' => array('1' => 0, '2' => 1, '3' => 0, '4' => 1, '5' => 0)); $updatePfParams = array('organization_name' => 'Google', 'city-Primary' => 'Mumbai', 'contact_type' => 'Individual', 'country-Primary' => '1228', 'do_not_email' => '1', 'do_not_mail' => '1', 'do_not_phone' => '1', 'do_not_trade' => '1', 'do_not_sms' => '1', 'email-Primary' => '*****@*****.**', 'geo_code_1-Primary' => '31.694842', 'geo_code_2-Primary' => '-106.29998', 'im-Primary-provider_id' => '1', 'im-Primary' => 'john.doe', 'on_hold' => '1', 'openid' => '*****@*****.**', 'phone-Primary-1' => '02115245336', 'phone-Primary-2' => '9766323895', 'postal_code-Primary' => '12345', 'postal_code_suffix-Primary' => '123', 'state_province-Primary' => '1004', 'street_address-Primary' => 'Oberoi Garden', 'supplemental_address_1-Primary' => 'A-wing:3037', 'supplemental_address_2-Primary' => 'Andhery', 'user_unique_id' => '1122334455', 'is_bulkmail' => '1', 'world_region' => 'India', 'tag' => array('2' => '1', '5' => '1')); $createParams = array_merge($updateCParams, $updatePfParams); //create the contact using create profile contact. $contactID = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, $contactId, NULL, NULL, NULL, TRUE); //check the contact ids $this->assertEquals($contactId, $contactID, 'check for Contact ids'); //check the values in DB. foreach ($updateCParams as $key => $val) { if (!is_array($updateCParams[$key])) { if ($key == 'contact_source') { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source', 'id', $updateCParams[$key], "Check for {$key} creation."); } else { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key, 'id', $updateCParams[$key], "Check for {$key} creation."); } } } //check privacy options. foreach ($updateCParams['privacy'] as $key => $value) { $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key, 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'); } $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type', 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id', 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id', $updateCParams['birth_date'], 'Check for birth_date creation.'); $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id', $updateCParams['deceased_date'], 'Check for deceased_date creation.'); $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)); $checkPrefComm = array(); foreach ($dbPrefComm as $key => $value) { if ($value) { $checkPrefComm[$value] = 1; } } $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']); //Now check DB for Address $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'is_primary' => 1); $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $updatePfParams), 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $updatePfParams), 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $updatePfParams), 'city' => CRM_Utils_Array::value('city-Primary', $updatePfParams), 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $updatePfParams), 'country_id' => CRM_Utils_Array::value('country-Primary', $updatePfParams), 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $updatePfParams), 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $updatePfParams), 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $updatePfParams)); $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); //Now check DB for Email $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $updatePfParams)); $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); //Now check DB for IM $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $updatePfParams), 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $updatePfParams)); $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); //Now check DB for Phone $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'is_primary' => 1); $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams)); $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); //Now check DB for Mobile $searchParams = array('contact_id' => $contactId, 'location_type_id' => 1, 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2])); $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams)); $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); //get the value of relationship $values = array(); $searchParams = array('contact_id' => $contactId); $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values); //Now check values of Relationship Count. $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count'); foreach ($values['relationship']['data'] as $key => $val) { //Now check values of Relationship organization. $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization'); //Now check values of Relationship type. $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); //delete the organization. $this->contactDelete(CRM_Utils_Array::value('cid', $val)); } //Now check values of tag ids. $tags = CRM_Core_BAO_EntityTag::getTag($contactId); foreach ($tags as $key => $val) { $tagIds[$key] = 1; } $this->assertAttributesEquals($updatePfParams['tag'], $tagIds); //cleanup DB by deleting the contact $this->contactDelete($contactId); }
/** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only * contact_id. We'll tweak this function to be more full featured over a period * of time. This is the inverse function of create. It also stores all the retrieved * values in the default array * * @param array $params (reference ) an assoc array of name/value pairs * @param array $defaults (reference ) an assoc array to hold the name / value pairs * in a hierarchical manner * @param array $ids (reference) the array that holds all the db ids * @param boolean $microformat for location in microformat * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ static function &retrieve(&$params, &$defaults, $microformat = false) { if (array_key_exists('contact_id', $params)) { $params['id'] = $params['contact_id']; } else { if (array_key_exists('id', $params)) { $params['contact_id'] = $params['id']; } } $contact = self::_getValues($params, $defaults); unset($params['id']); //get the block information for this contact $entityBlock = array('contact_id' => $params['contact_id']); $blocks = CRM_Core_BAO_Location::getValues($entityBlock, $microformat); $defaults = array_merge($defaults, $blocks); foreach ($blocks as $block => $value) { $contact->{$block} = $value; } $contact->notes =& CRM_Core_BAO_Note::getValues($params, $defaults); $contact->relationship =& CRM_Contact_BAO_Relationship::getValues($params, $defaults); $contact->groupContact =& CRM_Contact_BAO_GroupContact::getValues($params, $defaults); return $contact; }
/** * Fetch object based on array of properties. * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $defaults * (reference ) an assoc array to hold the name / value pairs. * in a hierarchical manner * @param bool $microformat * For location in microformat. * * @return CRM_Contact_BAO_Contact */ public static function &retrieve(&$params, &$defaults, $microformat = FALSE) { if (array_key_exists('contact_id', $params)) { $params['id'] = $params['contact_id']; } elseif (array_key_exists('id', $params)) { $params['contact_id'] = $params['id']; } $contact = self::getValues($params, $defaults); unset($params['id']); //get the block information for this contact $entityBlock = array('contact_id' => $params['contact_id']); $blocks = CRM_Core_BAO_Location::getValues($entityBlock, $microformat); $defaults = array_merge($defaults, $blocks); foreach ($blocks as $block => $value) { $contact->{$block} = $value; } if (!isset($params['noNotes'])) { $contact->notes = CRM_Core_BAO_Note::getValues($params, $defaults); } if (!isset($params['noRelationships'])) { $contact->relationship = CRM_Contact_BAO_Relationship::getValues($params, $defaults); } if (!isset($params['noGroups'])) { $contact->groupContact = CRM_Contact_BAO_GroupContact::getValues($params, $defaults); } if (!isset($params['noWebsite'])) { $contact->website = CRM_Core_BAO_Website::getValues($params, $defaults); } return $contact; }
/** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only * contact_id. We'll tweak this function to be more full featured over a period * of time. This is the inverse function of create. It also stores all the retrieved * values in the default array * * @param array $params (reference ) an assoc array of name/value pairs * @param array $defaults (reference ) an assoc array to hold the name / value pairs * in a hierarchical manner * @param array $ids (reference) the array that holds all the db ids * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ function retrieve(&$params, &$defaults, &$ids) { $contact = CRM_Contact_BAO_Contact::getValues($params, $defaults, $ids); unset($params['id']); require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contact->contact_type) . ".php"; eval('$contact->contact_type_object =& CRM_Contact_BAO_' . $contact->contact_type . '::getValues( $params, $defaults, $ids );'); $locParams = $params + array('entity_id' => $params['contact_id'], 'entity_table' => CRM_Contact_BAO_Contact::getTableName()); $contact->location =& CRM_Core_BAO_Location::getValues($locParams, $defaults, $ids, 3); $contact->notes =& CRM_Core_BAO_Note::getValues($params, $defaults, $ids); $contact->relationship =& CRM_Contact_BAO_Relationship::getValues($params, $defaults, $ids); $contact->groupContact =& CRM_Contact_BAO_GroupContact::getValues($params, $defaults, $ids); $activityParam = array('entity_id' => $params['contact_id']); $contact->activity =& CRM_Core_BAO_History::getValues($activityParam, $defaults, 'Activity'); $activityParam = array('contact_id' => $params['contact_id']); $defaults['openActivity'] = array('data' => CRM_Contact_BAO_Contact::getOpenActivities($activityParam, 0, 3), 'totalCount' => CRM_Contact_BAO_Contact::getNumOpenActivity($params['contact_id'])); return $contact; }