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