3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
30 * Verify that the REST API bindings correctly parse and authenticate requests.
32 class WebTest_Utils_RestTest
extends CiviSeleniumTestCase
{
35 protected $session_id;
36 protected $nocms_contact_id;
38 protected function assertAPIErrorCode($apiResult, $cmpvar, $prefix = '') {
39 if (!empty($prefix)) {
42 $this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ?
'' : $apiResult['error_message']));
43 //$this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . print_r($apiResult, TRUE));
46 protected function setUp() {
48 //URL should eventually be adapted for multisite
49 $this->url
= "{$this->settings->sandboxURL}/{$this->sboxPath}sites/all/modules/civicrm/extern/rest.php";
51 if (!property_exists($this->settings
, 'siteKey') ||
empty($this->settings
->siteKey
)) {
52 $this->markTestSkipped('CiviSeleniumSettings is missing siteKey');
54 if (!property_exists($this->settings
, 'adminApiKey') ||
empty($this->settings
->adminApiKey
)) {
55 $this->markTestSkipped('CiviSeleniumSettings is missing adminApiKey');
59 protected function tearDown() {
61 if (isset($this->nocms_contact_id
)) {
62 $deleteParams = array(
63 "id" => $this->nocms_contact_id
,
66 $res = $this->webtest_civicrm_api("Contact", "delete", $deleteParams);
67 unset($this->nocms_contact_id
);
72 * Build a list of test cases. Each test case defines a set of REST query
73 * parameters and an expected outcome for the REST request (eg is_error=>1 or is_error=>0).
75 * @return array; each item is a list of parameters for testAPICalls
77 function apiTestCases() {
80 // entity,action: omit apiKey, valid entity+action
83 "entity" => "Contact",
85 "key" => $this->settings
->siteKey
,
91 // entity,action: valid apiKey, valid entity+action
94 "entity" => "Contact",
96 "key" => $this->settings
->siteKey
,
98 "api_key" => $this->settings
->adminApiKey
,
103 // entity,action: bad apiKey, valid entity+action
106 "entity" => "Contact",
108 "key" => $this->settings
->siteKey
,
110 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
115 // entity,action: valid apiKey, invalid entity+action
118 "entity" => "Contactses",
120 "key" => $this->settings
->siteKey
,
122 "api_key" => $this->settings
->adminApiKey
,
127 // q=civicrm/entity/action: omit apiKey, valid entity+action
130 "q" => "civicrm/contact/get",
131 "key" => $this->settings
->siteKey
,
137 // q=civicrm/entity/action: valid apiKey, valid entity+action
140 "q" => "civicrm/contact/get",
141 "key" => $this->settings
->siteKey
,
143 "api_key" => $this->settings
->adminApiKey
,
148 // q=civicrm/entity/action: invalid apiKey, valid entity+action
151 "q" => "civicrm/contact/get",
152 "key" => $this->settings
->siteKey
,
154 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
159 // q=civicrm/entity/action: valid apiKey, invalid entity+action
162 "q" => "civicrm/contactses/get",
163 "key" => $this->settings
->siteKey
,
165 "api_key" => $this->settings
->adminApiKey
,
170 // q=civicrm/entity/action: valid apiKey, invalid entity+action
171 // XXX Actually Ping is valid, no?
174 "q" => "civicrm/ping",
175 "key" => $this->settings
->siteKey
,
177 "api_key" => $this->settings
->adminApiKey
,
186 * @dataProvider apiTestCases
188 function testAPICalls($query, $is_error) {
189 $client = CRM_Utils_HttpClient
::singleton();
190 list($status, $data) = $client->post($this->url
, $query);
191 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
192 $result = json_decode($data, TRUE);
193 if ($result === NULL) {
194 $msg = print_r(array('query' => $query, 'response data' => $data), TRUE);
195 $this->assertNotNull($result, $msg);
197 $this->assertAPIErrorCode($result, $is_error);
201 * Submit a request with an API key that exists but does not correspond to
202 * a real user. Submit in "?entity=X&action=X" notation
204 function testNotCMSUser_entityAction() {
205 $client = CRM_Utils_HttpClient
::singleton();
207 //Create contact with api_key
208 $test_key = "testing1234";
209 $contactParams = array(
210 "api_key" => $test_key,
211 "contact_type" => "Individual",
212 "first_name" => "RestTester1"
214 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
215 $this->nocms_contact_id
= $contact["id"];
217 // The key associates with a real contact but not a real user
219 "entity" => "Contact",
221 "key" => $this->settings
->siteKey
,
223 "api_key" => $test_key
225 list($status, $data) = $client->post($this->url
, $params);
226 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
227 $result = json_decode($data, TRUE);
228 $this->assertNotNull($result);
229 $this->assertAPIErrorCode($result, 1);
233 * Submit a request with an API key that exists but does not correspond to
234 * a real user. Submit in "?q=civicrm/$entity/$action" notation
236 function testNotCMSUser_q() {
237 $client = CRM_Utils_HttpClient
::singleton();
239 //Create contact with api_key
240 $test_key = "testing1234";
241 $contactParams = array(
242 "api_key" => $test_key,
243 "contact_type" => "Individual",
244 "first_name" => "RestTester1"
246 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
247 $this->nocms_contact_id
= $contact["id"];
249 // The key associates with a real contact but not a real user
251 "q" => "civicrm/contact/get",
252 "key" => $this->settings
->siteKey
,
254 "api_key" => $test_key
256 list($status, $data) = $client->post($this->url
, $params);
257 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
258 $result = json_decode($data, TRUE);
259 $this->assertNotNull($result);
260 $this->assertAPIErrorCode($result, 1);