2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
40 require_once 'tests/service/SOAPTestCase.php';
42 * This class is meant to test everything SOAP
45 class SOAPAPI3Test extends SOAPTestCase
47 public $_contactId = '';
48 private static $helperObject;
54 public function setUp()
56 $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']);
61 self::$helperObject = new APIv3Helper();
62 $GLOBALS['db']->commit();
65 public function tearDown()
67 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
68 $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
69 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
70 unset($GLOBALS['reload_vardefs']);
75 * Ensure we can create a session on the server.
78 public function testCanLogin(){
79 $result = $this->_login();
80 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
81 'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
84 public function testSearchByModule()
86 $seedData = self::$helperObject->populateSeedDataForSearchTest($GLOBALS['current_user']->id);
88 $searchModules = array('Accounts','Contacts','Opportunities');
89 $searchString = "UNIT TEST";
93 $results = $this->_soapClient->call('search_by_module',
95 'session' => $this->_sessionId,
96 'search' => $searchString,
97 'modules' => $searchModules,
100 'user' => $GLOBALS['current_user']->id)
103 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
104 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
105 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
106 $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
107 $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
110 public function testSearchByModuleWithReturnFields()
112 $seedData = self::$helperObject->populateSeedDataForSearchTest($GLOBALS['current_user']->id);
114 $returnFields = array('name','id','deleted');
115 $searchModules = array('Accounts','Contacts','Opportunities');
116 $searchString = "UNIT TEST";
120 $results = $this->_soapClient->call('search_by_module',
122 'session' => $this->_sessionId,
123 'search' => $searchString,
124 'modules' => $searchModules,
126 'max' => $maxResults,
127 'user' => $GLOBALS['current_user']->id,
128 'fields' => $returnFields)
131 $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
132 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
133 $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
134 $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
135 $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
138 public function testGetVardefsMD5()
140 $GLOBALS['reload_vardefs'] = TRUE;
141 //Test a regular module
142 $result = $this->_getVardefsMD5('Currencies');
144 $soapHelper = new SugarWebServiceUtilv3();
145 $actualVardef = $soapHelper->get_return_module_fields($a,'Currencies','');
146 $actualMD5 = md5(serialize($actualVardef));
147 $this->assertEquals($actualMD5, $result[0], "Unable to retrieve vardef md5.");
150 $result = $this->_getVardefsMD5('BadModule');
151 $this->assertEquals('Module Does Not Exist', $result['faultstring']);
154 public function testGetUpcomingActivities()
156 $expected = $this->_createUpcomingActivities(); //Seed the data.
157 $results = $this->_soapClient->call('get_upcoming_activities',array('session'=>$this->_sessionId));
158 $this->_removeUpcomingActivities();
160 $this->assertEquals($expected[0] ,$results[0]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
161 $this->assertEquals($expected[1] ,$results[1]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
165 public function testSetEntriesForAccount()
167 $result = $this->_setEntriesForAccount();
168 $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
169 'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
174 * Get Module Layout functions not exposed to soap service, make sure they are not available.
177 public function testGetModuleLayoutMD5()
179 $result = $this->_getModuleLayoutMD5();
180 $this->assertContains('Client',$result['faultcode']);
184 /**********************************
185 * HELPER PUBLIC FUNCTIONS
186 **********************************/
187 private function _removeUpcomingActivities()
189 $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
190 $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
193 private function _createUpcomingActivities()
195 $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
196 $GLOBALS['current_user']->setPreference('timef','H:i') ;
198 $date1 = $timedate->asUser($timedate->getNow()->modify("+2 days"));
199 $date2 = $timedate->asUser($timedate->getNow()->modify("+4 days"));
204 $c->new_with_id = TRUE;
205 $c->status = 'Not Planned';
206 $c->date_start = $date1;
207 $c->name = "UNIT TEST";
208 $c->assigned_user_id = $GLOBALS['current_user']->id;
214 $c->new_with_id = TRUE;
215 $c->status = 'Planned';
216 $c->date_start = $date1;
217 $c->name = "UNIT TEST";
218 $c->assigned_user_id = $GLOBALS['current_user']->id;
224 $t->new_with_id = TRUE;
225 $t->status = 'Not Started';
226 $t->date_due = $date2;
227 $t->name = "UNIT TEST";
228 $t->assigned_user_id = $GLOBALS['current_user']->id;
230 $GLOBALS['db']->commit();
232 return array($callID, $taskID);
235 public function _getVardefsMD5($module)
237 $result = $this->_soapClient->call('get_module_fields_md5',array('session'=>$this->_sessionId,'module'=> $module ));
241 public function _getModuleLayoutMD5()
243 $result = $this->_soapClient->call('get_module_layout_md5',
244 array('session'=>$this->_sessionId,'module_names'=> array('Accounts'),'types' => array('default'),'views' => array('list')));
248 public function _setEntriesForAccount() {
250 $current_date = $timedate->nowDb();
252 $name = 'SugarAccount' . $time;
253 $email1 = 'account@'. $time. 'sugar.com';
254 $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")))));
255 $soap_version_test_accountId = $result['ids'][0];
256 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));