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';
39 require_once 'tests/SugarTestViewConvertLeadUtilities.php';
42 class ConvertLeadTests extends Sugar_PHPUnit_Framework_TestCase
44 public function setUp()
46 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
50 public function tearDown()
52 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53 unset($GLOBALS['app_list_strings']);
54 unset($GLOBALS['current_user']);
60 public function testOpportunityNameValueFilled()
62 $lead = SugarTestLeadUtilities::createLead();
63 $lead->opportunity_name = 'SBizzle Dollar Store';
66 $_REQUEST['module'] = 'Leads';
67 $_REQUEST['action'] = 'ConvertLead';
68 $_REQUEST['record'] = $lead->id;
70 // Check that the opportunity name doesn't get populated when it's not in the Leads editview layout
71 require_once('include/MVC/Controller/ControllerFactory.php');
72 require_once('include/MVC/View/ViewFactory.php');
73 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
75 $GLOBALS['app']->controller->execute();
76 $output = ob_get_clean();
78 $matches_one = array();
79 $pattern = '/SBizzle Dollar Store/';
80 preg_match($pattern, $output, $matches_one);
81 $this->assertTrue(count($matches_one) == 0, "Opportunity name got carried over to the Convert Leads page when it shouldn't have.");
83 // Add the opportunity_name to the Leads EditView
84 SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
86 // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
88 $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
89 $GLOBALS['app']->controller->execute();
90 $output = ob_get_clean();
91 $matches_two = array();
92 $pattern = '/SBizzle Dollar Store/';
93 preg_match($pattern, $output, $matches_two);
94 $this->assertTrue(count($matches_two) > 0, "Opportunity name did not carry over to the Convert Leads page when it should have.");
96 SugarTestStudioUtilities::removeAllCreatedFields();
97 unset($GLOBALS['app']->controller);
98 unset($_REQUEST['module']);
99 unset($_REQUEST['action']);
100 unset($_REQUEST['record']);
101 SugarTestLeadUtilities::removeAllCreatedLeads();
107 public function testActivityMove() {
109 $lead = SugarTestLeadUtilities::createLead();
110 $contact = SugarTestContactUtilities::createContact();
111 $meeting = SugarTestMeetingUtilities::createMeeting();
112 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
113 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
114 $_REQUEST['record'] = $lead->id;
116 // refresh the meeting to include parent_id and parent_type
117 $meeting_id = $meeting->id;
118 $meeting = new Meeting();
119 $meeting->retrieve($meeting_id);
121 // action: move meeting from lead to contact
122 $convertObj = new TestViewConvertLead();
123 $convertObj->moveActivityWrapper($meeting, $contact);
125 // verification 1, parent id should be contact id
126 $this->assertTrue($meeting->parent_id == $contact->id, 'Meeting parent id is not converted to contact id.');
128 // verification 2, parent type should be "Contacts"
129 $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
131 // verification 3, record should be deleted from meetings_leads table
132 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
133 $result = $GLOBALS['db']->query($sql);
134 $row = $GLOBALS['db']->fetchByAssoc($result);
135 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
137 // verification 4, record should be added to meetings_contacts table
138 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact->id}' and deleted=0";
139 $result = $GLOBALS['db']->query($sql);
140 $row = $GLOBALS['db']->fetchByAssoc($result);
141 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
144 unset($_REQUEST['record']);
145 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact->id}'");
146 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
147 SugarTestMeetingUtilities::removeMeetingContacts();
148 SugarTestMeetingUtilities::removeAllCreatedMeetings();
149 SugarTestContactUtilities::removeAllCreatedContacts();
150 SugarTestLeadUtilities::removeAllCreatedLeads();
153 public function testActivityCopy() {
155 $lead = SugarTestLeadUtilities::createLead();
156 $contact = SugarTestContactUtilities::createContact();
157 $meeting = SugarTestMeetingUtilities::createMeeting();
158 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
159 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
160 $_REQUEST['record'] = $lead->id;
162 // refresh the meeting to include parent_id and parent_type
163 $meeting_id = $meeting->id;
164 $meeting = new Meeting();
165 $meeting->retrieve($meeting_id);
167 // action: copy meeting from lead to contact
168 $convertObj = new TestViewConvertLead();
169 $convertObj->copyActivityWrapper($meeting, $contact);
171 // 1. the original meeting should still have the same parent_type and parent_id
172 $meeting->retrieve($meeting_id);
173 $this->assertEquals('Leads', $meeting->parent_type, 'parent_type of the original meeting was changed from Leads to '.$meeting->parent_type);
174 $this->assertEquals($lead->id, $meeting->parent_id, 'parent_id of the original meeting was changed from '.$lead->id.' to '.$meeting->parent_id);
176 // 2. a newly created meeting with 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 $result = $GLOBALS['db']->query($sql);
179 $row = $GLOBALS['db']->fetchByAssoc($result);
180 $this->assertNotNull($row, 'Could not find the newly created meeting with parent_type=Contacts and parent_id='.$contact->id);
181 $new_meeting_id = '';
183 $new_meeting_id = $row['id'];
186 // 3. record should not be deleted from meetings_leads table
187 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
188 $result = $GLOBALS['db']->query($sql);
189 $row = $GLOBALS['db']->fetchByAssoc($result);
190 $this->assertNotNull($row, "Meeting-Lead relationship was removed.");
192 // 4. new meeting record should be added to meetings_contacts table
193 $sql = "select id from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id='{$contact->id}' and deleted=0";
194 $result = $GLOBALS['db']->query($sql);
195 $row = $GLOBALS['db']->fetchByAssoc($result);
196 $this->assertFalse(empty($row), "Meeting-Contact relationship has not been added.");
199 unset($_REQUEST['record']);
200 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts'");
201 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
202 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id= '{$contact->id}'");
203 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
204 SugarTestMeetingUtilities::removeMeetingContacts();
205 SugarTestMeetingUtilities::removeAllCreatedMeetings();
206 SugarTestContactUtilities::removeAllCreatedContacts();
207 SugarTestLeadUtilities::removeAllCreatedLeads();
210 public function testConversionAndMoveActivities() {
211 global $sugar_config;
214 $lead = SugarTestLeadUtilities::createLead();
215 $account = SugarTestAccountUtilities::createAccount();
216 $meeting = SugarTestMeetingUtilities::createMeeting();
217 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
218 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
219 $_REQUEST['record'] = $lead->id;
221 // set the request/post parameters before converting the lead
222 $_REQUEST['module'] = 'Leads';
223 $_REQUEST['action'] = 'ConvertLead';
224 $_REQUEST['record'] = $lead->id;
225 $_REQUEST['handle'] = 'save';
226 $_REQUEST['selectedAccount'] = $account->id;
227 $sugar_config['lead_conv_activity_opt'] = 'move';
228 $_POST['lead_conv_ac_op_sel'] = 'Contacts';
230 // call display to trigger conversion
231 $vc = new ViewConvertLead();
235 $meeting_id = $meeting->id;
236 $meeting = new Meeting();
237 $meeting->retrieve($meeting_id);
240 $lead_id = $lead->id;
242 $lead->retrieve($lead_id);
244 // retrieve the new contact id from the conversion
245 $contact_id = $lead->contact_id;
247 // 1. Lead's contact_id should not be null
248 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
250 // 2. Lead status should be 'Converted'
251 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
253 // 3. new parent_type should be Contacts
254 $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
256 // 4. new parent_id should be contact id
257 $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
259 // 5. record should be deleted from meetings_leads table
260 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
261 $result = $GLOBALS['db']->query($sql);
262 $row = $GLOBALS['db']->fetchByAssoc($result);
263 $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
265 // 6. record should be added to meetings_contacts table
266 $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
267 $result = $GLOBALS['db']->query($sql);
268 $row = $GLOBALS['db']->fetchByAssoc($result);
269 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
272 unset($_REQUEST['record']);
273 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
274 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
275 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
276 $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
277 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
278 SugarTestMeetingUtilities::removeMeetingContacts();
279 SugarTestMeetingUtilities::removeAllCreatedMeetings();
280 SugarTestAccountUtilities::removeAllCreatedAccounts();
281 SugarTestLeadUtilities::removeAllCreatedLeads();
284 public function testConversionAndCopyActivities() {
285 global $sugar_config;
288 $lead = SugarTestLeadUtilities::createLead();
289 $account = SugarTestAccountUtilities::createAccount();
290 $meeting = SugarTestMeetingUtilities::createMeeting();
291 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
292 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
293 $_REQUEST['record'] = $lead->id;
295 // set the request/post parameters before converting the lead
296 $_REQUEST['module'] = 'Leads';
297 $_REQUEST['action'] = 'ConvertLead';
298 $_REQUEST['record'] = $lead->id;
299 $_REQUEST['handle'] = 'save';
300 $_REQUEST['selectedAccount'] = $account->id;
301 $sugar_config['lead_conv_activity_opt'] = 'copy';
302 $_POST['lead_conv_ac_op_sel'] = array('Contacts');
304 // call display to trigger conversion
305 $vc = new ViewConvertLead();
309 $meeting_id = $meeting->id;
310 $meeting = new Meeting();
311 $meeting->retrieve($meeting_id);
314 $lead_id = $lead->id;
316 $lead->retrieve($lead_id);
318 // retrieve the new contact id from the conversion
319 $contact_id = $lead->contact_id;
321 // 1. Lead's contact_id should not be null
322 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
324 // 2. Lead status should be 'Converted'
325 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
327 // 3. parent_type of the original meeting should be Leads
328 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
330 // 4. parent_id of the original meeting should be contact id
331 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
333 // 5. record should NOT be deleted from meetings_leads table
334 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
335 $result = $GLOBALS['db']->query($sql);
336 $row = $GLOBALS['db']->fetchByAssoc($result);
337 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
339 // 6. record should be added to meetings_contacts table
340 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
341 $result = $GLOBALS['db']->query($sql);
342 $row = $GLOBALS['db']->fetchByAssoc($result);
343 $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
345 // 7. the parent_type of the new meeting should be Contacts
346 $new_meeting_id = $row['meeting_id'];
347 $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
348 $result = $GLOBALS['db']->query($sql);
349 $row = $GLOBALS['db']->fetchByAssoc($result);
350 $this->assertFalse(empty($row), "New meeting is not added for contact.");
351 $this->assertEquals('Contacts', $row['parent_type'], 'Parent type of the new meeting should be Contacts');
353 // 8. the parent_id of the new meeting should be contact id
354 $this->assertEquals($contact_id, $row['parent_id'], 'Parent id of the new meeting should be contact id.');
357 unset($_REQUEST['record']);
358 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
359 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
360 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
361 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
362 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
363 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
364 SugarTestMeetingUtilities::removeMeetingContacts();
365 SugarTestMeetingUtilities::removeAllCreatedMeetings();
366 SugarTestAccountUtilities::removeAllCreatedAccounts();
367 SugarTestLeadUtilities::removeAllCreatedLeads();
370 public function testConversionAndDoNothing() {
371 global $sugar_config;
374 $lead = SugarTestLeadUtilities::createLead();
375 $account = SugarTestAccountUtilities::createAccount();
376 $meeting = SugarTestMeetingUtilities::createMeeting();
377 SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
378 $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
379 $_REQUEST['record'] = $lead->id;
381 // set the request/post parameters before converting the lead
382 $_REQUEST['module'] = 'Leads';
383 $_REQUEST['action'] = 'ConvertLead';
384 $_REQUEST['record'] = $lead->id;
385 $_REQUEST['handle'] = 'save';
386 $_REQUEST['selectedAccount'] = $account->id;
387 $sugar_config['lead_conv_activity_opt'] = 'none';
389 // call display to trigger conversion
390 $vc = new ViewConvertLead();
394 $meeting_id = $meeting->id;
395 $meeting = new Meeting();
396 $meeting->retrieve($meeting_id);
399 $lead_id = $lead->id;
401 $lead->retrieve($lead_id);
403 // retrieve the new contact id from the conversion
404 $contact_id = $lead->contact_id;
406 // 1. Lead's contact_id should not be null
407 $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
409 // 2. Lead status should be 'Converted'
410 $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
412 // 3. parent_type of the original meeting should be Leads
413 $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
415 // 4. parent_id of the original meeting should be contact id
416 $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
418 // 5. record should NOT be deleted from meetings_leads table
419 $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
420 $result = $GLOBALS['db']->query($sql);
421 $row = $GLOBALS['db']->fetchByAssoc($result);
422 $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
424 // 6. record should NOT be added to meetings_contacts table
425 $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
426 $result = $GLOBALS['db']->query($sql);
427 $row = $GLOBALS['db']->fetchByAssoc($result);
428 $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
431 unset($_REQUEST['record']);
432 $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
433 $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
434 $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
435 $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
436 $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
437 SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
438 SugarTestMeetingUtilities::removeMeetingContacts();
439 SugarTestMeetingUtilities::removeAllCreatedMeetings();
440 SugarTestAccountUtilities::removeAllCreatedAccounts();
441 SugarTestLeadUtilities::removeAllCreatedLeads();
444 public function testMeetingsUsersRelationships()
446 global $current_user;
448 $bean = SugarTestMeetingUtilities::createMeeting();
449 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
451 if ($bean->object_name == "Meeting")
453 $convert_lead->setMeetingsUsersRelationship($bean);
456 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
458 SugarTestMeetingUtilities::removeMeetingUsers();
459 SugarTestMeetingUtilities::removeAllCreatedMeetings();
463 class TestViewConvertLead extends ViewConvertLead
465 public function moveActivityWrapper($activity, $bean) {
466 parent::moveActivity($activity, $bean);
469 public function copyActivityWrapper($activity, $bean) {
470 parent::copyActivityAndRelateToBean($activity, $bean);
473 public function testMeetingsUsersRelationships()
475 global $current_user;
477 $bean = SugarTestMeetingUtilities::createMeeting();
478 $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
480 if ($bean->object_name == "Meeting")
482 $convert_lead->setMeetingsUsersRelationship($bean);
485 $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
487 SugarTestMeetingUtilities::removeMeetingUsers();
488 SugarTestMeetingUtilities::removeAllCreatedMeetings();