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 ********************************************************************************/
39 require_once('tests/service/APIv3Helper.php');
41 class RESTAPI4_1Test extends Sugar_PHPUnit_Framework_TestCase
43 protected $_lastRawResponse;
46 protected $another_user;
53 public function setUp()
55 global $beanList, $beanFiles, $current_user;
56 global $beanList, $beanFiles;
59 require('include/modules.php');
61 $this->_soapURL = $GLOBALS['sugar_config']['site_url'] . '/service/v4_1/soap.php';
63 $current_user = SugarTestUserUtilities::createAnonymousUser();
64 $this->another_user = SugarTestUserUtilities::createAnonymousUser();
67 $current_user = SugarTestUserUtilities::createAnonymousUser();
68 $this->another_user = SugarTestUserUtilities::createAnonymousUser();
70 $this->contact1 = SugarTestContactUtilities::createContact();
71 $this->contact1->contacts_users_id = $current_user->id;
72 $this->contact1->first_name = 'First1';
73 $this->contact1->last_name = 'Last1';
74 $this->contact1->save();
76 $this->contact1->user_sync->add($current_user);
77 $this->contact1->sync_contact = 1;
78 $this->contact1->save();
80 $this->contact2 = SugarTestContactUtilities::createContact();
81 $this->contact2->contacts_users_id = $this->another_user->id;
82 $this->contact2->first_name = 'First2';
83 $this->contact2->last_name = 'Last2';
84 $this->contact2->save();
86 $this->contact2->user_sync->add($this->another_user);
87 $this->contact2->sync_contact = 1;
88 $this->contact2->save();
90 $this->call1 = SugarTestCallUtilities::createCall();
91 $this->call1->name = 'RESTAPI4_1Test1';
92 $this->call1->load_relationship('users');
93 $this->call1->users->add($current_user);
96 $this->call2 = SugarTestCallUtilities::createCall();
97 $this->call2->name = 'RESTAPI4_1Test2';
98 $this->call2->load_relationship('users');
99 $this->call2->users->add($current_user);
100 $this->call2->save();
102 $this->meeting1 = SugarTestMeetingUtilities::createMeeting();
103 $this->meeting1->name = 'RESTAPI4_1Test1';
104 $this->meeting1->load_relationship('users');
105 $this->meeting1->users->add($current_user);
106 $this->meeting1->save();
108 $this->meeting2 = SugarTestMeetingUtilities::createMeeting();
109 $this->meeting2->name = 'RESTAPI4_1Test2';
110 $this->meeting2->load_relationship('users');
111 $this->meeting2->users->add($this->another_user);
112 $this->meeting2->save();
114 $this->meeting3 = SugarTestMeetingUtilities::createMeeting();
115 $this->meeting3->name = 'RESTAPI4_1Test3';
116 $this->meeting3->load_relationship('users');
117 $this->meeting3->users->add($current_user);
118 $this->meeting3->save();
120 $this->meeting4 = SugarTestMeetingUtilities::createMeeting();
121 $this->meeting4->name = 'SOAPAPI4_1Test4';
122 $this->meeting4->load_relationship('users');
123 $this->meeting4->users->add($current_user);
124 $this->meeting4->mark_deleted($this->meeting4->id);
125 $this->meeting4->deleted = 1;
126 $this->meeting4->save();
127 $GLOBALS['db']->commit();
130 public function tearDown()
132 SugarTestContactUtilities::removeCreatedContactsUsersRelationships();
133 SugarTestContactUtilities::removeAllCreatedContacts();
134 SugarTestMeetingUtilities::removeMeetingContacts();
135 SugarTestMeetingUtilities::removeAllCreatedMeetings();
136 SugarTestCallUtilities::removeAllCreatedCalls();
137 unset($GLOBALS['beanList']);
138 unset($GLOBALS['beanFiles']);
139 unset($GLOBALS['app_list_strings']);
140 unset($GLOBALS['app_strings']);
141 unset($GLOBALS['mod_strings']);
142 unset($GLOBALS['current_user']);
145 protected function _makeRESTCall($method,$parameters)
147 // specify the REST web service to interact with
148 $url = $GLOBALS['sugar_config']['site_url'].'/service/v4_1/rest.php';
149 // Open a curl session for making the call
150 $curl = curl_init($url);
151 // set URL and other appropriate options
152 curl_setopt($curl, CURLOPT_URL, $url);
153 curl_setopt($curl, CURLOPT_POST, 1);
154 curl_setopt($curl, CURLOPT_HEADER, 0);
155 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
156 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
157 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
158 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
159 // build the request URL
160 $json = json_encode($parameters);
161 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
162 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
163 // Make the REST call, returning the result
164 $response = curl_exec($curl);
165 // Close the connection
168 $this->_lastRawResponse = $response;
170 // Convert the result from JSON format to a PHP array
171 return json_decode($response,true);
174 protected function _returnLastRawResponse()
176 return "Error in web services call. Response was: {$this->_lastRawResponse}";
179 protected function _login()
181 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
182 global $current_user;
183 return $this->_makeRESTCall('login',
187 'user_name' => $current_user->user_name,
188 'password' => $current_user->user_hash,
191 'application_name' => 'mobile',
192 'name_value_list' => array(),
201 public function testGetModifiedRelationships()
203 $result = $this->_login();
204 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
205 $session = $result['id'];
207 $callsAndMeetingsFields = array('id', 'date_modified', 'deleted', 'name', 'rt.deleted synced');
208 $contactsSelectFields = array('id', 'date_modified', 'deleted', 'first_name', 'last_name', 'rt.deleted synced');
210 global $timedate, $current_user;
211 $one_hour_ago = $timedate->asDb($timedate->getNow()->get("-1 hours"));
212 $one_hour_later = $timedate->asDb($timedate->getNow()->get("+1 hours"));
214 $result = $this->_makeRESTCall('get_modified_relationships',
216 'session' => $session,
217 'module_name' => 'Users',
218 'related_module' => 'Contacts',
219 'from_date' => $one_hour_ago,
220 'to_date' => $one_hour_later,
224 'user_id' => $current_user->id,
225 'select_fields' => $contactsSelectFields,
226 'relationship_name' => 'contacts_users',
227 'deletion_date' => '',
231 $this->assertNotEmpty($result['entry_list']);
232 $this->assertEquals(1, $result['result_count']);
233 $this->assertEquals(1, $result['next_offset']);
236 $result = $this->_makeRESTCall('get_modified_relationships',
238 'session' => $session,
239 'module_name' => 'Users',
240 'related_module' => 'Meetings',
241 'from_date' => $one_hour_ago,
242 'to_date' => $one_hour_later,
246 'user_id' => $current_user->id,
247 'select_fields' => $callsAndMeetingsFields,
248 'relationship_name' => 'meetings_users',
249 'deletion_date' => '',
253 $this->assertNotEmpty($result['entry_list']);
254 $this->assertEquals(2, $result['result_count']);
255 $this->assertEquals(2, $result['next_offset']);
257 $result = $this->_makeRESTCall('get_modified_relationships',
259 'session' => $session,
260 'module_name' => 'Users',
261 'related_module' => 'Meetings',
262 'from_date' => $one_hour_ago,
263 'to_date' => $one_hour_later,
267 'user_id' => $current_user->id,
268 'select_fields' => $callsAndMeetingsFields,
269 'relationship_name' => 'meetings_users',
270 'deletion_date' => $one_hour_ago,
274 $this->assertNotEmpty($result['entry_list']);
275 $this->assertEquals(1, $result['result_count']);
276 $this->assertEquals(1, $result['next_offset']);