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 ********************************************************************************/
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 static protected $_contactId = '';
46 static protected $_opportunities = array();
48 public static function setUpBeforeClass()
50 parent::setUpBeforeClass();
51 $contact = SugarTestContactUtilities::createContact();
52 self::$_contactId = $contact->id;
55 public function setUp()
57 $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php';
62 public function tearDown() {
63 $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
64 $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
68 public static function tearDownAfterClass()
70 if(!empty(self::$_opportunities)) {
71 $GLOBALS['db']->query('DELETE FROM opportunities WHERE id IN (\'' . implode("', '", self::$_opportunities) . '\')');
73 parent::tearDownAfterClass();
77 * Ensure we can create a session on the server.
80 public function testCanLogin(){
81 $result = $this->_login();
82 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
83 'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
86 public function testSetEntryForContact()
88 $result = $this->_setEntryForContact();
89 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
90 'Can not create new contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
93 public function testGetEntryForContact() {
94 $setresult = $this->_setEntryForContact();
95 $result = $this->_getEntryForContact($setresult['id']);
96 if (empty($this->_soapClient->faultcode)) {
97 if (($result['entry_list'][0]['name_value_list'][2]['value'] == 1) &&
98 ($result['entry_list'][0]['name_value_list'][3]['value'] == "Cold Call")) {
100 $this->assertEquals($result['entry_list'][0]['name_value_list'][2]['value'],1,"testGetEntryForContact method - Get Entry For contact is not same as Set Entry");
103 $this->fail('Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
110 public function testGetEntryForContactNoSelectFields(){
111 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>self::$_contactId,'select_fields'=>array(), 'link_name_to_fields_array' => array()));
112 $this->assertTrue(!empty($result['entry_list'][0]['name_value_list']), "testGetEntryForContactNoSelectFields returned no field data");
116 public function testSetEntriesForAccount() {
117 $result = $this->_setEntriesForAccount();
118 $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
119 'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
122 public function testSetEntryForOpportunity() {
123 $result = $this->_setEntryForOpportunity();
124 $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
125 'Can not create new account using testSetEntryForOpportunity. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
128 public function testSetRelationshipForOpportunity() {
129 $setresult = $this->_setEntryForOpportunity();
130 $result = $this->_setRelationshipForOpportunity($setresult['id']);
131 $this->assertTrue(($result['created'] > 0), 'testSetRelationshipForOpportunity method - Relationship for opportunity to Contact could not be created');
136 public function testGetRelationshipForOpportunity()
138 $setresult = $this->_setEntryForOpportunity();
139 $this->_setRelationshipForOpportunity($setresult['id']);
140 $result = $this->_getRelationshipForOpportunity($setresult['id']);
142 $result['entry_list'][0]['id'],
144 "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
148 public function testSearchByModule() {
149 $result = $this->_searchByModule();
150 $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");
153 /**********************************
154 * HELPER PUBLIC FUNCTIONS
155 **********************************/
157 public function _setEntryForContact() {
159 $current_date = $timedate->nowDb();
161 $first_name = 'SugarContactFirst' . $time;
162 $last_name = 'SugarContactLast';
163 $email1 = 'contact@sugar.com';
164 $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"))));
165 SugarTestContactUtilities::setCreatedContact(array($result['id']));
169 public function _getEntryForContact($id)
171 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Contacts','id'=>$id,
172 'select_fields'=>array('last_name', 'first_name', 'do_not_call', 'lead_source', 'email1'),
173 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))));
177 public function _setEntriesForAccount()
181 $current_date = $timedate->nowDb();
183 $name = 'SugarAccount' . $time;
184 $email1 = 'account@'. $time. 'sugar.com';
185 $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")))));
186 $soap_version_test_accountId = $result['ids'][0];
187 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
191 public function _setEntryForOpportunity() {
193 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
195 $name = 'SugarOpportunity' . $time;
196 $account = SugarTestAccountUtilities::createAccount();
197 $sales_stage = 'Prospecting';
200 $result = $this->_soapClient->call('set_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities',
201 'name_value_lists'=>array(array('name'=>'name' , 'value'=>"$name"), array('name'=>'amount' , 'value'=>"$amount"),
202 array('name'=>'probability' , 'value'=>"$probability"), array('name'=>'sales_stage' , 'value'=>"$sales_stage"),
203 array('name'=>'account_id' , 'value'=>$account->id))));
204 self::$_opportunities[] = $result['id'];
208 public function _getEntryForOpportunity($id) {
209 $result = $this->_soapClient->call('get_entry',array('session'=>$this->_sessionId,'module_name'=>'Opportunities','id'=>$id,'select_fields'=>array('name', 'amount'), 'link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name')))));
213 public function _setRelationshipForOpportunity($id) {
214 $result = $this->_soapClient->call('set_relationship',array('session'=>$this->_sessionId,'module_name' => 'Opportunities',
215 'module_id' => $id, 'link_field_name' => 'contacts',
216 'related_ids' =>array(self::$_contactId), 'name_value_list' => array(array('name' => 'contact_role', 'value' => 'testrole')), 'delete'=>0));
220 public function _getRelationshipForOpportunity($id)
222 $result = $this->_soapClient->call('get_relationships',
224 'session' => $this->_sessionId,
225 'module_name' => 'Opportunities',
227 'link_field_name' => 'contacts',
228 'related_module_query' => '',
229 'related_fields' => array('id'),
230 'related_module_link_name_to_fields_array' => array(array('name' => 'contacts', 'value' => array('id', 'first_name', 'last_name'))),
237 public function _searchByModule() {
238 $result = $this->_soapClient->call('search_by_module',
240 'session' => $this->_sessionId,
241 'search_string' => 'Sugar',
242 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
244 'max_results' => '10')