Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 | 12 | /** |
6d6dc885 | 13 | * Test class for API functions. |
6a488035 | 14 | * |
6c6e6187 | 15 | * @package CiviCRM_APIv3 |
acb109b7 | 16 | * @group headless |
6a488035 TO |
17 | */ |
18 | class api_v3_APITest extends CiviUnitTestCase { | |
19 | public $DBResetRequired = FALSE; | |
b7c9bc4c | 20 | |
6c6e6187 | 21 | protected $_apiversion = 3; |
6a488035 | 22 | |
00be9182 | 23 | public function testAPIReplaceVariables() { |
9099cab3 | 24 | $result = []; |
6a488035 TO |
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'; | |
9099cab3 | 33 | $params = [ |
6a488035 TO |
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', | |
9099cab3 | 42 | ]; |
845d6d75 | 43 | _civicrm_api_replace_variables($params, $result); |
6a488035 TO |
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 | ||
d424ffde | 53 | /** |
d177a2a6 | 54 | * Test that error doesn't occur for non-existent file. |
d424ffde | 55 | */ |
00be9182 | 56 | public function testAPIWrapperIncludeNoFile() { |
d177a2a6 EM |
57 | $this->callAPIFailure( |
58 | 'RandomFile', | |
59 | 'get', | |
9099cab3 | 60 | [], |
4f94e3fa | 61 | 'API (RandomFile, get) does not exist (join the API team and implement it!)' |
d177a2a6 | 62 | ); |
6a488035 TO |
63 | } |
64 | ||
00be9182 | 65 | public function testAPIWrapperCamelCaseFunction() { |
9099cab3 | 66 | $this->callAPISuccess('OptionGroup', 'Get', []); |
6a488035 TO |
67 | } |
68 | ||
00be9182 | 69 | public function testAPIWrapperLcaseFunction() { |
9099cab3 | 70 | $this->callAPISuccess('OptionGroup', 'get', []); |
6a488035 TO |
71 | } |
72 | ||
6d6dc885 EM |
73 | /** |
74 | * Test resolver. | |
75 | */ | |
00be9182 | 76 | public function testAPIResolver() { |
6d6dc885 EM |
77 | $oldPath = get_include_path(); |
78 | set_include_path($oldPath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver'); | |
6a488035 | 79 | |
9099cab3 | 80 | $result = $this->callAPISuccess('contact', 'example_action1', []); |
6a488035 | 81 | $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok'); |
9099cab3 | 82 | $result = $this->callAPISuccess('contact', 'example_action2', []); |
6a488035 | 83 | $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok'); |
9099cab3 | 84 | $result = $this->callAPISuccess('test_entity', 'example_action3', []); |
6a488035 TO |
85 | $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok'); |
86 | ||
6d6dc885 | 87 | set_include_path($oldPath); |
6a488035 TO |
88 | } |
89 | ||
00be9182 | 90 | public function testFromCamel() { |
9099cab3 | 91 | $cases = [ |
6a488035 TO |
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', | |
9099cab3 | 100 | ]; |
6a488035 TO |
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 | ||
00be9182 | 107 | public function testToCamel() { |
9099cab3 | 108 | $cases = [ |
6a488035 TO |
109 | 'Contribution' => 'Contribution', |
110 | 'contribution' => 'Contribution', | |
111 | 'OptionValue' => 'OptionValue', | |
112 | 'optionValue' => 'OptionValue', | |
113 | 'option_value' => 'OptionValue', | |
114 | 'UFJoin' => 'UFJoin', | |
6a488035 | 115 | 'uf_join' => 'UFJoin', |
9099cab3 | 116 | ]; |
6a488035 TO |
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 | } | |
92915c55 | 122 | |
6c6e6187 | 123 | /** |
d177a2a6 | 124 | * Test that calling via wrapper works. |
6c6e6187 | 125 | */ |
00be9182 | 126 | public function testv3Wrapper() { |
92e4c2a5 | 127 | try { |
9099cab3 | 128 | $result = civicrm_api3('contact', 'get', []); |
6b359437 | 129 | } |
ff48e573 | 130 | catch (CRM_Core_Exception $e) { |
6b359437 | 131 | $this->fail("This should have been a success test"); |
132 | } | |
c4ac4df4 | 133 | $this->assertTrue(is_array($result)); |
6b359437 | 134 | $this->assertAPISuccess($result); |
135 | } | |
136 | ||
137 | /** | |
d177a2a6 | 138 | * Test exception is thrown. |
6b359437 | 139 | */ |
d177a2a6 | 140 | public function testV3WrapperException() { |
92e4c2a5 | 141 | try { |
9099cab3 | 142 | civicrm_api3('contact', 'create', ['debug' => 1]); |
6b359437 | 143 | } |
9b873358 | 144 | catch (CiviCRM_API3_Exception $e) { |
62749b5a | 145 | $this->assertEquals('mandatory_missing', $e->getErrorCode()); |
6b359437 | 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 | ||
d177a2a6 EM |
154 | /** |
155 | * Test result parsing for null. | |
156 | */ | |
157 | public function testCreateNoStringNullResult() { | |
76fa28f1 TO |
158 | // create an example contact |
159 | // $contact = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage')->toArray(); | |
9099cab3 | 160 | $result = $this->callAPISuccess('ContributionPage', 'create', [ |
76fa28f1 TO |
161 | 'title' => "Test Contribution Page", |
162 | 'financial_type_id' => 1, | |
163 | 'currency' => 'USD', | |
164 | 'goal_amount' => 100, | |
9099cab3 | 165 | ]); |
76fa28f1 TO |
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 | |
9099cab3 | 175 | $result = $this->callAPISuccess('ContributionPage', 'create', [ |
76fa28f1 TO |
176 | 'id' => $contact['id'], |
177 | 'title' => 'New title', | |
178 | 'currency' => '', | |
9099cab3 | 179 | ]); |
76fa28f1 | 180 | |
d177a2a6 | 181 | // Check return format. |
76fa28f1 TO |
182 | $this->assertEquals(1, $result['count']); |
183 | foreach ($result['values'] as $resultValue) { | |
184 | $this->assertEquals('New title', $resultValue['title']); | |
d177a2a6 EM |
185 | // BUG: $resultValue['location'] === 'null'. |
186 | $this->assertEquals('', $resultValue['currency']); | |
76fa28f1 TO |
187 | } |
188 | } | |
189 | ||
6a488035 | 190 | } |