From bb2b2399ac7f0c3ab4ca69bf6c79f6d874b24e00 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 11 Jan 2023 09:18:41 +1300 Subject: [PATCH] Fix undefined property, general test cleanup --- tests/phpunit/api/v3/ProfileTest.php | 114 +++++++++------------------ 1 file changed, 38 insertions(+), 76 deletions(-) diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index 0a051c9f9f..44dc57c1fb 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -26,8 +26,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Set up for test. - * - * @throws \CRM_Core_Exception */ public function setUp(): void { parent::setUp(); @@ -41,8 +39,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Cleanup after test. - * - * @throws \CRM_Core_Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); @@ -77,11 +73,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check with success. - * - * @throws \CRM_Core_Exception */ public function testProfileGet(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $expected = reset($profileFieldValues); $contactId = key($profileFieldValues); $params = [ @@ -99,20 +93,18 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * * We are checking that there is no error. * - * @throws \CRM_Core_Exception */ public function testProfileGetWithAddressCustomData(): void { - $this->_createIndividualContact(); - $this->entity = 'Address'; + $this->createIndividualContact(); $this->createCustomGroupWithFieldOfType(['extends' => 'Address']); $this->callAPISuccess('UFField', 'create', [ 'uf_group_id' => $this->_profileID, - 'field_name' => $this->getCustomFieldName('text'), + 'field_name' => $this->getCustomFieldName(), 'visibility' => 'Public Pages and Listings', 'label' => 'My custom field', 'field_type' => 'Contact', ]); - $this->callAPISuccess('Address', 'get', ['contact_id' => $this->_contactID, 'api.Address.create' => [$this->getCustomFieldName('text') => 'my field']]); + $this->callAPISuccess('Address', 'get', ['contact_id' => $this->_contactID, 'api.Address.create' => [$this->getCustomFieldName() => 'my field']]); $this->callAPISuccess('Profile', 'get', ['profile_id' => $this->_profileID, 'contact_id' => $this->_contactID])['values']; } @@ -120,7 +112,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Test getting multiple profiles. */ public function testProfileGetMultiple(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $expected = reset($profileFieldValues); $contactId = key($profileFieldValues); $params = [ @@ -150,11 +142,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Test getting billing profile filled using is_billing. - * - * @throws \CRM_Core_Exception */ public function testProfileGetBillingUseIsBillingLocation(): void { - $individual = $this->_createIndividualContact(); + $individual = $this->createIndividualContact(); $contactId = key($individual); $this->callAPISuccess('address', 'create', [ 'is_billing' => 1, @@ -186,11 +176,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Test getting multiple profiles, including billing. - * - * @throws \CRM_Core_Exception */ public function testProfileGetMultipleHasBillingLocation(): void { - $individual = $this->_createIndividualContact(); + $individual = $this->createIndividualContact(); $contactId = key($individual); $this->callAPISuccess('address', 'create', [ 'contact_id' => $contactId, @@ -229,7 +217,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Get Billing empty contact - this will return generic defaults. * - * @throws \CRM_Core_Exception */ public function testProfileGetBillingEmptyContact(): void { $this->callAPISuccess('Setting', 'create', ['defaultContactCountry' => 1228]); @@ -256,7 +243,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check contact activity profile without activity id. */ public function testContactActivityGetWithoutActivityId(): void { - [$params] = $this->_createContactWithActivity(); + [$params] = $this->createContactWithActivity(); unset($params['activity_id']); $this->callAPIFailure('profile', 'get', $params, 'Mandatory key(s) missing from params array: activity_id'); @@ -266,7 +253,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check contact activity profile wrong activity id. */ public function testContactActivityGetWrongActivityId(): void { - [$params] = $this->_createContactWithActivity(); + [$params] = $this->createContactWithActivity(); $params['activity_id'] = 100001; $this->callAPIFailure('profile', 'get', $params, 'Invalid Activity Id (aid).'); } @@ -291,7 +278,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $activityValues = array_pop($activity); - [$params] = $this->_createContactWithActivity(); + [$params] = $this->createContactWithActivity(); $params['activity_id'] = $activityValues['id']; $this->callAPIFailure('profile', 'get', $params, 'This activity cannot be edited or viewed via this profile.'); @@ -299,11 +286,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check contact activity profile with success. - * - * @throws \CRM_Core_Exception */ public function testContactActivityGetSuccess(): void { - [$params, $expected] = $this->_createContactWithActivity(); + [$params, $expected] = $this->createContactWithActivity(); $result = $this->callAPISuccess('profile', 'get', $params); @@ -317,8 +302,8 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check getfields works & gives us our fields */ public function testGetFields(): void { - $this->_createIndividualProfile(); - $this->_addCustomFieldToProfile($this->_profileID); + $this->createIndividualProfile(); + $this->addCustomFieldToProfile($this->_profileID); $result = $this->callAPIAndDocument('profile', 'getfields', [ 'action' => 'submit', 'profile_id' => $this->_profileID, @@ -333,9 +318,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { } /** - * Check getfields works & gives us our fields - participant profile - * - * @throws \CRM_Core_Exception + * Check getfields works & gives us our fields - participant profile. */ public function testGetFieldsParticipantProfile(): void { $result = $this->callAPISuccess('profile', 'getfields', [ @@ -350,9 +333,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check getfields works & gives us our fields - membership_batch_entry - * (getting to the end with no e-notices is pretty good evidence it's working) - * - * @throws \CRM_Core_Exception + * (getting to the end with no e-notices is pretty good evidence it's working). */ public function testGetFieldsMembershipBatchProfile(): void { $result = $this->callAPISuccess('profile', 'getfields', [ @@ -374,8 +355,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check getfields works & gives us our fields - do them all * (getting to the end with no e-notices is pretty good evidence it's working) - * - * @throws \CRM_Core_Exception */ public function testGetFieldsAllProfiles(): void { $result = $this->callAPISuccess('uf_group', 'get', ['return' => 'id'])['values']; @@ -414,11 +393,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check with missing required field in profile. - * - * @throws \CRM_Core_Exception */ public function testProfileSubmitCheckProfileRequired(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $contactId = key($profileFieldValues); $updateParams = [ 'first_name' => 'abc2', @@ -442,11 +419,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check with success. - * - * @throws \CRM_Core_Exception */ public function testProfileSubmit(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $contactId = key($profileFieldValues); $updateParams = [ @@ -486,8 +461,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * 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 problems. - * - * @throws \CRM_Core_Exception */ public function testProfileSubmitCheckCaching(): void { $this->callAPISuccess('membership_type', 'delete', ['id' => $this->_membershipTypeID]); @@ -511,8 +484,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Test that the fields are returned in the right order despite the faffing * around that goes on. - * - * @throws \CRM_Core_Exception */ public function testMembershipGetFieldsOrder(): void { $result = $this->callAPISuccess('profile', 'getfields', [ @@ -530,9 +501,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { } /** - * Check we can submit membership batch profiles (create mode) - * - * @throws \CRM_Core_Exception + * Check we can submit membership batch profiles (create mode). */ public function testProfileSubmitMembershipBatch(): void { // @todo - figure out why this doesn't pass validate financials @@ -552,11 +521,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Set is deprecated but we need to ensure it still works. - * - * @throws \CRM_Core_Exception */ public function testLegacySet(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $contactId = key($profileFieldValues); $updateParams = [ @@ -591,7 +558,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check contact activity profile without activity id. */ public function testContactActivitySubmitWithoutActivityId(): void { - [$params, $expected] = $this->_createContactWithActivity(); + [$params, $expected] = $this->createContactWithActivity(); $params = array_merge($params, $expected); unset($params['activity_id']); @@ -602,7 +569,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check contact activity profile wrong activity id. */ public function testContactActivitySubmitWrongActivityId(): void { - [$params, $expected] = $this->_createContactWithActivity(); + [$params, $expected] = $this->createContactWithActivity(); $params = array_merge($params, $expected); $params['activity_id'] = 100001; $this->callAPIFailure('profile', 'submit', $params, 'Invalid Activity Id (aid).'); @@ -633,7 +600,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $activityValues = array_pop($activity['values']); - [$params, $expected] = $this->_createContactWithActivity(); + [$params, $expected] = $this->createContactWithActivity(); $params = array_merge($params, $expected); $params['activity_id'] = $activityValues['id']; @@ -643,11 +610,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check contact activity profile with success. - * - * @throws \CRM_Core_Exception */ public function testContactActivitySubmitSuccess(): void { - [$params] = $this->_createContactWithActivity(); + [$params] = $this->createContactWithActivity(); $updateParams = [ 'first_name' => 'abc2', @@ -695,7 +660,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * Check with success. */ public function testProfileApply(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $contactId = key($profileFieldValues); $params = [ @@ -753,11 +718,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check success with tags. - * - * @throws \CRM_Core_Exception */ public function testSubmitWithTags(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $params = reset($profileFieldValues); $contactId = key($profileFieldValues); $params['profile_id'] = $this->_profileID; @@ -800,7 +763,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * @throws \Exception */ public function testSubmitWithNote(): void { - $profileFieldValues = $this->_createIndividualContact(); + $profileFieldValues = $this->createIndividualContact(); $params = reset($profileFieldValues); $contactId = key($profileFieldValues); $params['profile_id'] = $this->_profileID; @@ -826,8 +789,8 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testSubmitGreetingFields() { - $profileFieldValues = $this->_createIndividualContact(); + public function testSubmitGreetingFields(): void { + $profileFieldValues = $this->createIndividualContact(); $params = reset($profileFieldValues); $contactId = key($profileFieldValues); $params['profile_id'] = $this->_profileID; @@ -841,7 +804,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { 'label' => 'Email Greeting', ]); - $emailGreetings = array_column(civicrm_api3('OptionValue', 'get', ['option_group_id' => "email_greeting"])['values'], NULL, 'name'); + $emailGreetings = array_column(civicrm_api3('OptionValue', 'get', ['option_group_id' => 'email_greeting'])['values'], NULL, 'name'); $params['email_greeting'] = $emailGreetings['Customized']['value']; // Custom greeting should be required @@ -853,7 +816,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { // Api3 will not return custom greeting field so resorting to this $greeting = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'email_greeting_custom'); - $this->assertEquals("Hello fool!", $greeting); + $this->assertEquals('Hello fool!', $greeting); } /** @@ -862,10 +825,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { * * @param array $params * - * @return mixed - * @throws \CRM_Core_Exception + * @return array */ - public function _createIndividualContact($params = []) { + public function createIndividualContact(array $params = []): array { $contactParams = array_merge([ 'first_name' => 'abc1', 'last_name' => 'xyz1', @@ -893,7 +855,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { ], $params); $this->_contactID = $this->individualCreate($contactParams); - $this->_createIndividualProfile(); + $this->createIndividualProfile(); // expected result of above created profile with contact Id $contactId $profileData[$this->_contactID] = [ 'first_name' => 'abc1', @@ -910,7 +872,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * @return array */ - public function _createContactWithActivity() { + public function createContactWithActivity(): array { $ufGroupID = $this->callAPISuccess('UFGroup', 'create', [ 'group_type' => 'Individual,Contact,Activity', 'title' => 'Test Contact-Activity Profile', @@ -987,7 +949,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { ]); // hack: xml data set did not accept  (CRM_Core_DAO::VALUE_SEPARATOR) - should be possible - // to unhack now we use the api. + // to un-hack now we use the api. CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $ufGroupID, 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1'); $sourceContactId = $this->individualCreate(); @@ -1014,7 +976,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $keys = array_keys($contact['values']); $contactId = array_pop($keys); - $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']) + $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 = [ @@ -1058,7 +1020,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Create a profile. */ - public function _createIndividualProfile() { + public function createIndividualProfile(): void { $ufGroupParams = [ 'group_type' => 'Individual,Contact', // really we should remove this & test the ufField create sets it @@ -1127,7 +1089,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * @param int $profileID */ - public function _addCustomFieldToProfile($profileID) { + public function addCustomFieldToProfile(int $profileID): void { $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, ''); $this->uFFieldCreate([ 'uf_group_id' => $profileID, -- 2.25.1