Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_contribute_recur* functions | |
14 | * | |
6c6e6187 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Contribution | |
acb109b7 | 17 | * @group headless |
6a488035 | 18 | */ |
6a488035 | 19 | class api_v3_ContributionRecurTest extends CiviUnitTestCase { |
6a488035 | 20 | protected $params; |
d943e78d | 21 | protected $_entity = 'ContributionRecur'; |
b7c9bc4c | 22 | |
430ae6dd TO |
23 | public $DBResetRequired = FALSE; |
24 | ||
d943e78d | 25 | /** |
26 | * @throws \CRM_Core_Exception | |
27 | */ | |
00be9182 | 28 | public function setUp() { |
6a488035 | 29 | parent::setUp(); |
d943e78d | 30 | $this->useTransaction(); |
6a488035 | 31 | $this->ids['contact'][0] = $this->individualCreate(); |
9099cab3 | 32 | $this->params = [ |
6a488035 TO |
33 | 'contact_id' => $this->ids['contact'][0], |
34 | 'installments' => '12', | |
35 | 'frequency_interval' => '1', | |
2f3d0664 | 36 | 'amount' => '500.00', |
6a488035 TO |
37 | 'contribution_status_id' => 1, |
38 | 'start_date' => '2012-01-01 00:00:00', | |
39 | 'currency' => 'USD', | |
40 | 'frequency_unit' => 'day', | |
9099cab3 | 41 | ]; |
6a488035 TO |
42 | } |
43 | ||
d943e78d | 44 | /** |
45 | * Basic create test. | |
46 | * | |
47 | * @dataProvider versionThreeAndFour | |
48 | * | |
49 | * @param int $version | |
50 | * | |
51 | * @throws \CRM_Core_Exception | |
52 | */ | |
53 | public function testCreateContributionRecur($version) { | |
54 | $this->basicCreateTest($version); | |
6a488035 TO |
55 | } |
56 | ||
d943e78d | 57 | /** |
58 | * Basic get test. | |
59 | * | |
60 | * @dataProvider versionThreeAndFour | |
61 | * | |
62 | * @param int $version | |
63 | * | |
64 | * @throws \CRM_Core_Exception | |
65 | */ | |
66 | public function testGetContributionRecur($version) { | |
67 | $this->_apiversion = $version; | |
225d474b | 68 | $this->callAPISuccess($this->_entity, 'create', $this->params); |
d943e78d | 69 | $getParams = ['amount' => '500']; |
fc928539 | 70 | $result = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__); |
71 | $this->assertEquals(1, $result['count']); | |
6a488035 TO |
72 | } |
73 | ||
d943e78d | 74 | /** |
75 | * @dataProvider versionThreeAndFour | |
76 | * | |
77 | * @throws \CRM_Core_Exception | |
78 | */ | |
81c55926 RT |
79 | public function testCreateContributionRecurWithToken() { |
80 | // create token | |
81 | $this->createLoggedInUser(); | |
9099cab3 | 82 | $token = $this->callAPISuccess('PaymentToken', 'create', [ |
8950ecdc | 83 | 'payment_processor_id' => $this->processorCreate(), |
0cc123b5 RT |
84 | 'token' => 'hhh', |
85 | 'contact_id' => $this->individualCreate(), | |
9099cab3 | 86 | ]); |
81c55926 RT |
87 | $params['payment_token_id'] = $token['id']; |
88 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
89 | $this->assertEquals(1, $result['count']); | |
90 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
91 | $this->getAndCheck($this->params, $result['id'], $this->_entity); | |
92 | } | |
93 | ||
d943e78d | 94 | /** |
95 | * @dataProvider versionThreeAndFour | |
96 | * | |
97 | * @param $version | |
98 | * | |
99 | * @throws \CRM_Core_Exception | |
100 | */ | |
101 | public function testDeleteContributionRecur($version) { | |
102 | $this->basicDeleteTest($version); | |
6a488035 TO |
103 | } |
104 | ||
d943e78d | 105 | /** |
106 | * Test expected apiv3 outputs. | |
107 | * | |
108 | * @throws \CRM_Core_Exception | |
109 | */ | |
6a488035 | 110 | public function testGetFieldsContributionRecur() { |
9099cab3 | 111 | $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']); |
6a488035 TO |
112 | $this->assertEquals(12, $result['values']['start_date']['type']); |
113 | } | |
96025800 | 114 | |
9cfc631e | 115 | /** |
116 | * Test that we can cancel a contribution and add a cancel_reason via the api. | |
d943e78d | 117 | * |
118 | * @throws \CRM_Core_Exception | |
9cfc631e | 119 | */ |
120 | public function testContributionRecurCancel() { | |
121 | $result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
3d3449ce | 122 | $this->callAPISuccess('ContributionRecur', 'cancel', ['id' => $result['id'], 'cancel_reason' => 'just cos', 'processor_message' => 'big fail']); |
9cfc631e | 123 | $cancelled = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $result['id']]); |
124 | $this->assertEquals('just cos', $cancelled['cancel_reason']); | |
125 | $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', 'Cancelled'), $cancelled['contribution_status_id']); | |
126 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($cancelled['cancel_date']))); | |
3d3449ce | 127 | $activity = $this->callAPISuccessGetSingle('Activity', ['activity_type_id' => 'Cancel Recurring Contribution', 'record_type_id' => $result['id']]); |
128 | $this->assertEquals('Recurring contribution cancelled', $activity['subject']); | |
129 | $this->assertEquals('big fail<br/>The recurring contribution of 500.00, every 1 day has been cancelled.', $activity['details']); | |
130 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($activity['activity_date_time']))); | |
131 | $this->assertEquals($this->params['contact_id'], $activity['source_contact_id']); | |
132 | $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), $activity['status_id']); | |
9cfc631e | 133 | } |
134 | ||
6a488035 | 135 | } |