]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI2Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / service / SOAPAPI2Test.php
1 <?php
2 require_once('include/nusoap/nusoap.php');
3 require_once('include/TimeDate.php');
4 /**
5  * This class is meant to test everything SOAP
6  *
7  */
8 class SOAPAPI2Test extends Sugar_PHPUnit_Framework_TestCase
9 {
10         public $_user = null;
11         public $_soapClient = null;
12         public $_session = null;
13         public $_sessionId = '';
14     public $_contactId = '';
15         
16     /**
17      * Create test user
18      *
19      */
20         public function setUp() 
21     {
22         $this->_soapClient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php',false,false,false,false,false,600,600);
23         $this->_setupTestUser();
24     }
25
26     /**
27      * Remove anything that was used during this test
28      *
29      */
30     public function tearDown() {
31         global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
32         $this->_tearDownTestUser();
33         $this->_user = null;
34         $this->_sessionId = '';
35         unset($soap_version_test_accountId);
36         unset($soap_version_test_opportunityId);
37         unset($soap_version_test_contactId);
38     }
39
40         /**
41          * Ensure we can create a session on the server.
42          *
43          */
44     public function testCanLogin(){
45                 $result = $this->_login();
46         $this->assertTrue(!empty($result['id']) && $result['id'] != -1, 
47             'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
48     }
49          
50     public function testSetEntryForContact() {
51         global $soap_version_test_contactId;
52         $result = $this->_setEntryForContact();
53                 $soap_version_test_contactId = $result['id'];
54         $this->assertTrue(!empty($result['id']) && $result['id'] != -1, 
55             'Can not create new contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
56     } // fn
57
58     public function testGetEntryForContact() {
59         $result = $this->_getEntryForContact();
60         if (empty($this->_soapClient->faultcode)) {
61                 if (($result['entry_list'][0]['name_value_list'][2]['value'] == 1) &&
62                         ($result['entry_list'][0]['name_value_list'][3]['value'] == "Cold Call") &&
63                         ($result['relationship_list'][0][0]['records'][0][1]['value'] == 'contact@sugar.com')) {
64                         
65                         $this->assertEquals($result['entry_list'][0]['name_value_list'][2]['value'],1,"testGetEntryForContact method - Get Entry For contact is not same as Set Entry");
66                 } // else
67         } else {
68                 $this->assertTrue(empty($this->_soapClient->faultcode), 'Can not retrieve newly created contact. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
69         }
70     } // fn
71     
72     /**
73      * @ticket 38986
74      */
75     public function testGetEntryForContactNoSelectFields(){
76         global $soap_version_test_contactId;
77                 $this->_login();
78                 $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()));
79                 $this->assertTrue(!empty($result['entry_list'][0]['name_value_list']), "testGetEntryForContactNoSelectFields returned no field data");
80         
81     }
82         
83     public function testSetEntriesForAccount() {
84         $result = $this->_setEntriesForAccount();
85         $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1, 
86             'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
87     } // fn
88
89     public function testSetEntryForOpportunity() {
90         $result = $this->_setEntryForOpportunity();
91         $this->assertTrue(!empty($result['id']) && $result['id'] != -1, 
92             'Can not create new account using testSetEntryForOpportunity. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
93     } // fn
94     
95     public function testSetRelationshipForOpportunity() {
96         $result = $this->_setRelationshipForOpportunity();
97         $this->assertTrue(($result['created'] > 0), 'testSetRelationshipForOpportunity method - Relationship for opportunity to Contact could not be created');
98         
99     } // fn
100     
101     
102     public function testGetRelationshipForOpportunity() 
103     {    
104         global $soap_version_test_contactId;
105         $result = $this->_getRelationshipForOpportunity();
106         $this->assertEquals(
107             $result['entry_list'][0]['id'],
108             $soap_version_test_contactId, 
109             "testGetRelationshipForOpportunity - Get Relationship of Opportunity to Contact failed"
110             );          
111     } // fn
112     
113     public function testSearchByModule() {
114         $result = $this->_searchByModule();
115         $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");       
116     } // fn
117     
118     /**********************************
119      * HELPER PUBLIC FUNCTIONS
120      **********************************/
121     
122     /**
123      * Attempt to login to the soap server
124      *
125      * @return $set_entry_result - this should contain an id and error.  The id corresponds
126      * to the session_id.
127      */
128     public function _login(){
129                 global $current_user;   
130         $result = $this->_soapClient->call('login',
131             array('user_auth' => 
132                 array('user_name' => $current_user->user_name,
133                     'password' => $current_user->user_hash, 
134                     'version' => '.01'), 
135                 'application_name' => 'SoapTest',
136                 'name_value_list'=>array())
137             );
138         $this->_sessionId = $result['id'];
139                 return $result;
140     }
141     
142     public function _setEntryForContact() {
143                 $this->_login();
144                 global $timedate;
145                 $current_date = $timedate->convert_to_gmt_datetime('now');
146         $time = mt_rand();
147         $first_name = 'SugarContactFirst' . $time;
148         $last_name = 'SugarContactLast';
149         $email1 = 'contact@sugar.com';
150                 $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"))));
151                 SugarTestContactUtilities::setCreatedContact(array($this->_contactId));
152                 return $result;
153     } // fn
154     
155     public function _getEntryForContact() {
156         global $soap_version_test_contactId;
157                 $this->_login();
158                 $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);
159                 return $result;
160     }
161         
162     public function _setEntriesForAccount() {
163         global $soap_version_test_accountId;
164                 $this->_login();
165                 global $timedate;
166                 $current_date = $timedate->convert_to_gmt_datetime('now');
167         $time = mt_rand();
168         $name = 'SugarAccount' . $time;
169         $email1 = 'account@'. $time. 'sugar.com';
170                 $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")))));
171                 $soap_version_test_accountId = $result['ids'][0];
172                 $GLOBALS['log']->fatal("_setEntriesForAccount id = " . $soap_version_test_accountId);
173                 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
174                 return $result;
175     } // fn
176
177     public function _setEntryForOpportunity() {
178         global $soap_version_test_accountId, $soap_version_test_opportunityId;
179                 $this->_login();
180                 global $timedate;
181                 $date_closed = $timedate->convert_to_gmt_datetime(strtotime('+1 week'));
182         $time = mt_rand();
183         $name = 'SugarOpportunity' . $time;
184         $account_id = $soap_version_test_accountId;
185         $sales_stage = 'Prospecting';
186         $probability = 10;
187         $amount = 1000;
188                 $GLOBALS['log']->fatal("_setEntryForOpportunity id = " . $soap_version_test_accountId);
189                 $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"))));
190                 $soap_version_test_opportunityId = $result['id'];
191                 return $result;
192     } // fn
193     
194   public function _getEntryForOpportunity() {
195         global $soap_version_test_opportunityId;
196                 $this->_login();
197                 $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);
198                 return $result;
199     }
200     
201     public function _setRelationshipForOpportunity() {
202         
203         global $soap_version_test_contactId, $soap_version_test_opportunityId;
204                 $this->_login();
205                 $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));
206                 return $result;         
207     } // fn
208     
209     public function _getRelationshipForOpportunity() {
210         global $soap_version_test_opportunityId;
211                 $this->_login();
212                 $result = $this->_soapClient->call('get_relationships',
213                                 array(
214                 'session' => $this->_sessionId,
215                 'module_name' => 'Opportunities',
216                 'module_id' => "$soap_version_test_opportunityId",
217                 'link_field_name' => 'contacts',
218                 'related_module_query' => '',
219                 'related_fields' => array('id'),
220                 'related_module_link_name_to_fields_array' => array(array('name' =>  'contacts', 'value' => array('id', 'first_name', 'last_name'))),
221                 'deleted'=>0,
222                                 )
223                         );
224                 return $result;         
225     } // fn
226     
227     public function _searchByModule() {
228                 $this->_login();
229                 $result = $this->_soapClient->call('search_by_module',
230                                 array(
231                 'session' => $this->_sessionId,
232                 'search_string' => 'Sugar',
233                                 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
234                 'offset' => '0',
235                 'max_results' => '10')
236             );
237                 return $result;         
238     } // fn
239     
240     /**
241      * Create a test user
242      *
243      */
244         public function _setupTestUser() {
245         $this->_user = SugarTestUserUtilities::createAnonymousUser();
246         $this->_user->status = 'Active';
247         $this->_user->is_admin = 1;
248         $this->_user->save();
249         $GLOBALS['current_user'] = $this->_user;
250     }
251         
252     /**
253      * Remove user created for test
254      *
255      */
256         public function _tearDownTestUser() {
257        SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
258        unset($GLOBALS['current_user']);
259     }
260 }
261 ?>