]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/data/SaveRelationshipChangesTest.php
Release 6.5.5
[Github/sugarcrm.git] / tests / data / SaveRelationshipChangesTest.php
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
37
38
39 class SaveRelationshipChangesTest extends Sugar_PHPUnit_Framework_TestCase
40 {
41
42     public function setUp()
43     {
44         SugarTestHelper::setUp('dictionary');
45         SugarTestHelper::setUp('moduleList');
46         SugarTestHelper::setUp('app_strings');
47         SugarTestHelper::setUp('app_list_strings');
48         SugarTestHelper::setUp('current_user', array(true, 1));
49     }
50
51     public function tearDown()
52     {
53         SugarTestHelper::tearDown();
54     }
55
56     public function setRelationshipInfoDataProvider()
57     {
58         return array(
59             array(
60                 1,
61                 'accounts_contacts',
62                 array(1, 'contacts'),
63             ),
64             array(
65                 1,
66                 'member_accounts',
67                 array(1, 'member_of'),
68             ),
69             array(
70                 1,
71                 'accounts_opportunities',
72                 array(1, 'opportunities'),
73             ),
74         );
75     }
76
77
78     /**
79      * @dataProvider setRelationshipInfoDataProvider
80      */
81     public function testSetRelationshipInfoViaRequestVars($id, $rel, $expected)
82     {
83         $bean = new MockAccountSugarBean();
84
85         $_REQUEST['relate_to'] = $rel;
86         $_REQUEST['relate_id'] = $id;
87
88         $return = $bean->set_relationship_info();
89
90         $this->assertSame($expected, $return);
91     }
92
93     /**
94      * @dataProvider setRelationshipInfoDataProvider
95      */
96     public function testSetRelationshipInfoViaBeanProperties($id, $rel, $expected)
97     {
98         $bean = new MockAccountSugarBean();
99
100         $bean->not_use_rel_in_req = true;
101         $bean->new_rel_id = $id;
102         $bean->new_rel_relname = $rel;
103
104         $return = $bean->set_relationship_info();
105
106         $this->assertSame($expected, $return);
107     }
108
109     public function testHandlePresetRelationshipsAdd()
110     {
111         $acc = SugarTestAccountUtilities::createAccount();
112
113         $macc = new MockAccountSugarBean();
114         $macc->disable_row_level_security = true;
115         $macc->retrieve($acc->id);
116
117         // create an contact
118         $contact = SugarTestContactUtilities::createContact();
119
120         // set the contact id from the bean.
121         $macc->contact_id = $contact->id;
122
123         $new_rel_id = $macc->handle_preset_relationships($contact->id, 'contacts');
124
125         $this->assertFalse($new_rel_id);
126
127         // make sure the relationship exists
128
129         $sql = "SELECT account_id, contact_id from accounts_contacts where account_id = '" . $macc->id . "' AND contact_id = '" . $contact->id . "' and deleted = 0";
130         $result = $GLOBALS['db']->query($sql);
131         $row = $GLOBALS['db']->fetchByAssoc($result);
132
133         $this->assertSame(array('account_id' => $macc->id, 'contact_id' => $contact->id), $row);
134
135         SugarTestAccountUtilities::removeAllCreatedAccounts();
136         SugarTestContactUtilities::removeAllCreatedContacts();
137
138         unset($macc);
139
140     }
141
142     public function testHandlePresetRelationshipsDelete()
143     {
144         $acc = SugarTestAccountUtilities::createAccount();
145
146         $macc = new MockAccountSugarBean();
147         $macc->disable_row_level_security = true;
148         $macc->retrieve($acc->id);
149
150         // create an contact
151         $contact = SugarTestContactUtilities::createContact();
152
153
154         // insert a dummy row
155         $rel_row_id = create_guid();
156         $sql = "INSERT INTO accounts_contacts (id, account_id, contact_id) VALUES ('" . $rel_row_id . "','" . $macc->id . "','" . $contact->id . "')";
157         $GLOBALS['db']->query($sql);
158         $GLOBALS['db']->commit();
159
160         // set the contact id from the bean.
161         $macc->rel_fields_before_value['contact_id'] = $contact->id;
162
163         $new_rel_id = $macc->handle_preset_relationships($contact->id, 'contacts');
164
165         $this->assertEquals($contact->id, $new_rel_id);
166
167         // make sure the relationship exists
168
169         $sql = "SELECT account_id, contact_id from accounts_contacts where account_id = '" . $macc->id . "' AND contact_id = '" . $contact->id . "' and deleted = 0";
170         $result = $GLOBALS['db']->query($sql);
171         $row = $GLOBALS['db']->fetchByAssoc($result);
172
173         $this->assertFalse($row);
174
175         SugarTestAccountUtilities::removeAllCreatedAccounts();
176         SugarTestContactUtilities::removeAllCreatedContacts();
177
178         unset($macc);
179
180     }
181
182     public function testHandleRemainingRelateFields()
183     {
184         // create a test relationship
185         // save cache reset value
186         $_cacheResetValue = SugarCache::$isCacheReset;
187         //$rel = $this->createRelationship('Accounts');
188
189         $rel = SugarTestRelationshipUtilities::createRelationship(array(
190                     'relationship_type' => 'one-to-many',
191                     'lhs_module' => 'Accounts',
192                     'rhs_module' => 'Accounts',
193                 ));
194
195         if($rel == false) {
196             $this->fail('Relationship Not Created');
197         }
198
199         $rel_name = $rel->getName();
200         $id = $rel->getIDName('Accounts');
201
202         $acc1 = SugarTestAccountUtilities::createAccount();
203         $acc2 = SugarTestAccountUtilities::createAccount();
204
205         $macc = new MockAccountSugarBean();
206         $macc->disable_row_level_security = true;
207         $macc->retrieve($acc2->id);
208
209         $macc->$id = $acc1->id;
210
211         $ret = $macc->handle_remaining_relate_fields();
212         $this->assertContains($rel_name, $ret['add']['success']);
213
214         $macc->rel_fields_before_value[$id] = $acc1->id;
215         $macc->$id = '';
216         $ret = $macc->handle_remaining_relate_fields();
217
218         $this->assertContains($rel_name, $ret['remove']['success']);
219
220         // variable cleanup
221         // delete the test relationship
222         //$this->removeRelationship($rel_name, 'Accounts');
223         SugarTestRelationshipUtilities::removeAllCreatedRelationships();
224
225         unset($macc);
226         SugarTestAccountUtilities::removeAllCreatedAccounts();
227         // reset the isCacheReset Value since this is all one request.
228         SugarCache::$isCacheReset = $_cacheResetValue;
229     }
230
231     public function handleRequestRelateProvider()
232     {
233         return array(
234             array('member_of', true),
235             array('MEMBER_OF', true),
236             array(time(), false),
237         );
238     }
239
240     /**
241      *
242      * @dataProvider handleRequestRelateProvider
243      * @param $rel_link_name
244      */
245     public function testHandleRequestRelate($rel_link_name, $expected)
246     {
247         $acc1 = SugarTestAccountUtilities::createAccount();
248         $acc2 = SugarTestAccountUtilities::createAccount();
249
250         $macc = new MockAccountSugarBean();
251         $macc->retrieve($acc2->id);
252
253
254         $ret = $macc->handle_request_relate($acc1->id, $rel_link_name);
255
256         $this->assertSame($expected, $ret);
257
258         unset($macc);
259         SugarTestAccountUtilities::removeAllCreatedAccounts();
260
261     }
262 }
263
264 class MockAccountSugarBean extends Account
265 {
266     public function set_relationship_info(array $exclude = array())
267     {
268         return parent::set_relationship_info($exclude);
269     }
270
271     public function handle_preset_relationships($new_rel_id, $new_rel_name, $exclude = array())
272     {
273         return parent::handle_preset_relationships($new_rel_id, $new_rel_name, $exclude);
274     }
275
276     public function handle_remaining_relate_fields($exclude = array())
277     {
278         return parent::handle_remaining_relate_fields($exclude);
279     }
280
281     public function handle_request_relate($new_rel_id, $new_rel_link)
282     {
283         return parent::handle_request_relate($new_rel_id, $new_rel_link);
284     }
285 }