2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 ********************************************************************************/
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';
43 * This class is meant to test everything SOAP
46 class SOAPAPI3Test extends SOAPTestCase
48 public $_contactId = '';
49 private static $helperObject;
55 public function setUp()
57 $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v3/soap.php';
59 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
60 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
62 self::$helperObject = new APIv3Helper();
66 * Remove anything that was used during this test
69 public function 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);
78 * Ensure we can create a session on the server.
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);
87 public function testSearchByModule()
91 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
93 $searchModules = array('Accounts','Contacts','Opportunities');
94 $searchString = "UNIT TEST";
98 $results = $this->_soapClient->call('search_by_module',
100 'session' => $this->_sessionId,
101 'search' => $searchString,
102 'modules' => $searchModules,
104 'max' => $maxResults,
105 'user' => $this->_user->id)
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%' ");
118 public function testSearchByModuleWithReturnFields()
122 $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
124 $returnFields = array('name','id','deleted');
125 $searchModules = array('Accounts','Contacts','Opportunities');
126 $searchString = "UNIT TEST";
130 $results = $this->_soapClient->call('search_by_module',
132 'session' => $this->_sessionId,
133 'search' => $searchString,
134 'modules' => $searchModules,
136 'max' => $maxResults,
137 'user' => $this->_user->id,
138 'fields' => $returnFields)
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']));
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%' ");
152 public function testGetVardefsMD5()
154 $GLOBALS['reload_vardefs'] = TRUE;
155 //Test a regular module
156 $result = $this->_getVardefsMD5('Accounts');
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.");
164 $result = $this->_getVardefsMD5('BadModule');
165 $this->assertTrue($result['faultstring'] == 'Module Does Not Exist');
166 unset($GLOBALS['reload_vardefs']);
169 public function testGetUpcomingActivities()
172 $expected = $this->_createUpcomingActivities(); //Seed the data.
173 $results = $this->_soapClient->call('get_upcoming_activities',array('session'=>$this->_sessionId));
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);
178 $this->_removeUpcomingActivities();
182 * Get Module Layout functions not exposed to soap service, make sure they are not available.
185 public function testGetModuleLayoutMD5()
187 $result = $this->_getModuleLayoutMD5();
188 $this->assertContains('Client',$result['faultcode']);
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);
197 /**********************************
198 * HELPER PUBLIC FUNCTIONS
199 **********************************/
200 private function _removeUpcomingActivities()
202 $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
203 $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
206 private function _createUpcomingActivities()
208 $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
209 $GLOBALS['current_user']->setPreference('timef','H:i') ;
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
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;
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;
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;
244 return array($callID, $taskID);
247 public function _getVardefsMD5($module)
250 $result = $this->_soapClient->call('get_module_fields_md5',array('session'=>$this->_sessionId,'module'=> $module ));
254 public function _getModuleLayoutMD5()
257 $result = $this->_soapClient->call('get_module_layout_md5',
258 array('session'=>$this->_sessionId,'module_names'=> array('Accounts'),'types' => array('default'),'views' => array('list')));
262 public function _setEntryForContact() {
265 $current_date = $timedate->nowDb();
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));
275 public function _getEntryForContact() {
276 global $soap_version_test_contactId;
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);
283 public function _setEntriesForAccount() {
284 global $soap_version_test_accountId;
287 $current_date = $timedate->nowDb();
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));
298 public function _setEntryForOpportunity() {
299 global $soap_version_test_accountId, $soap_version_test_opportunityId;
302 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
304 $name = 'SugarOpportunity' . $time;
305 $account_id = $soap_version_test_accountId;
306 $sales_stage = 'Prospecting';
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'];
315 public function _setRelationshipForOpportunity() {
316 global $soap_version_test_contactId, $soap_version_test_opportunityId;
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'))));
322 public function _getRelationshipForOpportunity() {
323 global $soap_version_test_opportunityId;
325 $result = $this->_soapClient->call('get_relationships',
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'))))
338 public function _searchByModule() {
340 $result = $this->_soapClient->call('search_by_module',
342 'session' => $this->_sessionId,
343 'search_string' => 'Sugar',
344 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
346 'max_results' => '10')