]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RestTestCase.php
Release 6.4.0
[Github/sugarcrm.git] / tests / service / RestTestCase.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38 //Uses the latest version of SugarWebServiceUtil implementation
39 require_once('service/v4/SugarWebServiceUtilv4.php');
40
41 /**
42  * RestTestCase
43  *
44  * Abstract class to separate out the common setup, tearDown and utility methods for testing REST API calls
45  * @author Collin Lee
46  * 
47  */
48 abstract class RestTestCase extends Sugar_PHPUnit_Framework_TestCase
49 {
50
51         protected $_soapClient = null;
52     protected $_sessionId;
53     protected $_lastRawResponse;
54
55     public function setUp()
56     {
57                 require('include/modules.php');
58                 $GLOBALS['beanList'] = $beanList;
59                 $GLOBALS['beanFiles'] = $beanFiles;
60
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');
65     }
66
67     /**
68      * tearDown
69      *
70      * This function helps clean up global variables that may have been set and removes the anonymous user created
71      */
72     public function tearDown()
73     {
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']);
81     }
82
83     /**
84      * _makeRestCall
85      *
86      * This function helps wrap the REST call using the CURL libraries
87      *
88      * @param $method String name of the method to call
89      * @param $parameters Mixed array of arguments depending on the method call
90      *
91      * @return mixed JSON decoded response made from REST call
92      */
93     protected function _makeRESTCall($method,$parameters)
94     {
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
114         curl_close($curl);
115
116         $this->_lastRawResponse = $response;
117
118         // Convert the result from JSON format to a PHP array
119         return json_decode($response,true);
120     }
121
122     protected function _returnLastRawResponse()
123     {
124         return "Error in web services call. Response was: {$this->_lastRawResponse}";
125     }
126
127
128     /**
129      * _login
130      *
131      * This function helps make the login call
132      *
133      * @param $user The SugarCRM User bean instance to login with
134      * @return mixed The REST response from the login operation
135      */
136     protected function _login($user)
137     {
138         $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
139         return $this->_makeRESTCall('login',
140             array(
141                 'user_auth' =>
142                     array(
143                         'user_name' => $user->user_name,
144                         'password' => $user->user_hash,
145                         'version' => '.01',
146                         ),
147                 'application_name' => 'mobile',
148                 'name_value_list' => array(),
149                 )
150             );
151     }
152
153 }