2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 ********************************************************************************/
38 require_once 'modules/Leads/views/view.convertlead.php';
41 class ConvertLeadTests extends Sugar_PHPUnit_Framework_TestCase
43 public function setUp()
45 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
46 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
49 public function tearDown()
51 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
52 unset($GLOBALS['app_list_strings']);
53 unset($GLOBALS['current_user']);
59 public function testOpportunityNameValueFilled(){
60 $lead = SugarTestLeadUtilities::createLead();
61 $lead->opportunity_name = 'SBizzle Dollar Store';
64 $_REQUEST['module'] = 'Leads';
65 $_REQUEST['action'] = 'ConvertLead';
66 $_REQUEST['record'] = $lead->id;
68 // Check that the opportunity name doesn't get populated when it's not in the Leads editview layout
69 require_once('include/MVC/Controller/ControllerFactory.php');
70 require_once('include/MVC/View/ViewFactory.php');
71 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
73 $GLOBALS['app']->controller->execute();
74 $output = ob_get_clean();
76 $matches_one = array();
77 $pattern = '/SBizzle Dollar Store/';
78 preg_match($pattern, $output, $matches_one);
79 $this->assertTrue(count($matches_one) == 0, "Opportunity name got carried over to the Convert Leads page when it shouldn't have.");
81 // Add the opportunity_name to the Leads EditView
82 SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
84 // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
86 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
87 $GLOBALS['app']->controller->execute();
88 $output = ob_get_clean();
89 $matches_two = array();
90 $pattern = '/SBizzle Dollar Store/';
91 preg_match($pattern, $output, $matches_two);
92 $this->assertTrue(count($matches_two) > 0, "Opportunity name did not carry over to the Convert Leads page when it should have.");
94 SugarTestStudioUtilities::removeAllCreatedFields();
95 unset($GLOBALS['app']->controller);
96 unset($_REQUEST['module']);
97 unset($_REQUEST['action']);
98 unset($_REQUEST['record']);
99 SugarTestLeadUtilities::removeAllCreatedLeads();
104 public function testActivityMove() {
106 $lead = SugarTestLeadUtilities::createLead();
107 $contact = SugarTestContactUtilities::createContact();
108 $meeting = SugarTestMeetingUtilities::createMeeting();
109 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
110 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
111 $_REQUEST['record'] = $lead->id;
113 // refresh the meeting to include parent_id and parent_type
114 $meeting_id = $meeting->id;
115 $meeting = new Meeting();
116 $meeting->retrieve($meeting_id);
118 // action: move meeting from lead to contact
119 $convertObj = new TestViewConvertLead();
120 $convertObj->moveActivityWrapper($meeting, $contact);
122 // verification 1, parent id should be contact id
123 $this->assertTrue($meeting->parent_id == $contact->id, 'Meeting parent id is not converted to contact id.');
125 // verification 2, parent type should be "Contacts"
126 $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
128 // verification 3, record should be deleted from meetings_leads table
129 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
130 $result = $GLOBALS['db']->query($sql);
131 $row = $GLOBALS['db']->fetchByAssoc($result);
132 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
134 // verification 4, record should be added to meetings_contacts table
135 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact->id}' and deleted=0";
136 $result = $GLOBALS['db']->query($sql);
137 $row = $GLOBALS['db']->fetchByAssoc($result);
138 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
141 unset($_REQUEST['record']);
142 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact->id}'");
143 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
144 SugarTestMeetingUtilities::removeMeetingContacts();
145 SugarTestMeetingUtilities::removeAllCreatedMeetings();
146 SugarTestContactUtilities::removeAllCreatedContacts();
147 SugarTestLeadUtilities::removeAllCreatedLeads();
150 public function testActivityCopy() {
152 $lead = SugarTestLeadUtilities::createLead();
153 $contact = SugarTestContactUtilities::createContact();
154 $meeting = SugarTestMeetingUtilities::createMeeting();
155 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
156 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
157 $_REQUEST['record'] = $lead->id;
159 // refresh the meeting to include parent_id and parent_type
160 $meeting_id = $meeting->id;
161 $meeting = new Meeting();
162 $meeting->retrieve($meeting_id);
164 // action: copy meeting from lead to contact
165 $convertObj = new TestViewConvertLead();
166 $convertObj->copyActivityWrapper($meeting, $contact);
168 // 1. the original meeting should still have the same parent_type and parent_id
169 $meeting->retrieve($meeting_id);
170 $this->assertEquals('Leads', $meeting->parent_type, 'parent_type of the original meeting was changed from Leads to '.$meeting->parent_type);
171 $this->assertEquals($lead->id, $meeting->parent_id, 'parent_id of the original meeting was changed from '.$lead->id.' to '.$meeting->parent_id);
173 // 2. a newly created meeting with parent type=Contatcs and parent_id=$contact->id
174 $sql = "select id from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts' and deleted=0";
175 $result = $GLOBALS['db']->query($sql);
176 $row = $GLOBALS['db']->fetchByAssoc($result);
177 $this->assertNotNull($row, 'Could not find the newly created meeting with parent_type=Contacts and parent_id='.$contact->id);
178 $new_meeting_id = '';
180 $new_meeting_id = $row['id'];
183 // 3. record should not be deleted from meetings_leads table
184 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
185 $result = $GLOBALS['db']->query($sql);
186 $row = $GLOBALS['db']->fetchByAssoc($result);
187 $this->assertNotNull($row, "Meeting-Lead relationship was removed.");
189 // 4. new meeting record should be added to meetings_contacts table
190 $sql = "select id from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id='{$contact->id}' and deleted=0";
191 $result = $GLOBALS['db']->query($sql);
192 $row = $GLOBALS['db']->fetchByAssoc($result);
193 $this->assertFalse(empty($row), "Meeting-Contact relationship has not been added.");
196 unset($_REQUEST['record']);
197 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts'");
198 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
199 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id= '{$contact->id}'");
200 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
201 SugarTestMeetingUtilities::removeMeetingContacts();
202 SugarTestMeetingUtilities::removeAllCreatedMeetings();
203 SugarTestContactUtilities::removeAllCreatedContacts();
204 SugarTestLeadUtilities::removeAllCreatedLeads();
207 public function testConversionAndMoveActivities() {
208 global $sugar_config;
211 $lead = SugarTestLeadUtilities::createLead();
212 $account = SugarTestAccountUtilities::createAccount();
213 $meeting = SugarTestMeetingUtilities::createMeeting();
214 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
215 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
216 $_REQUEST['record'] = $lead->id;
218 // set the request/post parameters before converting the lead
219 $_REQUEST['module'] = 'Leads';
220 $_REQUEST['action'] = 'ConvertLead';
221 $_REQUEST['record'] = $lead->id;
222 $_REQUEST['handle'] = 'save';
223 $_REQUEST['selectedAccount'] = $account->id;
224 $sugar_config['lead_conv_activity_opt'] = 'move';
225 $_POST['lead_conv_ac_op_sel'] = 'Contacts';
227 // call display to trigger conversion
228 $vc = new ViewConvertLead();
232 $meeting_id = $meeting->id;
233 $meeting = new Meeting();
234 $meeting->retrieve($meeting_id);
237 $lead_id = $lead->id;
239 $lead->retrieve($lead_id);
241 // retrieve the new contact id from the conversion
242 $contact_id = $lead->contact_id;
244 // 1. Lead's contact_id should not be null
245 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
247 // 2. Lead status should be 'Converted'
248 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
250 // 3. new parent_type should be Contacts
251 $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
253 // 4. new parent_id should be contact id
254 $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
256 // 5. record should be deleted from meetings_leads table
257 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
258 $result = $GLOBALS['db']->query($sql);
259 $row = $GLOBALS['db']->fetchByAssoc($result);
260 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
262 // 6. record should be added to meetings_contacts table
263 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
264 $result = $GLOBALS['db']->query($sql);
265 $row = $GLOBALS['db']->fetchByAssoc($result);
266 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
269 unset($_REQUEST['record']);
270 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
271 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
272 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
273 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
274 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
275 SugarTestMeetingUtilities::removeMeetingContacts();
276 SugarTestMeetingUtilities::removeAllCreatedMeetings();
277 SugarTestAccountUtilities::removeAllCreatedAccounts();
278 SugarTestLeadUtilities::removeAllCreatedLeads();
281 public function testConversionAndCopyActivities() {
282 global $sugar_config;
285 $lead = SugarTestLeadUtilities::createLead();
286 $account = SugarTestAccountUtilities::createAccount();
287 $meeting = SugarTestMeetingUtilities::createMeeting();
288 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
289 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
290 $_REQUEST['record'] = $lead->id;
292 // set the request/post parameters before converting the lead
293 $_REQUEST['module'] = 'Leads';
294 $_REQUEST['action'] = 'ConvertLead';
295 $_REQUEST['record'] = $lead->id;
296 $_REQUEST['handle'] = 'save';
297 $_REQUEST['selectedAccount'] = $account->id;
298 $sugar_config['lead_conv_activity_opt'] = 'copy';
299 $_POST['lead_conv_ac_op_sel'] = array('Contacts');
301 // call display to trigger conversion
302 $vc = new ViewConvertLead();
306 $meeting_id = $meeting->id;
307 $meeting = new Meeting();
308 $meeting->retrieve($meeting_id);
311 $lead_id = $lead->id;
313 $lead->retrieve($lead_id);
315 // retrieve the new contact id from the conversion
316 $contact_id = $lead->contact_id;
318 // 1. Lead's contact_id should not be null
319 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
321 // 2. Lead status should be 'Converted'
322 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
324 // 3. parent_type of the original meeting should be Leads
325 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
327 // 4. parent_id of the original meeting should be contact id
328 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
330 // 5. record should NOT be deleted from meetings_leads table
331 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
332 $result = $GLOBALS['db']->query($sql);
333 $row = $GLOBALS['db']->fetchByAssoc($result);
334 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
336 // 6. record should be added to meetings_contacts table
337 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
338 $result = $GLOBALS['db']->query($sql);
339 $row = $GLOBALS['db']->fetchByAssoc($result);
340 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
342 // 7. the parent_type of the new meeting should be Contacts
343 $new_meeting_id = $row['meeting_id'];
344 $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
345 $result = $GLOBALS['db']->query($sql);
346 $row = $GLOBALS['db']->fetchByAssoc($result);
347 $this->assertFalse(empty($row), "New meeting is not added for contact.");
348 $this->assertEquals('Contacts', $row['parent_type'], 'Parent type of the new meeting should be Contacts');
350 // 8. the parent_id of the new meeting should be contact id
351 $this->assertEquals($contact_id, $row['parent_id'], 'Parent id of the new meeting should be contact id.');
354 unset($_REQUEST['record']);
355 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
356 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
357 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
358 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
359 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
360 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
361 SugarTestMeetingUtilities::removeMeetingContacts();
362 SugarTestMeetingUtilities::removeAllCreatedMeetings();
363 SugarTestAccountUtilities::removeAllCreatedAccounts();
364 SugarTestLeadUtilities::removeAllCreatedLeads();
367 public function testConversionAndDoNothing() {
368 global $sugar_config;
371 $lead = SugarTestLeadUtilities::createLead();
372 $account = SugarTestAccountUtilities::createAccount();
373 $meeting = SugarTestMeetingUtilities::createMeeting();
374 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
375 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
376 $_REQUEST['record'] = $lead->id;
378 // set the request/post parameters before converting the lead
379 $_REQUEST['module'] = 'Leads';
380 $_REQUEST['action'] = 'ConvertLead';
381 $_REQUEST['record'] = $lead->id;
382 $_REQUEST['handle'] = 'save';
383 $_REQUEST['selectedAccount'] = $account->id;
384 $sugar_config['lead_conv_activity_opt'] = 'none';
386 // call display to trigger conversion
387 $vc = new ViewConvertLead();
391 $meeting_id = $meeting->id;
392 $meeting = new Meeting();
393 $meeting->retrieve($meeting_id);
396 $lead_id = $lead->id;
398 $lead->retrieve($lead_id);
400 // retrieve the new contact id from the conversion
401 $contact_id = $lead->contact_id;
403 // 1. Lead's contact_id should not be null
404 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
406 // 2. Lead status should be 'Converted'
407 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
409 // 3. parent_type of the original meeting should be Leads
410 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
412 // 4. parent_id of the original meeting should be contact id
413 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
415 // 5. record should NOT be deleted from meetings_leads table
416 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
417 $result = $GLOBALS['db']->query($sql);
418 $row = $GLOBALS['db']->fetchByAssoc($result);
419 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
421 // 6. record should NOT be added to meetings_contacts table
422 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
423 $result = $GLOBALS['db']->query($sql);
424 $row = $GLOBALS['db']->fetchByAssoc($result);
425 $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
428 unset($_REQUEST['record']);
429 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
430 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
431 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
432 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
433 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
434 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
435 SugarTestMeetingUtilities::removeMeetingContacts();
436 SugarTestMeetingUtilities::removeAllCreatedMeetings();
437 SugarTestAccountUtilities::removeAllCreatedAccounts();
438 SugarTestLeadUtilities::removeAllCreatedLeads();
442 class TestViewConvertLead extends ViewConvertLead
444 public function moveActivityWrapper($activity, $bean) {
445 parent::moveActivity($activity, $bean);
448 public function copyActivityWrapper($activity, $bean) {
449 parent::copyActivityAndRelateToBean($activity, $bean);