Add unit test for api ContributionRecur.cancel, add support for cancel_reason
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionRecurTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Test APIv3 civicrm_contribute_recur* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class api_v3_ContributionRecurTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37 protected $params;
38 protected $ids = array();
39 protected $_entity = 'contribution_recur';
40
41 public $DBResetRequired = FALSE;
42
43 public function setUp() {
44 parent::setUp();
45 $this->useTransaction(TRUE);
46 $this->ids['contact'][0] = $this->individualCreate();
47 $this->params = array(
48 'contact_id' => $this->ids['contact'][0],
49 'installments' => '12',
50 'frequency_interval' => '1',
51 'amount' => '500',
52 'contribution_status_id' => 1,
53 'start_date' => '2012-01-01 00:00:00',
54 'currency' => 'USD',
55 'frequency_unit' => 'day',
56 );
57 }
58
59 public function testCreateContributionRecur() {
60 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
61 $this->assertEquals(1, $result['count']);
62 $this->assertNotNull($result['values'][$result['id']]['id']);
63 $this->getAndCheck($this->params, $result['id'], $this->_entity);
64 }
65
66 public function testGetContributionRecur() {
67 $this->callAPISuccess($this->_entity, 'create', $this->params);
68 $getParams = array(
69 'amount' => '500',
70 );
71 $result = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
72 $this->assertEquals(1, $result['count']);
73 }
74
75 public function testCreateContributionRecurWithToken() {
76 // create token
77 $this->createLoggedInUser();
78 $token = $this->callAPISuccess('PaymentToken', 'create', array(
79 'payment_processor_id' => $this->processorCreate(),
80 'token' => 'hhh',
81 'contact_id' => $this->individualCreate(),
82 ));
83 $params['payment_token_id'] = $token['id'];
84 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
85 $this->assertEquals(1, $result['count']);
86 $this->assertNotNull($result['values'][$result['id']]['id']);
87 $this->getAndCheck($this->params, $result['id'], $this->_entity);
88 }
89
90 public function testDeleteContributionRecur() {
91 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
92 $deleteParams = array('id' => $result['id']);
93 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
94 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
95 $this->assertEquals(0, $checkDeleted['count']);
96 }
97
98 public function testGetFieldsContributionRecur() {
99 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
100 $this->assertEquals(12, $result['values']['start_date']['type']);
101 }
102
103 /**
104 * Test that we can cancel a contribution and add a cancel_reason via the api.
105 */
106 public function testContributionRecurCancel() {
107 $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
108 $this->callAPISuccess('ContributionRecur', 'cancel', ['id' => $result['id'], 'cancel_reason' => 'just cos']);
109 $cancelled = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $result['id']]);
110 $this->assertEquals('just cos', $cancelled['cancel_reason']);
111 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', 'Cancelled'), $cancelled['contribution_status_id']);
112 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($cancelled['cancel_date'])));
113 }
114
115 }