From 24a67831e7d2c481f629114798221c9a9ba86c39 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 9 Jun 2020 19:50:02 +1200 Subject: [PATCH] [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 --- CRM/Member/BAO/Membership.php | 18 ++++----- CRM/Member/Import/Parser.php | 2 + CRM/Member/Import/Parser/Membership.php | 4 +- .../CRM/Member/Form/MembershipTest.php | 1 + .../Member/Import/Parser/MembershipTest.php | 39 +++++++++++++++++-- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 42582dac5cdc..18cffb95a5a6 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -897,13 +897,11 @@ public static function getContactMembership($contactID, $memType, $isTest, $memb * * @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 @@ public static function &importableFields($contactType = 'Individual', $status = } } $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 fb47d1f458a2..045d336a847e 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 b43b507705f7..50e75e19ee31 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -125,7 +125,7 @@ public function preview(&$values) { */ public function summary(&$values) { $erroneousField = NULL; - $response = $this->setActiveFieldValues($values, $erroneousField); + $this->setActiveFieldValues($values, $erroneousField); $errorRequired = FALSE; @@ -268,7 +268,7 @@ public function import($onDuplicate, &$values) { } $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 ec0cee5f8f1f..ef0d0e99bda8 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1217,6 +1217,7 @@ protected function getBaseSubmitParams() { * @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 efc8748ce0c0..59de09c1ce9f 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 @@ public function setUp() { '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 @@ public function tearDown() { '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 @@ protected function createImportObject(array $fields): \CRM_Member_Import_Parser_ 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')]); + } + }