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 ConvertLeadTests 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();
254 public function testConversionAndMoveActivities() {
255 global $sugar_config;
258 $lead = SugarTestLeadUtilities::createLead();
259 $account = SugarTestAccountUtilities::createAccount();
260 $meeting = SugarTestMeetingUtilities::createMeeting();
261 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
262 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
263 $_REQUEST['record'] = $lead->id;
265 // set the request/post parameters before converting the lead
266 $_REQUEST['module'] = 'Leads';
267 $_REQUEST['action'] = 'ConvertLead';
268 $_REQUEST['record'] = $lead->id;
269 $_REQUEST['handle'] = 'save';
270 $_REQUEST['selectedAccount'] = $account->id;
271 $sugar_config['lead_conv_activity_opt'] = 'move';
272 $_POST['lead_conv_ac_op_sel'] = 'Contacts';
274 // call display to trigger conversion
275 $vc = new ViewConvertLead();
279 $meeting_id = $meeting->id;
280 $meeting = new Meeting();
281 $meeting->retrieve($meeting_id);
284 $lead_id = $lead->id;
286 $lead->retrieve($lead_id);
288 // retrieve the new contact id from the conversion
289 $contact_id = $lead->contact_id;
291 // 1. Lead's contact_id should not be null
292 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
294 // 2. Lead status should be 'Converted'
295 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
297 // 3. new parent_type should be Contacts
298 $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
300 // 4. new parent_id should be contact id
301 $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
303 // 5. record should be deleted from meetings_leads table
304 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
305 $result = $GLOBALS['db']->query($sql);
306 $row = $GLOBALS['db']->fetchByAssoc($result);
307 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
309 // 6. record should be added to meetings_contacts table
310 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
311 $result = $GLOBALS['db']->query($sql);
312 $row = $GLOBALS['db']->fetchByAssoc($result);
313 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
316 unset($_REQUEST['record']);
317 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
318 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
319 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
320 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
321 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
322 SugarTestMeetingUtilities::removeMeetingContacts();
323 SugarTestMeetingUtilities::removeAllCreatedMeetings();
324 SugarTestAccountUtilities::removeAllCreatedAccounts();
325 SugarTestLeadUtilities::removeAllCreatedLeads();
328 public function testConversionAndCopyActivities() {
329 global $sugar_config;
332 $lead = SugarTestLeadUtilities::createLead();
333 $account = SugarTestAccountUtilities::createAccount();
334 $meeting = SugarTestMeetingUtilities::createMeeting();
335 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
336 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
337 $_REQUEST['record'] = $lead->id;
339 // set the request/post parameters before converting the lead
340 $_REQUEST['module'] = 'Leads';
341 $_REQUEST['action'] = 'ConvertLead';
342 $_REQUEST['record'] = $lead->id;
343 $_REQUEST['handle'] = 'save';
344 $_REQUEST['selectedAccount'] = $account->id;
345 $sugar_config['lead_conv_activity_opt'] = 'copy';
346 $_POST['lead_conv_ac_op_sel'] = array('Contacts');
348 // call display to trigger conversion
349 $vc = new ViewConvertLead();
353 $meeting_id = $meeting->id;
354 $meeting = new Meeting();
355 $meeting->retrieve($meeting_id);
358 $lead_id = $lead->id;
360 $lead->retrieve($lead_id);
362 // retrieve the new contact id from the conversion
363 $contact_id = $lead->contact_id;
365 // 1. Lead's contact_id should not be null
366 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
368 // 2. Lead status should be 'Converted'
369 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
371 // 3. parent_type of the original meeting should be Leads
372 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
374 // 4. parent_id of the original meeting should be contact id
375 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
377 // 5. record should NOT be deleted from meetings_leads table
378 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
379 $result = $GLOBALS['db']->query($sql);
380 $row = $GLOBALS['db']->fetchByAssoc($result);
381 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
383 // 6. record should be added to meetings_contacts table
384 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
385 $result = $GLOBALS['db']->query($sql);
386 $row = $GLOBALS['db']->fetchByAssoc($result);
387 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
389 // 7. the parent_type of the new meeting should be empty
390 $new_meeting_id = $row['meeting_id'];
391 $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
392 $result = $GLOBALS['db']->query($sql);
393 $row = $GLOBALS['db']->fetchByAssoc($result);
394 $this->assertFalse(empty($row), "New meeting is not added for contact.");
395 $this->assertEmpty($row['parent_type'], 'Parent type of the new meeting should be Empty');
397 // 8. the parent_id of the new meeting should be contact id
398 $this->assertEmpty($row['parent_id'], 'Parent id of the new meeting should be empty.');
401 unset($_REQUEST['record']);
402 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
403 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
404 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
405 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
406 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
407 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
408 SugarTestMeetingUtilities::removeMeetingContacts();
409 SugarTestMeetingUtilities::removeAllCreatedMeetings();
410 SugarTestAccountUtilities::removeAllCreatedAccounts();
411 SugarTestLeadUtilities::removeAllCreatedLeads();
414 public function testConversionAndDoNothing() {
415 global $sugar_config;
418 $lead = SugarTestLeadUtilities::createLead();
419 $account = SugarTestAccountUtilities::createAccount();
420 $meeting = SugarTestMeetingUtilities::createMeeting();
421 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
422 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
423 $_REQUEST['record'] = $lead->id;
425 // set the request/post parameters before converting the lead
426 $_REQUEST['module'] = 'Leads';
427 $_REQUEST['action'] = 'ConvertLead';
428 $_REQUEST['record'] = $lead->id;
429 $_REQUEST['handle'] = 'save';
430 $_REQUEST['selectedAccount'] = $account->id;
431 $sugar_config['lead_conv_activity_opt'] = 'none';
433 // call display to trigger conversion
434 $vc = new ViewConvertLead();
438 $meeting_id = $meeting->id;
439 $meeting = new Meeting();
440 $meeting->retrieve($meeting_id);
443 $lead_id = $lead->id;
445 $lead->retrieve($lead_id);
447 // retrieve the new contact id from the conversion
448 $contact_id = $lead->contact_id;
450 // 1. Lead's contact_id should not be null
451 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
453 // 2. Lead status should be 'Converted'
454 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
456 // 3. parent_type of the original meeting should be Leads
457 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
459 // 4. parent_id of the original meeting should be contact id
460 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
462 // 5. record should NOT be deleted from meetings_leads table
463 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
464 $result = $GLOBALS['db']->query($sql);
465 $row = $GLOBALS['db']->fetchByAssoc($result);
466 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
468 // 6. record should NOT be added to meetings_contacts table
469 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
470 $result = $GLOBALS['db']->query($sql);
471 $row = $GLOBALS['db']->fetchByAssoc($result);
472 $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
475 unset($_REQUEST['record']);
476 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
477 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
478 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
479 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
480 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
481 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
482 SugarTestMeetingUtilities::removeMeetingContacts();
483 SugarTestMeetingUtilities::removeAllCreatedMeetings();
484 SugarTestAccountUtilities::removeAllCreatedAccounts();
485 SugarTestLeadUtilities::removeAllCreatedLeads();
488 public function testMeetingsUsersRelationships()
490 global $current_user;
492 $bean = SugarTestMeetingUtilities::createMeeting();
493 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
495 if ($bean->object_name == "Meeting")
497 $convert_lead->setMeetingsUsersRelationship($bean);
500 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
502 SugarTestMeetingUtilities::removeMeetingUsers();
503 SugarTestMeetingUtilities::removeAllCreatedMeetings();
507 class TestViewConvertLead extends ViewConvertLead
509 public function moveActivityWrapper($activity, $bean) {
510 parent::moveActivity($activity, $bean);
513 public function copyActivityWrapper($activity, $bean,$parent=array()) {
514 parent::copyActivityAndRelateToBean($activity, $bean,$parent);
517 public function testMeetingsUsersRelationships()
519 global $current_user;
521 $bean = SugarTestMeetingUtilities::createMeeting();
522 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
524 if ($bean->object_name == "Meeting")
526 $convert_lead->setMeetingsUsersRelationship($bean);
529 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
531 SugarTestMeetingUtilities::removeMeetingUsers();
532 SugarTestMeetingUtilities::removeAllCreatedMeetings();