+ /**
+ * CRM-20008 Tests repeattransaction creates pending membership.
+ */
+ public function testRepeatTransactionPendingMembership() {
+ list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
+ $this->callAPISuccess('membership', 'create', array(
+ 'id' => $membership['id'],
+ 'end_date' => 'yesterday',
+ 'status_id' => 'Expired',
+ ));
+ $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
+ 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
+ 'contribution_status_id' => 'Pending',
+ 'trxn_id' => uniqid(),
+ ));
+ $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
+ 'id' => $membership['id'],
+ 'return' => 'status_id',
+ ));
+
+ // Let's see if the membership payments got created while we're at it.
+ $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
+ 'memberhip_id' => $membership['id'],
+ ));
+ $this->assertEquals(2, $membershipPayments['count']);
+
+ $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
+ $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
+ $membership = $this->callAPISuccessGetSingle('membership', array(
+ 'id' => $membership['id'],
+ 'return' => 'status_id, end_date',
+ ));
+ $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
+ $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
+
+ $this->quickCleanUpFinancialEntities();
+ $this->contactDelete($originalContribution['values'][1]['contact_id']);
+ }
+
+ /**
+ * Test sending a mail via the API.
+ */
+ public function testSendMailWithAPISetFromDetails() {
+ $mut = new CiviMailUtils($this, TRUE);
+ $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
+ $this->callAPISuccess('contribution', 'sendconfirmation', array(
+ 'id' => $contribution['id'],
+ 'receipt_from_email' => 'api@civicrm.org',
+ 'receipt_from_name' => 'CiviCRM LLC',
+ ));
+ $mut->checkMailLog(array(
+ 'From: CiviCRM LLC <api@civicrm.org>',
+ 'Contribution Information',
+ 'Please print this confirmation for your records',
+ ), array(
+ 'Event',
+ )
+ );
+ $mut->stop();
+ }
+
+ /**
+ * Test sending a mail via the API.
+ */
+ public function testSendMailWithNoFromSetFallToDomain() {
+ $this->createLoggedInUser();
+ $mut = new CiviMailUtils($this, TRUE);
+ $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
+ $this->callAPISuccess('contribution', 'sendconfirmation', array(
+ 'id' => $contribution['id'],
+ ));
+ $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
+ $mut->checkMailLog(array(
+ 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
+ 'Contribution Information',
+ 'Please print this confirmation for your records',
+ ), array(
+ 'Event',
+ )
+ );
+ $mut->stop();
+ }
+
+ /**
+ * Test sending a mail via the API.
+ */
+ public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
+ $this->createLoggedInUser();
+ $mut = new CiviMailUtils($this, TRUE);
+ $contribution = $this->setUpRepeatTransaction(array(), 'single');
+ $this->callAPISuccess('contribution', 'repeattransaction', array(
+ 'contribution_status_id' => 'Completed',
+ 'trxn_id' => uniqid(),
+ 'original_contribution_id' => $contribution,
+ ));
+ $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
+ $mut->checkMailLog(array(
+ 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
+ 'Contribution Information',
+ 'Please print this confirmation for your records',
+ ), array(
+ 'Event',
+ )
+ );
+ $mut->stop();
+ }
+
+ /**
+ * Test sending a mail via the API.
+ */
+ public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
+ $mut = new CiviMailUtils($this, TRUE);
+ $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
+ $paymentProcessorID = $this->paymentProcessorCreate();
+ $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
+ 'contact_id' => $this->_individualId,
+ 'installments' => '12',
+ 'frequency_interval' => '1',
+ 'amount' => '500',
+ 'contribution_status_id' => 1,
+ 'start_date' => '2012-01-01 00:00:00',
+ 'currency' => 'USD',
+ 'frequency_unit' => 'month',
+ 'payment_processor_id' => $paymentProcessorID,
+ ));
+ $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
+ $this->_params,
+ array(
+ 'contribution_recur_id' => $contributionRecur['id'],
+ 'contribution_page_id' => $contributionPage['id']))
+ );
+ $this->callAPISuccess('contribution', 'repeattransaction', array(
+ 'contribution_status_id' => 'Completed',
+ 'trxn_id' => uniqid(),
+ 'original_contribution_id' => $originalContribution,
+ )
+ );
+ $mut->checkMailLog(array(
+ 'From: CiviCRM LLC <contributionpage@civicrm.org>',
+ 'Contribution Information',
+ 'Please print this confirmation for your records',
+ ), array(
+ 'Event',
+ )
+ );
+ $mut->stop();
+ }
+
+ /**
+ * Test sending a mail via the API.
+ */
+ public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
+ $mut = new CiviMailUtils($this, TRUE);
+ $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
+ $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
+ foreach ($fromEmail['values'] as $from) {
+ $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
+ }
+ $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
+ $this->callAPISuccess('contribution', 'repeattransaction', array(
+ 'contribution_status_id' => 'Completed',
+ 'trxn_id' => uniqid(),
+ 'original_contribution_id' => $originalContribution,
+ )
+ );
+ $mut->checkMailLog(array(
+ 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
+ 'Contribution Information',
+ 'Please print this confirmation for your records',
+ ), array(
+ 'Event',
+ )
+ );
+ $mut->stop();
+ }
+
+ /**
+ * Create a Contribution Page with is_email_receipt = TRUE.
+ *
+ * @param array $params
+ * Params to overwrite with.
+ *
+ * @return array|int
+ */
+ protected function createReceiptableContributionPage($params = array()) {
+ $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
+ 'receipt_from_name' => 'Mickey Mouse',
+ 'receipt_from_email' => 'mickey@mouse.com',
+ 'title' => "Test Contribution Page",
+ 'financial_type_id' => 1,
+ 'currency' => 'CAD',
+ 'is_monetary' => TRUE,
+ 'is_email_receipt' => TRUE,
+ ), $params));
+ return $contributionPage;
+ }
+
+ /**
+ * function to test card_type and pan truncation.
+ */
+ public function testCardTypeAndPanTruncation() {
+ $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
+ $contactId = $this->individualCreate();
+ $params = array(
+ 'contact_id' => $contactId,
+ 'receive_date' => '2016-01-20',
+ 'total_amount' => 100,
+ 'financial_type_id' => 1,
+ 'payment_instrument' => 'Credit Card',
+ 'card_type_id' => $creditCardTypeIDs['Visa'],
+ 'pan_truncation' => 4567,
+ );
+ $contribution = $this->callAPISuccess('contribution', 'create', $params);
+ $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
+ $financialTrxn = $this->callAPISuccessGetSingle(
+ 'FinancialTrxn',
+ array(
+ 'id' => $lastFinancialTrxnId['financialTrxnId'],
+ 'return' => array('card_type_id', 'pan_truncation'),
+ )
+ );
+ $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
+ $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
+ $params = array(
+ 'id' => $contribution['id'],
+ 'pan_truncation' => 2345,
+ 'card_type_id' => $creditCardTypeIDs['Amex'],
+ );
+ $contribution = $this->callAPISuccess('contribution', 'create', $params);
+ $financialTrxn = $this->callAPISuccessGetSingle(
+ 'FinancialTrxn',
+ array(
+ 'id' => $lastFinancialTrxnId['financialTrxnId'],
+ 'return' => array('card_type_id', 'pan_truncation'),
+ )
+ );
+ $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
+ $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
+ }
+