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