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

/tests/service/SOAPAPI2Test.php

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