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 ********************************************************************************/
38 require_once('tests/SugarTestViewConvertLeadUtilities.php');
39 require_once 'modules/Leads/views/view.convertlead.php';
40 require_once 'tests/SugarTestViewConvertLeadUtilities.php';
43 class ConvertLeadTest extends Sugar_PHPUnit_Framework_TestCase
45 public function setUp()
47 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
48 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
51 public function tearDown()
53 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
54 unset($GLOBALS['app_list_strings']);
55 unset($GLOBALS['current_user']);
61 public function testOpportunityNameValueFilled()
63 $lead = SugarTestLeadUtilities::createLead();
64 $lead->opportunity_name = 'SBizzle Dollar Store';
67 $_REQUEST['module'] = 'Leads';
68 $_REQUEST['action'] = 'ConvertLead';
69 $_REQUEST['record'] = $lead->id;
71 // Check that the opportunity name doesn't get populated when it's not in the Leads editview layout
72 require_once('include/MVC/Controller/ControllerFactory.php');
73 require_once('include/MVC/View/ViewFactory.php');
74 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
76 $GLOBALS['app']->controller->execute();
77 $output = ob_get_clean();
79 $matches_one = array();
80 $pattern = '/SBizzle Dollar Store/';
81 preg_match($pattern, $output, $matches_one);
82 $this->assertTrue(count($matches_one) == 0, "Opportunity name got carried over to the Convert Leads page when it shouldn't have.");
84 // Add the opportunity_name to the Leads EditView
85 SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
87 // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
89 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
90 $GLOBALS['app']->controller->execute();
91 $output = ob_get_clean();
92 $matches_two = array();
93 $pattern = '/SBizzle Dollar Store/';
94 preg_match($pattern, $output, $matches_two);
95 $this->assertTrue(count($matches_two) > 0, "Opportunity name did not carry over to the Convert Leads page when it should have.");
97 SugarTestStudioUtilities::removeAllCreatedFields();
98 unset($GLOBALS['app']->controller);
99 unset($_REQUEST['module']);
100 unset($_REQUEST['action']);
101 unset($_REQUEST['record']);
102 SugarTestLeadUtilities::removeAllCreatedLeads();
108 public function testActivityMove() {
110 $lead = SugarTestLeadUtilities::createLead();
111 $contact = SugarTestContactUtilities::createContact();
112 $meeting = SugarTestMeetingUtilities::createMeeting();
113 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
114 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
115 $_REQUEST['record'] = $lead->id;
117 // refresh the meeting to include parent_id and parent_type
118 $meeting_id = $meeting->id;
119 $meeting = new Meeting();
120 $meeting->retrieve($meeting_id);
122 // action: move meeting from lead to contact
123 $convertObj = new TestViewConvertLead();
124 $convertObj->moveActivityWrapper($meeting, $contact);
126 // verification 1, parent id should be contact id
127 $this->assertTrue($meeting->parent_id == $contact->id, 'Meeting parent id is not converted to contact id.');
129 // verification 2, parent type should be "Contacts"
130 $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
132 // verification 3, record should be deleted from meetings_leads table
133 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
134 $result = $GLOBALS['db']->query($sql);
135 $row = $GLOBALS['db']->fetchByAssoc($result);
136 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
138 // verification 4, record should be added to meetings_contacts table
139 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact->id}' and deleted=0";
140 $result = $GLOBALS['db']->query($sql);
141 $row = $GLOBALS['db']->fetchByAssoc($result);
142 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
145 unset($_REQUEST['record']);
146 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact->id}'");
147 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
148 SugarTestMeetingUtilities::removeMeetingContacts();
149 SugarTestMeetingUtilities::removeAllCreatedMeetings();
150 SugarTestContactUtilities::removeAllCreatedContacts();
151 SugarTestLeadUtilities::removeAllCreatedLeads();
155 public function testActivityCopyWithParent() {
156 // lets the run the activity copy again, only this time we pass in a parent account
157 $lead = SugarTestLeadUtilities::createLead();
158 $contact = SugarTestContactUtilities::createContact();
159 $meeting = SugarTestMeetingUtilities::createMeeting();
160 $account = SugarTestAccountUtilities::createAccount();
161 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
162 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
163 $_REQUEST['record'] = $lead->id;
165 // refresh the meeting to include parent_id and parent_type
166 $meeting_id = $meeting->id;
167 $meeting = new Meeting();
168 $meeting->retrieve($meeting_id);
170 // action: copy meeting from lead to contact
171 $convertObj = new TestViewConvertLead();
172 $convertObj->copyActivityWrapper($meeting, $contact, array('id'=>$account->id,'type'=>'Accounts'));
175 // 2a a newly created meeting with no parent info passed in, so parent id and type are empty
176 //parent type=Contatcs and parent_id=$contact->id
177 //$sql = "select id from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts' and deleted=0";
178 $sql = "select id, parent_id from meetings where name = '{$meeting->name}'";
179 $result = $GLOBALS['db']->query($sql);
180 while ($row = $GLOBALS['db']->fetchByAssoc($result)){
181 //skip if this is the original message
182 if($row['id'] == $meeting_id){
186 $this->assertEquals($row['parent_id'], $account->id, 'parent id of meeting should be equal to passed in account id: '.$account->id);
192 public function testActivityCopyWithNoParent() {
194 $lead = SugarTestLeadUtilities::createLead();
195 $contact = SugarTestContactUtilities::createContact();
196 $meeting = SugarTestMeetingUtilities::createMeeting();
197 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
198 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
199 $_REQUEST['record'] = $lead->id;
201 // refresh the meeting to include parent_id and parent_type
202 $meeting_id = $meeting->id;
203 $meeting = new Meeting();
204 $meeting->retrieve($meeting_id);
206 // action: copy meeting from lead to contact
207 $convertObj = new TestViewConvertLead();
208 $convertObj->copyActivityWrapper($meeting, $contact);
210 // 1. the original meeting should still have the same parent_type and parent_id
211 $meeting->retrieve($meeting_id);
212 $this->assertEquals('Leads', $meeting->parent_type, 'parent_type of the original meeting was changed from Leads to '.$meeting->parent_type);
213 $this->assertEquals($lead->id, $meeting->parent_id, 'parent_id of the original meeting was changed from '.$lead->id.' to '.$meeting->parent_id);
215 // 2. a newly created meeting with no parent info passed in, so parent id and type are empty
216 $new_meeting_id = '';
217 $sql = "select id, parent_id from meetings where name = '{$meeting->name}'";
218 $result = $GLOBALS['db']->query($sql);
219 while ($row = $GLOBALS['db']->fetchByAssoc($result)){
220 //skip if this is the original message
221 if($row['id'] == $meeting_id){
224 $new_meeting_id = $row['id'];
225 $this->assertEmpty($row['parent_id'],'parent id of meeting should be empty as no parent was sent in ');
230 // 3. record should not be deleted from meetings_leads table
231 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
232 $result = $GLOBALS['db']->query($sql);
233 $row = $GLOBALS['db']->fetchByAssoc($result);
234 $this->assertNotNull($row, "Meeting-Lead relationship was removed.");
236 // 4. new meeting record should be added to meetings_contacts table
237 $sql = "select id from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id='{$contact->id}' and deleted=0";
238 $result = $GLOBALS['db']->query($sql);
239 $row = $GLOBALS['db']->fetchByAssoc($result);
240 $this->assertFalse(empty($row), "Meeting-Contact relationship has not been added.");
243 unset($_REQUEST['record']);
244 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts'");
245 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
246 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id= '{$contact->id}'");
247 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
248 SugarTestMeetingUtilities::removeMeetingContacts();
249 SugarTestMeetingUtilities::removeAllCreatedMeetings();
250 SugarTestContactUtilities::removeAllCreatedContacts();
251 SugarTestLeadUtilities::removeAllCreatedLeads();
255 * @outputBuffering enabled
257 public function testConversionAndMoveActivities() {
258 global $sugar_config;
261 $lead = SugarTestLeadUtilities::createLead();
262 $account = SugarTestAccountUtilities::createAccount();
263 $meeting = SugarTestMeetingUtilities::createMeeting();
264 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
265 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
266 $_REQUEST['record'] = $lead->id;
268 // set the request/post parameters before converting the lead
269 $_REQUEST['module'] = 'Leads';
270 $_REQUEST['action'] = 'ConvertLead';
271 $_REQUEST['record'] = $lead->id;
272 $_REQUEST['handle'] = 'save';
273 $_REQUEST['selectedAccount'] = $account->id;
274 $sugar_config['lead_conv_activity_opt'] = 'move';
275 $_POST['lead_conv_ac_op_sel'] = 'Contacts';
277 // call display to trigger conversion
278 $vc = new ViewConvertLead();
282 $meeting_id = $meeting->id;
283 $meeting = new Meeting();
284 $meeting->retrieve($meeting_id);
287 $lead_id = $lead->id;
289 $lead->retrieve($lead_id);
291 // retrieve the new contact id from the conversion
292 $contact_id = $lead->contact_id;
294 // 1. Lead's contact_id should not be null
295 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
297 // 2. Lead status should be 'Converted'
298 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
300 // 3. new parent_type should be Contacts
301 $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
303 // 4. new parent_id should be contact id
304 $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
306 // 5. record should be deleted from meetings_leads table
307 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
308 $result = $GLOBALS['db']->query($sql);
309 $row = $GLOBALS['db']->fetchByAssoc($result);
310 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
312 // 6. record should be added to meetings_contacts table
313 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
314 $result = $GLOBALS['db']->query($sql);
315 $row = $GLOBALS['db']->fetchByAssoc($result);
316 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
319 unset($_REQUEST['record']);
320 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
321 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
322 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
323 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
324 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
325 SugarTestMeetingUtilities::removeMeetingContacts();
326 SugarTestMeetingUtilities::removeAllCreatedMeetings();
327 SugarTestAccountUtilities::removeAllCreatedAccounts();
328 SugarTestLeadUtilities::removeAllCreatedLeads();
332 * @outputBuffering enabled
334 public function testConversionAndCopyActivities() {
335 global $sugar_config;
338 $lead = SugarTestLeadUtilities::createLead();
339 $account = SugarTestAccountUtilities::createAccount();
340 $meeting = SugarTestMeetingUtilities::createMeeting();
341 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
342 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
343 $_REQUEST['record'] = $lead->id;
345 // set the request/post parameters before converting the lead
346 $_REQUEST['module'] = 'Leads';
347 $_REQUEST['action'] = 'ConvertLead';
348 $_REQUEST['record'] = $lead->id;
349 $_REQUEST['handle'] = 'save';
350 $_REQUEST['selectedAccount'] = $account->id;
351 $sugar_config['lead_conv_activity_opt'] = 'copy';
352 $_POST['lead_conv_ac_op_sel'] = array('Contacts');
354 // call display to trigger conversion
355 $vc = new ViewConvertLead();
359 $meeting_id = $meeting->id;
360 $meeting = new Meeting();
361 $meeting->retrieve($meeting_id);
364 $lead_id = $lead->id;
366 $lead->retrieve($lead_id);
368 // retrieve the new contact id from the conversion
369 $contact_id = $lead->contact_id;
371 // 1. Lead's contact_id should not be null
372 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
374 // 2. Lead status should be 'Converted'
375 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
377 // 3. parent_type of the original meeting should be Leads
378 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
380 // 4. parent_id of the original meeting should be contact id
381 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
383 // 5. record should NOT be deleted from meetings_leads table
384 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
385 $result = $GLOBALS['db']->query($sql);
386 $row = $GLOBALS['db']->fetchByAssoc($result);
387 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
389 // 6. record should be added to meetings_contacts table
390 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
391 $result = $GLOBALS['db']->query($sql);
392 $row = $GLOBALS['db']->fetchByAssoc($result);
393 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
395 // 7. the parent_type of the new meeting should be empty
396 $new_meeting_id = $row['meeting_id'];
397 $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
398 $result = $GLOBALS['db']->query($sql);
399 $row = $GLOBALS['db']->fetchByAssoc($result);
400 $this->assertFalse(empty($row), "New meeting is not added for contact.");
401 $this->assertEmpty($row['parent_type'], 'Parent type of the new meeting should be Empty');
403 // 8. the parent_id of the new meeting should be contact id
404 $this->assertEmpty($row['parent_id'], 'Parent id of the new meeting should be empty.');
407 unset($_REQUEST['record']);
408 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
409 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
410 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
411 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
412 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
413 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
414 SugarTestMeetingUtilities::removeMeetingContacts();
415 SugarTestMeetingUtilities::removeAllCreatedMeetings();
416 SugarTestAccountUtilities::removeAllCreatedAccounts();
417 SugarTestLeadUtilities::removeAllCreatedLeads();
421 * @outputBuffering enabled
423 public function testConversionAndDoNothing() {
424 global $sugar_config;
427 $lead = SugarTestLeadUtilities::createLead();
428 $account = SugarTestAccountUtilities::createAccount();
429 $meeting = SugarTestMeetingUtilities::createMeeting();
430 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
431 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
432 $_REQUEST['record'] = $lead->id;
434 // set the request/post parameters before converting the lead
435 $_REQUEST['module'] = 'Leads';
436 $_REQUEST['action'] = 'ConvertLead';
437 $_REQUEST['record'] = $lead->id;
438 $_REQUEST['handle'] = 'save';
439 $_REQUEST['selectedAccount'] = $account->id;
440 $sugar_config['lead_conv_activity_opt'] = 'none';
442 // call display to trigger conversion
443 $vc = new ViewConvertLead();
447 $meeting_id = $meeting->id;
448 $meeting = new Meeting();
449 $meeting->retrieve($meeting_id);
452 $lead_id = $lead->id;
454 $lead->retrieve($lead_id);
456 // retrieve the new contact id from the conversion
457 $contact_id = $lead->contact_id;
459 // 1. Lead's contact_id should not be null
460 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
462 // 2. Lead status should be 'Converted'
463 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
465 // 3. parent_type of the original meeting should be Leads
466 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
468 // 4. parent_id of the original meeting should be contact id
469 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
471 // 5. record should NOT be deleted from meetings_leads table
472 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
473 $result = $GLOBALS['db']->query($sql);
474 $row = $GLOBALS['db']->fetchByAssoc($result);
475 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
477 // 6. record should NOT be added to meetings_contacts table
478 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
479 $result = $GLOBALS['db']->query($sql);
480 $row = $GLOBALS['db']->fetchByAssoc($result);
481 $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
484 unset($_REQUEST['record']);
485 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
486 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
487 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
488 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
489 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
490 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
491 SugarTestMeetingUtilities::removeMeetingContacts();
492 SugarTestMeetingUtilities::removeAllCreatedMeetings();
493 SugarTestAccountUtilities::removeAllCreatedAccounts();
494 SugarTestLeadUtilities::removeAllCreatedLeads();
497 public function testMeetingsUsersRelationships()
499 global $current_user;
501 $bean = SugarTestMeetingUtilities::createMeeting();
502 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
504 if ($bean->object_name == "Meeting")
506 $convert_lead->setMeetingsUsersRelationship($bean);
509 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
511 SugarTestMeetingUtilities::removeMeetingUsers();
512 SugarTestMeetingUtilities::removeAllCreatedMeetings();
516 class TestViewConvertLead extends ViewConvertLead
518 public function moveActivityWrapper($activity, $bean) {
519 parent::moveActivity($activity, $bean);
522 public function copyActivityWrapper($activity, $bean,$parent=array()) {
523 parent::copyActivityAndRelateToBean($activity, $bean,$parent);
526 public function testMeetingsUsersRelationships()
528 global $current_user;
530 $bean = SugarTestMeetingUtilities::createMeeting();
531 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
533 if ($bean->object_name == "Meeting")
535 $convert_lead->setMeetingsUsersRelationship($bean);
538 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
540 SugarTestMeetingUtilities::removeMeetingUsers();
541 SugarTestMeetingUtilities::removeAllCreatedMeetings();