]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Leads/ConvertLeadTests.php
Release 6.4.1
[Github/sugarcrm.git] / tests / modules / Leads / ConvertLeadTests.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38 require_once 'modules/Leads/views/view.convertlead.php';
39
40
41 class ConvertLeadTests extends Sugar_PHPUnit_Framework_TestCase
42 {
43     public function setUp()
44     {
45         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
46         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
47     }
48     
49     public function tearDown()
50     {
51         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
52         unset($GLOBALS['app_list_strings']);
53         unset($GLOBALS['current_user']);
54     }
55     
56     /**
57     * @group bug39787
58     */
59     public function testOpportunityNameValueFilled()
60     {
61         $lead = SugarTestLeadUtilities::createLead();
62         $lead->opportunity_name = 'SBizzle Dollar Store';
63         $lead->save();
64         
65         $_REQUEST['module'] = 'Leads';
66         $_REQUEST['action'] = 'ConvertLead';
67         $_REQUEST['record'] = $lead->id;
68         
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']);
73         ob_start();
74         $GLOBALS['app']->controller->execute();
75         $output = ob_get_clean();
76         
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.");
81
82         // Add the opportunity_name to the Leads EditView
83         SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
84         
85         // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
86         ob_start();
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.");
94         
95         SugarTestStudioUtilities::removeAllCreatedFields();
96         unset($GLOBALS['app']->controller);
97         unset($_REQUEST['module']);
98         unset($_REQUEST['action']);
99         unset($_REQUEST['record']);
100         SugarTestLeadUtilities::removeAllCreatedLeads();
101     }
102
103     /**
104      * @group bug44033
105      */
106     public function testActivityMove() {
107         // init
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;
114
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);
119
120         // action: move meeting from lead to contact
121         $convertObj = new TestViewConvertLead();
122         $convertObj->moveActivityWrapper($meeting, $contact);
123
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.');
126
127         // verification 2, parent type should be "Contacts"
128         $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
129
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.");
135
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.");
141
142         // clean up
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();
150     }
151
152     public function testActivityCopy() {
153         // init
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;
160
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);
165
166         // action: copy meeting from lead to contact
167         $convertObj = new TestViewConvertLead();
168         $convertObj->copyActivityWrapper($meeting, $contact);
169
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);
174
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 = '';
181         if ($row) {
182             $new_meeting_id = $row['id'];
183         }
184
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.");
190
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.");
196
197         // clean up
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();
207     }
208
209     public function testConversionAndMoveActivities() {
210         global $sugar_config;
211
212         // init
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;
219
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';
228
229         // call display to trigger conversion
230         $vc = new ViewConvertLead();
231         $vc->display();
232
233         // refresh meeting
234         $meeting_id = $meeting->id;
235         $meeting = new Meeting();
236         $meeting->retrieve($meeting_id);
237
238         // refresh lead
239         $lead_id = $lead->id;
240         $lead = new Lead();
241         $lead->retrieve($lead_id);
242
243         // retrieve the new contact id from the conversion
244         $contact_id = $lead->contact_id;
245
246         // 1. Lead's contact_id should not be null
247         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
248
249         // 2. Lead status should be 'Converted'
250         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
251
252         // 3. new parent_type should be Contacts
253         $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
254
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.');
257
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.");
263
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.");
269
270         // clean up
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();
281     }
282
283     public function testConversionAndCopyActivities() {
284         global $sugar_config;
285
286         // init
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;
293
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');
302
303         // call display to trigger conversion
304         $vc = new ViewConvertLead();
305         $vc->display();
306
307         // refresh meeting
308         $meeting_id = $meeting->id;
309         $meeting = new Meeting();
310         $meeting->retrieve($meeting_id);
311
312         // refresh lead
313         $lead_id = $lead->id;
314         $lead = new Lead();
315         $lead->retrieve($lead_id);
316
317         // retrieve the new contact id from the conversion
318         $contact_id = $lead->contact_id;
319
320         // 1. Lead's contact_id should not be null
321         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
322
323         // 2. Lead status should be 'Converted'
324         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
325
326         // 3. parent_type of the original meeting should be Leads
327         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
328
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.');
331
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.");
337
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.");
343
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');
351
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.');
354
355         // clean up
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();
367     }
368
369     public function testConversionAndDoNothing() {
370         global $sugar_config;
371
372         // init
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;
379
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';
387
388         // call display to trigger conversion
389         $vc = new ViewConvertLead();
390         $vc->display();
391
392         // refresh meeting
393         $meeting_id = $meeting->id;
394         $meeting = new Meeting();
395         $meeting->retrieve($meeting_id);
396
397         // refresh lead
398         $lead_id = $lead->id;
399         $lead = new Lead();
400         $lead->retrieve($lead_id);
401
402         // retrieve the new contact id from the conversion
403         $contact_id = $lead->contact_id;
404
405         // 1. Lead's contact_id should not be null
406         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
407
408         // 2. Lead status should be 'Converted'
409         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
410
411         // 3. parent_type of the original meeting should be Leads
412         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
413
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.');
416
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.");
422
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.");
428
429         // clean up
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();
441     }
442
443     public function testMeetingsUsersRelationships()
444     {
445         global $current_user;
446
447         $bean = SugarTestMeetingUtilities::createMeeting();
448         $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
449
450         if ($bean->object_name == "Meeting")
451         {
452             $convert_lead->setMeetingsUsersRelationship($bean);
453         }
454
455         $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
456
457         SugarTestMeetingUtilities::removeMeetingUsers();
458         SugarTestMeetingUtilities::removeAllCreatedMeetings();
459     }
460 }
461
462 class TestViewConvertLead extends ViewConvertLead
463 {
464     public function moveActivityWrapper($activity, $bean) {
465         parent::moveActivity($activity, $bean);
466     }
467
468     public function copyActivityWrapper($activity, $bean) {
469         parent::copyActivityAndRelateToBean($activity, $bean);
470     }
471
472     public function testMeetingsUsersRelationships()
473     {
474         global $current_user;
475         
476         $bean = SugarTestMeetingUtilities::createMeeting();
477         $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
478         
479         if ($bean->object_name == "Meeting")
480         {
481             $convert_lead->setMeetingsUsersRelationship($bean);
482         }
483         
484         $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
485         
486         SugarTestMeetingUtilities::removeMeetingUsers();
487         SugarTestMeetingUtilities::removeAllCreatedMeetings();
488     }
489 }