3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for API functions.
15 * @package CiviCRM_APIv3
18 class api_v3_APITest
extends CiviUnitTestCase
{
19 public $DBResetRequired = FALSE;
21 protected $_apiversion = 3;
23 public function testAPIReplaceVariables() {
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';
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',
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']);
54 * Test that error doesn't occur for non-existent file.
56 public function testAPIWrapperIncludeNoFile() {
57 $this->callAPIFailure(
61 'API (RandomFile, get) does not exist (join the API team and implement it!)'
65 public function testAPIWrapperCamelCaseFunction() {
66 $this->callAPISuccess('OptionGroup', 'Get', []);
69 public function testAPIWrapperLcaseFunction() {
70 $this->callAPISuccess('OptionGroup', 'get', []);
76 public function testAPIResolver() {
77 $oldPath = get_include_path();
78 set_include_path($oldPath . PATH_SEPARATOR
. dirname(__FILE__
) . '/dataset/resolver');
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');
87 set_include_path($oldPath);
90 public function testFromCamel() {
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',
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));
107 public function testToCamel() {
109 'Contribution' => 'Contribution',
110 'contribution' => 'Contribution',
111 'OptionValue' => 'OptionValue',
112 'optionValue' => 'OptionValue',
113 'option_value' => 'OptionValue',
114 'UFJoin' => 'UFJoin',
115 'uf_join' => 'UFJoin',
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));
124 * Test that calling via wrapper works.
126 public function testv3Wrapper() {
128 $result = civicrm_api3('contact', 'get', []);
130 catch (CRM_Core_Exception
$e) {
131 $this->fail("This should have been a success test");
133 $this->assertTrue(is_array($result));
134 $this->assertAPISuccess($result);
138 * Test exception is thrown.
140 public function testV3WrapperException() {
142 civicrm_api3('contact', 'create', ['debug' => 1]);
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);
151 $this->fail('Exception was expected');
155 * Test result parsing for null.
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,
164 'goal_amount' => 100,
166 $contact = array_shift($result['values']);
168 $this->assertTrue(is_numeric($contact['id']));
169 $this->assertNotEmpty($contact['title']);
170 // preferred_mail_format preferred_communication_method preferred_language gender_id
172 $this->assertNotEmpty($contact['currency']);
174 // update the contact
175 $result = $this->callAPISuccess('ContributionPage', 'create', [
176 'id' => $contact['id'],
177 'title' => 'New title',
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']);