]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/Bug44280Test.php
Release 6.2.2
[Github/sugarcrm.git] / tests / service / Bug44280Test.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('include/nusoap/nusoap.php');
39
40
41 /**
42  * @group bug44280
43  */
44 class Bug44280Test extends Sugar_PHPUnit_Framework_TestCase
45 {
46         public $_user = null;
47         public $_soapClient = null;
48         public $_session = null;
49         public $_sessionId = null;
50     public $accnt1;
51     public $accnt2;
52     public $cont1;
53     public $cont2;
54
55         
56
57         public function setUp() 
58     {
59         $this->_soapClient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/service/v2/soap.php',false,false,false,false,false,600,600);
60
61         $this->_user = SugarTestUserUtilities::createAnonymousUser();
62         $this->_user->status = 'Active';
63         $this->_user->is_admin = 1;
64         $this->_user->save();
65         $GLOBALS['current_user'] = $this->_user;
66
67        
68         
69                  
70     }
71
72     /**
73      * Remove anything that was used during this test
74      *
75      */
76     public function tearDown() {
77         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
78         unset($GLOBALS['current_user']);
79
80         unset($this->_user);
81         unset($this->_soapClient);
82         unset($this->_session);
83         unset($this->_sessionId);
84
85         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$this->cont1->id}'");
86         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$this->cont2->id}'");
87         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE contact_id= '{$this->cont1->id}'");
88         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE contact_id= '{$this->cont2->id}'");
89         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$this->accnt1->id}'");
90         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$this->accnt2->id}'");
91
92         unset($this->accnt1); unset($this->accnt2);
93         unset($this->cont1); unset($this->cont2);
94
95     }
96
97     public function createAccount($name,$user_id) {
98         $account = new Account();
99                 $account->id = uniqid();
100         $account->name = $name;
101         $account->assigned_user_id = $user_id;
102         $account->new_with_id = true;
103         $account->disable_custom_fields = true;
104         $account->save();
105
106         return $account;
107     }
108
109     public function createContact($first_name, $last_name, $email){
110         $contact = new Contact();
111                 $contact->id = uniqid();
112         $contact->first_name = $first_name;
113         $contact->last_name = $last_name;
114         $contact->email1 = $email;
115         $contact->new_with_id = true;
116         $contact->disable_custom_fields = true;
117         $contact->save();
118
119         return $contact;
120     }
121     
122     public function testSetEntries() {
123         $this->_login();
124
125         // first create two accounts with identical account names
126         $this->accnt1 = $this->createAccount("sugar_account_name","sugarUser1");
127         $this->accnt2 = $this->createAccount("sugar_account_name","sugarUser2");
128
129         // now creating two contacts and relate them to the above accounts
130
131         $this->cont1 = $this->createContact("first1", "last1", "adsf@asdf.com");
132         $this->cont2 = $this->createContact("first2", "last2", "adsf@asdf.com");
133
134          // this will be used in set_entries call
135         $accounts_list=array( 'session'=>$this->_sessionId, 'module_name' => 'Accounts',
136                                    'name_value_lists' => array(
137                                         array(
138                                            array('name'=>'id','value'=>$this->accnt1->id),
139                                            array('name'=>'first_name','value'=>$this->accnt1->name),
140                                            array('name'=>'account_id','value'=>$this->accnt1->id),
141                                            array('name'=>'team_id','value'=>'1'),
142                                            array('name'=>'soap_dts_c','value'=>'2011-06-02 17:37:49'),
143                                            array('name'=>'contactid_4d_c','value'=>'123456'),
144                                            array('name'=>'phone_work','value'=>'1234567890'),
145                                            array('name'=>'title','value'=>''),
146                                        ),
147                                         array(
148                                            array('name'=>'id','value'=>$this->accnt2->id),
149                                            array('name'=>'first_name','value'=>$this->accnt2->name),
150                                            array('name'=>'account_id','value'=>$this->accnt2->id),
151                                            array('name'=>'team_id','value'=>'1'),
152                                            array('name'=>'soap_dts_c','value'=>'2011-06-02 16:37:49'),
153                                            array('name'=>'contactid_4d_c','value'=>'999991'),
154                                            array('name'=>'phone_work','value'=>'987654321'),
155                                            array('name'=>'title','value'=>''),
156                                        )
157                                         )
158                                        );
159         // add the accounts
160          $result = $this->_soapClient->call('set_entries', $accounts_list);
161
162         // add the contacts & set the relationship to account
163         $contacts_list = array( 'session'=>$this->_sessionId, 'module_name' => 'Contacts',
164                                    'name_value_lists' => array(
165                                         array(
166                                            array('name'=>'last_name','value'=>$this->cont1->last_name),
167                                            array('name'=>'email','value'=>$this->cont1->email1),
168                                            array('name'=>'first_name','value'=>$this->cont1->first_name),
169                                            array('name'=>'id','value'=>$this->cont1->id),
170                                            array('name'=>'account_name','value'=>$this->accnt1->name),
171                                            array('name'=>'account_id','value'=>$this->accnt1->id),
172
173
174                                        ),
175                                         array(
176                                             array('name'=>'last_name','value'=>$this->cont2->last_name),
177                                             array('name'=>'email','value'=>$this->cont2->email1),
178                                             array('name'=>'first_name','value'=>$this->cont2->first_name),
179                                             array('name'=>'id','value'=>$this->cont2->id),
180                                             array('name'=>'account_name','value'=>$this->accnt2->name),
181                                             array('name'=>'account_id','value'=>$this->accnt2->id),
182
183                                        )
184                                         )
185                                        );
186
187
188         $result2 = $this->_soapClient->call('set_entries', $contacts_list);
189
190          // lets check first relationship
191         $query1 = "SELECT account_id FROM accounts_contacts WHERE contact_id='{$this->cont1->id}'";
192         $cont1_account_result = $GLOBALS['db']->query($query1,true,"");
193         $row1 = $GLOBALS['db']->fetchByAssoc($cont1_account_result);
194         if(isset($row1) ){
195
196             $this->assertEquals($this->accnt1->id, $row1["account_id"], "check first account-contact relationship");
197
198           }
199
200
201           // lets check second relationship
202         $query2 = "SELECT account_id FROM accounts_contacts WHERE contact_id='{$this->cont2->id}'";
203         $cont2_account_result = $GLOBALS['db']->query($query2,true,"");
204         $row2 = $GLOBALS['db']->fetchByAssoc($cont2_account_result);
205         if(isset($row2) ){
206
207             $this->assertEquals($this->accnt2->id, $row2["account_id"], "check second account-contact relationship");
208
209           }
210
211          
212     }  
213     
214
215     /**
216      * Attempt to login to the soap server
217      *
218      * @return $set_entry_result - this should contain an id and error.  The id corresponds
219      * to the session_id.
220      */
221     public function _login(){
222                 global $current_user;   
223         $result = $this->_soapClient->call('login',
224             array('user_auth' => 
225                 array('user_name' => $current_user->user_name,
226                     'password' => $current_user->user_hash,
227                     'version' => '1.0'),
228                 'application_name' => 'SoapTest')
229             );
230          $this->_sessionId = $result['id'];
231                 return $result;
232     }
233     
234
235         
236 }
237 ?>