From 7865d848a2d955823acbebb693ac70680845edff Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 12 May 2015 14:42:59 +1200 Subject: [PATCH] CRM-16368 refactoring by Lola on membership form --- CRM/Member/Form.php | 47 +- CRM/Member/Form/Membership.php | 481 ++++++++++-------- .../CRM/Member/Form/MembershipTest.php | 167 +++++- 3 files changed, 445 insertions(+), 250 deletions(-) diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 72c1bdcadc..1a1901f930 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -59,25 +59,17 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { protected $_fromEmails = array(); public function preProcess() { - $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); - $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'membership'); - $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); - $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); - $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); + $params = array(); + $params['action'] = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); + $params['context'] = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'membership'); + $params['id'] = CRM_Utils_Request::retrieve('id', 'Positive', $this); + $params['mode'] = CRM_Utils_Request::retrieve('mode', 'String', $this); + + $this->setContextVariables($params); $this->assign('context', $this->_context); $this->assign('membershipMode', $this->_mode); $this->assign('contactID', $this->_contactID); - - if ($this->_mode) { - $this->assignPaymentRelatedVariables(); - } - - if ($this->_id) { - $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); - $this->_membershipIDs[] = $this->_id; - } - $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); } /** @@ -223,4 +215,29 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { } } + protected function setContextVariables($params) { + $variables = array( + 'action' => '_action', + 'context' => '_context', + 'id' => '_id', + 'cid' => '_contactID', + 'mode' => '_mode', + ); + foreach ($variables as $paramKey => $classVar) { + if (isset($params[$paramKey]) && !isset($this->$classVar)) { + $this->$classVar = $params[$paramKey]; + } + } + + if ($this->_mode) { + $this->assignPaymentRelatedVariables(); + } + + if ($this->_id) { + $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); + $this->_membershipIDs[] = $this->_id; + } + $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); + } + } diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 1aa6cd1e0f..7a075a1944 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -1028,32 +1028,222 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; /** * Process the form submission. - * - * - * @return void */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { CRM_Member_BAO_Membership::del($this->_id); return; } + // get the submitted form values. + $this->_params = $this->controller->exportValues($this->_name); - $allMemberStatus = CRM_Member_PseudoConstant::membershipStatus(); - $allContributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + $createdMemberships = $this->submit($this->_params); + + $this->setUserContext(); + } + + /** + * Send email receipt. + * + * @param CRM_Core_Form $form + * Form object. + * @param array $formValues + * @param object $membership + * Object. + * + * @return bool + * true if mail was sent successfully + */ + public static function emailReceipt(&$form, &$formValues, &$membership) { + // retrieve 'from email id' for acknowledgement + $receiptFrom = $formValues['from_email_address']; + + if (!empty($formValues['payment_instrument_id'])) { + $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); + $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']]; + } + + // retrieve custom data + $customFields = $customValues = array(); + if (property_exists($form, '_groupTree') + && !empty($form->_groupTree) + ) { + foreach ($form->_groupTree as $groupID => $group) { + if ($groupID == 'info') { + continue; + } + foreach ($group['fields'] as $k => $field) { + $field['title'] = $field['label']; + $customFields["custom_{$k}"] = $field; + } + } + } + + $members = array(array('member_id', '=', $membership->id, 0, 0)); + // check whether its a test drive + if ($form->_mode == 'test') { + $members[] = array('member_test', '=', 1, 0, 0); + } + + CRM_Core_BAO_UFGroup::getValues($formValues['contact_id'], $customFields, $customValues, FALSE, $members); + + if ($form->_mode) { + $name = ''; + if (!empty($form->_params['billing_first_name'])) { + $name = $form->_params['billing_first_name']; + } + + if (!empty($form->_params['billing_middle_name'])) { + $name .= " {$form->_params['billing_middle_name']}"; + } + + if (!empty($form->_params['billing_last_name'])) { + $name .= " {$form->_params['billing_last_name']}"; + } + + $form->assign('billingName', $name); + + // assign the address formatted up for display + $addressParts = array( + "street_address-{$form->_bltID}", + "city-{$form->_bltID}", + "postal_code-{$form->_bltID}", + "state_province-{$form->_bltID}", + "country-{$form->_bltID}", + ); + $addressFields = array(); + foreach ($addressParts as $part) { + list($n, $id) = explode('-', $part); + if (isset($form->_params['billing_' . $part])) { + $addressFields[$n] = $form->_params['billing_' . $part]; + } + } + $form->assign('address', CRM_Utils_Address::format($addressFields)); + $date = CRM_Utils_Date::format($form->_params['credit_card_exp_date']); + $date = CRM_Utils_Date::mysqlToIso($date); + $form->assign('credit_card_exp_date', $date); + $form->assign('credit_card_number', + CRM_Utils_System::mungeCreditCard($form->_params['credit_card_number']) + ); + $form->assign('credit_card_type', $form->_params['credit_card_type']); + $form->assign('contributeMode', 'direct'); + $form->assign('isAmountzero', 0); + $form->assign('is_pay_later', 0); + $form->assign('isPrimary', 1); + } + + $form->assign('module', 'Membership'); + $form->assign('contactID', $formValues['contact_id']); + + $form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues))); + + if (!empty($formValues['contribution_id'])) { + $form->assign('contributionID', $formValues['contribution_id']); + } + elseif (isset($form->_onlinePendingContributionId)) { + $form->assign('contributionID', $form->_onlinePendingContributionId); + } + + if (!empty($formValues['contribution_status_id'])) { + $form->assign('contributionStatusID', $formValues['contribution_status_id']); + $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name')); + } + + if (!empty($formValues['is_renew'])) { + $form->assign('receiptType', 'membership renewal'); + } + else { + $form->assign('receiptType', 'membership signup'); + } + $form->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $formValues))); + $form->assign('formValues', $formValues); + + if (empty($lineItem)) { + $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y')); + if (!CRM_Utils_System::isNull($membership->end_date)) { + $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y')); + } + $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id)); + } + + $form->assign('customValues', $customValues); + $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry'); + if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) { + // in this case the form is being called statically from the batch editing screen + // having one class in the form layer call another statically is not greate + // & we should aim to move this function to the BAO layer in future. + // however, we can assume that the contact_id passed in by the batch + // function will be the recipient + list($form->_contributorDisplayName, $form->_contributorEmail) + = CRM_Contact_BAO_Contact_Location::getEmailDetails($formValues['contact_id']); + if (empty($form->_receiptContactId) || $isBatchProcess) { + $form->_receiptContactId = $formValues['contact_id']; + } + } + $template = CRM_Core_Smarty::singleton(); + $taxAmt = $template->get_template_vars('dataArray'); + $eventTaxAmt = $template->get_template_vars('totalTaxAmount'); + $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); + $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); + if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { + $isEmailPdf = TRUE; + } + else { + $isEmailPdf = FALSE; + } + + list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( + array( + 'groupName' => 'msg_tpl_workflow_membership', + 'valueName' => 'membership_offline_receipt', + 'contactId' => $form->_receiptContactId, + 'from' => $receiptFrom, + 'toName' => $form->_contributorDisplayName, + 'toEmail' => $form->_contributorEmail, + 'PDFFilename' => ts('receipt') . '.pdf', + 'isEmailPdf' => $isEmailPdf, + 'contributionId' => $formValues['contribution_id'], + 'isTest' => (bool) ($form->_action & CRM_Core_Action::PREVIEW), + ) + ); + + return TRUE; + } + + /** + * @param $params array + + * @return array + */ + public function submit($formValues) { $isTest = ($this->_mode == 'test') ? 1 : 0; + $joinDate = $startDate = $endDate = NULL; + $membershipTypes = $membership = $calcDate = array(); + $memType = $membershipType = NULL; + + $receiptSend = $mailSend = FALSE; + + $params = $softParams = $ids = array(); + + $allMemberStatus = CRM_Member_PseudoConstant::membershipStatus(); + $allContributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + $lineItems = NULL; if (!empty($this->_lineItem)) { $lineItems = $this->_lineItem; } + // Set variables that we normally get from context. + // In form mode these are set in preProcess. + //TODO: set memberships, fixme + $this->setContextVariables($formValues); + $self->_memTypeSelected = self::getSelectedMemberships($priceSetID, $params); + $config = CRM_Core_Config::singleton(); - // get the submitted form values. - $this->_params = $formValues = $this->controller->exportValues($this->_name); - $this->convertDateFieldsToMySQL($formValues); - $params = $softParams = $ids = array(); + $this->convertDateFieldsToMySQL($formValues); $membershipTypeValues = array(); foreach ($this->_memTypeSelected as $memType) { @@ -1061,7 +1251,7 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; } //take the required membership recur values. - if ($this->_mode && !empty($this->_params['auto_renew'])) { + if ($this->_mode && !empty($formValues['auto_renew'])) { $params['is_recur'] = $this->_params['is_recur'] = $formValues['is_recur'] = TRUE; $mapping = array( 'frequency_interval' => 'duration_interval', @@ -1253,9 +1443,9 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; if (!$this->_onlinePendingContributionId) { if (empty($formValues['source'])) { $params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', array( - 1 => $membershipType, - 2 => $userName, - )); + 1 => $membershipType, + 2 => $userName, + )); } else { $params['contribution_source'] = $formValues['source']; @@ -1590,14 +1780,6 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; // suppress form values in template. $this->assign('cancelled', $cancelled); - // FIX ME: need to recheck this - // here we might updated dates, so get from object. - foreach ($calcDates[$membership->membership_type_id] as $date => & $val) { - if ($membership->$date) { - $val = $membership->$date; - } - } - $createdMemberships[] = $membership; } else { @@ -1692,57 +1874,31 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; // send email receipt $mailSend = self::emailReceipt($this, $formValues, $membership); } - $statusMsg = ''; - if (($this->_action & CRM_Core_Action::UPDATE)) { - //end date can be modified by hooks, so if end date is set then use it. - $endDate = ($membership->end_date) ? $membership->end_date : $endDate; - $statusMsg = ts('Membership for %1 has been updated.', array(1 => $this->_memberDisplayName)); - if ($endDate && $endDate !== 'null') { - $endDate = CRM_Utils_Date::customFormat($endDate); - $statusMsg .= ' ' . ts('The membership End Date is %1.', array(1 => $endDate)); - } - if ($receiptSend) { - $statusMsg .= ' ' . ts('A confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); - } + // finally set membership id if already not set + if (!$this->_id) { + $this->_id = $membership->id; } - elseif (($this->_action & CRM_Core_Action::ADD)) { - // FIX ME: fix status messages - - $statusMsg = array(); - foreach ($membershipTypes as $memType => $membershipType) { - $statusMsg[$memType] = ts('%1 membership for %2 has been added.', array( - 1 => $membershipType, - 2 => $this->_memberDisplayName, - )); - - $membership = $createdMemberships[$memType]; - $memEndDate = ($membership->end_date) ? $membership->end_date : $endDate; - //get the end date from calculated dates. - if (!$memEndDate && empty($params['is_recur'])) { - $memEndDate = CRM_Utils_Array::value('end_date', $calcDates[$memType]); - } + $statusMsg = ''; + if (($this->_action & CRM_Core_Action::UPDATE)) { + $statusMsg = $this->getStatusMessageForUpdate($membership, $endDate, $receiptSend); - if ($memEndDate && $memEndDate !== 'null') { - $memEndDate = CRM_Utils_Date::customFormat($memEndDate); - $statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', array(1 => $memEndDate)); - } - } - $statusMsg = implode('
', $statusMsg); - if ($receiptSend && $mailSend) { - $statusMsg .= ' ' . ts('A membership confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); - } } - - // finally set membership id if already not set - if (!$this->_id) { - $this->_id = $membership->id; + elseif (($this->_action & CRM_Core_Action::ADD)) { + $statusMsg = $this->getStatusMessageForCreate($endDate, $receiptSend, $membershipTypes, $createdMemberships, + $params, $calcDates, $mailSend); } CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); + return $createdMemberships; + } + + protected function setUserContext() { $buttonName = $this->controller->getButtonName(); + $session = CRM_Core_Session::singleton(); + if ($this->_context == 'standalone') { if ($buttonName == $this->getButtonName('upload', 'new')) { $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/add', @@ -1763,172 +1919,67 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; } /** - * Send email receipt. - * - * @param CRM_Core_Form $form - * Form object. - * @param array $formValues - * @param object $membership - * Object. - * - * @return bool - * true if mail was sent successfully + * @param $membership + * @param $endDate + * @param $receiptSend + * @return string */ - public static function emailReceipt(&$form, &$formValues, &$membership) { - // retrieve 'from email id' for acknowledgement - $receiptFrom = $formValues['from_email_address']; - - if (!empty($formValues['payment_instrument_id'])) { - $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); - $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']]; - } - - // retrieve custom data - $customFields = $customValues = array(); - if (property_exists($form, '_groupTree') - && !empty($form->_groupTree) - ) { - foreach ($form->_groupTree as $groupID => $group) { - if ($groupID == 'info') { - continue; - } - foreach ($group['fields'] as $k => $field) { - $field['title'] = $field['label']; - $customFields["custom_{$k}"] = $field; - } - } - } - - $members = array(array('member_id', '=', $membership->id, 0, 0)); - // check whether its a test drive - if ($form->_mode == 'test') { - $members[] = array('member_test', '=', 1, 0, 0); - } - - CRM_Core_BAO_UFGroup::getValues($formValues['contact_id'], $customFields, $customValues, FALSE, $members); - - if ($form->_mode) { - $name = ''; - if (!empty($form->_params['billing_first_name'])) { - $name = $form->_params['billing_first_name']; - } - - if (!empty($form->_params['billing_middle_name'])) { - $name .= " {$form->_params['billing_middle_name']}"; - } - - if (!empty($form->_params['billing_last_name'])) { - $name .= " {$form->_params['billing_last_name']}"; - } - - $form->assign('billingName', $name); - - // assign the address formatted up for display - $addressParts = array( - "street_address-{$form->_bltID}", - "city-{$form->_bltID}", - "postal_code-{$form->_bltID}", - "state_province-{$form->_bltID}", - "country-{$form->_bltID}", - ); - $addressFields = array(); - foreach ($addressParts as $part) { - list($n, $id) = explode('-', $part); - if (isset($form->_params['billing_' . $part])) { - $addressFields[$n] = $form->_params['billing_' . $part]; - } - } - $form->assign('address', CRM_Utils_Address::format($addressFields)); + protected function getStatusMessageForUpdate($membership, $endDate, $receiptSend) { + //end date can be modified by hooks, so if end date is set then use it. + $endDate = ($membership->end_date) ? $membership->end_date : $endDate; - $date = CRM_Utils_Date::format($form->_params['credit_card_exp_date']); - $date = CRM_Utils_Date::mysqlToIso($date); - $form->assign('credit_card_exp_date', $date); - $form->assign('credit_card_number', - CRM_Utils_System::mungeCreditCard($form->_params['credit_card_number']) - ); - $form->assign('credit_card_type', $form->_params['credit_card_type']); - $form->assign('contributeMode', 'direct'); - $form->assign('isAmountzero', 0); - $form->assign('is_pay_later', 0); - $form->assign('isPrimary', 1); + $statusMsg = ts('Membership for %1 has been updated.', array(1 => $this->_memberDisplayName)); + if ($endDate && $endDate !== 'null') { + $endDate = CRM_Utils_Date::customFormat($endDate); + $statusMsg .= ' ' . ts('The membership End Date is %1.', array(1 => $endDate)); } - $form->assign('module', 'Membership'); - $form->assign('contactID', $formValues['contact_id']); - - $form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues))); - - if (!empty($formValues['contribution_id'])) { - $form->assign('contributionID', $formValues['contribution_id']); - } - elseif (isset($form->_onlinePendingContributionId)) { - $form->assign('contributionID', $form->_onlinePendingContributionId); + if ($receiptSend) { + $statusMsg .= ' ' . ts('A confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); } + return $statusMsg; + } - if (!empty($formValues['contribution_status_id'])) { - $form->assign('contributionStatusID', $formValues['contribution_status_id']); - $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name')); - } + /** + * @param $endDate + * @param $receiptSend + * @param $membershipTypes + * @param $createdMemberships + * @param $params + * @param $calcDates + * @param $mailSent + * @internal param $membership + * @return array|string + */ + protected function getStatusMessageForCreate($endDate, $receiptSend, $membershipTypes, $createdMemberships, + $params, $calcDates, $mailSent) { + // FIX ME: fix status messages + + $statusMsg = array(); + foreach ($membershipTypes as $memType => $membershipType) { + $statusMsg[$memType] = ts('%1 membership for %2 has been added.', array( + 1 => $membershipType, + 2 => $this->_memberDisplayName, + )); - if (!empty($formValues['is_renew'])) { - $form->assign('receiptType', 'membership renewal'); - } - else { - $form->assign('receiptType', 'membership signup'); - } - $form->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $formValues))); - $form->assign('formValues', $formValues); + $membership = $createdMemberships[$memType]; + $memEndDate = ($membership->end_date) ? $membership->end_date : $endDate; - if (empty($lineItem)) { - $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y')); - if (!CRM_Utils_System::isNull($membership->end_date)) { - $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y')); + //get the end date from calculated dates. + if (!$memEndDate && empty($params['is_recur'])) { + $memEndDate = CRM_Utils_Array::value('end_date', $calcDates[$memType]); } - $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id)); - } - $form->assign('customValues', $customValues); - $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry'); - if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) { - // in this case the form is being called statically from the batch editing screen - // having one class in the form layer call another statically is not greate - // & we should aim to move this function to the BAO layer in future. - // however, we can assume that the contact_id passed in by the batch - // function will be the recipient - list($form->_contributorDisplayName, $form->_contributorEmail) - = CRM_Contact_BAO_Contact_Location::getEmailDetails($formValues['contact_id']); - if (empty($form->_receiptContactId) || $isBatchProcess) { - $form->_receiptContactId = $formValues['contact_id']; + if ($memEndDate && $memEndDate !== 'null') { + $memEndDate = CRM_Utils_Date::customFormat($memEndDate); + $statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', array(1 => $memEndDate)); } } - $template = CRM_Core_Smarty::singleton(); - $taxAmt = $template->get_template_vars('dataArray'); - $eventTaxAmt = $template->get_template_vars('totalTaxAmount'); - $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); - $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); - if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { - $isEmailPdf = TRUE; - } - else { - $isEmailPdf = FALSE; + $statusMsg = implode('
', $statusMsg); + if ($receiptSend && $mailSent) { + $statusMsg .= ' ' . ts('A membership confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); } - - list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( - array( - 'groupName' => 'msg_tpl_workflow_membership', - 'valueName' => 'membership_offline_receipt', - 'contactId' => $form->_receiptContactId, - 'from' => $receiptFrom, - 'toName' => $form->_contributorDisplayName, - 'toEmail' => $form->_contributorEmail, - 'PDFFilename' => ts('receipt') . '.pdf', - 'isEmailPdf' => $isEmailPdf, - 'contributionId' => $formValues['contribution_id'], - 'isTest' => (bool) ($form->_action & CRM_Core_Action::PREVIEW), - ) - ); - - return TRUE; + return $statusMsg; } } diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index 0c09c2f990..fb57392d60 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1,4 +1,29 @@ (801) 534-1262 - * @copyright Copyright CiviCRM LLC (C) 2009 - * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html - * GNU Affero General Public License version 3 - * @package CiviCRM - * - * This file is part of CiviCRM * - * CiviCRM is free software; you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * CiviCRM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this program. If not, see - * . */ /** @@ -36,12 +42,45 @@ require_once 'CiviTest/CiviUnitTestCase.php'; require_once 'HTML/QuickForm/Page.php'; /** - * Test APIv2 civicrm_activity_* functions + * Test CRM_Member_Form_Membership functions. * * @package CiviCRM */ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { + /** + * Assume empty database with just civicrm_data. + */ + protected $_individualId; + protected $_contribution; + protected $_financialTypeId = 1; + protected $_apiversion; + protected $_entity = 'Membership'; + protected $_params; + protected $_ids = array(); + protected $_paymentProcessorID; + + /** + * Parameters to create payment processor. + * + * @var array + */ + protected $_processorParams = array(); + + /** + * ID of created membership. + * + * @var int + */ + protected $_membershipID; + + /** + * Payment instrument mapping. + * + * @var array + */ + protected $paymentInstruments = array(); + /** * Test setup for every test. * @@ -62,6 +101,52 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { dirname(__FILE__) . '/dataset/data.xml' ) ); + + $this->_apiversion = 3; + $this->_individualId = $this->individualCreate(); + $this->_paymentProcessorID = $this->processorCreate(); + $this->_params = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '20120511', + 'total_amount' => 100.00, + 'financial_type_id' => $this->_financialTypeId, + 'non_deductible_amount' => 10.00, + 'fee_amount' => 5.00, + 'net_amount' => 95.00, + 'source' => 'SSF', + 'contribution_status_id' => 1, + ); + $this->_processorParams = array( + 'domain_id' => 1, + 'name' => 'Dummy', + 'payment_processor_type_id' => 10, + 'financial_account_id' => 12, + 'is_active' => 1, + 'user_name' => '', + 'url_site' => 'http://dummy.com', + 'url_recur' => 'http://dummy.com', + 'billing_mode' => 1, + ); + $this->_pageParams = array( + 'title' => 'Test Contribution Page', + 'financial_type_id' => 1, + 'currency' => 'USD', + 'financial_account_id' => 1, + 'payment_processor' => $paymentProcessor->id, + 'is_active' => 1, + 'is_allow_other_amount' => 1, + 'min_amount' => 10, + 'max_amount' => 1000, + ); + $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); + $this->paymentInstruments = $instruments['values']; + } + + /** + * Clean up after each test. + */ + public function tearDown() { + $this->quickCleanUpFinancialEntities(); } /** @@ -387,5 +472,47 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { $this->assertTrue($rc); } + /** + * Test the submit function of the membership form. + */ + public function testSubmit() { + $form = new CRM_Member_Form_Membership(); + $params = array( + 'cid' => $this->_individualId, + 'join_date' => date('m/d/Y', time()), + 'start_date' => '', + 'end_date' => '', + 'membership_type_id' => array('13'), + 'auto_renew' => '0', + 'max_related' => '', + 'num_terms' => '1', + 'source' => '', + 'total_amount' => '50.00', + 'financial_type_id' => '5', //Member dues, see data.xml + 'soft_credit_type_id' => '', + 'soft_credit_contact_id' => '', + 'from_email_address' => '"Demonstrators Anonymous" ', + 'receipt_text_signup' => 'Thank you text', + 'payment_processor_id' => $this->_paymentProcessorID, + 'credit_card_number' => '4111111111111111', + 'cvv2' => '123', + 'credit_card_exp_date' => array( + 'M' => '9', + 'Y' => '2019', // TODO: Future proof + ), + 'credit_card_type' => 'Visa', + 'billing_first_name' => 'Test', + 'billing_middlename' => 'Last', + 'billing_street_address-5' => '10 Test St', + 'billing_city-5' => 'Test', + 'billing_state_province_id-5' => '1003', + 'billing_postal_code-5' => '90210', + 'billing_country_id-5' => '1228', + ); + $form->submit($params); + // TODO: This will still fail right now. + //$this->callAPISuccessGetCount('Membership', array('contact_id' => $this->_individualId), 1); + } + } // class CRM_Member_Form_MembershipTest -- 2.25.1