]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI2Test.php
Release 6.4.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     static protected $_contactId = '';
46     static protected $_opportunities = array();
47
48     public static function setUpBeforeClass()
49     {
50         parent::setUpBeforeClass();
51         $contact = SugarTestContactUtilities::createContact();
52         self::$_contactId = $contact->id;
53     }
54
55         public function setUp()
56     {
57         $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php';
58                 parent::setUp();
59                 $this->_login();
60     }
61
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%' ");
65         parent::tearDown();
66     }
67
68     public static function tearDownAfterClass()
69     {
70         if(!empty(self::$_opportunities)) {
71             $GLOBALS['db']->query('DELETE FROM opportunities WHERE id IN (\'' . implode("', '", self::$_opportunities) . '\')');
72         }
73         parent::tearDownAfterClass();
74     }
75
76     /**
77          * Ensure we can create a session on the server.
78          *
79          */
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);
84     }
85
86     public function testSetEntryForContact()
87     {
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);
91     } // fn
92
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")) {
99
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");
101                 } // else
102         } else {
103                 $this->fail('Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
104         }
105     } // fn
106
107     /**
108      * @ticket 38986
109      */
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");
113
114     }
115
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);
120     } // fn
121
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);
126     } // fn
127
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');
132
133     } // fn
134
135
136     public function testGetRelationshipForOpportunity()
137     {
138         $setresult = $this->_setEntryForOpportunity();
139         $this->_setRelationshipForOpportunity($setresult['id']);
140         $result = $this->_getRelationshipForOpportunity($setresult['id']);
141         $this->assertEquals(
142             $result['entry_list'][0]['id'],
143             self::$_contactId,
144             "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
145             );
146     } // fn
147
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");
151     } // fn
152
153     /**********************************
154      * HELPER PUBLIC FUNCTIONS
155      **********************************/
156
157     public function _setEntryForContact() {
158                 global $timedate;
159                 $current_date = $timedate->nowDb();
160         $time = mt_rand();
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']));
166                 return $result;
167     } // fn
168
169     public function _getEntryForContact($id)
170     {
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')))));
174                 return $result;
175     }
176
177     public function _setEntriesForAccount()
178     {
179                 $this->_login();
180                 global $timedate;
181                 $current_date = $timedate->nowDb();
182         $time = mt_rand();
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));
188                 return $result;
189     } // fn
190
191     public function _setEntryForOpportunity() {
192                 global $timedate;
193                 $date_closed = $timedate->getNow()->get("+1 week")->asDb();
194         $time = mt_rand();
195         $name = 'SugarOpportunity' . $time;
196         $account = SugarTestAccountUtilities::createAccount();
197         $sales_stage = 'Prospecting';
198         $probability = 10;
199         $amount = 1000;
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'];
205                 return $result;
206     } // fn
207
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')))));
210                 return $result;
211     }
212
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));
217                 return $result;
218     } // fn
219
220     public function _getRelationshipForOpportunity($id)
221     {
222                 $result = $this->_soapClient->call('get_relationships',
223                                 array(
224                 'session' => $this->_sessionId,
225                 'module_name' => 'Opportunities',
226                 'module_id' => $id,
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'))),
231                 'deleted'=>0,
232                                 )
233                         );
234                 return $result;
235     } // fn
236
237     public function _searchByModule() {
238                 $result = $this->_soapClient->call('search_by_module',
239                                 array(
240                 'session' => $this->_sessionId,
241                 'search_string' => 'Sugar',
242                                 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
243                 'offset' => '0',
244                 'max_results' => '10')
245             );
246
247                 return $result;
248     } // fn
249 }