Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | ||
31 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
32 | ||
33 | /** | |
34 | * Test class for API functions | |
35 | * | |
36 | * @package CiviCRM_APIv3 | |
37 | */ | |
38 | class api_v3_APITest extends CiviUnitTestCase { | |
39 | public $DBResetRequired = FALSE; | |
40 | public $_eNoticeCompliant = TRUE; | |
41 | ||
42 | protected $_apiversion; | |
43 | ||
44 | /** | |
45 | * Sets up the fixture, for example, opens a network connection. | |
46 | * This method is called before a test is executed. | |
47 | * | |
48 | * @access protected | |
49 | */ | |
50 | protected function setUp() { | |
51 | parent::setUp(); | |
52 | $this->_apiversion = 3; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Tears down the fixture, for example, closes a network connection. | |
57 | * This method is called after a test is executed. | |
58 | * | |
59 | * @access protected | |
60 | */ | |
61 | protected function tearDown() {} | |
62 | ||
63 | function testAPIReplaceVariables() { | |
64 | $result = array(); | |
65 | $result['testfield'] = 6; | |
66 | $result['api.tag.get'] = 999; | |
67 | $result['api.tag.create']['id'] = 8; | |
68 | $result['api.entity.create.0']['id'] = 7; | |
69 | $result['api.tag.create'][2]['id'] = 'superman'; | |
70 | $result['api.tag.create']['values']['0']['display'] = 'batman'; | |
71 | $result['api.tag.create.api.tag.create']['values']['0']['display'] = 'krypton'; | |
72 | $result['api.tag.create']['values']['0']['api_tag_get'] = 'darth vader'; | |
73 | $params = array( | |
74 | 'activity_type_id' => '$value.testfield', | |
75 | 'tag_id' => '$value.api.tag.create.id', | |
76 | 'tag1_id' => '$value.api.entity.create.0.id', | |
77 | 'tag3_id' => '$value.api.tag.create.2.id', | |
78 | 'display' => '$value.api.tag.create.values.0.display', | |
79 | 'number' => '$value.api.tag.get', | |
80 | 'big_rock' => '$value.api.tag.create.api.tag.create.values.0.display', | |
81 | 'villain' => '$value.api.tag.create.values.0.api_tag_get.display', | |
82 | ); | |
83 | _civicrm_api_replace_variables('Activity', 'Get', $params, $result); | |
84 | $this->assertEquals(999, $params['number']); | |
85 | $this->assertEquals(8, $params['tag_id']); | |
86 | $this->assertEquals(6, $params['activity_type_id']); | |
87 | $this->assertEquals(7, $params['tag1_id']); | |
88 | $this->assertEquals('superman', $params['tag3_id']); | |
89 | $this->assertEquals('batman', $params['display']); | |
90 | $this->assertEquals('krypton', $params['big_rock']); | |
91 | } | |
92 | ||
93 | /* | |
94 | * test that error doesn't occur for non-existant file | |
95 | */ | |
96 | function testAPIWrapperIncludeNoFile() { | |
97 | ||
98 | ||
99 | $result = civicrm_api('RandomFile', 'get', array('version' => 3)); | |
100 | $this->assertEquals($result['is_error'], 1); | |
101 | $this->assertEquals($result['error_message'], 'API (RandomFile,get) does not exist (join the API team and implement it!)'); | |
102 | } | |
103 | ||
104 | function testAPIWrapperCamelCaseFunction() { | |
105 | $result = civicrm_api('OptionGroup', 'Get', array( | |
106 | 'version' => 3, | |
107 | )); | |
108 | $this->assertEquals(0, $result['is_error']); | |
109 | } | |
110 | ||
111 | function testAPIWrapperLcaseFunction() { | |
112 | $result = civicrm_api('OptionGroup', 'get', array( | |
113 | 'version' => 3, | |
114 | )); | |
115 | $this->assertEquals(0, $result['is_error']); | |
116 | } | |
117 | ||
118 | function testAPIResolver() { | |
119 | $oldpath = get_include_path(); | |
120 | set_include_path($oldpath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver'); | |
121 | ||
122 | $result = civicrm_api('contact', 'example_action1', array( | |
123 | 'version' => 3, | |
124 | )); | |
125 | $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok'); | |
126 | $result = civicrm_api('contact', 'example_action2', array( | |
127 | 'version' => 3, | |
128 | )); | |
129 | $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok'); | |
130 | $result = civicrm_api('test_entity', 'example_action3', array( | |
131 | 'version' => 3, | |
132 | )); | |
133 | $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok'); | |
134 | ||
135 | set_include_path($oldpath); | |
136 | } | |
137 | ||
138 | function testFromCamel() { | |
139 | $cases = array( | |
140 | 'Contribution' => 'contribution', | |
141 | 'contribution' => 'contribution', | |
142 | 'OptionValue' => 'option_value', | |
143 | 'optionValue' => 'option_value', | |
144 | 'option_value' => 'option_value', | |
145 | 'UFJoin' => 'uf_join', | |
146 | 'ufJoin' => 'uf_join', | |
147 | 'uf_join' => 'uf_join', | |
148 | ); | |
149 | foreach ($cases as $input => $expected) { | |
150 | $actual = _civicrm_api_get_entity_name_from_camel($input); | |
151 | $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual)); | |
152 | } | |
153 | } | |
154 | ||
155 | function testToCamel() { | |
156 | $cases = array( | |
157 | 'Contribution' => 'Contribution', | |
158 | 'contribution' => 'Contribution', | |
159 | 'OptionValue' => 'OptionValue', | |
160 | 'optionValue' => 'OptionValue', | |
161 | 'option_value' => 'OptionValue', | |
162 | 'UFJoin' => 'UFJoin', | |
163 | // dommage 'ufJoin' => 'UFJoin', | |
164 | 'uf_join' => 'UFJoin', | |
165 | ); | |
166 | foreach ($cases as $input => $expected) { | |
167 | $actual = _civicrm_api_get_camel_name($input); | |
168 | $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual)); | |
169 | } | |
170 | } | |
6b359437 | 171 | /** |
172 | * Test that calling via wrapper works | |
173 | */ | |
174 | function testv3Wrapper() { | |
175 | try{ | |
176 | $result = civicrm_api3('contact', 'get', array()); | |
177 | } | |
178 | catch (CRM_Exception $e){ | |
179 | $this->fail("This should have been a success test"); | |
180 | } | |
181 | $this->assertAPISuccess($result); | |
182 | } | |
183 | ||
184 | /** | |
185 | * test exception is thrown | |
186 | */ | |
187 | function testv3WrapperException(){ | |
188 | try{ | |
189 | $result = civicrm_api3('contact', 'create', array('debug' => 1)); | |
190 | } | |
191 | catch (CiviCRM_API3_Exception $e){ | |
192 | $this->assertEquals('undefined', $e->getErrorCode()); | |
193 | $this->assertEquals('Mandatory key(s) missing from params array: contact_type', $e->getMessage()); | |
194 | $extra = $e->getExtraParams(); | |
195 | $this->assertArrayHasKey('trace', $extra); | |
196 | return; | |
197 | } | |
198 | $this->fail('Exception was expected'); | |
199 | } | |
200 | ||
6a488035 TO |
201 | } |
202 |