]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPI4_1Test.php
Release 6.4.2
[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-2012 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;
56         $beanList = array();
57                 $beanFiles = array();
58                 require('include/modules.php');
59
60         $this->_soapURL = $GLOBALS['sugar_config']['site_url'] . '/service/v4_1/soap.php';
61         parent::setUp();
62         $this->_login();
63         global $current_user;
64         $current_user = SugarTestUserUtilities::createAnonymousUser();
65         $this->another_user = SugarTestUserUtilities::createAnonymousUser();
66
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();
72
73         $this->contact1->user_sync->add($current_user);
74         $this->contact1->sync_contact = 1;
75         $this->contact1->save();
76
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();
82
83         $this->contact2->user_sync->add($this->another_user);
84         $this->contact2->sync_contact = 1;
85         $this->contact2->save();
86
87         $this->call1 = SugarTestCallUtilities::createCall();
88         $this->call1->name = 'RESTAPI4_1Test1';
89         $this->call1->load_relationship('users');
90         $this->call1->users->add($current_user);
91         $this->call1->save();
92
93         $this->call2 = SugarTestCallUtilities::createCall();
94         $this->call2->name = 'RESTAPI4_1Test2';
95         $this->call2->load_relationship('users');
96         $this->call2->users->add($current_user);
97         $this->call2->save();
98
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();
104
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();
110
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();
116
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();
125     }
126
127     public function tearDown()
128         {
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']);
140         }
141
142     protected function _makeRESTCall($method,$parameters)
143     {
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
163         curl_close($curl);
164
165         $this->_lastRawResponse = $response;
166
167         // Convert the result from JSON format to a PHP array
168         return json_decode($response,true);
169     }
170
171     protected function _returnLastRawResponse()
172     {
173         return "Error in web services call. Response was: {$this->_lastRawResponse}";
174     }
175
176     protected function _login()
177     {
178         $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
179         global $current_user;
180         return $this->_makeRESTCall('login',
181             array(
182                 'user_auth' =>
183                     array(
184                         'user_name' => $current_user->user_name,
185                         'password' => $current_user->user_hash,
186                         'version' => '.01',
187                         ),
188                 'application_name' => 'mobile',
189                 'name_value_list' => array(),
190                 )
191             );
192     }
193
194
195     /**
196      *
197      */
198     public function testGetModifiedRelationships()
199     {
200         $result = $this->_login();
201         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
202         $session = $result['id'];
203
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');
206
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"));
210
211         $result = $this->_makeRESTCall('get_modified_relationships',
212             array(
213                 'session' => $session,
214                 'module_name' => 'Users',
215                 'related_module' => 'Contacts',
216                 'from_date' => $one_hour_ago,
217                 'to_date' => $one_hour_later,
218                 'offset' => 0,
219                 'max_results' => 10,
220                 'deleted' => 0,
221                 'user_id' => $current_user->id,
222                 'select_fields' => $contactsSelectFields,
223                 'relationship_name' => 'contacts_users',
224                 'deletion_date' => '',
225             )
226         );
227
228         $this->assertNotEmpty($result['entry_list']);
229         $this->assertEquals(1, $result['result_count']);
230         $this->assertEquals(1, $result['next_offset']);
231
232
233         $result = $this->_makeRESTCall('get_modified_relationships',
234             array(
235                 'session' => $session,
236                 'module_name' => 'Users',
237                 'related_module' => 'Meetings',
238                 'from_date' => $one_hour_ago,
239                 'to_date' => $one_hour_later,
240                 'offset' => 0,
241                 'max_results' => 10,
242                 'deleted' => 0,
243                 'user_id' => $current_user->id,
244                 'select_fields' => $callsAndMeetingsFields,
245                 'relationship_name' => 'meetings_users',
246                 'deletion_date' => '',
247             )
248         );
249
250         $this->assertNotEmpty($result['entry_list']);
251         $this->assertEquals(2, $result['result_count']);
252         $this->assertEquals(2, $result['next_offset']);
253
254         $result = $this->_makeRESTCall('get_modified_relationships',
255             array(
256                 'session' => $session,
257                 'module_name' => 'Users',
258                 'related_module' => 'Meetings',
259                 'from_date' => $one_hour_ago,
260                 'to_date' => $one_hour_later,
261                 'offset' => 0,
262                 'max_results' => 10,
263                 'deleted' => 1,
264                 'user_id' => $current_user->id,
265                 'select_fields' => $callsAndMeetingsFields,
266                 'relationship_name' => 'meetings_users',
267                 'deletion_date' => $one_hour_ago,
268             )
269         );
270
271         $this->assertNotEmpty($result['entry_list']);
272         $this->assertEquals(1, $result['result_count']);
273         $this->assertEquals(1, $result['next_offset']);
274
275     }
276
277 }