2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 //Uses the latest version of SugarWebServiceUtil implementation
39 require_once('service/v4/SugarWebServiceUtilv4.php');
44 * Abstract class to separate out the common setup, tearDown and utility methods for testing REST API calls
48 abstract class RestTestCase extends Sugar_PHPUnit_Framework_TestCase
51 protected $_soapClient = null;
52 protected $_sessionId;
53 protected $_lastRawResponse;
55 public function setUp()
57 require('include/modules.php');
58 $GLOBALS['beanList'] = $beanList;
59 $GLOBALS['beanFiles'] = $beanFiles;
61 //Reload langauge strings
62 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
63 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
64 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Accounts');
70 * This function helps clean up global variables that may have been set and removes the anonymous user created
72 public function tearDown()
74 if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']);
75 if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']);
76 unset($GLOBALS['beanList']);
77 unset($GLOBALS['beanFiles']);
78 unset($GLOBALS['app_list_strings']);
79 unset($GLOBALS['app_strings']);
80 unset($GLOBALS['mod_strings']);
86 * This function helps wrap the REST call using the CURL libraries
88 * @param $method String name of the method to call
89 * @param $parameters Mixed array of arguments depending on the method call
91 * @return mixed JSON decoded response made from REST call
93 protected function _makeRESTCall($method,$parameters)
95 // specify the REST web service to interact with
96 $url = $GLOBALS['sugar_config']['site_url'].'/service/v4/rest.php';
97 // Open a curl session for making the call
98 $curl = curl_init($url);
99 // set URL and other appropriate options
100 curl_setopt($curl, CURLOPT_URL, $url);
101 curl_setopt($curl, CURLOPT_POST, 1);
102 curl_setopt($curl, CURLOPT_HEADER, 0);
103 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
104 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
105 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
106 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
107 // build the request URL
108 $json = json_encode($parameters);
109 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
110 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
111 // Make the REST call, returning the result
112 $response = curl_exec($curl);
113 // Close the connection
116 $this->_lastRawResponse = $response;
118 // Convert the result from JSON format to a PHP array
119 return json_decode($response,true);
122 protected function _returnLastRawResponse()
124 return "Error in web services call. Response was: {$this->_lastRawResponse}";
131 * This function helps make the login call
133 * @param $user The SugarCRM User bean instance to login with
134 * @return mixed The REST response from the login operation
136 protected function _login($user)
138 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
139 return $this->_makeRESTCall('login',
143 'user_name' => $user->user_name,
144 'password' => $user->user_hash,
147 'application_name' => 'mobile',
148 'name_value_list' => array(),