2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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;
58 require('include/modules.php');
60 $this->_soapURL = $GLOBALS['sugar_config']['site_url'] . '/service/v4_1/soap.php';
64 $current_user = SugarTestUserUtilities::createAnonymousUser();
65 $this->another_user = SugarTestUserUtilities::createAnonymousUser();
67 $this->contact1 = SugarTestContactUtilities::createContact();
68 $this->contact1->contacts_users_id = $current_user->id;
69 $this->contact1->first_name = 'First1';
70 $this->contact1->last_name = 'Last1';
71 $this->contact1->save();
73 $this->contact1->user_sync->add($current_user);
74 $this->contact1->sync_contact = 1;
75 $this->contact1->save();
77 $this->contact2 = SugarTestContactUtilities::createContact();
78 $this->contact2->contacts_users_id = $this->another_user->id;
79 $this->contact2->first_name = 'First2';
80 $this->contact2->last_name = 'Last2';
81 $this->contact2->save();
83 $this->contact2->user_sync->add($this->another_user);
84 $this->contact2->sync_contact = 1;
85 $this->contact2->save();
87 $this->call1 = SugarTestCallUtilities::createCall();
88 $this->call1->name = 'RESTAPI4_1Test1';
89 $this->call1->load_relationship('users');
90 $this->call1->users->add($current_user);
93 $this->call2 = SugarTestCallUtilities::createCall();
94 $this->call2->name = 'RESTAPI4_1Test2';
95 $this->call2->load_relationship('users');
96 $this->call2->users->add($current_user);
99 $this->meeting1 = SugarTestMeetingUtilities::createMeeting();
100 $this->meeting1->name = 'RESTAPI4_1Test1';
101 $this->meeting1->load_relationship('users');
102 $this->meeting1->users->add($current_user);
103 $this->meeting1->save();
105 $this->meeting2 = SugarTestMeetingUtilities::createMeeting();
106 $this->meeting2->name = 'RESTAPI4_1Test2';
107 $this->meeting2->load_relationship('users');
108 $this->meeting2->users->add($this->another_user);
109 $this->meeting2->save();
111 $this->meeting3 = SugarTestMeetingUtilities::createMeeting();
112 $this->meeting3->name = 'RESTAPI4_1Test3';
113 $this->meeting3->load_relationship('users');
114 $this->meeting3->users->add($current_user);
115 $this->meeting3->save();
117 $this->meeting4 = SugarTestMeetingUtilities::createMeeting();
118 $this->meeting4->name = 'SOAPAPI4_1Test4';
119 $this->meeting4->load_relationship('users');
120 $this->meeting4->users->add($current_user);
121 $this->meeting4->mark_deleted($this->meeting4->id);
122 $this->meeting4->deleted = 1;
123 $this->meeting4->save();
124 $GLOBALS['db']->commit();
127 public function tearDown()
129 SugarTestContactUtilities::removeCreatedContactsUsersRelationships();
130 SugarTestContactUtilities::removeAllCreatedContacts();
131 SugarTestMeetingUtilities::removeMeetingContacts();
132 SugarTestMeetingUtilities::removeAllCreatedMeetings();
133 SugarTestCallUtilities::removeAllCreatedCalls();
134 unset($GLOBALS['beanList']);
135 unset($GLOBALS['beanFiles']);
136 unset($GLOBALS['app_list_strings']);
137 unset($GLOBALS['app_strings']);
138 unset($GLOBALS['mod_strings']);
139 unset($GLOBALS['current_user']);
142 protected function _makeRESTCall($method,$parameters)
144 // specify the REST web service to interact with
145 $url = $GLOBALS['sugar_config']['site_url'].'/service/v4_1/rest.php';
146 // Open a curl session for making the call
147 $curl = curl_init($url);
148 // set URL and other appropriate options
149 curl_setopt($curl, CURLOPT_URL, $url);
150 curl_setopt($curl, CURLOPT_POST, 1);
151 curl_setopt($curl, CURLOPT_HEADER, 0);
152 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
153 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
154 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
155 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
156 // build the request URL
157 $json = json_encode($parameters);
158 $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
159 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
160 // Make the REST call, returning the result
161 $response = curl_exec($curl);
162 // Close the connection
165 $this->_lastRawResponse = $response;
167 // Convert the result from JSON format to a PHP array
168 return json_decode($response,true);
171 protected function _returnLastRawResponse()
173 return "Error in web services call. Response was: {$this->_lastRawResponse}";
176 protected function _login()
178 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
179 global $current_user;
180 return $this->_makeRESTCall('login',
184 'user_name' => $current_user->user_name,
185 'password' => $current_user->user_hash,
188 'application_name' => 'mobile',
189 'name_value_list' => array(),
198 public function testGetModifiedRelationships()
200 $result = $this->_login();
201 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
202 $session = $result['id'];
204 $callsAndMeetingsFields = array('id', 'date_modified', 'deleted', 'name', 'rt.deleted synced');
205 $contactsSelectFields = array('id', 'date_modified', 'deleted', 'first_name', 'last_name', 'rt.deleted synced');
207 global $timedate, $current_user;
208 $one_hour_ago = $timedate->asDb($timedate->getNow()->get("-1 hours"));
209 $one_hour_later = $timedate->asDb($timedate->getNow()->get("+1 hours"));
211 $result = $this->_makeRESTCall('get_modified_relationships',
213 'session' => $session,
214 'module_name' => 'Users',
215 'related_module' => 'Contacts',
216 'from_date' => $one_hour_ago,
217 'to_date' => $one_hour_later,
221 'user_id' => $current_user->id,
222 'select_fields' => $contactsSelectFields,
223 'relationship_name' => 'contacts_users',
224 'deletion_date' => '',
228 $this->assertNotEmpty($result['entry_list']);
229 $this->assertEquals(1, $result['result_count']);
230 $this->assertEquals(1, $result['next_offset']);
233 $result = $this->_makeRESTCall('get_modified_relationships',
235 'session' => $session,
236 'module_name' => 'Users',
237 'related_module' => 'Meetings',
238 'from_date' => $one_hour_ago,
239 'to_date' => $one_hour_later,
243 'user_id' => $current_user->id,
244 'select_fields' => $callsAndMeetingsFields,
245 'relationship_name' => 'meetings_users',
246 'deletion_date' => '',
250 $this->assertNotEmpty($result['entry_list']);
251 $this->assertEquals(2, $result['result_count']);
252 $this->assertEquals(2, $result['next_offset']);
254 $result = $this->_makeRESTCall('get_modified_relationships',
256 'session' => $session,
257 'module_name' => 'Users',
258 'related_module' => 'Meetings',
259 'from_date' => $one_hour_ago,
260 'to_date' => $one_hour_later,
264 'user_id' => $current_user->id,
265 'select_fields' => $callsAndMeetingsFields,
266 'relationship_name' => 'meetings_users',
267 'deletion_date' => $one_hour_ago,
271 $this->assertNotEmpty($result['entry_list']);
272 $this->assertEquals(1, $result['result_count']);
273 $this->assertEquals(1, $result['next_offset']);