commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / api / v3 / APITest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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;
38
39 protected $_apiversion = 3;
40
41 public function testAPIReplaceVariables() {
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($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-existent file.
73 */
74 public function testAPIWrapperIncludeNoFile() {
75 $this->callAPIFailure(
76 'RandomFile',
77 'get',
78 array(),
79 'API (RandomFile,get) does not exist (join the API team and implement it!)'
80 );
81 }
82
83 public function testAPIWrapperCamelCaseFunction() {
84 $this->callAPISuccess('OptionGroup', 'Get', array());
85 }
86
87 public function testAPIWrapperLcaseFunction() {
88 $this->callAPISuccess('OptionGroup', 'get', array());
89 }
90
91 /**
92 * Test resolver.
93 */
94 public function testAPIResolver() {
95 $oldPath = get_include_path();
96 set_include_path($oldPath . PATH_SEPARATOR . dirname(__FILE__) . '/dataset/resolver');
97
98 $result = $this->callAPISuccess('contact', 'example_action1', array());
99 $this->assertEquals($result['values'][0], 'civicrm_api3_generic_example_action1 is ok');
100 $result = $this->callAPISuccess('contact', 'example_action2', array());
101 $this->assertEquals($result['values'][0], 'civicrm_api3_contact_example_action2 is ok');
102 $result = $this->callAPISuccess('test_entity', 'example_action3', array());
103 $this->assertEquals($result['values'][0], 'civicrm_api3_test_entity_example_action3 is ok');
104
105 set_include_path($oldPath);
106 }
107
108 public function testFromCamel() {
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
125 public function testToCamel() {
126 $cases = array(
127 'Contribution' => 'Contribution',
128 'contribution' => 'Contribution',
129 'OptionValue' => 'OptionValue',
130 'optionValue' => 'OptionValue',
131 'option_value' => 'OptionValue',
132 'UFJoin' => 'UFJoin',
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 }
140
141 /**
142 * Test that calling via wrapper works.
143 */
144 public function testv3Wrapper() {
145 try {
146 $result = civicrm_api3('contact', 'get', array());
147 }
148 catch (CRM_Exception $e) {
149 $this->fail("This should have been a success test");
150 }
151 $this->assertTrue(is_array($result));
152 $this->assertAPISuccess($result);
153 }
154
155 /**
156 * Test exception is thrown.
157 */
158 public function testV3WrapperException() {
159 try {
160 civicrm_api3('contact', 'create', array('debug' => 1));
161 }
162 catch (CiviCRM_API3_Exception $e) {
163 $this->assertEquals('mandatory_missing', $e->getErrorCode());
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
172 /**
173 * Test result parsing for null.
174 */
175 public function testCreateNoStringNullResult() {
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
199 // Check return format.
200 $this->assertEquals(1, $result['count']);
201 foreach ($result['values'] as $resultValue) {
202 $this->assertEquals('New title', $resultValue['title']);
203 // BUG: $resultValue['location'] === 'null'.
204 $this->assertEquals('', $resultValue['currency']);
205 }
206 }
207
208 }