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