From ce14544c26113b35836a5981c2dfd41d55bc8e44 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 18 Jul 2018 15:24:52 +1200 Subject: [PATCH] Extract code for getting additional return properties, test Fix for test fail when full suite run, make sure Case is enabled, fix params --- CRM/Export/BAO/Export.php | 30 +- CRM/Export/BAO/ExportProcessor.php | 36 ++ tests/phpunit/CRM/Export/BAO/ExportTest.php | 445 ++++++++++++++++++++ 3 files changed, 484 insertions(+), 27 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 449d66a8ce..f6f655a9f0 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -269,23 +269,19 @@ class CRM_Export_BAO_Export { case CRM_Contact_BAO_Query::MODE_EVENT: $paymentFields = TRUE; $paymentTableId = 'participant_id'; - $extraReturnProperties = array(); break; case CRM_Contact_BAO_Query::MODE_MEMBER: $paymentFields = TRUE; $paymentTableId = 'membership_id'; - $extraReturnProperties = array(); break; case CRM_Contact_BAO_Query::MODE_PLEDGE: - $extraReturnProperties = CRM_Pledge_BAO_Query::extraReturnProperties($queryMode); $paymentFields = TRUE; $paymentTableId = 'pledge_payment_id'; break; case CRM_Contact_BAO_Query::MODE_CASE: - $extraReturnProperties = CRM_Case_BAO_Query::extraReturnProperties($queryMode); $paymentFields = FALSE; $paymentTableId = ''; break; @@ -293,12 +289,10 @@ class CRM_Export_BAO_Export { default: $paymentFields = FALSE; $paymentTableId = ''; - $extraReturnProperties = array(); } $extraProperties = array( 'paymentFields' => $paymentFields, 'paymentTableId' => $paymentTableId, - 'extraReturnProperties' => $extraReturnProperties, ); return $extraProperties; } @@ -444,13 +438,7 @@ class CRM_Export_BAO_Export { } } else { - $returnProperties = [ - 'location_type' => 1, - 'im_provider' => 1, - 'phone_type_id' => 1, - 'provider_id' => 1, - 'current_employer' => 1, - ]; + $returnProperties = []; $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE); foreach ($fields as $key => $var) { if ($key && (substr($key, 0, 6) != 'custom')) { @@ -461,23 +449,11 @@ class CRM_Export_BAO_Export { $extraProperties = self::defineExtraProperties($queryMode); $paymentFields = $extraProperties['paymentFields']; - $extraReturnProperties = $extraProperties['extraReturnProperties']; $paymentTableId = $extraProperties['paymentTableId']; - if ($queryMode != CRM_Contact_BAO_Query::MODE_CONTACTS) { - $componentReturnProperties = CRM_Contact_BAO_Query::defaultReturnProperties($queryMode); - if ($queryMode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { - // soft credit columns are not automatically populated, because contribution search doesn't require them by default - $componentReturnProperties = array_merge( - $componentReturnProperties, - CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE)); - } - $returnProperties = array_merge($returnProperties, $componentReturnProperties); - - if (!empty($extraReturnProperties)) { - $returnProperties = array_merge($returnProperties, $extraReturnProperties); - } + $returnProperties = array_merge($returnProperties, $processor->getAdditionalReturnProperties()); + if ($queryMode != CRM_Contact_BAO_Query::MODE_CONTACTS) { // unset non exportable fields for components $nonExpoFields = array( 'groups', diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 8479f038a3..fa8df0352a 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -182,4 +182,40 @@ class CRM_Export_BAO_ExportProcessor { return array($query, $select, $from, $where, $having); } + /** + * Get array of fields to return, over & above those defined in the main contact exportable fields. + * + * These include export mode specific fields & some fields apparently required as 'exportableFields' + * but not returned by the function of the same name. + * + * @return array + * Array of fields to return in the format ['field_name' => 1,...] + */ + public function getAdditionalReturnProperties() { + + $missing = [ + 'location_type', + 'im_provider', + 'phone_type_id', + 'provider_id', + 'current_employer', + ]; + if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) { + $componentSpecificFields = []; + } + else { + $componentSpecificFields = CRM_Contact_BAO_Query::defaultReturnProperties($this->getQueryMode()); + } + if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_PLEDGE) { + $componentSpecificFields = array_merge($componentSpecificFields, CRM_Pledge_BAO_Query::extraReturnProperties($this->getQueryMode())); + } + if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CASE) { + $componentSpecificFields = array_merge($componentSpecificFields, CRM_Case_BAO_Query::extraReturnProperties($this->getQueryMode())); + } + if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { + $componentSpecificFields = array_merge($componentSpecificFields, CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE)); + } + return array_merge(array_fill_keys($missing, 1), $componentSpecificFields); + } + } diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 50c8d0a173..2fc5a0f405 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -876,6 +876,451 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { } } + /** + * Test our export all field metadata retrieval. + * + * @dataProvider additionalFieldsDataProvider + * @param int $exportMode + * @param $expected + */ + public function testAdditionalReturnProperties($exportMode, $expected) { + $this->ensureComponentIsEnabled($exportMode); + $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND'); + $metadata = $processor->getAdditionalReturnProperties(); + $this->assertEquals($expected, $metadata); + } + + /** + * Test our export all field metadata retrieval. + * + * @dataProvider allFieldsDataProvider + * @param int $exportMode + * @param $expected + */ + public function testDefaultReturnProperties($exportMode, $expected) { + $this->ensureComponentIsEnabled($exportMode); + $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND'); + $metadata = $processor->getDefaultReturnProperties(); + $this->assertEquals($expected, $metadata); + } + + /** + * Get fields returned from additionalFields function. + * + * @return array + */ + public function additionalFieldsDataProvider() { + return [ + [ + 'anything that will then be defaulting ton contact', + $this->getExtraReturnProperties(), + ], + [ + CRM_Export_Form_Select::ACTIVITY_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getActivityReturnProperties()), + ], + [ + CRM_Export_Form_Select::CASE_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getCaseReturnProperties()), + ], + [ + CRM_Export_Form_Select::CONTRIBUTE_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getContributionReturnProperties()), + ], + [ + CRM_Export_Form_Select::EVENT_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getEventReturnProperties()), + ], + [ + CRM_Export_Form_Select::MEMBER_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getMembershipReturnProperties()), + ], + [ + CRM_Export_Form_Select::PLEDGE_EXPORT, + array_merge($this->getExtraReturnProperties(), $this->getPledgeReturnProperties()), + ], + + ]; + } + + /** + * get data for testing field metadata by query mode. + */ + public function allFieldsDataProvider() { + return [ + [ + 'anything that will then be defaulting ton contact', + $this->getBasicReturnProperties(TRUE), + ], + [ + CRM_Export_Form_Select::ACTIVITY_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getActivityReturnProperties()), + ], + [ + CRM_Export_Form_Select::CASE_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getCaseReturnProperties()), + ], + [ + CRM_Export_Form_Select::CONTRIBUTE_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getContributionReturnProperties()), + ], + [ + CRM_Export_Form_Select::EVENT_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getEventReturnProperties()), + ], + [ + CRM_Export_Form_Select::MEMBER_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getMembershipReturnProperties()), + ], + [ + CRM_Export_Form_Select::PLEDGE_EXPORT, + array_merge($this->getBasicReturnProperties(FALSE), $this->getPledgeReturnProperties()), + ], + ]; + } + + /** + * Get return properties manually added in. + */ + public function getExtraReturnProperties() { + return [ + 'location_type' => 1, + 'im_provider' => 1, + 'phone_type_id' => 1, + 'provider_id' => 1, + 'current_employer' => 1, + ]; + } + + /** + * Get basic return properties. + * + * @bool $isContactMode + * Are we in contact mode or not + * + * @return array + */ + protected function getBasicReturnProperties($isContactMode) { + $returnProperties = [ + 'id' => 1, + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'do_not_email' => 1, + 'do_not_phone' => 1, + 'do_not_mail' => 1, + 'do_not_sms' => 1, + 'do_not_trade' => 1, + 'is_opt_out' => 1, + 'legal_identifier' => 1, + 'external_identifier' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'nick_name' => 1, + 'legal_name' => 1, + 'image_URL' => 1, + 'preferred_communication_method' => 1, + 'preferred_language' => 1, + 'preferred_mail_format' => 1, + 'hash' => 1, + 'contact_source' => 1, + 'first_name' => 1, + 'middle_name' => 1, + 'last_name' => 1, + 'prefix_id' => 1, + 'suffix_id' => 1, + 'formal_title' => 1, + 'communication_style_id' => 1, + 'email_greeting_id' => 1, + 'postal_greeting_id' => 1, + 'addressee_id' => 1, + 'job_title' => 1, + 'gender_id' => 1, + 'birth_date' => 1, + 'is_deceased' => 1, + 'deceased_date' => 1, + 'household_name' => 1, + 'organization_name' => 1, + 'sic_code' => 1, + 'user_unique_id' => 1, + 'current_employer_id' => 1, + 'contact_is_deleted' => 1, + 'created_date' => 1, + 'modified_date' => 1, + 'addressee' => 1, + 'email_greeting' => 1, + 'postal_greeting' => 1, + 'current_employer' => 1, + 'location_type' => 1, + 'street_address' => 1, + 'street_number' => 1, + 'street_number_suffix' => 1, + 'street_name' => 1, + 'street_unit' => 1, + 'supplemental_address_1' => 1, + 'supplemental_address_2' => 1, + 'supplemental_address_3' => 1, + 'city' => 1, + 'postal_code_suffix' => 1, + 'postal_code' => 1, + 'geo_code_1' => 1, + 'geo_code_2' => 1, + 'address_name' => 1, + 'master_id' => 1, + 'county' => 1, + 'state_province' => 1, + 'country' => 1, + 'phone' => 1, + 'phone_ext' => 1, + 'email' => 1, + 'on_hold' => 1, + 'is_bulkmail' => 1, + 'signature_text' => 1, + 'signature_html' => 1, + 'im_provider' => 1, + 'im' => 1, + 'openid' => 1, + 'world_region' => 1, + 'url' => 1, + 'groups' => 1, + 'tags' => 1, + 'notes' => 1, + 'phone_type_id' => 1, + 'provider_id' => 1, + ]; + if (!$isContactMode) { + unset($returnProperties['groups']); + unset($returnProperties['tags']); + unset($returnProperties['notes']); + } + return $returnProperties; + } + + /** + * Get return properties for pledges. + * + * @return array + */ + public function getPledgeReturnProperties() { + return [ + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'pledge_id' => 1, + 'pledge_amount' => 1, + 'pledge_total_paid' => 1, + 'pledge_create_date' => 1, + 'pledge_start_date' => 1, + 'pledge_next_pay_date' => 1, + 'pledge_next_pay_amount' => 1, + 'pledge_status' => 1, + 'pledge_is_test' => 1, + 'pledge_contribution_page_id' => 1, + 'pledge_financial_type' => 1, + 'pledge_frequency_interval' => 1, + 'pledge_frequency_unit' => 1, + 'pledge_currency' => 1, + 'pledge_campaign_id' => 1, + 'pledge_balance_amount' => 1, + 'pledge_payment_id' => 1, + 'pledge_payment_scheduled_amount' => 1, + 'pledge_payment_scheduled_date' => 1, + 'pledge_payment_paid_amount' => 1, + 'pledge_payment_paid_date' => 1, + 'pledge_payment_reminder_date' => 1, + 'pledge_payment_reminder_count' => 1, + 'pledge_payment_status' => 1, + ]; + } + + /** + * Get membership return properties. + * + * @return array + */ + public function getMembershipReturnProperties() { + return [ + 'location_type' => 1, + 'im_provider' => 1, + 'phone_type_id' => 1, + 'provider_id' => 1, + 'current_employer' => 1, + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'membership_type' => 1, + 'member_is_test' => 1, + 'member_is_pay_later' => 1, + 'join_date' => 1, + 'membership_start_date' => 1, + 'membership_end_date' => 1, + 'membership_source' => 1, + 'membership_status' => 1, + 'membership_id' => 1, + 'owner_membership_id' => 1, + 'max_related' => 1, + 'membership_recur_id' => 1, + 'member_campaign_id' => 1, + 'member_is_override' => 1, + 'member_auto_renew' => 1, + ]; + } + + /** + * Get return properties for events. + * + * @return array + */ + public function getEventReturnProperties() { + return [ + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'event_id' => 1, + 'event_title' => 1, + 'event_start_date' => 1, + 'event_end_date' => 1, + 'event_type' => 1, + 'participant_id' => 1, + 'participant_status' => 1, + 'participant_status_id' => 1, + 'participant_role' => 1, + 'participant_role_id' => 1, + 'participant_note' => 1, + 'participant_register_date' => 1, + 'participant_source' => 1, + 'participant_fee_level' => 1, + 'participant_is_test' => 1, + 'participant_is_pay_later' => 1, + 'participant_fee_amount' => 1, + 'participant_discount_name' => 1, + 'participant_fee_currency' => 1, + 'participant_registered_by_id' => 1, + 'participant_campaign_id' => 1, + ]; + } + + /** + * Get return properties for activities. + * + * @return array + */ + public function getActivityReturnProperties() { + return [ + 'activity_id' => 1, + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'activity_type' => 1, + 'activity_type_id' => 1, + 'activity_subject' => 1, + 'activity_date_time' => 1, + 'activity_duration' => 1, + 'activity_location' => 1, + 'activity_details' => 1, + 'activity_status' => 1, + 'activity_priority' => 1, + 'source_contact' => 1, + 'source_record_id' => 1, + 'activity_is_test' => 1, + 'activity_campaign_id' => 1, + 'result' => 1, + 'activity_engagement_level' => 1, + 'parent_id' => 1, + ]; + } + + /** + * Get return properties for Case. + * + * @return array + */ + public function getCaseReturnProperties() { + return [ + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'phone' => 1, + 'case_start_date' => 1, + 'case_end_date' => 1, + 'case_subject' => 1, + 'case_source_contact_id' => 1, + 'case_activity_status' => 1, + 'case_activity_duration' => 1, + 'case_activity_medium_id' => 1, + 'case_activity_details' => 1, + 'case_activity_is_auto' => 1, + 'contact_id' => 1, + 'case_id' => 1, + 'case_activity_subject' => 1, + 'case_status' => 1, + 'case_type' => 1, + 'case_role' => 1, + 'case_deleted' => 1, + 'case_recent_activity_date' => 1, + 'case_recent_activity_type' => 1, + 'case_scheduled_activity_date' => 1, + ]; + } + + /** + * Get return properties for contribution. + * + * @return array + */ + public function getContributionReturnProperties() { + return [ + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + 'display_name' => 1, + 'financial_type' => 1, + 'contribution_source' => 1, + 'receive_date' => 1, + 'thankyou_date' => 1, + 'cancel_date' => 1, + 'total_amount' => 1, + 'accounting_code' => 1, + 'payment_instrument' => 1, + 'payment_instrument_id' => 1, + 'contribution_check_number' => 1, + 'non_deductible_amount' => 1, + 'fee_amount' => 1, + 'net_amount' => 1, + 'trxn_id' => 1, + 'invoice_id' => 1, + 'invoice_number' => 1, + 'currency' => 1, + 'cancel_reason' => 1, + 'receipt_date' => 1, + 'product_name' => 1, + 'sku' => 1, + 'product_option' => 1, + 'fulfilled_date' => 1, + 'contribution_start_date' => 1, + 'contribution_end_date' => 1, + 'is_test' => 1, + 'is_pay_later' => 1, + 'contribution_status' => 1, + 'contribution_recur_id' => 1, + 'amount_level' => 1, + 'contribution_note' => 1, + 'contribution_batch' => 1, + 'contribution_campaign_title' => 1, + 'contribution_campaign_id' => 1, + 'contribution_product_id' => 1, + 'contribution_soft_credit_name' => 1, + 'contribution_soft_credit_amount' => 1, + 'contribution_soft_credit_type' => 1, + 'contribution_soft_credit_contact_id' => 1, + 'contribution_soft_credit_contribution_id' => 1, + ]; + } + /** * Test the column definition when 'all' fields defined. * -- 2.25.1