3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for API functions.
31 * @package CiviCRM_APIv3
34 class api_v3_APITest
extends CiviUnitTestCase
{
35 public $DBResetRequired = FALSE;
37 protected $_apiversion = 3;
39 public function testAPIReplaceVariables() {
41 $result['testfield'] = 6;
42 $result['api.tag.get'] = 999;
43 $result['api.tag.create']['id'] = 8;
44 $result['api.entity.create.0']['id'] = 7;
45 $result['api.tag.create'][2]['id'] = 'superman';
46 $result['api.tag.create']['values']['0']['display'] = 'batman';
47 $result['api.tag.create.api.tag.create']['values']['0']['display'] = 'krypton';
48 $result['api.tag.create']['values']['0']['api_tag_get'] = 'darth vader';
50 'activity_type_id' => '$value.testfield',
51 'tag_id' => '$value.api.tag.create.id',
52 'tag1_id' => '$value.api.entity.create.0.id',
53 'tag3_id' => '$value.api.tag.create.2.id',
54 'display' => '$value.api.tag.create.values.0.display',
55 'number' => '$value.api.tag.get',
56 'big_rock' => '$value.api.tag.create.api.tag.create.values.0.display',
57 'villain' => '$value.api.tag.create.values.0.api_tag_get.display',
59 _civicrm_api_replace_variables($params, $result);
60 $this->assertEquals(999, $params['number']);
61 $this->assertEquals(8, $params['tag_id']);
62 $this->assertEquals(6, $params['activity_type_id']);
63 $this->assertEquals(7, $params['tag1_id']);
64 $this->assertEquals('superman', $params['tag3_id']);
65 $this->assertEquals('batman', $params['display']);
66 $this->assertEquals('krypton', $params['big_rock']);
70 * Test that error doesn't occur for non-existent file.
72 public function testAPIWrapperIncludeNoFile() {
73 $this->callAPIFailure(
77 'API (RandomFile, get) does not exist (join the API team and implement it!)'
81 public function testAPIWrapperCamelCaseFunction() {
82 $this->callAPISuccess('OptionGroup', 'Get', array());
85 public function testAPIWrapperLcaseFunction() {
86 $this->callAPISuccess('OptionGroup', 'get', array());
92 public function testAPIResolver() {
93 $oldPath = get_include_path();
94 set_include_path($oldPath . PATH_SEPARATOR
. dirname(__FILE__
) . '/dataset/resolver');
96 $result = $this->callAPISuccess('contact', 'example_action1', array());
97 $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok');
98 $result = $this->callAPISuccess('contact', 'example_action2', array());
99 $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok');
100 $result = $this->callAPISuccess('test_entity', 'example_action3', array());
101 $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok');
103 set_include_path($oldPath);
106 public function testFromCamel() {
108 'Contribution' => 'contribution',
109 'contribution' => 'contribution',
110 'OptionValue' => 'option_value',
111 'optionValue' => 'option_value',
112 'option_value' => 'option_value',
113 'UFJoin' => 'uf_join',
114 'ufJoin' => 'uf_join',
115 'uf_join' => 'uf_join',
117 foreach ($cases as $input => $expected) {
118 $actual = _civicrm_api_get_entity_name_from_camel($input);
119 $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual));
123 public function testToCamel() {
125 'Contribution' => 'Contribution',
126 'contribution' => 'Contribution',
127 'OptionValue' => 'OptionValue',
128 'optionValue' => 'OptionValue',
129 'option_value' => 'OptionValue',
130 'UFJoin' => 'UFJoin',
131 'uf_join' => 'UFJoin',
133 foreach ($cases as $input => $expected) {
134 $actual = _civicrm_api_get_camel_name($input);
135 $this->assertEquals($expected, $actual, sprintf('input=%s expected=%s actual=%s', $input, $expected, $actual));
140 * Test that calling via wrapper works.
142 public function testv3Wrapper() {
144 $result = civicrm_api3('contact', 'get', array());
146 catch (CRM_Exception
$e) {
147 $this->fail("This should have been a success test");
149 $this->assertTrue(is_array($result));
150 $this->assertAPISuccess($result);
154 * Test exception is thrown.
156 public function testV3WrapperException() {
158 civicrm_api3('contact', 'create', array('debug' => 1));
160 catch (CiviCRM_API3_Exception
$e) {
161 $this->assertEquals('mandatory_missing', $e->getErrorCode());
162 $this->assertEquals('Mandatory key(s) missing from params array: contact_type', $e->getMessage());
163 $extra = $e->getExtraParams();
164 $this->assertArrayHasKey('trace', $extra);
167 $this->fail('Exception was expected');
171 * Test result parsing for null.
173 public function testCreateNoStringNullResult() {
174 // create an example contact
175 // $contact = CRM_Core_DAO::createTestObject('CRM_Contribute_DAO_ContributionPage')->toArray();
176 $result = $this->callAPISuccess('ContributionPage', 'create', array(
177 'title' => "Test Contribution Page",
178 'financial_type_id' => 1,
180 'goal_amount' => 100,
182 $contact = array_shift($result['values']);
184 $this->assertTrue(is_numeric($contact['id']));
185 $this->assertNotEmpty($contact['title']);
186 // preferred_mail_format preferred_communication_method preferred_language gender_id
188 $this->assertNotEmpty($contact['currency']);
190 // update the contact
191 $result = $this->callAPISuccess('ContributionPage', 'create', array(
192 'id' => $contact['id'],
193 'title' => 'New title',
197 // Check return format.
198 $this->assertEquals(1, $result['count']);
199 foreach ($result['values'] as $resultValue) {
200 $this->assertEquals('New title', $resultValue['title']);
201 // BUG: $resultValue['location'] === 'null'.
202 $this->assertEquals('', $resultValue['currency']);