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 ********************************************************************************/
37 require_once 'tests/service/SOAPTestCase.php';
38 require_once('include/TimeDate.php');
40 * This class is meant to test everything SOAP
43 class SOAPAPI2Test extends SOAPTestCase
45 public $_contactId = '';
51 public function setUp()
53 $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php';
58 * Remove anything that was used during this test
61 public function tearDown()
64 global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
65 unset($soap_version_test_accountId);
66 unset($soap_version_test_opportunityId);
67 unset($soap_version_test_contactId);
71 * Ensure we can create a session on the server.
74 public function testCanLogin(){
75 $result = $this->_login();
76 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
77 'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
80 public function testSetEntryForContact() {
81 global $soap_version_test_contactId;
82 $result = $this->_setEntryForContact();
83 $soap_version_test_contactId = $result['id'];
84 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
85 'Can not create new contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
88 public function testGetEntryForContact() {
89 $result = $this->_getEntryForContact();
90 if (empty($this->_soapClient->faultcode)) {
91 if (($result['entry_list'][0]['name_value_list'][2]['value'] == 1) &&
92 ($result['entry_list'][0]['name_value_list'][3]['value'] == "Cold Call")) {
94 $this->assertEquals($result['entry_list'][0]['name_value_list'][2]['value'],1,"testGetEntryForContact method - Get Entry For contact is not same as Set Entry");
97 $this->assertTrue(empty($this->_soapClient->faultcode), 'Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
104 public function testGetEntryForContactNoSelectFields(){
105 global $soap_version_test_contactId;
107 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>$soap_version_test_contactId,'select_fields'=>array(), 'link_name_to_fields_array' => array()));
108 $this->assertTrue(!empty($result['entry_list'][0]['name_value_list']), "testGetEntryForContactNoSelectFields returned no field data");
112 public function testSetEntriesForAccount() {
113 $result = $this->_setEntriesForAccount();
114 $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
115 'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
118 public function testSetEntryForOpportunity() {
119 $result = $this->_setEntryForOpportunity();
120 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
121 'Can not create new account using testSetEntryForOpportunity. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
124 public function testSetRelationshipForOpportunity() {
125 $result = $this->_setRelationshipForOpportunity();
126 $this->assertTrue(($result['created'] > 0), 'testSetRelationshipForOpportunity method - Relationship for opportunity to Contact could not be created');
131 public function testGetRelationshipForOpportunity()
133 global $soap_version_test_contactId;
134 $result = $this->_getRelationshipForOpportunity();
136 $result['entry_list'][0]['id'],
137 $soap_version_test_contactId,
138 "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
142 public function testSearchByModule() {
143 $result = $this->_searchByModule();
144 $this->assertTrue(($result['entry_list'][0]['records'] > 0 && $result['entry_list'][1]['records'] && $result['entry_list'][2]['records']), "testSearchByModule - could not retrieve any data by search");
147 /**********************************
148 * HELPER PUBLIC FUNCTIONS
149 **********************************/
152 * Attempt to login to the soap server
154 * @return $set_entry_result - this should contain an id and error. The id corresponds
157 public function _login(){
158 global $current_user;
159 $result = $this->_soapClient->call('login',
161 array('user_name' => $current_user->user_name,
162 'password' => $current_user->user_hash,
164 'application_name' => 'SoapTest',
165 'name_value_list'=>array())
167 $this->_sessionId = $result['id'];
171 public function _setEntryForContact() {
174 $current_date = $timedate->nowDb();
176 $first_name = 'SugarContactFirst' . $time;
177 $last_name = 'SugarContactLast';
178 $email1 = 'contact@sugar.com';
179 $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"))));
180 SugarTestContactUtilities::setCreatedContact(array($this->_contactId));
184 public function _getEntryForContact() {
185 global $soap_version_test_contactId;
187 $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', 'email1'), 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))))); $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_contactId);
191 public function _setEntriesForAccount() {
192 global $soap_version_test_accountId;
195 $current_date = $timedate->nowDb();
197 $name = 'SugarAccount' . $time;
198 $email1 = 'account@'. $time. 'sugar.com';
199 $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")))));
200 $soap_version_test_accountId = $result['ids'][0];
201 $GLOBALS['log']->fatal("_setEntriesForAccount id = " . $soap_version_test_accountId);
202 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
206 public function _setEntryForOpportunity() {
207 global $soap_version_test_accountId, $soap_version_test_opportunityId;
210 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
212 $name = 'SugarOpportunity' . $time;
213 $account_id = $soap_version_test_accountId;
214 $sales_stage = 'Prospecting';
217 $GLOBALS['log']->fatal("_setEntryForOpportunity id = " . $soap_version_test_accountId);
218 $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"))));
219 $soap_version_test_opportunityId = $result['id'];
223 public function _getEntryForOpportunity() {
224 global $soap_version_test_opportunityId;
226 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities','id'=>$soap_version_test_opportunityId,'select_fields'=>array('name', 'amount'), 'link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name'))))); $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_opportunityId);
230 public function _setRelationshipForOpportunity() {
232 global $soap_version_test_contactId, $soap_version_test_opportunityId;
234 $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')), 'delete'=>0));
238 public function _getRelationshipForOpportunity() {
239 global $soap_version_test_opportunityId;
241 $result = $this->_soapClient->call('get_relationships',
243 'session' => $this->_sessionId,
244 'module_name' => 'Opportunities',
245 'module_id' => "$soap_version_test_opportunityId",
246 'link_field_name' => 'contacts',
247 'related_module_query' => '',
248 'related_fields' => array('id'),
249 'related_module_link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name'))),
256 public function _searchByModule() {
258 $result = $this->_soapClient->call('search_by_module',
260 'session' => $this->_sessionId,
261 'search_string' => 'Sugar',
262 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
264 'max_results' => '10')