]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Leads/ConvertLeadTests.php
Release $ver
[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 require_once 'tests/SugarTestViewConvertLeadUtilities.php';
40
41
42 class ConvertLeadTests extends Sugar_PHPUnit_Framework_TestCase
43 {
44     public function setUp()
45     {
46         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
48     }
49     
50     public function tearDown()
51     {
52         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53         unset($GLOBALS['app_list_strings']);
54         unset($GLOBALS['current_user']);
55     }
56     
57     /**
58     * @group bug39787
59     */
60     public function testOpportunityNameValueFilled()
61     {
62         $lead = SugarTestLeadUtilities::createLead();
63         $lead->opportunity_name = 'SBizzle Dollar Store';
64         $lead->save();
65         
66         $_REQUEST['module'] = 'Leads';
67         $_REQUEST['action'] = 'ConvertLead';
68         $_REQUEST['record'] = $lead->id;
69         
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']);
74         ob_start();
75         $GLOBALS['app']->controller->execute();
76         $output = ob_get_clean();
77         
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.");
82
83         // Add the opportunity_name to the Leads EditView
84         SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
85         
86         // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
87         ob_start();
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.");
95         
96         SugarTestStudioUtilities::removeAllCreatedFields();
97         unset($GLOBALS['app']->controller);
98         unset($_REQUEST['module']);
99         unset($_REQUEST['action']);
100         unset($_REQUEST['record']);
101         SugarTestLeadUtilities::removeAllCreatedLeads();
102     }
103
104     /**
105      * @group bug44033
106      */
107     public function testActivityMove() {
108         // init
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;
115
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);
120
121         // action: move meeting from lead to contact
122         $convertObj = new TestViewConvertLead();
123         $convertObj->moveActivityWrapper($meeting, $contact);
124
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.');
127
128         // verification 2, parent type should be "Contacts"
129         $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
130
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.");
136
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.");
142
143         // clean up
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();
151     }
152
153     public function testActivityCopy() {
154         // init
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;
161
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);
166
167         // action: copy meeting from lead to contact
168         $convertObj = new TestViewConvertLead();
169         $convertObj->copyActivityWrapper($meeting, $contact);
170
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);
175
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 = '';
182         if ($row) {
183             $new_meeting_id = $row['id'];
184         }
185
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.");
191
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.");
197
198         // clean up
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();
208     }
209
210     public function testConversionAndMoveActivities() {
211         global $sugar_config;
212
213         // init
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;
220
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';
229
230         // call display to trigger conversion
231         $vc = new ViewConvertLead();
232         $vc->display();
233
234         // refresh meeting
235         $meeting_id = $meeting->id;
236         $meeting = new Meeting();
237         $meeting->retrieve($meeting_id);
238
239         // refresh lead
240         $lead_id = $lead->id;
241         $lead = new Lead();
242         $lead->retrieve($lead_id);
243
244         // retrieve the new contact id from the conversion
245         $contact_id = $lead->contact_id;
246
247         // 1. Lead's contact_id should not be null
248         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
249
250         // 2. Lead status should be 'Converted'
251         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
252
253         // 3. new parent_type should be Contacts
254         $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
255
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.');
258
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.");
264
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.");
270
271         // clean up
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();
282     }
283
284     public function testConversionAndCopyActivities() {
285         global $sugar_config;
286
287         // init
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;
294
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');
303
304         // call display to trigger conversion
305         $vc = new ViewConvertLead();
306         $vc->display();
307
308         // refresh meeting
309         $meeting_id = $meeting->id;
310         $meeting = new Meeting();
311         $meeting->retrieve($meeting_id);
312
313         // refresh lead
314         $lead_id = $lead->id;
315         $lead = new Lead();
316         $lead->retrieve($lead_id);
317
318         // retrieve the new contact id from the conversion
319         $contact_id = $lead->contact_id;
320
321         // 1. Lead's contact_id should not be null
322         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
323
324         // 2. Lead status should be 'Converted'
325         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
326
327         // 3. parent_type of the original meeting should be Leads
328         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
329
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.');
332
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.");
338
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.");
344
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');
352
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.');
355
356         // clean up
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();
368     }
369
370     public function testConversionAndDoNothing() {
371         global $sugar_config;
372
373         // init
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;
380
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';
388
389         // call display to trigger conversion
390         $vc = new ViewConvertLead();
391         $vc->display();
392
393         // refresh meeting
394         $meeting_id = $meeting->id;
395         $meeting = new Meeting();
396         $meeting->retrieve($meeting_id);
397
398         // refresh lead
399         $lead_id = $lead->id;
400         $lead = new Lead();
401         $lead->retrieve($lead_id);
402
403         // retrieve the new contact id from the conversion
404         $contact_id = $lead->contact_id;
405
406         // 1. Lead's contact_id should not be null
407         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
408
409         // 2. Lead status should be 'Converted'
410         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
411
412         // 3. parent_type of the original meeting should be Leads
413         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
414
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.');
417
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.");
423
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.");
429
430         // clean up
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();
442     }
443
444     public function testMeetingsUsersRelationships()
445     {
446         global $current_user;
447
448         $bean = SugarTestMeetingUtilities::createMeeting();
449         $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
450
451         if ($bean->object_name == "Meeting")
452         {
453             $convert_lead->setMeetingsUsersRelationship($bean);
454         }
455
456         $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
457
458         SugarTestMeetingUtilities::removeMeetingUsers();
459         SugarTestMeetingUtilities::removeAllCreatedMeetings();
460     }
461 }
462
463 class TestViewConvertLead extends ViewConvertLead
464 {
465     public function moveActivityWrapper($activity, $bean) {
466         parent::moveActivity($activity, $bean);
467     }
468
469     public function copyActivityWrapper($activity, $bean) {
470         parent::copyActivityAndRelateToBean($activity, $bean);
471     }
472
473     public function testMeetingsUsersRelationships()
474     {
475         global $current_user;
476         
477         $bean = SugarTestMeetingUtilities::createMeeting();
478         $convert_lead = SugarTestViewConvertLeadUtilities::createViewConvertLead();
479         
480         if ($bean->object_name == "Meeting")
481         {
482             $convert_lead->setMeetingsUsersRelationship($bean);
483         }
484         
485         $this->assertTrue(is_object($bean->users), "Relationship wasn't set.");
486         
487         SugarTestMeetingUtilities::removeMeetingUsers();
488         SugarTestMeetingUtilities::removeAllCreatedMeetings();
489     }
490 }