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;
41 * @param string $prefix
43 protected function assertAPIErrorCode($apiResult, $cmpvar, $prefix = '') {
44 if (!empty($prefix)) {
47 $this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ?
'' : $apiResult['error_message']));
48 //$this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . print_r($apiResult, TRUE));
51 protected function setUp() {
53 //URL should eventually be adapted for multisite
54 $this->url
= "{$this->settings->sandboxURL}/{$this->sboxPath}sites/all/modules/civicrm/extern/rest.php";
56 if (!property_exists($this->settings
, 'siteKey') ||
empty($this->settings
->siteKey
)) {
57 $this->markTestSkipped('CiviSeleniumSettings is missing siteKey');
59 if (!property_exists($this->settings
, 'adminApiKey') ||
empty($this->settings
->adminApiKey
)) {
60 $this->markTestSkipped('CiviSeleniumSettings is missing adminApiKey');
64 protected function tearDown() {
66 if (isset($this->nocms_contact_id
)) {
67 $deleteParams = array(
68 "id" => $this->nocms_contact_id
,
71 $res = $this->webtest_civicrm_api("Contact", "delete", $deleteParams);
72 unset($this->nocms_contact_id
);
77 * Build a list of test cases. Each test case defines a set of REST query
78 * parameters and an expected outcome for the REST request (eg is_error=>1 or is_error=>0).
80 * @return array; each item is a list of parameters for testAPICalls
82 function apiTestCases() {
85 // entity,action: omit apiKey, valid entity+action
88 "entity" => "Contact",
90 "key" => $this->settings
->siteKey
,
96 // entity,action: valid apiKey, valid entity+action
99 "entity" => "Contact",
101 "key" => $this->settings
->siteKey
,
103 "api_key" => $this->settings
->adminApiKey
,
108 // entity,action: bad apiKey, valid entity+action
111 "entity" => "Contact",
113 "key" => $this->settings
->siteKey
,
115 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
120 // entity,action: valid apiKey, invalid entity+action
123 "entity" => "Contactses",
125 "key" => $this->settings
->siteKey
,
127 "api_key" => $this->settings
->adminApiKey
,
132 // q=civicrm/entity/action: omit apiKey, valid entity+action
135 "q" => "civicrm/contact/get",
136 "key" => $this->settings
->siteKey
,
142 // q=civicrm/entity/action: valid apiKey, valid entity+action
145 "q" => "civicrm/contact/get",
146 "key" => $this->settings
->siteKey
,
148 "api_key" => $this->settings
->adminApiKey
,
153 // q=civicrm/entity/action: invalid apiKey, valid entity+action
156 "q" => "civicrm/contact/get",
157 "key" => $this->settings
->siteKey
,
159 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
164 // q=civicrm/entity/action: valid apiKey, invalid entity+action
167 "q" => "civicrm/contactses/get",
168 "key" => $this->settings
->siteKey
,
170 "api_key" => $this->settings
->adminApiKey
,
175 // q=civicrm/entity/action: valid apiKey, invalid entity+action
176 // XXX Actually Ping is valid, no?
179 "q" => "civicrm/ping",
180 "key" => $this->settings
->siteKey
,
182 "api_key" => $this->settings
->adminApiKey
,
191 * @dataProvider apiTestCases
193 function testAPICalls($query, $is_error) {
194 $client = CRM_Utils_HttpClient
::singleton();
195 list($status, $data) = $client->post($this->url
, $query);
196 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
197 $result = json_decode($data, TRUE);
198 if ($result === NULL) {
199 $msg = print_r(array('query' => $query, 'response data' => $data), TRUE);
200 $this->assertNotNull($result, $msg);
202 $this->assertAPIErrorCode($result, $is_error);
206 * Submit a request with an API key that exists but does not correspond to
207 * a real user. Submit in "?entity=X&action=X" notation
209 function testNotCMSUser_entityAction() {
210 $client = CRM_Utils_HttpClient
::singleton();
212 //Create contact with api_key
213 $test_key = "testing1234";
214 $contactParams = array(
215 "api_key" => $test_key,
216 "contact_type" => "Individual",
217 "first_name" => "RestTester1"
219 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
220 $this->nocms_contact_id
= $contact["id"];
222 // The key associates with a real contact but not a real user
224 "entity" => "Contact",
226 "key" => $this->settings
->siteKey
,
228 "api_key" => $test_key
230 list($status, $data) = $client->post($this->url
, $params);
231 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
232 $result = json_decode($data, TRUE);
233 $this->assertNotNull($result);
234 $this->assertAPIErrorCode($result, 1);
238 * Submit a request with an API key that exists but does not correspond to
239 * a real user. Submit in "?q=civicrm/$entity/$action" notation
241 function testNotCMSUser_q() {
242 $client = CRM_Utils_HttpClient
::singleton();
244 //Create contact with api_key
245 $test_key = "testing1234";
246 $contactParams = array(
247 "api_key" => $test_key,
248 "contact_type" => "Individual",
249 "first_name" => "RestTester1"
251 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
252 $this->nocms_contact_id
= $contact["id"];
254 // The key associates with a real contact but not a real user
256 "q" => "civicrm/contact/get",
257 "key" => $this->settings
->siteKey
,
259 "api_key" => $test_key
261 list($status, $data) = $client->post($this->url
, $params);
262 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
263 $result = json_decode($data, TRUE);
264 $this->assertNotNull($result);
265 $this->assertAPIErrorCode($result, 1);