Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 | 28 | /** |
6d6dc885 | 29 | * Test class for API functions. |
6a488035 | 30 | * |
6c6e6187 | 31 | * @package CiviCRM_APIv3 |
acb109b7 | 32 | * @group headless |
6a488035 TO |
33 | */ |
34 | class api_v3_APITest extends CiviUnitTestCase { | |
35 | public $DBResetRequired = FALSE; | |
b7c9bc4c | 36 | |
6c6e6187 | 37 | protected $_apiversion = 3; |
6a488035 | 38 | |
00be9182 | 39 | public function testAPIReplaceVariables() { |
9099cab3 | 40 | $result = []; |
6a488035 TO |
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'; | |
9099cab3 | 49 | $params = [ |
6a488035 TO |
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', | |
9099cab3 | 58 | ]; |
845d6d75 | 59 | _civicrm_api_replace_variables($params, $result); |
6a488035 TO |
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 | ||
d424ffde | 69 | /** |
d177a2a6 | 70 | * Test that error doesn't occur for non-existent file. |
d424ffde | 71 | */ |
00be9182 | 72 | public function testAPIWrapperIncludeNoFile() { |
d177a2a6 EM |
73 | $this->callAPIFailure( |
74 | 'RandomFile', | |
75 | 'get', | |
9099cab3 | 76 | [], |
4f94e3fa | 77 | 'API (RandomFile, get) does not exist (join the API team and implement it!)' |
d177a2a6 | 78 | ); |
6a488035 TO |
79 | } |
80 | ||
00be9182 | 81 | public function testAPIWrapperCamelCaseFunction() { |
9099cab3 | 82 | $this->callAPISuccess('OptionGroup', 'Get', []); |
6a488035 TO |
83 | } |
84 | ||
00be9182 | 85 | public function testAPIWrapperLcaseFunction() { |
9099cab3 | 86 | $this->callAPISuccess('OptionGroup', 'get', []); |
6a488035 TO |
87 | } |
88 | ||
6d6dc885 EM |
89 | /** |
90 | * Test resolver. | |
91 | */ | |
00be9182 | 92 | public function testAPIResolver() { |
6d6dc885 EM |
93 | $oldPath = get_include_path(); |
94 | set_include_path($oldPath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver'); | |
6a488035 | 95 | |
9099cab3 | 96 | $result = $this->callAPISuccess('contact', 'example_action1', []); |
6a488035 | 97 | $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok'); |
9099cab3 | 98 | $result = $this->callAPISuccess('contact', 'example_action2', []); |
6a488035 | 99 | $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok'); |
9099cab3 | 100 | $result = $this->callAPISuccess('test_entity', 'example_action3', []); |
6a488035 TO |
101 | $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok'); |
102 | ||
6d6dc885 | 103 | set_include_path($oldPath); |
6a488035 TO |
104 | } |
105 | ||
00be9182 | 106 | public function testFromCamel() { |
9099cab3 | 107 | $cases = [ |
6a488035 TO |
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', | |
9099cab3 | 116 | ]; |
6a488035 TO |
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 | ||
00be9182 | 123 | public function testToCamel() { |
9099cab3 | 124 | $cases = [ |
6a488035 TO |
125 | 'Contribution' => 'Contribution', |
126 | 'contribution' => 'Contribution', | |
127 | 'OptionValue' => 'OptionValue', | |
128 | 'optionValue' => 'OptionValue', | |
129 | 'option_value' => 'OptionValue', | |
130 | 'UFJoin' => 'UFJoin', | |
6a488035 | 131 | 'uf_join' => 'UFJoin', |
9099cab3 | 132 | ]; |
6a488035 TO |
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 | } | |
92915c55 | 138 | |
6c6e6187 | 139 | /** |
d177a2a6 | 140 | * Test that calling via wrapper works. |
6c6e6187 | 141 | */ |
00be9182 | 142 | public function testv3Wrapper() { |
92e4c2a5 | 143 | try { |
9099cab3 | 144 | $result = civicrm_api3('contact', 'get', []); |
6b359437 | 145 | } |
9b873358 | 146 | catch (CRM_Exception $e) { |
6b359437 | 147 | $this->fail("This should have been a success test"); |
148 | } | |
c4ac4df4 | 149 | $this->assertTrue(is_array($result)); |
6b359437 | 150 | $this->assertAPISuccess($result); |
151 | } | |
152 | ||
153 | /** | |
d177a2a6 | 154 | * Test exception is thrown. |
6b359437 | 155 | */ |
d177a2a6 | 156 | public function testV3WrapperException() { |
92e4c2a5 | 157 | try { |
9099cab3 | 158 | civicrm_api3('contact', 'create', ['debug' => 1]); |
6b359437 | 159 | } |
9b873358 | 160 | catch (CiviCRM_API3_Exception $e) { |
62749b5a | 161 | $this->assertEquals('mandatory_missing', $e->getErrorCode()); |
6b359437 | 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 | ||
d177a2a6 EM |
170 | /** |
171 | * Test result parsing for null. | |
172 | */ | |
173 | public function testCreateNoStringNullResult() { | |
76fa28f1 TO |
174 | // create an example contact |
175 | // $contact = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage')->toArray(); | |
9099cab3 | 176 | $result = $this->callAPISuccess('ContributionPage', 'create', [ |
76fa28f1 TO |
177 | 'title' => "Test Contribution Page", |
178 | 'financial_type_id' => 1, | |
179 | 'currency' => 'USD', | |
180 | 'goal_amount' => 100, | |
9099cab3 | 181 | ]); |
76fa28f1 TO |
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 | |
9099cab3 | 191 | $result = $this->callAPISuccess('ContributionPage', 'create', [ |
76fa28f1 TO |
192 | 'id' => $contact['id'], |
193 | 'title' => 'New title', | |
194 | 'currency' => '', | |
9099cab3 | 195 | ]); |
76fa28f1 | 196 | |
d177a2a6 | 197 | // Check return format. |
76fa28f1 TO |
198 | $this->assertEquals(1, $result['count']); |
199 | foreach ($result['values'] as $resultValue) { | |
200 | $this->assertEquals('New title', $resultValue['title']); | |
d177a2a6 EM |
201 | // BUG: $resultValue['location'] === 'null'. |
202 | $this->assertEquals('', $resultValue['currency']); | |
76fa28f1 TO |
203 | } |
204 | } | |
205 | ||
6a488035 | 206 | } |