From: eileen Date: Tue, 9 Jun 2020 07:50:02 +0000 (+1200) Subject: [Ref] Unit test attempt to create reported bugs , minor refactor X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=24a67831e7d2c481f629114798221c9a9ba86c39;p=civicrm-core.git [Ref] Unit test attempt to create reported bugs , minor refactor This lays the ground work to test a couple of reported bugs. Minor cleanup of code --- diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 42582dac5c..18cffb95a5 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -897,13 +897,11 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty * * @return array * array of importable Fields + * @throws \CRM_Core_Exception */ - public static function &importableFields($contactType = 'Individual', $status = TRUE) { - if (!self::$_importableFields) { - if (!self::$_importableFields) { - self::$_importableFields = []; - } - + public static function importableFields($contactType = 'Individual', $status = TRUE) { + $fields = Civi::cache('fields')->get('membership_importable_fields' . $contactType . $status); + if (!$fields) { if (!$status) { $fields = ['' => ['title' => '- ' . ts('do not import') . ' -']]; } @@ -941,16 +939,16 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty } } $tmpContactField['external_identifier'] = $contactFields['external_identifier']; - $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " " . ts('(match to contact)'); + $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)'); - $tmpFields['membership_contact_id']['title'] = $tmpFields['membership_contact_id']['title'] . " " . ts('(match to contact)'); + $tmpFields['membership_contact_id']['title'] .= ' ' . ts('(match to contact)'); $fields = array_merge($fields, $tmpContactField); $fields = array_merge($fields, $tmpFields); $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Membership')); - self::$_importableFields = $fields; + Civi::cache('fields')->set('membership_importable_fields' . $contactType . $status, $fields); } - return self::$_importableFields; + return $fields; } /** diff --git a/CRM/Member/Import/Parser.php b/CRM/Member/Import/Parser.php index fb47d1f458..045d336a84 100644 --- a/CRM/Member/Import/Parser.php +++ b/CRM/Member/Import/Parser.php @@ -13,6 +13,8 @@ * * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing + * + * Class CRM_Member_Import_Parser */ abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index b43b507705..50e75e19ee 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -125,7 +125,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser { */ public function summary(&$values) { $erroneousField = NULL; - $response = $this->setActiveFieldValues($values, $erroneousField); + $this->setActiveFieldValues($values, $erroneousField); $errorRequired = FALSE; @@ -268,7 +268,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser { } $session = CRM_Core_Session::singleton(); - $dateType = $session->get('dateTypes'); + $dateType = CRM_Core_Session::singleton()->get('dateTypes'); $formatted = []; $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership'; $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index ec0cee5f8f..ef0d0e99bd 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1217,6 +1217,7 @@ Expires: ', * @param int $contactId Id of contact on which the memberships will be created. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) { $form = $this->getForm(); diff --git a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php index efc8748ce0..59de09c1ce 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php @@ -31,6 +31,8 @@ * @group headless */ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; + /** * Membership type name used in test function. * @@ -79,8 +81,8 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { 'fixed_period_start_day' => 101, 'fixed_period_rollover_day' => 1231, ]; - $ids = []; - $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); + + $membershipType = CRM_Member_BAO_MembershipType::add($params); $this->_membershipTypeID = $membershipType->id; $this->_mebershipStatusID = $this->membershipStatusCreate('test status'); @@ -102,10 +104,10 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { 'civicrm_membership_payment', 'civicrm_contact', ]; - $this->quickCleanup($tablesToTruncate, TRUE); $this->relationshipTypeDelete($this->_relationshipTypeId); $this->membershipTypeDelete(['id' => $this->_membershipTypeID]); $this->membershipStatusDelete($this->_mebershipStatusID); + $this->quickCleanup($tablesToTruncate, TRUE); } /** @@ -328,4 +330,35 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { return $membershipImporter; } + /** + * Test importing to a custom field. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testImportCustomData() { + $donaldDuckID = $this->individualCreate(['first_name' => 'Donald', 'last_name' => 'Duck']); + $this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Membership']); + $membershipImporter = $this->createImportObject([ + 'membership_contact_id', + 'membership_type_id', + 'membership_start_date', + $this->getCustomFieldName('text'), + $this->getCustomFieldName('select_string'), + ]); + $importValues = [ + $donaldDuckID, + $this->_membershipTypeID, + date('Y-m-d'), + 'blah', + 'Red', + ]; + + $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues); + $this->assertEquals(CRM_Import_Parser::VALID, $importResponse); + $membership = $this->callAPISuccessGetSingle('Membership', []); + $this->assertEquals('blah', $membership[$this->getCustomFieldName('text')]); + $this->assertEquals('R', $membership[$this->getCustomFieldName('select_string')]); + } + }