PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/service/SOAPAPI3Test.php

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