2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 require_once('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
42 class RESTAPI4Test extends Sugar_PHPUnit_Framework_TestCase
45 protected $_admin_user;
46 protected $_lastRawResponse;
48 private static $helperObject;
53 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');
65 //Create an anonymous user for login purposes/
66 $this->_user = SugarTestUserUtilities::createAnonymousUser();
68 $this->_admin_user = SugarTestUserUtilities::createAnonymousUser();
69 $this->_admin_user->status = 'Active';
70 $this->_admin_user->is_admin = 1;
71 $this->_admin_user->save();
72 $GLOBALS['current_user'] = $this->_user;
74 self::$helperObject = new APIv3Helper();
76 //Disable access to the website field.
77 $this->aclRole = new ACLRole();
78 $this->aclRole->name = "Unit Test";
79 $this->aclRole->save();
80 $this->aclRole->set_relationship('acl_roles_users', array('role_id'=>$this->aclRole->id ,'user_id'=> $this->_user->id), false);
83 public function tearDown()
85 $GLOBALS['db']->query("DELETE FROM acl_roles WHERE id IN ( SELECT role_id FROM acl_user_roles WHERE user_id = '{$GLOBALS['current_user']->id}' )");
86 $GLOBALS['db']->query("DELETE FROM acl_user_roles WHERE user_id = '{$GLOBALS['current_user']->id}'");
88 if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']);
89 if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']);
90 unset($GLOBALS['beanList']);
91 unset($GLOBALS['beanFiles']);
92 unset($GLOBALS['app_list_strings']);
93 unset($GLOBALS['app_strings']);
94 unset($GLOBALS['mod_strings']);
95 unset($GLOBALS['current_user']);
96 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
99 protected function _makeRESTCall($method,$parameters)
101 // specify the REST web service to interact with
102 $url = $GLOBALS['sugar_config']['site_url'].'/service/v4/rest.php';
103 // Open a curl session for making the call
104 $curl = curl_init($url);
105 // set URL and other appropriate options
106 curl_setopt($curl, CURLOPT_URL, $url);
107 curl_setopt($curl, CURLOPT_POST, 1);
108 curl_setopt($curl, CURLOPT_HEADER, 0);
109 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
110 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
111 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
112 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
113 // build the request URL
114 $json = json_encode($parameters);
115 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
116 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
117 // Make the REST call, returning the result
118 $response = curl_exec($curl);
119 // Close the connection
122 $this->_lastRawResponse = $response;
124 // Convert the result from JSON format to a PHP array
125 return json_decode($response,true);
128 protected function _returnLastRawResponse()
130 return "Error in web services call. Response was: {$this->_lastRawResponse}";
133 protected function _login($user = null)
136 $user = $this->_user;
137 return $this->_makeRESTCall('login',
141 'user_name' => $user->user_name,
142 'password' => $user->user_hash,
145 'application_name' => 'mobile',
146 'name_value_list' => array(),
151 * Ensure the ability to retrieve a module list of recrods that are favorites.
154 public function testGetModuleFavoriteList()
156 $result = $this->_login($this->_admin_user);
157 $session = $result['id'];
159 $account = new Account();
160 $account->id = uniqid();
161 $account->new_with_id = TRUE;
162 $account->name = "Test " . $account->id;
165 $this->_markBeanAsFavorite($session, "Accounts", $account->id);
167 $whereClause = "accounts.name='{$account->name}'";
168 $module = 'Accounts';
171 $returnFields = array('name');
172 $linkNameFields = "";
176 $result = $this->_makeRESTCall('get_entry_list', array($session, $module, $whereClause, $orderBy,$offset, $returnFields,$linkNameFields, $maxResults, $deleted, $favorites));
178 $this->assertEquals($account->id, $result['entry_list'][0]['id'],'Unable to retrieve account favorite list.');
180 $GLOBALS['db']->query("DELETE FROM accounts WHERE id = '{$account->id}'");
181 $GLOBALS['db']->query("DELETE FROM sugarfavorites WHERE record_id = '{$account->id}'");
185 * Test set entries call with name value list format key=>value.
188 public function testSetEntriesCall()
190 $result = $this->_login();
191 $session = $result['id'];
192 $module = 'Contacts';
196 array('first_name' => 'Unit Test', 'last_name' => $c1_uuid),
197 array('first_name' => 'Unit Test', 'last_name' => $c2_uuid)
199 $results = $this->_makeRESTCall('set_entries',
201 'session' => $session,
203 'name_value_lists' => $contacts,
205 $this->assertTrue(isset($results['ids']) && count($results['ids']) == 2);
207 $actual_results = $this->_makeRESTCall('get_entries',
209 'session' => $session,
211 'ids' => $results['ids'],
212 'select_fields' => array('first_name','last_name')
215 $this->assertTrue(isset($actual_results['entry_list']) && count($actual_results['entry_list']) == 2);
216 $this->assertEquals($actual_results['entry_list'][0]['name_value_list']['last_name']['value'], $c1_uuid);
217 $this->assertEquals($actual_results['entry_list'][1]['name_value_list']['last_name']['value'], $c2_uuid);
222 * Test search by module with favorites flag enabled.
225 public function testSearchByModuleWithFavorites()
227 $result = $this->_login($this->_admin_user);
228 $session = $result['id'];
230 $account = new Account();
231 $account->id = uniqid();
232 $account->assigned_user_id = $this->_user->id;
233 $account->team_id = 1;
234 $account->new_with_id = TRUE;
235 $account->name = "Unit Test Fav " . $account->id;
237 $this->_markBeanAsFavorite($session, "Accounts", $account->id);
240 $account2 = new Account();
241 $account2->id = uniqid();
242 $account2->new_with_id = TRUE;
243 $account2->name = "Unit Test Fav " . $account->id;
244 $account->assigned_user_id = $this->_user->id;
247 $searchModules = array('Accounts');
248 $searchString = "Unit Test Fav ";
252 $results = $this->_makeRESTCall('search_by_module',
254 'session' => $session,
255 'search_string' => $searchString,
256 'modules' => $searchModules,
258 'max_results' => $maxResults,
259 'assigned_user_id' => $this->_user->id,
260 'select_fields' => array(),
261 'unified_search_only' => true,
266 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'Unit Test %' ");
267 $GLOBALS['db']->query("DELETE FROM sugarfavorites WHERE record_id = '{$account->id}'");
268 $GLOBALS['db']->query("DELETE FROM sugarfavorites WHERE record_id = '{$account2->id}'");
270 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$account->id,'Accounts'), "Unable to find {$account->id} id in favorites search.");
271 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$account2->id,'Accounts'), "Account {$account2->id} id in favorites search should not be there.");
274 * Private helper function to mark a bean as a favorite item.
276 * @param string $session
277 * @param string $moduleName
278 * @param string $recordID
280 private function _markBeanAsFavorite($session, $moduleName, $recordID)
282 $result = $this->_makeRESTCall('set_entry',
284 'session' => $session,
285 'module' => 'SugarFavorites',
286 'name_value_list' => array(
287 array('name' => 'record_id', 'value' => $recordID),
288 array('name' => 'module', 'value' => $moduleName),