]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPI4_1Test.php
Release 6.5.10
[Github/sugarcrm.git] / tests / service / RESTAPI4_1Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2013 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
39 require_once('tests/service/APIv3Helper.php');
40
41 class RESTAPI4_1Test extends Sugar_PHPUnit_Framework_TestCase
42 {
43     protected $_lastRawResponse;
44     protected $contact1;
45     protected $contact2;
46     protected $another_user;
47     protected $meeting1;
48     protected $meeting2;
49     protected $meeting3;
50     protected $call1;
51     protected $call2;
52
53     public function setUp()
54     {
55         global $beanList, $beanFiles, $current_user;
56         global $beanList, $beanFiles;
57         $beanList = array();
58         $beanFiles = array();
59         require('include/modules.php');
60
61         $this->_soapURL = $GLOBALS['sugar_config']['site_url'] . '/service/v4_1/soap.php';
62         parent::setUp();
63         $current_user = SugarTestUserUtilities::createAnonymousUser();
64         $this->another_user = SugarTestUserUtilities::createAnonymousUser();
65         $this->_login();
66         global $current_user;
67         $current_user = SugarTestUserUtilities::createAnonymousUser();
68         $this->another_user = SugarTestUserUtilities::createAnonymousUser();
69
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();
75
76         $this->contact1->user_sync->add($current_user);
77         $this->contact1->sync_contact = 1;
78         $this->contact1->save();
79
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();
85
86         $this->contact2->user_sync->add($this->another_user);
87         $this->contact2->sync_contact = 1;
88         $this->contact2->save();
89
90         $this->call1 = SugarTestCallUtilities::createCall();
91         $this->call1->name = 'RESTAPI4_1Test1';
92         $this->call1->load_relationship('users');
93         $this->call1->users->add($current_user);
94         $this->call1->save();
95
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();
101
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();
107
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();
113
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();
119
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();
128     }
129
130     public function tearDown()
131         {
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']);
143         }
144
145     protected function _makeRESTCall($method,$parameters)
146     {
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
166         curl_close($curl);
167
168         $this->_lastRawResponse = $response;
169
170         // Convert the result from JSON format to a PHP array
171         return json_decode($response,true);
172     }
173
174     protected function _returnLastRawResponse()
175     {
176         return "Error in web services call. Response was: {$this->_lastRawResponse}";
177     }
178
179     protected function _login()
180     {
181         $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
182         global $current_user;
183         return $this->_makeRESTCall('login',
184             array(
185                 'user_auth' =>
186                     array(
187                         'user_name' => $current_user->user_name,
188                         'password' => $current_user->user_hash,
189                         'version' => '.01',
190                         ),
191                 'application_name' => 'mobile',
192                 'name_value_list' => array(),
193                 )
194             );
195     }
196
197
198     /**
199      *
200      */
201     public function testGetModifiedRelationships()
202     {
203         $result = $this->_login();
204         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
205         $session = $result['id'];
206
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');
209
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"));
213
214         $result = $this->_makeRESTCall('get_modified_relationships',
215             array(
216                 'session' => $session,
217                 'module_name' => 'Users',
218                 'related_module' => 'Contacts',
219                 'from_date' => $one_hour_ago,
220                 'to_date' => $one_hour_later,
221                 'offset' => 0,
222                 'max_results' => 10,
223                 'deleted' => 0,
224                 'user_id' => $current_user->id,
225                 'select_fields' => $contactsSelectFields,
226                 'relationship_name' => 'contacts_users',
227                 'deletion_date' => '',
228             )
229         );
230
231         $this->assertNotEmpty($result['entry_list']);
232         $this->assertEquals(1, $result['result_count']);
233         $this->assertEquals(1, $result['next_offset']);
234
235
236         $result = $this->_makeRESTCall('get_modified_relationships',
237             array(
238                 'session' => $session,
239                 'module_name' => 'Users',
240                 'related_module' => 'Meetings',
241                 'from_date' => $one_hour_ago,
242                 'to_date' => $one_hour_later,
243                 'offset' => 0,
244                 'max_results' => 10,
245                 'deleted' => 0,
246                 'user_id' => $current_user->id,
247                 'select_fields' => $callsAndMeetingsFields,
248                 'relationship_name' => 'meetings_users',
249                 'deletion_date' => '',
250             )
251         );
252
253         $this->assertNotEmpty($result['entry_list']);
254         $this->assertEquals(2, $result['result_count']);
255         $this->assertEquals(2, $result['next_offset']);
256
257         $result = $this->_makeRESTCall('get_modified_relationships',
258             array(
259                 'session' => $session,
260                 'module_name' => 'Users',
261                 'related_module' => 'Meetings',
262                 'from_date' => $one_hour_ago,
263                 'to_date' => $one_hour_later,
264                 'offset' => 0,
265                 'max_results' => 10,
266                 'deleted' => 1,
267                 'user_id' => $current_user->id,
268                 'select_fields' => $callsAndMeetingsFields,
269                 'relationship_name' => 'meetings_users',
270                 'deletion_date' => $one_hour_ago,
271             )
272         );
273
274         $this->assertNotEmpty($result['entry_list']);
275         $this->assertEquals(1, $result['result_count']);
276         $this->assertEquals(1, $result['next_offset']);
277
278     }
279
280 }