]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI2Test.php
Release 6.2.0
[Github/sugarcrm.git] / tests / service / SOAPAPI2Test.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 require_once 'tests/service/SOAPTestCase.php';
38 require_once('include/TimeDate.php');
39 /**
40  * This class is meant to test everything SOAP
41  *
42  */
43 class SOAPAPI2Test extends SOAPTestCase
44 {
45     public $_contactId = '';
46
47     /**
48      * Create test user
49      *
50      */
51         public function setUp()
52     {
53         $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php';
54                 parent::setUp();
55     }
56
57     /**
58      * Remove anything that was used during this test
59      *
60      */
61     public function tearDown()
62     {
63                 parent::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);
68     }
69
70         /**
71          * Ensure we can create a session on the server.
72          *
73          */
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);
78     }
79
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);
86     } // fn
87
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")) {
93
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");
95                 } // else
96         } else {
97                 $this->assertTrue(empty($this->_soapClient->faultcode), 'Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
98         }
99     } // fn
100
101     /**
102      * @ticket 38986
103      */
104     public function testGetEntryForContactNoSelectFields(){
105         global $soap_version_test_contactId;
106                 $this->_login();
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");
109
110     }
111
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);
116     } // fn
117
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);
122     } // fn
123
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');
127
128     } // fn
129
130
131     public function testGetRelationshipForOpportunity()
132     {
133         global $soap_version_test_contactId;
134         $result = $this->_getRelationshipForOpportunity();
135         $this->assertEquals(
136             $result['entry_list'][0]['id'],
137             $soap_version_test_contactId,
138             "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
139             );
140     } // fn
141
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");
145     } // fn
146
147     /**********************************
148      * HELPER PUBLIC FUNCTIONS
149      **********************************/
150
151     /**
152      * Attempt to login to the soap server
153      *
154      * @return $set_entry_result - this should contain an id and error.  The id corresponds
155      * to the session_id.
156      */
157     public function _login(){
158                 global $current_user;
159         $result = $this->_soapClient->call('login',
160             array('user_auth' =>
161                 array('user_name' => $current_user->user_name,
162                     'password' => $current_user->user_hash,
163                     'version' => '.01'),
164                 'application_name' => 'SoapTest',
165                 'name_value_list'=>array())
166             );
167         $this->_sessionId = $result['id'];
168                 return $result;
169     }
170
171     public function _setEntryForContact() {
172                 $this->_login();
173                 global $timedate;
174                 $current_date = $timedate->nowDb();
175         $time = mt_rand();
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));
181                 return $result;
182     } // fn
183
184     public function _getEntryForContact() {
185         global $soap_version_test_contactId;
186                 $this->_login();
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);
188                 return $result;
189     }
190
191     public function _setEntriesForAccount() {
192         global $soap_version_test_accountId;
193                 $this->_login();
194                 global $timedate;
195                 $current_date = $timedate->nowDb();
196         $time = mt_rand();
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));
203                 return $result;
204     } // fn
205
206     public function _setEntryForOpportunity() {
207         global $soap_version_test_accountId, $soap_version_test_opportunityId;
208                 $this->_login();
209                 global $timedate;
210                 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
211         $time = mt_rand();
212         $name = 'SugarOpportunity' . $time;
213         $account_id = $soap_version_test_accountId;
214         $sales_stage = 'Prospecting';
215         $probability = 10;
216         $amount = 1000;
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'];
220                 return $result;
221     } // fn
222
223   public function _getEntryForOpportunity() {
224         global $soap_version_test_opportunityId;
225                 $this->_login();
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);
227                 return $result;
228     }
229
230     public function _setRelationshipForOpportunity() {
231
232         global $soap_version_test_contactId, $soap_version_test_opportunityId;
233                 $this->_login();
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));
235                 return $result;
236     } // fn
237
238     public function _getRelationshipForOpportunity() {
239         global $soap_version_test_opportunityId;
240                 $this->_login();
241                 $result = $this->_soapClient->call('get_relationships',
242                                 array(
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'))),
250                 'deleted'=>0,
251                                 )
252                         );
253                 return $result;
254     } // fn
255
256     public function _searchByModule() {
257                 $this->_login();
258                 $result = $this->_soapClient->call('search_by_module',
259                                 array(
260                 'session' => $this->_sessionId,
261                 'search_string' => 'Sugar',
262                                 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
263                 'offset' => '0',
264                 'max_results' => '10')
265             );
266
267                 return $result;
268     } // fn
269 }