Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionRecurTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_contribute_recur* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
19 class api_v3_ContributionRecurTest extends CiviUnitTestCase {
20 protected $params;
21 protected $_entity = 'ContributionRecur';
22
23 public $DBResetRequired = FALSE;
24
25 /**
26 * @throws \CRM_Core_Exception
27 */
28 public function setUp() {
29 parent::setUp();
30 $this->useTransaction();
31 $this->ids['contact'][0] = $this->individualCreate();
32 $this->params = [
33 'contact_id' => $this->ids['contact'][0],
34 'installments' => '12',
35 'frequency_interval' => '1',
36 'amount' => '500',
37 'contribution_status_id' => 1,
38 'start_date' => '2012-01-01 00:00:00',
39 'currency' => 'USD',
40 'frequency_unit' => 'day',
41 ];
42 }
43
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);
55 }
56
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;
68 $this->callAPISuccess($this->_entity, 'create', $this->params);
69 $getParams = ['amount' => '500'];
70 $result = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
71 $this->assertEquals(1, $result['count']);
72 }
73
74 /**
75 * @dataProvider versionThreeAndFour
76 *
77 * @throws \CRM_Core_Exception
78 */
79 public function testCreateContributionRecurWithToken() {
80 // create token
81 $this->createLoggedInUser();
82 $token = $this->callAPISuccess('PaymentToken', 'create', [
83 'payment_processor_id' => $this->processorCreate(),
84 'token' => 'hhh',
85 'contact_id' => $this->individualCreate(),
86 ]);
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
94 /**
95 * @dataProvider versionThreeAndFour
96 *
97 * @param $version
98 *
99 * @throws \CRM_Core_Exception
100 */
101 public function testDeleteContributionRecur($version) {
102 $this->basicDeleteTest($version);
103 }
104
105 /**
106 * Test expected apiv3 outputs.
107 *
108 * @throws \CRM_Core_Exception
109 */
110 public function testGetFieldsContributionRecur() {
111 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
112 $this->assertEquals(12, $result['values']['start_date']['type']);
113 }
114
115 /**
116 * Test that we can cancel a contribution and add a cancel_reason via the api.
117 *
118 * @throws \CRM_Core_Exception
119 */
120 public function testContributionRecurCancel() {
121 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
122 $this->callAPISuccess('ContributionRecur', 'cancel', ['id' => $result['id'], 'cancel_reason' => 'just cos', 'processor_message' => 'big fail']);
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'])));
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']);
133 }
134
135 }