Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | * | |
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 | ); | |
61 | _civicrm_api_replace_variables('Activity', 'Get', $params, $result); | |
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 | ||
71 | /* | |
72 | * test that error doesn't occur for non-existant file | |
73 | */ | |
00be9182 | 74 | public function testAPIWrapperIncludeNoFile() { |
6a488035 | 75 | |
4e420887 | 76 | $result = $this->callAPIFailure('RandomFile', 'get', array(), 'API (RandomFile,get) does not exist (join the API team and implement it!)'); |
6a488035 TO |
77 | } |
78 | ||
00be9182 | 79 | public function testAPIWrapperCamelCaseFunction() { |
4e420887 | 80 | $result = $this->callAPISuccess('OptionGroup', 'Get', array()); |
6a488035 TO |
81 | } |
82 | ||
00be9182 | 83 | public function testAPIWrapperLcaseFunction() { |
4e420887 | 84 | $result = $this->callAPISuccess('OptionGroup', 'get', array()); |
6a488035 TO |
85 | } |
86 | ||
00be9182 | 87 | public function testAPIResolver() { |
6a488035 TO |
88 | $oldpath = get_include_path(); |
89 | set_include_path($oldpath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver'); | |
90 | ||
4e420887 | 91 | $result = $this->callAPISuccess('contact', 'example_action1', array()); |
6a488035 | 92 | $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok'); |
4e420887 | 93 | $result = $this->callAPISuccess('contact', 'example_action2', array()); |
6a488035 | 94 | $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok'); |
4e420887 | 95 | $result = $this->callAPISuccess('test_entity', 'example_action3', array()); |
6a488035 TO |
96 | $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok'); |
97 | ||
98 | set_include_path($oldpath); | |
99 | } | |
100 | ||
00be9182 | 101 | public function testFromCamel() { |
6a488035 TO |
102 | $cases = array( |
103 | 'Contribution' => 'contribution', | |
104 | 'contribution' => 'contribution', | |
105 | 'OptionValue' => 'option_value', | |
106 | 'optionValue' => 'option_value', | |
107 | 'option_value' => 'option_value', | |
108 | 'UFJoin' => 'uf_join', | |
109 | 'ufJoin' => 'uf_join', | |
110 | 'uf_join' => 'uf_join', | |
111 | ); | |
112 | foreach ($cases as $input => $expected) { | |
113 | $actual = _civicrm_api_get_entity_name_from_camel($input); | |
114 | $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual)); | |
115 | } | |
116 | } | |
117 | ||
00be9182 | 118 | public function testToCamel() { |
6a488035 TO |
119 | $cases = array( |
120 | 'Contribution' => 'Contribution', | |
121 | 'contribution' => 'Contribution', | |
122 | 'OptionValue' => 'OptionValue', | |
123 | 'optionValue' => 'OptionValue', | |
124 | 'option_value' => 'OptionValue', | |
125 | 'UFJoin' => 'UFJoin', | |
126 | // dommage 'ufJoin' => 'UFJoin', | |
127 | 'uf_join' => 'UFJoin', | |
128 | ); | |
129 | foreach ($cases as $input => $expected) { | |
130 | $actual = _civicrm_api_get_camel_name($input); | |
131 | $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual)); | |
132 | } | |
133 | } | |
6c6e6187 TO |
134 | /** |
135 | * Test that calling via wrapper works | |
136 | */ | |
00be9182 | 137 | public function testv3Wrapper() { |
6b359437 | 138 | try{ |
139 | $result = civicrm_api3('contact', 'get', array()); | |
140 | } | |
141 | catch (CRM_Exception $e){ | |
142 | $this->fail("This should have been a success test"); | |
143 | } | |
c4ac4df4 | 144 | $this->assertTrue(is_array($result)); |
6b359437 | 145 | $this->assertAPISuccess($result); |
146 | } | |
147 | ||
148 | /** | |
100fef9d | 149 | * Test exception is thrown |
6b359437 | 150 | */ |
00be9182 | 151 | public function testv3WrapperException(){ |
6b359437 | 152 | try{ |
153 | $result = civicrm_api3('contact', 'create', array('debug' => 1)); | |
154 | } | |
155 | catch (CiviCRM_API3_Exception $e){ | |
62749b5a | 156 | $this->assertEquals('mandatory_missing', $e->getErrorCode()); |
6b359437 | 157 | $this->assertEquals('Mandatory key(s) missing from params array: contact_type', $e->getMessage()); |
158 | $extra = $e->getExtraParams(); | |
159 | $this->assertArrayHasKey('trace', $extra); | |
160 | return; | |
161 | } | |
162 | $this->fail('Exception was expected'); | |
163 | } | |
164 | ||
76fa28f1 TO |
165 | public function testCreate_NoStringNullResult() { |
166 | // create an example contact | |
167 | // $contact = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage')->toArray(); | |
168 | $result = $this->callAPISuccess('ContributionPage', 'create', array( | |
169 | 'title' => "Test Contribution Page", | |
170 | 'financial_type_id' => 1, | |
171 | 'currency' => 'USD', | |
172 | 'goal_amount' => 100, | |
173 | )); | |
174 | $contact = array_shift($result['values']); | |
175 | ||
176 | $this->assertTrue(is_numeric($contact['id'])); | |
177 | $this->assertNotEmpty($contact['title']); | |
178 | // preferred_mail_format preferred_communication_method preferred_language gender_id | |
179 | // currency | |
180 | $this->assertNotEmpty($contact['currency']); | |
181 | ||
182 | // update the contact | |
183 | $result = $this->callAPISuccess('ContributionPage', 'create', array( | |
184 | 'id' => $contact['id'], | |
185 | 'title' => 'New title', | |
186 | 'currency' => '', | |
187 | )); | |
188 | ||
189 | // check return format | |
190 | $this->assertEquals(1, $result['count']); | |
191 | foreach ($result['values'] as $resultValue) { | |
192 | $this->assertEquals('New title', $resultValue['title']); | |
193 | $this->assertEquals('', $resultValue['currency']); // BUG: $resultValue['location'] === 'null' | |
194 | } | |
195 | } | |
196 | ||
6a488035 | 197 | } |