]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI3Test.php
Release 6.2.0
[Github/sugarcrm.git] / tests / service / SOAPAPI3Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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 require_once('include/TimeDate.php');
39 require_once('service/v3/SugarWebServiceUtilv3.php');
40 require_once('tests/service/APIv3Helper.php');
41 require_once 'tests/service/SOAPTestCase.php';
42 /**
43  * This class is meant to test everything SOAP
44  *
45  */
46 class SOAPAPI3Test extends SOAPTestCase
47 {
48     public $_contactId = '';
49     private static $helperObject;
50
51     /**
52      * Create test user
53      *
54      */
55         public function setUp()
56     {
57         $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v3/soap.php';
58         parent::setUp();
59         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
60         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
61
62         self::$helperObject = new APIv3Helper();
63     }
64
65     /**
66      * Remove anything that was used during this test
67      *
68      */
69     public function tearDown() {
70                 parent::tearDown();
71         global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
72         unset($soap_version_test_accountId);
73         unset($soap_version_test_opportunityId);
74         unset($soap_version_test_contactId);
75     }
76
77         /**
78          * Ensure we can create a session on the server.
79          *
80          */
81     public function testCanLogin(){
82                 $result = $this->_login();
83         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
84             'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
85     }
86
87     public function testSearchByModule()
88     {
89         $this->_login();
90
91         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
92
93         $searchModules = array('Accounts','Contacts','Opportunities');
94         $searchString = "UNIT TEST";
95         $offSet = 0;
96         $maxResults = 10;
97
98         $results = $this->_soapClient->call('search_by_module',
99                         array(
100                             'session' => $this->_sessionId,
101                             'search'  => $searchString,
102                             'modules' => $searchModules,
103                             'offset'  => $offSet,
104                             'max'     => $maxResults,
105                             'user'    => $this->_user->id)
106                         );
107
108         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
109         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
110         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
111         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
112         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
113         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
114         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
115         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
116     }
117
118     public function testSearchByModuleWithReturnFields()
119     {
120         $this->_login();
121
122         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
123
124         $returnFields = array('name','id','deleted');
125         $searchModules = array('Accounts','Contacts','Opportunities');
126         $searchString = "UNIT TEST";
127         $offSet = 0;
128         $maxResults = 10;
129
130         $results = $this->_soapClient->call('search_by_module',
131                         array(
132                             'session' => $this->_sessionId,
133                             'search'  => $searchString,
134                             'modules' => $searchModules,
135                             'offset'  => $offSet,
136                             'max'     => $maxResults,
137                             'user'    => $this->_user->id,
138                             'fields'  => $returnFields)
139                         );
140
141         $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
142         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
143         $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
144         $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
145         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
146
147         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
148         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
149         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
150     }
151
152     public function testGetVardefsMD5()
153     {
154         $GLOBALS['reload_vardefs'] = TRUE;
155         //Test a regular module
156         $result = $this->_getVardefsMD5('Accounts');
157         $a = new Account();
158         $soapHelper = new SugarWebServiceUtilv3();
159         $actualVardef = $soapHelper->get_return_module_fields($a,'Accounts','');
160         $actualMD5 = md5(serialize($actualVardef));
161         $this->assertEquals($actualMD5, $result[0], "Unable to retrieve vardef md5.");
162
163         //Test a fake module
164         $result = $this->_getVardefsMD5('BadModule');
165         $this->assertTrue($result['faultstring'] == 'Module Does Not Exist');
166         unset($GLOBALS['reload_vardefs']);
167     }
168
169     public function testGetUpcomingActivities()
170     {
171          $this->_login();
172          $expected = $this->_createUpcomingActivities(); //Seed the data.
173          $results = $this->_soapClient->call('get_upcoming_activities',array('session'=>$this->_sessionId));
174
175          $this->assertEquals($expected[0] ,$results[0]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
176          $this->assertEquals($expected[1] ,$results[1]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
177
178          $this->_removeUpcomingActivities();
179     }
180
181     /**
182      * Get Module Layout functions not exposed to soap service, make sure they are not available.
183      *
184      */
185     public function testGetModuleLayoutMD5()
186     {
187         $result = $this->_getModuleLayoutMD5();
188         $this->assertContains('Client',$result['faultcode']);
189     }
190
191     public function testSetEntriesForAccount() {
192         $result = $this->_setEntriesForAccount();
193         $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
194             'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
195     } // fn
196
197     /**********************************
198      * HELPER PUBLIC FUNCTIONS
199      **********************************/
200     private function _removeUpcomingActivities()
201     {
202         $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
203         $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
204     }
205
206     private function _createUpcomingActivities()
207     {
208         $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
209         $GLOBALS['current_user']->setPreference('timef','H:i') ;
210
211         $date1 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
212         $date2 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 4) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
213
214         $callID = uniqid();
215         $c = new Call();
216         $c->id = $callID;
217         $c->new_with_id = TRUE;
218         $c->status = 'Not Planned';
219         $c->date_start = $date1;
220         $c->name = "UNIT TEST";
221         $c->assigned_user_id = $this->_user->id;
222         $c->save(FALSE);
223
224         $callID = uniqid();
225         $c = new Call();
226         $c->id = $callID;
227         $c->new_with_id = TRUE;
228         $c->status = 'Planned';
229         $c->date_start = $date1;
230         $c->name = "UNIT TEST";
231         $c->assigned_user_id = $this->_user->id;
232         $c->save(FALSE);
233
234         $taskID = uniqid();
235         $t = new Task();
236         $t->id = $taskID;
237         $t->new_with_id = TRUE;
238         $t->status = 'Not Started';
239         $t->date_due = $date2;
240         $t->name = "UNIT TEST";
241         $t->assigned_user_id = $this->_user->id;
242         $t->save(FALSE);
243
244         return array($callID, $taskID);
245     }
246
247     public function _getVardefsMD5($module)
248     {
249         $this->_login();
250                 $result = $this->_soapClient->call('get_module_fields_md5',array('session'=>$this->_sessionId,'module'=> $module ));
251                 return $result;
252     }
253
254     public function _getModuleLayoutMD5()
255     {
256         $this->_login();
257                 $result = $this->_soapClient->call('get_module_layout_md5',
258                               array('session'=>$this->_sessionId,'module_names'=> array('Accounts'),'types' => array('default'),'views' => array('list')));
259                 return $result;
260     }
261
262     public function _setEntryForContact() {
263                 $this->_login();
264                 global $timedate;
265                 $current_date = $timedate->nowDb();
266         $time = mt_rand();
267         $first_name = 'SugarContactFirst' . $time;
268         $last_name = 'SugarContactLast';
269         $email1 = 'contact@sugar.com';
270                 $result = $this->_soapClient->call('set_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts', 'name_value_list'=>array(array('name'=>'last_name' , 'value'=>"$last_name"), array('name'=>'first_name' , 'value'=>"$first_name"), array('name'=>'do_not_call' , 'value'=>"1"), array('name'=>'birthdate' , 'value'=>"$current_date"), array('name'=>'lead_source' , 'value'=>"Cold Call"), array('name'=>'email1' , 'value'=>"$email1"))));
271                 SugarTestContactUtilities::setCreatedContact(array($this->_contactId));
272                 return $result;
273     } // fn
274
275     public function _getEntryForContact() {
276         global $soap_version_test_contactId;
277                 $this->_login();
278                 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>$soap_version_test_contactId,'select_fields'=>array('last_name', 'first_name', 'do_not_call', 'lead_source'), 'link_name_to_fields_array' => array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))));
279                 $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_contactId);
280                 return $result;
281     }
282
283     public function _setEntriesForAccount() {
284         global $soap_version_test_accountId;
285                 $this->_login();
286                 global $timedate;
287                 $current_date = $timedate->nowDb();
288         $time = mt_rand();
289         $name = 'SugarAccount' . $time;
290         $email1 = 'account@'. $time. 'sugar.com';
291                 $result = $this->_soapClient->call('set_entries',array('session'=>$this->_sessionId,'module_name'=>'Accounts', 'name_value_lists'=>array(array(array('name'=>'name' , 'value'=>"$name"), array('name'=>'email1' , 'value'=>"$email1")))));
292                 $soap_version_test_accountId = $result['ids'][0];
293                 $GLOBALS['log']->fatal("_setEntriesForAccount id = " . $soap_version_test_accountId);
294                 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
295                 return $result;
296     } // fn
297
298     public function _setEntryForOpportunity() {
299         global $soap_version_test_accountId, $soap_version_test_opportunityId;
300                 $this->_login();
301                 global $timedate;
302                 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
303         $time = mt_rand();
304         $name = 'SugarOpportunity' . $time;
305         $account_id = $soap_version_test_accountId;
306         $sales_stage = 'Prospecting';
307         $probability = 10;
308         $amount = 1000;
309                 $GLOBALS['log']->fatal("_setEntryForOpportunity id = " . $soap_version_test_accountId);
310                 $result = $this->_soapClient->call('set_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities', 'name_value_lists'=>array(array('name'=>'name' , 'value'=>"$name"), array('name'=>'amount' , 'value'=>"$amount"), array('name'=>'probability' , 'value'=>"$probability"), array('name'=>'sales_stage' , 'value'=>"$sales_stage"), array('name'=>'account_id' , 'value'=>"$account_id"))));
311                 $soap_version_test_opportunityId = $result['id'];
312                 return $result;
313     } // fn
314
315     public function _setRelationshipForOpportunity() {
316         global $soap_version_test_contactId, $soap_version_test_opportunityId;
317                 $this->_login();
318                 $result = $this->_soapClient->call('set_relationship',array('session'=>$this->_sessionId,'module_name' => 'Opportunities','module_id' => "$soap_version_test_opportunityId", 'link_field_name' => 'contacts','related_ids' =>array("$soap_version_test_contactId"), 'name_value_list' => array(array('name' => 'contact_role', 'value' => 'testrole'))));
319                 return $result;
320     } // fn
321
322     public function _getRelationshipForOpportunity() {
323         global $soap_version_test_opportunityId;
324                 $this->_login();
325                 $result = $this->_soapClient->call('get_relationships',
326                                 array(
327                 'session' => $this->_sessionId,
328                 'module_name' => 'Opportunities',
329                 'module_id' => "$soap_version_test_opportunityId",
330                 'link_field_name' => 'contacts',
331                 'related_module_query' => '',
332                 'related_fields' => array('id'),
333                 'related_module_link_name_to_fields_array' => array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))))
334             );
335                 return $result;
336     } // fn
337
338     public function _searchByModule() {
339                 $this->_login();
340                 $result = $this->_soapClient->call('search_by_module',
341                                 array(
342                 'session' => $this->_sessionId,
343                 'search_string' => 'Sugar',
344                                 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
345                 'offset' => '0',
346                 'max_results' => '10')
347             );
348
349                 return $result;
350     } // fn
351
352 }