Merge pull request #10820 from JMAConsulting/CRM-21029
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionRecurTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Contribute_BAO_ContributionRecurTest
30 * @group headless
31 */
32 class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase {
33 protected $_params = array();
34
35 public function setUp() {
36 parent::setUp();
37 $this->_ids['payment_processor'] = $this->paymentProcessorCreate();
38 $this->_params = array(
39 'contact_id' => $this->individualCreate(),
40 'amount' => 3.00,
41 'frequency_unit' => 'week',
42 'frequency_interval' => 1,
43 'installments' => 2,
44 'start_date' => 'yesterday',
45 'create_date' => 'yesterday',
46 'modified_date' => 'yesterday',
47 'cancel_date' => NULL,
48 'end_date' => '+ 2 weeks',
49 'processor_id' => '643411460836',
50 'trxn_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
51 'invoice_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
52 'contribution_status_id' => 1,
53 'is_test' => 0,
54 'cycle_day' => 1,
55 'next_sched_contribution_date' => '+ 1 week',
56 'failure_count' => 0,
57 'failure_retry_date' => NULL,
58 'auto_renew' => 0,
59 'currency' => 'USD',
60 'payment_processor_id' => $this->_ids['payment_processor'],
61 'is_email_receipt' => 1,
62 'financial_type_id' => 1,
63 'payment_instrument_id' => 1,
64 'campaign_id' => NULL,
65 );
66 }
67
68 public function teardown() {
69 $this->quickCleanup(array('civicrm_contribution_recur', 'civicrm_payment_processor'));
70 }
71
72 /**
73 * Test that an object can be retrieved & saved (per CRM-14986).
74 *
75 * This has been causing a DB error so we are checking for absence of error
76 */
77 public function testFindSave() {
78 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
79 $dao = new CRM_Contribute_BAO_ContributionRecur();
80 $dao->id = $contributionRecur['id'];
81 $dao->find(TRUE);
82 $dao->is_email_receipt = 0;
83 $dao->save();
84 }
85
86 /**
87 * Test cancellation works per CRM-14986.
88 *
89 * We are checking for absence of error.
90 */
91 public function testCancelRecur() {
92 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
93 CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($contributionRecur['id'], NULL);
94 }
95
96 /**
97 * Test checking if contribution recurr object can allow for changes to financial types.
98 *
99 */
100 public function testSupportFinancialTypeChange() {
101 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
102 $contribution = $this->callAPISuccess('contribution', 'create', array(
103 'contribution_recur_id' => $contributionRecur['id'],
104 'total_amount' => '3.00',
105 'financial_type_id' => 1,
106 'payment_instrument_id' => 1,
107 'currency' => 'USD',
108 'contact_id' => $this->individualCreate(),
109 'contribution_status_id' => 1,
110 'recieve_date' => 'yesterday',
111 ));
112 $this->assertTrue(CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($contributionRecur['id']));
113 }
114
115 /**
116 * Test we don't change unintended fields on API edit
117 */
118 public function testUpdateRecur() {
119 $createParams = $this->_params;
120 $createParams['currency'] = 'XAU';
121 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $createParams);
122 $editParams = array(
123 'id' => $contributionRecur['id'],
124 'end_date' => '+ 4 weeks',
125 );
126 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $editParams);
127 $dao = new CRM_Contribute_BAO_ContributionRecur();
128 $dao->id = $contributionRecur['id'];
129 $dao->find(TRUE);
130 $this->assertEquals('XAU', $dao->currency, 'Edit clobbered recur currency');
131 }
132
133 }