From bca450a5f7c9440fca98fae55321669ebad281bd Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 22 May 2019 09:11:20 +1200 Subject: [PATCH] [NFC] ProfileTest class - formatting & general cleanup --- Civi/Test/ContactTestTrait.php | 1 + tests/phpunit/api/v3/ProfileTest.php | 397 ++++++++++++++------------- 2 files changed, 211 insertions(+), 187 deletions(-) diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php index 448231bb1973..7bc10624ce86 100644 --- a/Civi/Test/ContactTestTrait.php +++ b/Civi/Test/ContactTestTrait.php @@ -89,6 +89,7 @@ public function individualCreate($params = array(), $seq = 0, $random = FALSE) { * * @return int * id of Household created + * @throws \Exception */ public function householdCreate($params = array(), $seq = 0) { $params = array_merge($this->sampleContact('Household', $seq), $params); diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index 6e2e88f7820b..a2ac644c2174 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -32,13 +32,17 @@ * @group headless */ class api_v3_ProfileTest extends CiviUnitTestCase { - protected $_apiversion; + protected $_profileID = 0; + protected $_membershipTypeID; + protected $_contactID; + /** + * Set up for test. + */ public function setUp() { - $this->_apiversion = 3; parent::setUp(); $config = CRM_Core_Config::singleton(); $countryLimit = $config->countryLimit; @@ -49,26 +53,32 @@ public function setUp() { $this->_membershipTypeID = $this->membershipTypeCreate(); } + /** + * Cleanup after test. + * + * @throws \Exception + */ public function tearDown() { - $this->quickCleanup(array( + $this->quickCleanup([ 'civicrm_contact', 'civicrm_phone', 'civicrm_address', 'civicrm_membership', 'civicrm_contribution', 'civicrm_uf_match', - ), TRUE); - $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); - // ok can't be bothered wring an api to do this & truncating is crazy - CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_uf_group WHERE id IN ($this->_profileID, 26)"); + 'civicrm_uf_group', + 'civicrm_uf_field', + ], TRUE); + $this->callAPISuccess('membership_type', 'delete', ['id' => $this->_membershipTypeID]); + parent::tearDown(); } /** * Check Without ProfileId. */ public function testProfileGetWithoutProfileId() { - $this->callAPIFailure('profile', 'get', array('contact_id' => 1), + $this->callAPIFailure('profile', 'get', ['contact_id' => 1], 'Mandatory key(s) missing from params array: profile_id' ); } @@ -77,7 +87,10 @@ public function testProfileGetWithoutProfileId() { * Check with no invalid profile Id. */ public function testProfileGetInvalidProfileId() { - $this->callAPIFailure('profile', 'get', array('contact_id' => 1, 'profile_id' => 1000)); + $this->callAPIFailure('profile', 'get', [ + 'contact_id' => 1, + 'profile_id' => 1000, + ]); } /** @@ -87,63 +100,69 @@ public function testProfileGet() { $profileFieldValues = $this->_createIndividualContact(); $expected = reset($profileFieldValues); $contactId = key($profileFieldValues); - $params = array( + $params = [ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, - ); - $result = $this->callAPISuccess('profile', 'get', $params); + ]; + $result = $this->callAPISuccess('profile', 'get', $params)['values']; foreach ($expected as $profileField => $value) { - $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'])); + $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result)); } } + /** + * Test getting multiple profiles. + */ public function testProfileGetMultiple() { $profileFieldValues = $this->_createIndividualContact(); $expected = reset($profileFieldValues); $contactId = key($profileFieldValues); - $params = array( - 'profile_id' => array($this->_profileID, 1, 'Billing'), + $params = [ + 'profile_id' => [$this->_profileID, 1, 'Billing'], 'contact_id' => $contactId, - ); + ]; - $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__)['values']; foreach ($expected as $profileField => $value) { - $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][$this->_profileID]), " error message: " . "missing/mismatching value for {$profileField}"); + $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result[$this->_profileID]), " error message: " . "missing/mismatching value for {$profileField}"); } - $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}"); - $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email'); - $this->assertEquals($result['values']['Billing'], array( + $this->assertEquals('abc1', $result[1]['first_name'], " error message: " . "missing/mismatching value for first name"); + $this->assertFalse(array_key_exists('email-Primary', $result[1]), 'profile 1 does not include email'); + $this->assertEquals($result['Billing'], [ 'billing_first_name' => 'abc1', 'billing_middle_name' => 'J.', 'billing_last_name' => 'xyz1', - 'billing_street_address-5' => '5 Saint Helier St', + 'billing_street_address-5' => '5 Saint Hellier St', 'billing_city-5' => 'Gotham City', 'billing_state_province_id-5' => '1021', 'billing_country_id-5' => '1228', 'billing_postal_code-5' => '90210', 'billing-email-5' => 'abc1.xyz1@yahoo.com', 'email-5' => 'abc1.xyz1@yahoo.com', - )); + ]); } + /** + * Test getting billing profile filled using is_billing. + */ public function testProfileGetBillingUseIsBillingLocation() { $individual = $this->_createIndividualContact(); $contactId = key($individual); - $this->callAPISuccess('address', 'create', array( + $this->callAPISuccess('address', 'create', [ 'is_billing' => 1, 'street_address' => 'is billing st', 'location_type_id' => 2, 'contact_id' => $contactId, - )); + ]); - $params = array( - 'profile_id' => array($this->_profileID, 1, 'Billing'), + $params = [ + 'profile_id' => [$this->_profileID, 1, 'Billing'], 'contact_id' => $contactId, - ); + ]; - $result = $this->callAPISuccess('profile', 'get', $params); - $this->assertEquals('abc1', $result['values'][1]['first_name']); - $this->assertEquals(array( + $result = $this->callAPISuccess('profile', 'get', $params)['values']; + $this->assertEquals('abc1', $result[1]['first_name']); + $this->assertEquals([ 'billing_first_name' => 'abc1', 'billing_middle_name' => 'J.', 'billing_last_name' => 'xyz1', @@ -154,33 +173,36 @@ public function testProfileGetBillingUseIsBillingLocation() { 'billing-email-5' => 'abc1.xyz1@yahoo.com', 'email-5' => 'abc1.xyz1@yahoo.com', 'billing_postal_code-5' => '', - ), $result['values']['Billing']); + ], $result['Billing']); } + /** + * Test getting multiple profiles, including billing. + */ public function testProfileGetMultipleHasBillingLocation() { $individual = $this->_createIndividualContact(); $contactId = key($individual); - $this->callAPISuccess('address', 'create', array( + $this->callAPISuccess('address', 'create', [ 'contact_id' => $contactId, 'street_address' => '25 Big Street', 'city' => 'big city', 'location_type_id' => 5, - )); - $this->callAPISuccess('email', 'create', array( + ]); + $this->callAPISuccess('email', 'create', [ 'contact_id' => $contactId, 'email' => 'big@once.com', 'location_type_id' => 2, 'is_billing' => 1, - )); + ]); - $params = array( - 'profile_id' => array($this->_profileID, 1, 'Billing'), + $params = [ + 'profile_id' => [$this->_profileID, 1, 'Billing'], 'contact_id' => $contactId, - ); + ]; $result = $this->callAPISuccess('profile', 'get', $params); $this->assertEquals('abc1', $result['values'][1]['first_name']); - $this->assertEquals($result['values']['Billing'], array( + $this->assertEquals($result['values']['Billing'], [ 'billing_first_name' => 'abc1', 'billing_middle_name' => 'J.', 'billing_last_name' => 'xyz1', @@ -191,20 +213,20 @@ public function testProfileGetMultipleHasBillingLocation() { 'billing-email-5' => 'big@once.com', 'email-5' => 'big@once.com', 'billing_postal_code-5' => '', - )); + ]); } /** - * Get Billing empty contact - this will return generic defaults + * Get Billing empty contact - this will return generic defaults. */ public function testProfileGetBillingEmptyContact() { $this->callAPISuccess('Setting', 'create', ['defaultContactCountry' => 1228]); - $params = array( - 'profile_id' => array('Billing'), - ); + $params = [ + 'profile_id' => ['Billing'], + ]; - $result = $this->callAPISuccess('profile', 'get', $params); - $this->assertEquals(array( + $result = $this->callAPISuccess('profile', 'get', $params)['values']; + $this->assertEquals([ 'billing_first_name' => '', 'billing_middle_name' => '', 'billing_last_name' => '', @@ -215,7 +237,7 @@ public function testProfileGetBillingEmptyContact() { 'billing_email-5' => '', 'email-5' => '', 'billing_postal_code-5' => '', - ), $result['values']['Billing']); + ], $result['Billing']); } /** @@ -239,28 +261,27 @@ public function testContactActivityGetWrongActivityId() { /** * Check contact activity profile with wrong activity type. + * + * @throws \Exception */ public function testContactActivityGetWrongActivityType() { - //flush cache by calling with reset - $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); - $sourceContactId = $this->householdCreate(); - $activityparams = array( + $activityParams = [ 'source_contact_id' => $sourceContactId, 'activity_type_id' => '2', 'subject' => 'Test activity', 'activity_date_time' => '20110316', 'duration' => '120', - 'location' => 'Pensulvania', + 'location' => 'Pennsylvania', 'details' => 'a test activity', 'status_id' => '1', 'priority_id' => '1', - ); + ]; - $activity = $this->callAPISuccess('activity', 'create', $activityparams); + $activity = $this->callAPISuccess('activity', 'create', $activityParams)['values']; - $activityValues = array_pop($activity['values']); + $activityValues = array_pop($activity); list($params) = $this->_createContactWithActivity(); @@ -288,10 +309,10 @@ public function testContactActivityGetSuccess() { public function testGetFields() { $this->_createIndividualProfile(); $this->_addCustomFieldToProfile($this->_profileID); - $result = $this->callAPIAndDocument('profile', 'getfields', array( + $result = $this->callAPIAndDocument('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => $this->_profileID, - ), __FUNCTION__, __FILE__, + ], __FUNCTION__, __FILE__, 'demonstrates retrieving profile fields passing in an id'); $this->assertArrayKeyExists('first_name', $result['values']); $this->assertEquals('2', $result['values']['first_name']['type']); @@ -302,17 +323,17 @@ public function testGetFields() { } /** - * Check getfields works & gives us our fields - partipant profile + * Check getfields works & gives us our fields - participant profile */ public function testGetFieldsParticipantProfile() { - $result = $this->callAPISuccess('profile', 'getfields', array( + $result = $this->callAPISuccess('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => 'participant_status', 'get_options' => 'all', - )); + ]); $this->assertTrue(array_key_exists('participant_status_id', $result['values'])); $this->assertEquals('Attended', $result['values']['participant_status_id']['options'][2]); - $this->assertEquals(array('participant_status'), $result['values']['participant_status_id']['api.aliases']); + $this->assertEquals(['participant_status'], $result['values']['participant_status_id']['api.aliases']); } /** @@ -320,18 +341,18 @@ public function testGetFieldsParticipantProfile() { * (getting to the end with no e-notices is pretty good evidence it's working) */ public function testGetFieldsMembershipBatchProfile() { - $result = $this->callAPISuccess('profile', 'getfields', array( + $result = $this->callAPISuccess('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => 'membership_batch_entry', 'get_options' => 'all', - )); + ]); $this->assertTrue(array_key_exists('total_amount', $result['values'])); $this->assertTrue(array_key_exists('financial_type_id', $result['values'])); - $this->assertEquals(array( + $this->assertEquals([ 'contribution_type_id', 'contribution_type', 'financial_type', - ), $result['values']['financial_type_id']['api.aliases']); + ], $result['values']['financial_type_id']['api.aliases']); $this->assertTrue(!array_key_exists('financial_type', $result['values'])); $this->assertEquals(12, $result['values']['receive_date']['type']); } @@ -341,14 +362,14 @@ public function testGetFieldsMembershipBatchProfile() { * (getting to the end with no e-notices is pretty good evidence it's working) */ public function testGetFieldsAllProfiles() { - $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id')); - $profileIDs = array_keys($result['values']); + $result = $this->callAPISuccess('uf_group', 'get', ['return' => 'id'])['values']; + $profileIDs = array_keys($result); foreach ($profileIDs as $profileID) { - $this->callAPISuccess('profile', 'getfields', array( + $this->callAPISuccess('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => $profileID, 'get_options' => 'all', - )); + ]); } } @@ -356,9 +377,9 @@ public function testGetFieldsAllProfiles() { * Check Without ProfileId. */ public function testProfileSubmitWithoutProfileId() { - $params = array( + $params = [ 'contact_id' => 1, - ); + ]; $this->callAPIFailure('profile', 'submit', $params, 'Mandatory key(s) missing from params array: profile_id' ); @@ -368,11 +389,11 @@ public function testProfileSubmitWithoutProfileId() { * Check with no invalid profile Id. */ public function testProfileSubmitInvalidProfileId() { - $params = array( + $params = [ 'contact_id' => 1, 'profile_id' => 1000, - ); - $result = $this->callAPIFailure('profile', 'submit', $params); + ]; + $this->callAPIFailure('profile', 'submit', $params); } /** @@ -381,15 +402,18 @@ public function testProfileSubmitInvalidProfileId() { public function testProfileSubmitCheckProfileRequired() { $profileFieldValues = $this->_createIndividualContact(); $contactId = key($profileFieldValues); - $updateParams = array( + $updateParams = [ 'first_name' => 'abc2', 'last_name' => 'xyz2', 'phone-1-1' => '022 321 826', 'country-1' => '1013', 'state_province-1' => '1000', - ); + ]; - $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId), + $params = array_merge([ + 'profile_id' => $this->_profileID, + 'contact_id' => $contactId, + ], $updateParams ); @@ -405,26 +429,26 @@ public function testProfileSubmit() { $profileFieldValues = $this->_createIndividualContact(); $contactId = key($profileFieldValues); - $updateParams = array( + $updateParams = [ 'first_name' => 'abc2', 'last_name' => 'xyz2', 'email-primary' => 'abc2.xyz2@gmail.com', 'phone-1-1' => '022 321 826', 'country-1' => '1013', 'state_province-1' => '1000', - ); + ]; - $params = array_merge(array( + $params = array_merge([ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, - ), $updateParams); + ], $updateParams); $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__); - $getParams = array( + $getParams = [ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, - ); + ]; $profileDetails = $this->callAPISuccess('profile', 'get', $getParams); foreach ($updateParams as $profileField => $value) { @@ -440,22 +464,22 @@ public function testProfileSubmit() { /** * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata - * First we delete & create to increment the version & then check for caching probs + * First we delete & create to increment the version & then check for caching problems. */ public function testProfileSubmitCheckCaching() { - $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); + $this->callAPISuccess('membership_type', 'delete', ['id' => $this->_membershipTypeID]); $this->_membershipTypeID = $this->membershipTypeCreate(); - $membershipTypes = $this->callAPISuccess('membership_type', 'get', array()); - $profileFields = $this->callAPISuccess('profile', 'getfields', array( + $membershipTypes = $this->callAPISuccess('membership_type', 'get', []); + $profileFields = $this->callAPISuccess('profile', 'getfields', [ 'get_options' => 'all', 'action' => 'submit', 'profile_id' => 'membership_batch_entry', - )); - $getoptions = $this->callAPISuccess('membership', 'getoptions', array( + ]); + $getoptions = $this->callAPISuccess('membership', 'getoptions', [ 'field' => 'membership_type', 'context' => 'validate', - )); + ]); $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values'])); $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options'])); @@ -465,12 +489,12 @@ public function testProfileSubmitCheckCaching() { * Test that the fields are returned in the right order despite the faffing around that goes on. */ public function testMembershipGetFieldsOrder() { - $result = $this->callAPISuccess('profile', 'getfields', array( + $result = $this->callAPISuccess('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => 'membership_batch_entry', - )); + ])['values']; $weight = 1; - foreach ($result['values'] as $fieldName => $field) { + foreach ($result as $fieldName => $field) { if ($fieldName == 'profile_id') { continue; } @@ -484,7 +508,7 @@ public function testMembershipGetFieldsOrder() { */ public function testProfileSubmitMembershipBatch() { $this->_contactID = $this->individualCreate(); - $this->callAPISuccess('profile', 'submit', array( + $this->callAPISuccess('profile', 'submit', [ 'profile_id' => 'membership_batch_entry', 'financial_type_id' => 1, 'membership_type' => $this->_membershipTypeID, @@ -493,7 +517,7 @@ public function testProfileSubmitMembershipBatch() { 'contribution_status_id' => 1, 'receive_date' => 'now', 'contact_id' => $this->_contactID, - )); + ]); } /** @@ -503,26 +527,26 @@ public function testLegacySet() { $profileFieldValues = $this->_createIndividualContact(); $contactId = key($profileFieldValues); - $updateParams = array( + $updateParams = [ 'first_name' => 'abc2', 'last_name' => 'xyz2', 'email-Primary' => 'abc2.xyz2@gmail.com', 'phone-1-1' => '022 321 826', 'country-1' => '1013', 'state_province-1' => '1000', - ); + ]; - $params = array_merge(array( + $params = array_merge([ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, - ), $updateParams); + ], $updateParams); $result = $this->callAPISuccess('profile', 'set', $params); $this->assertArrayKeyExists('values', $result); - $getParams = array( + $getParams = [ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, - ); + ]; $profileDetails = $this->callAPISuccess('profile', 'get', $getParams); foreach ($updateParams as $profileField => $value) { @@ -539,8 +563,7 @@ public function testContactActivitySubmitWithoutActivityId() { $params = array_merge($params, $expected); unset($params['activity_id']); - $result = $this->callAPIFailure('profile', 'submit', $params); - $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id'); + $this->callAPIFailure('profile', 'submit', $params, 'Mandatory key(s) missing from params array: activity_id'); } /** @@ -550,32 +573,31 @@ public function testContactActivitySubmitWrongActivityId() { list($params, $expected) = $this->_createContactWithActivity(); $params = array_merge($params, $expected); $params['activity_id'] = 100001; - $result = $this->callAPIFailure('profile', 'submit', $params); - $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).'); + $this->callAPIFailure('profile', 'submit', $params, 'Invalid Activity Id (aid).'); } /** * Check contact activity profile with wrong activity type. + * + * @throws \Exception */ public function testContactActivitySubmitWrongActivityType() { - //flush cache by calling with reset - CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); $sourceContactId = $this->householdCreate(); - $activityparams = array( + $activityParams = [ 'source_contact_id' => $sourceContactId, 'activity_type_id' => '2', 'subject' => 'Test activity', 'activity_date_time' => '20110316', 'duration' => '120', - 'location' => 'Pensulvania', + 'location' => 'Pennsylvania', 'details' => 'a test activity', 'status_id' => '1', 'priority_id' => '1', - ); + ]; - $activity = $this->callAPISuccess('activity', 'create', $activityparams); + $activity = $this->callAPISuccess('activity', 'create', $activityParams); $activityValues = array_pop($activity['values']); @@ -593,7 +615,7 @@ public function testContactActivitySubmitWrongActivityType() { public function testContactActivitySubmitSuccess() { list($params) = $this->_createContactWithActivity(); - $updateParams = array( + $updateParams = [ 'first_name' => 'abc2', 'last_name' => 'xyz2', 'email-Primary' => 'abc2.xyz2@yahoo.com', @@ -602,13 +624,13 @@ public function testContactActivitySubmitSuccess() { 'activity_duration' => '100', 'activity_date_time' => '2010-03-08 00:00:00', 'activity_status_id' => '2', - ); + ]; $profileParams = array_merge($params, $updateParams); $this->callAPISuccess('profile', 'submit', $profileParams); - $result = $this->callAPISuccess('profile', 'get', $params); + $result = $this->callAPISuccess('profile', 'get', $params)['values']; foreach ($updateParams as $profileField => $value) { - $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), " error message: " . "missing/mismatching value for {$profileField}" + $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result), " error message: " . "missing/mismatching value for {$profileField}" ); } } @@ -617,9 +639,9 @@ public function testContactActivitySubmitSuccess() { * Check profile apply Without ProfileId. */ public function testProfileApplyWithoutProfileId() { - $params = array( + $params = [ 'contact_id' => 1, - ); + ]; $this->callAPIFailure('profile', 'apply', $params, 'Mandatory key(s) missing from params array: profile_id'); } @@ -628,10 +650,10 @@ public function testProfileApplyWithoutProfileId() { * Check profile apply with no invalid profile Id. */ public function testProfileApplyInvalidProfileId() { - $params = array( + $params = [ 'contact_id' => 1, 'profile_id' => 1000, - ); + ]; $this->callAPIFailure('profile', 'apply', $params); } @@ -643,7 +665,7 @@ public function testProfileApply() { current($profileFieldValues); $contactId = key($profileFieldValues); - $params = array( + $params = [ 'profile_id' => $this->_profileID, 'contact_id' => $contactId, 'first_name' => 'abc2', @@ -652,49 +674,45 @@ public function testProfileApply() { 'phone-1-1' => '022 321 826', 'country-1' => '1013', 'state_province-1' => '1000', - ); + ]; $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__); // Expected field values - $expected['contact'] = array( + $expected['contact'] = [ 'contact_id' => $contactId, 'contact_type' => 'Individual', 'first_name' => 'abc2', 'last_name' => 'xyz2', - ); - $expected['email'] = array( + ]; + $expected['email'] = [ 'location_type_id' => 1, 'is_primary' => 1, 'email' => 'abc2.xyz2@gmail.com', - ); + ]; - $expected['phone'] = array( + $expected['phone'] = [ 'location_type_id' => 1, 'is_primary' => 1, 'phone_type_id' => 1, 'phone' => '022 321 826', - ); - $expected['address'] = array( + ]; + $expected['address'] = [ 'location_type_id' => 1, 'is_primary' => 1, 'country_id' => 1013, 'state_province_id' => 1000, - ); + ]; foreach ($expected['contact'] as $field => $value) { - $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}" + $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "missing/mismatching value for {$field}" ); } - foreach (array( - 'email', - 'phone', - 'address', - ) as $fieldType) { + foreach (['email', 'phone', 'address'] as $fieldType) { $typeValues = array_pop($result['values'][$fieldType]); foreach ($expected[$fieldType] as $field => $value) { - $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})" + $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "missing/mismatching value for {$field} ({$fieldType})" ); } } @@ -710,31 +728,31 @@ public function testSubmitWithTags() { $params['profile_id'] = $this->_profileID; $params['contact_id'] = $contactId; - $this->callAPISuccess('ufField', 'create', array( + $this->callAPISuccess('ufField', 'create', [ 'uf_group_id' => $this->_profileID, 'field_name' => 'tag', 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'label' => 'Tags', - )); + ]); $tag_1 = $this->callAPISuccess('tag', 'create', ['name' => 'abc'])['id']; $tag_2 = $this->callAPISuccess('tag', 'create', ['name' => 'def'])['id']; $params['tag'] = "$tag_1,$tag_2"; - $result = $this->callAPISuccess('profile', 'submit', $params); + $this->callAPISuccess('profile', 'submit', $params); $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]); $this->assertEquals(2, $tags['count']); $params['tag'] = [$tag_1]; - $result = $this->callAPISuccess('profile', 'submit', $params); + $this->callAPISuccess('profile', 'submit', $params); $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]); $this->assertEquals(1, $tags['count']); $params['tag'] = ''; - $result = $this->callAPISuccess('profile', 'submit', $params); + $this->callAPISuccess('profile', 'submit', $params); $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]); $this->assertEquals(0, $tags['count']); @@ -743,6 +761,8 @@ public function testSubmitWithTags() { /** * Check success with a note. + * + * @throws \Exception */ public function testSubmitWithNote() { $profileFieldValues = $this->_createIndividualContact(); @@ -751,13 +771,13 @@ public function testSubmitWithNote() { $params['profile_id'] = $this->_profileID; $params['contact_id'] = $contactId; - $this->callAPISuccess('ufField', 'create', array( + $this->callAPISuccess('ufField', 'create', [ 'uf_group_id' => $this->_profileID, 'field_name' => 'note', 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'label' => 'Note', - )); + ]); $params['note'] = "Hello 123"; $this->callAPISuccess('profile', 'submit', $params); @@ -768,6 +788,8 @@ public function testSubmitWithNote() { /** * Check handling a custom greeting. + * + * @throws \CiviCRM_API3_Exception */ public function testSubmitGreetingFields() { $profileFieldValues = $this->_createIndividualContact(); @@ -776,13 +798,13 @@ public function testSubmitGreetingFields() { $params['profile_id'] = $this->_profileID; $params['contact_id'] = $contactId; - $this->callAPISuccess('ufField', 'create', array( + $this->callAPISuccess('ufField', 'create', [ 'uf_group_id' => $this->_profileID, 'field_name' => 'email_greeting', 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'label' => 'Email Greeting', - )); + ]); $emailGreetings = array_column(civicrm_api3('OptionValue', 'get', ['option_group_id' => "email_greeting"])['values'], NULL, 'name'); @@ -801,16 +823,17 @@ public function testSubmitGreetingFields() { /** * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields + * * @param array $params * * @return mixed */ - public function _createIndividualContact($params = array()) { - $contactParams = array_merge(array( + public function _createIndividualContact($params = []) { + $contactParams = array_merge([ 'first_name' => 'abc1', 'last_name' => 'xyz1', 'email' => 'abc1.xyz1@yahoo.com', - 'api.address.create' => array( + 'api.address.create' => [ 'location_type_id' => 1, 'is_primary' => 1, 'street_address' => '5 Saint Helier St', @@ -823,26 +846,26 @@ public function _createIndividualContact($params = array()) { 'postal_code' => '90210', 'city' => 'Gotham City', 'is_billing' => 0, - ), - 'api.phone.create' => array( + ], + 'api.phone.create' => [ 'location_type_id' => '1', 'phone' => '021 512 755', 'phone_type_id' => '1', 'is_primary' => '1', - ), - ), $params); + ], + ], $params); $this->_contactID = $this->individualCreate($contactParams); $this->_createIndividualProfile(); // expected result of above created profile with contact Id $contactId - $profileData[$this->_contactID] = array( + $profileData[$this->_contactID] = [ 'first_name' => 'abc1', 'last_name' => 'xyz1', 'email-primary' => 'abc1.xyz1@yahoo.com', 'phone-1-1' => '021 512 755', 'country-1' => '1228', 'state_province-1' => '1021', - ); + ]; return $profileData; } @@ -862,12 +885,12 @@ public function _createContactWithActivity() { CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1'); $sourceContactId = $this->individualCreate(); - $contactParams = array( + $contactParams = [ 'first_name' => 'abc1', 'last_name' => 'xyz1', 'contact_type' => 'Individual', 'email' => 'abc1.xyz1@yahoo.com', - 'api.address.create' => array( + 'api.address.create' => [ 'location_type_id' => 1, 'is_primary' => 1, 'name' => 'Saint Helier St', @@ -877,8 +900,8 @@ public function _createContactWithActivity() { 'supplemental_address_1' => 'Hallmark Ct', 'supplemental_address_2' => 'Jersey Village', 'supplemental_address_3' => 'My Town', - ), - ); + ], + ]; $contact = $this->callAPISuccess('contact', 'create', $contactParams); @@ -888,31 +911,31 @@ public function _createContactWithActivity() { $this->assertEquals(0, $contact['values'][$contactId]['api.address.create']['is_error'], " error message: " . CRM_Utils_Array::value('error_message', $contact['values'][$contactId]['api.address.create']) ); - $activityParams = array( + $activityParams = [ 'source_contact_id' => $sourceContactId, 'assignee_contact_id' => $contactId, 'activity_type_id' => '1', 'subject' => 'Make-it-Happen Meeting', 'activity_date_time' => '2011-03-16 00:00:00', 'duration' => '120', - 'location' => 'Pensulvania', + 'location' => 'Pennsylvania', 'details' => 'a test activity', 'status_id' => '1', 'priority_id' => '1', - ); + ]; $activity = $this->callAPISuccess('activity', 'create', $activityParams); $activityValues = array_pop($activity['values']); // valid parameters for above profile - $profileParams = array( + $profileParams = [ 'profile_id' => 26, 'contact_id' => $contactId, 'activity_id' => $activityValues['id'], - ); + ]; // expected result of above created profile - $expected = array( + $expected = [ 'first_name' => 'abc1', 'last_name' => 'xyz1', 'email-Primary' => 'abc1.xyz1@yahoo.com', @@ -921,43 +944,43 @@ public function _createContactWithActivity() { 'activity_duration' => '120', 'activity_date_time' => '2011-03-16 00:00:00', 'activity_status_id' => '1', - ); + ]; - return array($profileParams, $expected); + return [$profileParams, $expected]; } /** * Create a profile. */ public function _createIndividualProfile() { - $ufGroupParams = array( + $ufGroupParams = [ 'group_type' => 'Individual,Contact', // really we should remove this & test the ufField create sets it 'name' => 'test_individual_contact_profile', 'title' => 'Flat Coffee', - 'api.uf_field.create' => array( - array( + 'api.uf_field.create' => [ + [ 'field_name' => 'first_name', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', 'field_type' => 'Individual', 'label' => 'First Name', - ), - array( + ], + [ 'field_name' => 'last_name', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', 'field_type' => 'Individual', 'label' => 'Last Name', - ), - array( + ], + [ 'field_name' => 'email', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'label' => 'Email', - ), - array( + ], + [ 'field_name' => 'phone', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', @@ -965,32 +988,32 @@ public function _createIndividualProfile() { 'location_type_id' => 1, 'phone_type_id' => 1, 'label' => 'Phone', - ), - array( + ], + [ 'field_name' => 'country', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'location_type_id' => 1, 'label' => 'Country', - ), - array( + ], + [ 'field_name' => 'state_province', 'is_required' => 1, 'visibility' => 'Public Pages and Listings', 'field_type' => 'Contact', 'location_type_id' => 1, 'label' => 'State Province', - ), - array( + ], + [ 'field_name' => 'postal_code', 'is_required' => 0, 'field_type' => 'Contact', 'location_type_id' => 1, 'label' => 'State Province', - ), - ), - ); + ], + ], + ]; $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams); $this->_profileID = $profile['id']; } @@ -1000,11 +1023,11 @@ public function _createIndividualProfile() { */ public function _addCustomFieldToProfile($profileID) { $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, ''); - $this->uFFieldCreate(array( + $this->uFFieldCreate([ 'uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact', - )); + ]); } }