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