Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / api / v3 / APITest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 class for API functions.
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_APITest extends CiviUnitTestCase {
35 public $DBResetRequired = FALSE;
36
37 protected $_apiversion = 3;
38
39 public function testAPIReplaceVariables() {
40 $result = [];
41 $result['testfield'] = 6;
42 $result['api.tag.get'] = 999;
43 $result['api.tag.create']['id'] = 8;
44 $result['api.entity.create.0']['id'] = 7;
45 $result['api.tag.create'][2]['id'] = 'superman';
46 $result['api.tag.create']['values']['0']['display'] = 'batman';
47 $result['api.tag.create.api.tag.create']['values']['0']['display'] = 'krypton';
48 $result['api.tag.create']['values']['0']['api_tag_get'] = 'darth vader';
49 $params = [
50 'activity_type_id' => '$value.testfield',
51 'tag_id' => '$value.api.tag.create.id',
52 'tag1_id' => '$value.api.entity.create.0.id',
53 'tag3_id' => '$value.api.tag.create.2.id',
54 'display' => '$value.api.tag.create.values.0.display',
55 'number' => '$value.api.tag.get',
56 'big_rock' => '$value.api.tag.create.api.tag.create.values.0.display',
57 'villain' => '$value.api.tag.create.values.0.api_tag_get.display',
58 ];
59 _civicrm_api_replace_variables($params, $result);
60 $this->assertEquals(999, $params['number']);
61 $this->assertEquals(8, $params['tag_id']);
62 $this->assertEquals(6, $params['activity_type_id']);
63 $this->assertEquals(7, $params['tag1_id']);
64 $this->assertEquals('superman', $params['tag3_id']);
65 $this->assertEquals('batman', $params['display']);
66 $this->assertEquals('krypton', $params['big_rock']);
67 }
68
69 /**
70 * Test that error doesn't occur for non-existent file.
71 */
72 public function testAPIWrapperIncludeNoFile() {
73 $this->callAPIFailure(
74 'RandomFile',
75 'get',
76 [],
77 'API (RandomFile, get) does not exist (join the API team and implement it!)'
78 );
79 }
80
81 public function testAPIWrapperCamelCaseFunction() {
82 $this->callAPISuccess('OptionGroup', 'Get', []);
83 }
84
85 public function testAPIWrapperLcaseFunction() {
86 $this->callAPISuccess('OptionGroup', 'get', []);
87 }
88
89 /**
90 * Test resolver.
91 */
92 public function testAPIResolver() {
93 $oldPath = get_include_path();
94 set_include_path($oldPath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver');
95
96 $result = $this->callAPISuccess('contact', 'example_action1', []);
97 $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok');
98 $result = $this->callAPISuccess('contact', 'example_action2', []);
99 $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok');
100 $result = $this->callAPISuccess('test_entity', 'example_action3', []);
101 $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok');
102
103 set_include_path($oldPath);
104 }
105
106 public function testFromCamel() {
107 $cases = [
108 'Contribution' => 'contribution',
109 'contribution' => 'contribution',
110 'OptionValue' => 'option_value',
111 'optionValue' => 'option_value',
112 'option_value' => 'option_value',
113 'UFJoin' => 'uf_join',
114 'ufJoin' => 'uf_join',
115 'uf_join' => 'uf_join',
116 ];
117 foreach ($cases as $input => $expected) {
118 $actual = _civicrm_api_get_entity_name_from_camel($input);
119 $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual));
120 }
121 }
122
123 public function testToCamel() {
124 $cases = [
125 'Contribution' => 'Contribution',
126 'contribution' => 'Contribution',
127 'OptionValue' => 'OptionValue',
128 'optionValue' => 'OptionValue',
129 'option_value' => 'OptionValue',
130 'UFJoin' => 'UFJoin',
131 'uf_join' => 'UFJoin',
132 ];
133 foreach ($cases as $input => $expected) {
134 $actual = _civicrm_api_get_camel_name($input);
135 $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual));
136 }
137 }
138
139 /**
140 * Test that calling via wrapper works.
141 */
142 public function testv3Wrapper() {
143 try {
144 $result = civicrm_api3('contact', 'get', []);
145 }
146 catch (CRM_Core_Exception $e) {
147 $this->fail("This should have been a success test");
148 }
149 $this->assertTrue(is_array($result));
150 $this->assertAPISuccess($result);
151 }
152
153 /**
154 * Test exception is thrown.
155 */
156 public function testV3WrapperException() {
157 try {
158 civicrm_api3('contact', 'create', ['debug' => 1]);
159 }
160 catch (CiviCRM_API3_Exception $e) {
161 $this->assertEquals('mandatory_missing', $e->getErrorCode());
162 $this->assertEquals('Mandatory key(s) missing from params array: contact_type', $e->getMessage());
163 $extra = $e->getExtraParams();
164 $this->assertArrayHasKey('trace', $extra);
165 return;
166 }
167 $this->fail('Exception was expected');
168 }
169
170 /**
171 * Test result parsing for null.
172 */
173 public function testCreateNoStringNullResult() {
174 // create an example contact
175 // $contact = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage')->toArray();
176 $result = $this->callAPISuccess('ContributionPage', 'create', [
177 'title' => "Test Contribution Page",
178 'financial_type_id' => 1,
179 'currency' => 'USD',
180 'goal_amount' => 100,
181 ]);
182 $contact = array_shift($result['values']);
183
184 $this->assertTrue(is_numeric($contact['id']));
185 $this->assertNotEmpty($contact['title']);
186 // preferred_mail_format preferred_communication_method preferred_language gender_id
187 // currency
188 $this->assertNotEmpty($contact['currency']);
189
190 // update the contact
191 $result = $this->callAPISuccess('ContributionPage', 'create', [
192 'id' => $contact['id'],
193 'title' => 'New title',
194 'currency' => '',
195 ]);
196
197 // Check return format.
198 $this->assertEquals(1, $result['count']);
199 foreach ($result['values'] as $resultValue) {
200 $this->assertEquals('New title', $resultValue['title']);
201 // BUG: $resultValue['location'] === 'null'.
202 $this->assertEquals('', $resultValue['currency']);
203 }
204 }
205
206 }