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 '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()
61 $lead = SugarTestLeadUtilities::createLead();
62 $lead->opportunity_name = 'SBizzle Dollar Store';
65 $_REQUEST['module'] = 'Leads';
66 $_REQUEST['action'] = 'ConvertLead';
67 $_REQUEST['record'] = $lead->id;
69 // Check that the opportunity name doesn't get populated when it's not in the Leads editview layout
70 require_once('include/MVC/Controller/ControllerFactory.php');
71 require_once('include/MVC/View/ViewFactory.php');
72 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
74 $GLOBALS['app']->controller->execute();
75 $output = ob_get_clean();
77 $matches_one = array();
78 $pattern = '/SBizzle Dollar Store/';
79 preg_match($pattern, $output, $matches_one);
80 $this->assertTrue(count($matches_one) == 0, "Opportunity name got carried over to the Convert Leads page when it shouldn't have.");
82 // Add the opportunity_name to the Leads EditView
83 SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
85 // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
87 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
88 $GLOBALS['app']->controller->execute();
89 $output = ob_get_clean();
90 $matches_two = array();
91 $pattern = '/SBizzle Dollar Store/';
92 preg_match($pattern, $output, $matches_two);
93 $this->assertTrue(count($matches_two) > 0, "Opportunity name did not carry over to the Convert Leads page when it should have.");
95 SugarTestStudioUtilities::removeAllCreatedFields();
96 unset($GLOBALS['app']->controller);
97 unset($_REQUEST['module']);
98 unset($_REQUEST['action']);
99 unset($_REQUEST['record']);
100 SugarTestLeadUtilities::removeAllCreatedLeads();
106 public function testActivityMove() {
108 $lead = SugarTestLeadUtilities::createLead();
109 $contact = SugarTestContactUtilities::createContact();
110 $meeting = SugarTestMeetingUtilities::createMeeting();
111 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
112 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
113 $_REQUEST['record'] = $lead->id;
115 // refresh the meeting to include parent_id and parent_type
116 $meeting_id = $meeting->id;
117 $meeting = new Meeting();
118 $meeting->retrieve($meeting_id);
120 // action: move meeting from lead to contact
121 $convertObj = new TestViewConvertLead();
122 $convertObj->moveActivityWrapper($meeting, $contact);
124 // verification 1, parent id should be contact id
125 $this->assertTrue($meeting->parent_id == $contact->id, 'Meeting parent id is not converted to contact id.');
127 // verification 2, parent type should be "Contacts"
128 $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
130 // verification 3, record should be deleted from meetings_leads table
131 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
132 $result = $GLOBALS['db']->query($sql);
133 $row = $GLOBALS['db']->fetchByAssoc($result);
134 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
136 // verification 4, record should be added to meetings_contacts table
137 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact->id}' and deleted=0";
138 $result = $GLOBALS['db']->query($sql);
139 $row = $GLOBALS['db']->fetchByAssoc($result);
140 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
143 unset($_REQUEST['record']);
144 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact->id}'");
145 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
146 SugarTestMeetingUtilities::removeMeetingContacts();
147 SugarTestMeetingUtilities::removeAllCreatedMeetings();
148 SugarTestContactUtilities::removeAllCreatedContacts();
149 SugarTestLeadUtilities::removeAllCreatedLeads();
152 public function testActivityCopy() {
154 $lead = SugarTestLeadUtilities::createLead();
155 $contact = SugarTestContactUtilities::createContact();
156 $meeting = SugarTestMeetingUtilities::createMeeting();
157 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
158 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
159 $_REQUEST['record'] = $lead->id;
161 // refresh the meeting to include parent_id and parent_type
162 $meeting_id = $meeting->id;
163 $meeting = new Meeting();
164 $meeting->retrieve($meeting_id);
166 // action: copy meeting from lead to contact
167 $convertObj = new TestViewConvertLead();
168 $convertObj->copyActivityWrapper($meeting, $contact);
170 // 1. the original meeting should still have the same parent_type and parent_id
171 $meeting->retrieve($meeting_id);
172 $this->assertEquals('Leads', $meeting->parent_type, 'parent_type of the original meeting was changed from Leads to '.$meeting->parent_type);
173 $this->assertEquals($lead->id, $meeting->parent_id, 'parent_id of the original meeting was changed from '.$lead->id.' to '.$meeting->parent_id);
175 // 2. a newly created meeting with parent type=Contatcs and parent_id=$contact->id
176 $sql = "select id from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts' and deleted=0";
177 $result = $GLOBALS['db']->query($sql);
178 $row = $GLOBALS['db']->fetchByAssoc($result);
179 $this->assertNotNull($row, 'Could not find the newly created meeting with parent_type=Contacts and parent_id='.$contact->id);
180 $new_meeting_id = '';
182 $new_meeting_id = $row['id'];
185 // 3. record should not be deleted from meetings_leads table
186 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
187 $result = $GLOBALS['db']->query($sql);
188 $row = $GLOBALS['db']->fetchByAssoc($result);
189 $this->assertNotNull($row, "Meeting-Lead relationship was removed.");
191 // 4. new meeting record should be added to meetings_contacts table
192 $sql = "select id from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id='{$contact->id}' and deleted=0";
193 $result = $GLOBALS['db']->query($sql);
194 $row = $GLOBALS['db']->fetchByAssoc($result);
195 $this->assertFalse(empty($row), "Meeting-Contact relationship has not been added.");
198 unset($_REQUEST['record']);
199 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts'");
200 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
201 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id= '{$contact->id}'");
202 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
203 SugarTestMeetingUtilities::removeMeetingContacts();
204 SugarTestMeetingUtilities::removeAllCreatedMeetings();
205 SugarTestContactUtilities::removeAllCreatedContacts();
206 SugarTestLeadUtilities::removeAllCreatedLeads();
209 public function testConversionAndMoveActivities() {
210 global $sugar_config;
213 $lead = SugarTestLeadUtilities::createLead();
214 $account = SugarTestAccountUtilities::createAccount();
215 $meeting = SugarTestMeetingUtilities::createMeeting();
216 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
217 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
218 $_REQUEST['record'] = $lead->id;
220 // set the request/post parameters before converting the lead
221 $_REQUEST['module'] = 'Leads';
222 $_REQUEST['action'] = 'ConvertLead';
223 $_REQUEST['record'] = $lead->id;
224 $_REQUEST['handle'] = 'save';
225 $_REQUEST['selectedAccount'] = $account->id;
226 $sugar_config['lead_conv_activity_opt'] = 'move';
227 $_POST['lead_conv_ac_op_sel'] = 'Contacts';
229 // call display to trigger conversion
230 $vc = new ViewConvertLead();
234 $meeting_id = $meeting->id;
235 $meeting = new Meeting();
236 $meeting->retrieve($meeting_id);
239 $lead_id = $lead->id;
241 $lead->retrieve($lead_id);
243 // retrieve the new contact id from the conversion
244 $contact_id = $lead->contact_id;
246 // 1. Lead's contact_id should not be null
247 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
249 // 2. Lead status should be 'Converted'
250 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
252 // 3. new parent_type should be Contacts
253 $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
255 // 4. new parent_id should be contact id
256 $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
258 // 5. record should be deleted from meetings_leads table
259 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
260 $result = $GLOBALS['db']->query($sql);
261 $row = $GLOBALS['db']->fetchByAssoc($result);
262 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
264 // 6. record should be added to meetings_contacts table
265 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
266 $result = $GLOBALS['db']->query($sql);
267 $row = $GLOBALS['db']->fetchByAssoc($result);
268 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
271 unset($_REQUEST['record']);
272 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
273 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
274 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
275 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
276 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
277 SugarTestMeetingUtilities::removeMeetingContacts();
278 SugarTestMeetingUtilities::removeAllCreatedMeetings();
279 SugarTestAccountUtilities::removeAllCreatedAccounts();
280 SugarTestLeadUtilities::removeAllCreatedLeads();
283 public function testConversionAndCopyActivities() {
284 global $sugar_config;
287 $lead = SugarTestLeadUtilities::createLead();
288 $account = SugarTestAccountUtilities::createAccount();
289 $meeting = SugarTestMeetingUtilities::createMeeting();
290 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
291 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
292 $_REQUEST['record'] = $lead->id;
294 // set the request/post parameters before converting the lead
295 $_REQUEST['module'] = 'Leads';
296 $_REQUEST['action'] = 'ConvertLead';
297 $_REQUEST['record'] = $lead->id;
298 $_REQUEST['handle'] = 'save';
299 $_REQUEST['selectedAccount'] = $account->id;
300 $sugar_config['lead_conv_activity_opt'] = 'copy';
301 $_POST['lead_conv_ac_op_sel'] = array('Contacts');
303 // call display to trigger conversion
304 $vc = new ViewConvertLead();
308 $meeting_id = $meeting->id;
309 $meeting = new Meeting();
310 $meeting->retrieve($meeting_id);
313 $lead_id = $lead->id;
315 $lead->retrieve($lead_id);
317 // retrieve the new contact id from the conversion
318 $contact_id = $lead->contact_id;
320 // 1. Lead's contact_id should not be null
321 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
323 // 2. Lead status should be 'Converted'
324 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
326 // 3. parent_type of the original meeting should be Leads
327 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
329 // 4. parent_id of the original meeting should be contact id
330 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
332 // 5. record should NOT be deleted from meetings_leads table
333 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
334 $result = $GLOBALS['db']->query($sql);
335 $row = $GLOBALS['db']->fetchByAssoc($result);
336 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
338 // 6. record should be added to meetings_contacts table
339 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
340 $result = $GLOBALS['db']->query($sql);
341 $row = $GLOBALS['db']->fetchByAssoc($result);
342 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
344 // 7. the parent_type of the new meeting should be Contacts
345 $new_meeting_id = $row['meeting_id'];
346 $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
347 $result = $GLOBALS['db']->query($sql);
348 $row = $GLOBALS['db']->fetchByAssoc($result);
349 $this->assertFalse(empty($row), "New meeting is not added for contact.");
350 $this->assertEquals('Contacts', $row['parent_type'], 'Parent type of the new meeting should be Contacts');
352 // 8. the parent_id of the new meeting should be contact id
353 $this->assertEquals($contact_id, $row['parent_id'], 'Parent id of the new meeting should be contact id.');
356 unset($_REQUEST['record']);
357 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
358 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
359 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
360 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
361 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
362 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
363 SugarTestMeetingUtilities::removeMeetingContacts();
364 SugarTestMeetingUtilities::removeAllCreatedMeetings();
365 SugarTestAccountUtilities::removeAllCreatedAccounts();
366 SugarTestLeadUtilities::removeAllCreatedLeads();
369 public function testConversionAndDoNothing() {
370 global $sugar_config;
373 $lead = SugarTestLeadUtilities::createLead();
374 $account = SugarTestAccountUtilities::createAccount();
375 $meeting = SugarTestMeetingUtilities::createMeeting();
376 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
377 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
378 $_REQUEST['record'] = $lead->id;
380 // set the request/post parameters before converting the lead
381 $_REQUEST['module'] = 'Leads';
382 $_REQUEST['action'] = 'ConvertLead';
383 $_REQUEST['record'] = $lead->id;
384 $_REQUEST['handle'] = 'save';
385 $_REQUEST['selectedAccount'] = $account->id;
386 $sugar_config['lead_conv_activity_opt'] = 'none';
388 // call display to trigger conversion
389 $vc = new ViewConvertLead();
393 $meeting_id = $meeting->id;
394 $meeting = new Meeting();
395 $meeting->retrieve($meeting_id);
398 $lead_id = $lead->id;
400 $lead->retrieve($lead_id);
402 // retrieve the new contact id from the conversion
403 $contact_id = $lead->contact_id;
405 // 1. Lead's contact_id should not be null
406 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
408 // 2. Lead status should be 'Converted'
409 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
411 // 3. parent_type of the original meeting should be Leads
412 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
414 // 4. parent_id of the original meeting should be contact id
415 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
417 // 5. record should NOT be deleted from meetings_leads table
418 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
419 $result = $GLOBALS['db']->query($sql);
420 $row = $GLOBALS['db']->fetchByAssoc($result);
421 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
423 // 6. record should NOT be added to meetings_contacts table
424 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
425 $result = $GLOBALS['db']->query($sql);
426 $row = $GLOBALS['db']->fetchByAssoc($result);
427 $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
430 unset($_REQUEST['record']);
431 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
432 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
433 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
434 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
435 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
436 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
437 SugarTestMeetingUtilities::removeMeetingContacts();
438 SugarTestMeetingUtilities::removeAllCreatedMeetings();
439 SugarTestAccountUtilities::removeAllCreatedAccounts();
440 SugarTestLeadUtilities::removeAllCreatedLeads();
443 public function testMeetingsUsersRelationships()
445 global $current_user;
447 $bean = SugarTestMeetingUtilities::createMeeting();
448 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
450 if ($bean->object_name == "Meeting")
452 $convert_lead->setMeetingsUsersRelationship($bean);
455 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
457 SugarTestMeetingUtilities::removeMeetingUsers();
458 SugarTestMeetingUtilities::removeAllCreatedMeetings();
462 class TestViewConvertLead extends ViewConvertLead
464 public function moveActivityWrapper($activity, $bean) {
465 parent::moveActivity($activity, $bean);
468 public function copyActivityWrapper($activity, $bean) {
469 parent::copyActivityAndRelateToBean($activity, $bean);
472 public function testMeetingsUsersRelationships()
474 global $current_user;
476 $bean = SugarTestMeetingUtilities::createMeeting();
477 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
479 if ($bean->object_name == "Meeting")
481 $convert_lead->setMeetingsUsersRelationship($bean);
484 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
486 SugarTestMeetingUtilities::removeMeetingUsers();
487 SugarTestMeetingUtilities::removeAllCreatedMeetings();