PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/service/SOAPAPI3Test.php

https://github.com/mikmagic/sugarcrm_dev
PHP | 352 lines | 251 code | 42 blank | 59 comment | 5 complexity | 221a4f2a178a93a6c5ff96dc7261366e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, AGPL-3.0
  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. require_once('include/TimeDate.php');
  37. require_once('service/v3/SugarWebServiceUtilv3.php');
  38. require_once('tests/service/APIv3Helper.php');
  39. require_once 'tests/service/SOAPTestCase.php';
  40. /**
  41. * This class is meant to test everything SOAP
  42. *
  43. */
  44. class SOAPAPI3Test extends SOAPTestCase
  45. {
  46. public $_contactId = '';
  47. private static $helperObject;
  48. /**
  49. * Create test user
  50. *
  51. */
  52. public function setUp()
  53. {
  54. $this->_soapURL = $GLOBALS['sugar_config']['site_url'].'/service/v3/soap.php';
  55. parent::setUp();
  56. $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
  57. $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
  58. self::$helperObject = new APIv3Helper();
  59. }
  60. /**
  61. * Remove anything that was used during this test
  62. *
  63. */
  64. public function tearDown() {
  65. parent::tearDown();
  66. global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
  67. unset($soap_version_test_accountId);
  68. unset($soap_version_test_opportunityId);
  69. unset($soap_version_test_contactId);
  70. }
  71. /**
  72. * Ensure we can create a session on the server.
  73. *
  74. */
  75. public function testCanLogin(){
  76. $result = $this->_login();
  77. $this->assertTrue(!empty($result['id']) && $result['id'] != -1,
  78. 'SOAP Session not created. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
  79. }
  80. public function testSearchByModule()
  81. {
  82. $this->_login();
  83. $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
  84. $searchModules = array('Accounts','Contacts','Opportunities');
  85. $searchString = "UNIT TEST";
  86. $offSet = 0;
  87. $maxResults = 10;
  88. $results = $this->_soapClient->call('search_by_module',
  89. array(
  90. 'session' => $this->_sessionId,
  91. 'search' => $searchString,
  92. 'modules' => $searchModules,
  93. 'offset' => $offSet,
  94. 'max' => $maxResults,
  95. 'user' => $this->_user->id)
  96. );
  97. $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
  98. $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
  99. $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
  100. $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
  101. $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
  102. $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
  103. $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
  104. $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
  105. }
  106. public function testSearchByModuleWithReturnFields()
  107. {
  108. $this->_login();
  109. $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
  110. $returnFields = array('name','id','deleted');
  111. $searchModules = array('Accounts','Contacts','Opportunities');
  112. $searchString = "UNIT TEST";
  113. $offSet = 0;
  114. $maxResults = 10;
  115. $results = $this->_soapClient->call('search_by_module',
  116. array(
  117. 'session' => $this->_sessionId,
  118. 'search' => $searchString,
  119. 'modules' => $searchModules,
  120. 'offset' => $offSet,
  121. 'max' => $maxResults,
  122. 'user' => $this->_user->id,
  123. 'fields' => $returnFields)
  124. );
  125. $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
  126. $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
  127. $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
  128. $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
  129. $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
  130. $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
  131. $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
  132. $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
  133. }
  134. public function testGetVardefsMD5()
  135. {
  136. $GLOBALS['reload_vardefs'] = TRUE;
  137. //Test a regular module
  138. $result = $this->_getVardefsMD5('Accounts');
  139. $a = new Account();
  140. $soapHelper = new SugarWebServiceUtilv3();
  141. $actualVardef = $soapHelper->get_return_module_fields($a,'Accounts','');
  142. $actualMD5 = md5(serialize($actualVardef));
  143. $this->assertEquals($actualMD5, $result[0], "Unable to retrieve vardef md5.");
  144. //Test a fake module
  145. $result = $this->_getVardefsMD5('BadModule');
  146. $this->assertTrue($result['faultstring'] == 'Module Does Not Exist');
  147. unset($GLOBALS['reload_vardefs']);
  148. }
  149. public function testGetUpcomingActivities()
  150. {
  151. $this->_login();
  152. $expected = $this->_createUpcomingActivities(); //Seed the data.
  153. $results = $this->_soapClient->call('get_upcoming_activities',array('session'=>$this->_sessionId));
  154. $this->assertEquals($expected[0] ,$results[0]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
  155. $this->assertEquals($expected[1] ,$results[1]['id'] , 'Unable to get upcoming activities Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
  156. $this->_removeUpcomingActivities();
  157. }
  158. /**
  159. * Get Module Layout functions not exposed to soap service, make sure they are not available.
  160. *
  161. */
  162. public function testGetModuleLayoutMD5()
  163. {
  164. $result = $this->_getModuleLayoutMD5();
  165. $this->assertContains('Client',$result['faultcode']);
  166. }
  167. public function testSetEntriesForAccount() {
  168. $result = $this->_setEntriesForAccount();
  169. $this->assertTrue(!empty($result['ids']) && $result['ids'][0] != -1,
  170. 'Can not create new account using testSetEntriesForAccount. Error ('.$this->_soapClient->faultcode.'): '.$this->_soapClient->faultstring.': '.$this->_soapClient->faultdetail);
  171. } // fn
  172. /**********************************
  173. * HELPER PUBLIC FUNCTIONS
  174. **********************************/
  175. private function _removeUpcomingActivities()
  176. {
  177. $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
  178. $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
  179. }
  180. private function _createUpcomingActivities()
  181. {
  182. $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
  183. $GLOBALS['current_user']->setPreference('timef','H:i') ;
  184. $date1 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
  185. $date2 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 4) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
  186. $callID = uniqid();
  187. $c = new Call();
  188. $c->id = $callID;
  189. $c->new_with_id = TRUE;
  190. $c->status = 'Not Planned';
  191. $c->date_start = $date1;
  192. $c->name = "UNIT TEST";
  193. $c->assigned_user_id = $this->_user->id;
  194. $c->save(FALSE);
  195. $callID = uniqid();
  196. $c = new Call();
  197. $c->id = $callID;
  198. $c->new_with_id = TRUE;
  199. $c->status = 'Planned';
  200. $c->date_start = $date1;
  201. $c->name = "UNIT TEST";
  202. $c->assigned_user_id = $this->_user->id;
  203. $c->save(FALSE);
  204. $taskID = uniqid();
  205. $t = new Task();
  206. $t->id = $taskID;
  207. $t->new_with_id = TRUE;
  208. $t->status = 'Not Started';
  209. $t->date_due = $date2;
  210. $t->name = "UNIT TEST";
  211. $t->assigned_user_id = $this->_user->id;
  212. $t->save(FALSE);
  213. return array($callID, $taskID);
  214. }
  215. public function _getVardefsMD5($module)
  216. {
  217. $this->_login();
  218. $result = $this->_soapClient->call('get_module_fields_md5',array('session'=>$this->_sessionId,'module'=> $module ));
  219. return $result;
  220. }
  221. public function _getModuleLayoutMD5()
  222. {
  223. $this->_login();
  224. $result = $this->_soapClient->call('get_module_layout_md5',
  225. array('session'=>$this->_sessionId,'module_names'=> array('Accounts'),'types' => array('default'),'views' => array('list')));
  226. return $result;
  227. }
  228. public function _setEntryForContact() {
  229. $this->_login();
  230. global $timedate;
  231. $current_date = $timedate->nowDb();
  232. $time = mt_rand();
  233. $first_name = 'SugarContactFirst' . $time;
  234. $last_name = 'SugarContactLast';
  235. $email1 = 'contact@sugar.com';
  236. $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"))));
  237. SugarTestContactUtilities::setCreatedContact(array($this->_contactId));
  238. return $result;
  239. } // fn
  240. public function _getEntryForContact() {
  241. global $soap_version_test_contactId;
  242. $this->_login();
  243. $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'), 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))));
  244. $GLOBALS['log']->fatal("_getEntryForContact" . " " . $soap_version_test_contactId);
  245. return $result;
  246. }
  247. public function _setEntriesForAccount() {
  248. global $soap_version_test_accountId;
  249. $this->_login();
  250. global $timedate;
  251. $current_date = $timedate->nowDb();
  252. $time = mt_rand();
  253. $name = 'SugarAccount' . $time;
  254. $email1 = 'account@'. $time. 'sugar.com';
  255. $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")))));
  256. $soap_version_test_accountId = $result['ids'][0];
  257. $GLOBALS['log']->fatal("_setEntriesForAccount id = " . $soap_version_test_accountId);
  258. SugarTestAccountUtilities::setCreatedAccount(array($soap_version_test_accountId));
  259. return $result;
  260. } // fn
  261. public function _setEntryForOpportunity() {
  262. global $soap_version_test_accountId, $soap_version_test_opportunityId;
  263. $this->_login();
  264. global $timedate;
  265. $date_closed = $timedate->getNow()->get("+1 week")->asDb();
  266. $time = mt_rand();
  267. $name = 'SugarOpportunity' . $time;
  268. $account_id = $soap_version_test_accountId;
  269. $sales_stage = 'Prospecting';
  270. $probability = 10;
  271. $amount = 1000;
  272. $GLOBALS['log']->fatal("_setEntryForOpportunity id = " . $soap_version_test_accountId);
  273. $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"))));
  274. $soap_version_test_opportunityId = $result['id'];
  275. return $result;
  276. } // fn
  277. public function _setRelationshipForOpportunity() {
  278. global $soap_version_test_contactId, $soap_version_test_opportunityId;
  279. $this->_login();
  280. $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'))));
  281. return $result;
  282. } // fn
  283. public function _getRelationshipForOpportunity() {
  284. global $soap_version_test_opportunityId;
  285. $this->_login();
  286. $result = $this->_soapClient->call('get_relationships',
  287. array(
  288. 'session' => $this->_sessionId,
  289. 'module_name' => 'Opportunities',
  290. 'module_id' => "$soap_version_test_opportunityId",
  291. 'link_field_name' => 'contacts',
  292. 'related_module_query' => '',
  293. 'related_fields' => array('id'),
  294. 'related_module_link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))))
  295. );
  296. return $result;
  297. } // fn
  298. public function _searchByModule() {
  299. $this->_login();
  300. $result = $this->_soapClient->call('search_by_module',
  301. array(
  302. 'session' => $this->_sessionId,
  303. 'search_string' => 'Sugar',
  304. 'modules' => array('Accounts', 'Contacts', 'Opportunities'),
  305. 'offset' => '0',
  306. 'max_results' => '10')
  307. );
  308. return $result;
  309. } // fn
  310. }