]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI3Test.php
Release 6.4.0
[Github/sugarcrm.git] / tests / service / SOAPAPI3Test.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
38 require_once('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
40 require_once 'tests/service/SOAPTestCase.php';
41 /**
42  * This class is meant to test everything SOAP
43  *
44  */
45 class SOAPAPI3Test extends SOAPTestCase
46 {
47     public $_contactId = '';
48     private static $helperObject;
49
50     /**
51      * Create test user
52      *
53      */
54         public function setUp()
55     {
56         $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v3/soap.php';
57         parent::setUp();
58         $this->_login();
59         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
60         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
61
62         self::$helperObject = new APIv3Helper();
63     }
64
65     public function tearDown()
66     {
67         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
68         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
69         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
70         unset($GLOBALS['reload_vardefs']);
71         parent::tearDown();
72     }
73
74         /**
75          * Ensure we can create a session on the server.
76          *
77          */
78     public function testCanLogin(){
79                 $result = $this->_login();
80         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
81             'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
82     }
83
84     public function testSearchByModule()
85     {
86         $seedData = self::$helperObject->populateSeedDataForSearchTest($GLOBALS['current_user']->id);
87
88         $searchModules = array('Accounts','Contacts','Opportunities');
89         $searchString = "UNIT TEST";
90         $offSet = 0;
91         $maxResults = 10;
92
93         $results = $this->_soapClient->call('search_by_module',
94                         array(
95                             'session' => $this->_sessionId,
96                             'search'  => $searchString,
97                             'modules' => $searchModules,
98                             'offset'  => $offSet,
99                             'max'     => $maxResults,
100                             'user'    => $GLOBALS['current_user']->id)
101                         );
102
103         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
104         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
105         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
106         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
107         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
108     }
109
110     public function testSearchByModuleWithReturnFields()
111     {
112         $seedData = self::$helperObject->populateSeedDataForSearchTest($GLOBALS['current_user']->id);
113
114         $returnFields = array('name','id','deleted');
115         $searchModules = array('Accounts','Contacts','Opportunities');
116         $searchString = "UNIT TEST";
117         $offSet = 0;
118         $maxResults = 10;
119
120         $results = $this->_soapClient->call('search_by_module',
121                         array(
122                             'session' => $this->_sessionId,
123                             'search'  => $searchString,
124                             'modules' => $searchModules,
125                             'offset'  => $offSet,
126                             'max'     => $maxResults,
127                             'user'    => $GLOBALS['current_user']->id,
128                             'fields'  => $returnFields)
129                         );
130
131         $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
132         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
133         $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
134         $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
135         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
136     }
137
138     public function testGetVardefsMD5()
139     {
140         $GLOBALS['reload_vardefs'] = TRUE;
141         //Test a regular module
142         $result = $this->_getVardefsMD5('Currencies');
143         $a = new Currency();
144         $soapHelper = new SugarWebServiceUtilv3();
145         $actualVardef = $soapHelper->get_return_module_fields($a,'Currencies','');
146         $actualMD5 = md5(serialize($actualVardef));
147         $this->assertEquals($actualMD5, $result[0], "Unable to retrieve vardef md5.");
148
149         //Test a fake module
150         $result = $this->_getVardefsMD5('BadModule');
151         $this->assertEquals('Module Does Not Exist', $result['faultstring']);
152     }
153
154     public function testGetUpcomingActivities()
155     {
156          $expected = $this->_createUpcomingActivities(); //Seed the data.
157          $results = $this->_soapClient->call('get_upcoming_activities',array('session'=>$this->_sessionId));
158          $this->_removeUpcomingActivities();
159
160          $this->assertEquals($expected[0] ,$results[0]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
161          $this->assertEquals($expected[1] ,$results[1]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
162
163     }
164
165     public function testSetEntriesForAccount()
166     {
167         $result = $this->_setEntriesForAccount();
168         $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
169             'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
170     } // fn
171
172
173     /**
174      * Get Module Layout functions not exposed to soap service, make sure they are not available.
175      *
176      */
177     public function testGetModuleLayoutMD5()
178     {
179         $result = $this->_getModuleLayoutMD5();
180         $this->assertContains('Client',$result['faultcode']);
181     }
182
183
184     /**********************************
185      * HELPER PUBLIC FUNCTIONS
186      **********************************/
187     private function _removeUpcomingActivities()
188     {
189         $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
190         $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
191     }
192
193     private function _createUpcomingActivities()
194     {
195         $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
196         $GLOBALS['current_user']->setPreference('timef','H:i') ;
197         global $timedate;
198         $date1 = $timedate->asUser($timedate->getNow()->modify("+2 days"));
199         $date2 = $timedate->asUser($timedate->getNow()->modify("+4 days"));
200
201         $callID = uniqid();
202         $c = new Call();
203         $c->id = $callID;
204         $c->new_with_id = TRUE;
205         $c->status = 'Not Planned';
206         $c->date_start = $date1;
207         $c->name = "UNIT TEST";
208         $c->assigned_user_id = $GLOBALS['current_user']->id;
209         $c->save(FALSE);
210
211         $callID = uniqid();
212         $c = new Call();
213         $c->id = $callID;
214         $c->new_with_id = TRUE;
215         $c->status = 'Planned';
216         $c->date_start = $date1;
217         $c->name = "UNIT TEST";
218         $c->assigned_user_id = $GLOBALS['current_user']->id;
219         $c->save(FALSE);
220
221         $taskID = uniqid();
222         $t = new Task();
223         $t->id = $taskID;
224         $t->new_with_id = TRUE;
225         $t->status = 'Not Started';
226         $t->date_due = $date2;
227         $t->name = "UNIT TEST";
228         $t->assigned_user_id = $GLOBALS['current_user']->id;
229         $t->save(FALSE);
230         $GLOBALS['db']->commit();
231
232         return array($callID, $taskID);
233     }
234
235     public function _getVardefsMD5($module)
236     {
237                 $result = $this->_soapClient->call('get_module_fields_md5',array('session'=>$this->_sessionId,'module'=> $module ));
238                 return $result;
239     }
240
241     public function _getModuleLayoutMD5()
242     {
243                 $result = $this->_soapClient->call('get_module_layout_md5',
244                               array('session'=>$this->_sessionId,'module_names'=> array('Accounts'),'types' => array('default'),'views' => array('list')));
245                 return $result;
246     }
247
248     public function _setEntriesForAccount() {
249                 global $timedate;
250                 $current_date = $timedate->nowDb();
251         $time = mt_rand();
252         $name = 'SugarAccount' . $time;
253         $email1 = 'account@'. $time. 'sugar.com';
254                 $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")))));
255                 $soap_version_test_accountId = $result['ids'][0];
256                 SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
257                 return $result;
258     } // fn
259
260 }