2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
39 class SaveRelationshipChangesTest extends Sugar_PHPUnit_Framework_TestCase
42 public function setUp()
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));
51 public function tearDown()
53 SugarTestHelper::tearDown();
56 public function setRelationshipInfoDataProvider()
67 array(1, 'member_of'),
71 'accounts_opportunities',
72 array(1, 'opportunities'),
79 * @dataProvider setRelationshipInfoDataProvider
81 public function testSetRelationshipInfoViaRequestVars($id, $rel, $expected)
83 $bean = new MockAccountSugarBean();
85 $_REQUEST['relate_to'] = $rel;
86 $_REQUEST['relate_id'] = $id;
88 $return = $bean->set_relationship_info();
90 $this->assertSame($expected, $return);
94 * @dataProvider setRelationshipInfoDataProvider
96 public function testSetRelationshipInfoViaBeanProperties($id, $rel, $expected)
98 $bean = new MockAccountSugarBean();
100 $bean->not_use_rel_in_req = true;
101 $bean->new_rel_id = $id;
102 $bean->new_rel_relname = $rel;
104 $return = $bean->set_relationship_info();
106 $this->assertSame($expected, $return);
109 public function testHandlePresetRelationshipsAdd()
111 $acc = SugarTestAccountUtilities::createAccount();
113 $macc = new MockAccountSugarBean();
114 $macc->disable_row_level_security = true;
115 $macc->retrieve($acc->id);
118 $contact = SugarTestContactUtilities::createContact();
120 // set the contact id from the bean.
121 $macc->contact_id = $contact->id;
123 $new_rel_id = $macc->handle_preset_relationships($contact->id, 'contacts');
125 $this->assertFalse($new_rel_id);
127 // make sure the relationship exists
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);
133 $this->assertSame(array('account_id' => $macc->id, 'contact_id' => $contact->id), $row);
135 SugarTestAccountUtilities::removeAllCreatedAccounts();
136 SugarTestContactUtilities::removeAllCreatedContacts();
142 public function testHandlePresetRelationshipsDelete()
144 $acc = SugarTestAccountUtilities::createAccount();
146 $macc = new MockAccountSugarBean();
147 $macc->disable_row_level_security = true;
148 $macc->retrieve($acc->id);
151 $contact = SugarTestContactUtilities::createContact();
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();
160 // set the contact id from the bean.
161 $macc->rel_fields_before_value['contact_id'] = $contact->id;
163 $new_rel_id = $macc->handle_preset_relationships($contact->id, 'contacts');
165 $this->assertEquals($contact->id, $new_rel_id);
167 // make sure the relationship exists
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);
173 $this->assertFalse($row);
175 SugarTestAccountUtilities::removeAllCreatedAccounts();
176 SugarTestContactUtilities::removeAllCreatedContacts();
182 public function testHandleRemainingRelateFields()
184 // create a test relationship
185 // save cache reset value
186 $_cacheResetValue = SugarCache::$isCacheReset;
187 //$rel = $this->createRelationship('Accounts');
189 $rel = SugarTestRelationshipUtilities::createRelationship(array(
190 'relationship_type' => 'one-to-many',
191 'lhs_module' => 'Accounts',
192 'rhs_module' => 'Accounts',
196 $this->fail('Relationship Not Created');
199 $rel_name = $rel->getName();
200 $id = $rel->getIDName('Accounts');
202 $acc1 = SugarTestAccountUtilities::createAccount();
203 $acc2 = SugarTestAccountUtilities::createAccount();
205 $macc = new MockAccountSugarBean();
206 $macc->disable_row_level_security = true;
207 $macc->retrieve($acc2->id);
209 $macc->$id = $acc1->id;
211 $ret = $macc->handle_remaining_relate_fields();
212 $this->assertContains($rel_name, $ret['add']['success']);
214 $macc->rel_fields_before_value[$id] = $acc1->id;
216 $ret = $macc->handle_remaining_relate_fields();
218 $this->assertContains($rel_name, $ret['remove']['success']);
221 // delete the test relationship
222 //$this->removeRelationship($rel_name, 'Accounts');
223 SugarTestRelationshipUtilities::removeAllCreatedRelationships();
226 SugarTestAccountUtilities::removeAllCreatedAccounts();
227 // reset the isCacheReset Value since this is all one request.
228 SugarCache::$isCacheReset = $_cacheResetValue;
231 public function handleRequestRelateProvider()
234 array('member_of', true),
235 array('MEMBER_OF', true),
236 array(time(), false),
242 * @dataProvider handleRequestRelateProvider
243 * @param $rel_link_name
245 public function testHandleRequestRelate($rel_link_name, $expected)
247 $acc1 = SugarTestAccountUtilities::createAccount();
248 $acc2 = SugarTestAccountUtilities::createAccount();
250 $macc = new MockAccountSugarBean();
251 $macc->retrieve($acc2->id);
254 $ret = $macc->handle_request_relate($acc1->id, $rel_link_name);
256 $this->assertSame($expected, $ret);
259 SugarTestAccountUtilities::removeAllCreatedAccounts();
264 class MockAccountSugarBean extends Account
266 public function set_relationship_info(array $exclude = array())
268 return parent::set_relationship_info($exclude);
271 public function handle_preset_relationships($new_rel_id, $new_rel_name, $exclude = array())
273 return parent::handle_preset_relationships($new_rel_id, $new_rel_name, $exclude);
276 public function handle_remaining_relate_fields($exclude = array())
278 return parent::handle_remaining_relate_fields($exclude);
281 public function handle_request_relate($new_rel_id, $new_rel_link)
283 return parent::handle_request_relate($new_rel_id, $new_rel_link);