]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Leads/ConvertLeadTests.php
Release 6.3.0RC3
[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-2011 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         $lead = SugarTestLeadUtilities::createLead();
61         $lead->opportunity_name = 'SBizzle Dollar Store';
62         $lead->save();
63         
64         $_REQUEST['module'] = 'Leads';
65         $_REQUEST['action'] = 'ConvertLead';
66         $_REQUEST['record'] = $lead->id;
67         
68         // Check that the opportunity name doesn't get populated when it's not in the Leads editview layout
69         require_once('include/MVC/Controller/ControllerFactory.php');
70         require_once('include/MVC/View/ViewFactory.php');
71         $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
72         ob_start();
73         $GLOBALS['app']->controller->execute();
74         $output = ob_get_clean();
75         
76         $matches_one = array();
77         $pattern = '/SBizzle Dollar Store/';
78         preg_match($pattern, $output, $matches_one);
79         $this->assertTrue(count($matches_one) == 0, "Opportunity name got carried over to the Convert Leads page when it shouldn't have.");
80
81         // Add the opportunity_name to the Leads EditView
82         SugarTestStudioUtilities::addFieldToLayout('Leads', 'editview', 'opportunity_name');
83         
84         // Check that the opportunity name now DOES get populated now that it's in the Leads editview layout
85         ob_start();
86         $GLOBALS['app']->controller = ControllerFactory::getController($_REQUEST['module']);
87         $GLOBALS['app']->controller->execute();
88         $output = ob_get_clean();
89         $matches_two = array();
90         $pattern = '/SBizzle Dollar Store/';
91         preg_match($pattern, $output, $matches_two);
92         $this->assertTrue(count($matches_two) > 0, "Opportunity name did not carry over to the Convert Leads page when it should have.");
93         
94         SugarTestStudioUtilities::removeAllCreatedFields();
95         unset($GLOBALS['app']->controller);
96         unset($_REQUEST['module']);
97         unset($_REQUEST['action']);
98         unset($_REQUEST['record']);
99         SugarTestLeadUtilities::removeAllCreatedLeads();
100     }
101     /**
102      * @group bug44033
103      */
104     public function testActivityMove() {
105         // init
106         $lead = SugarTestLeadUtilities::createLead();
107         $contact = SugarTestContactUtilities::createContact();
108         $meeting = SugarTestMeetingUtilities::createMeeting();
109         SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
110         $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
111         $_REQUEST['record'] = $lead->id;
112
113         // refresh the meeting to include parent_id and parent_type
114         $meeting_id = $meeting->id;
115         $meeting = new Meeting();
116         $meeting->retrieve($meeting_id);
117
118         // action: move meeting from lead to contact
119         $convertObj = new TestViewConvertLead();
120         $convertObj->moveActivityWrapper($meeting, $contact);
121
122         // verification 1, parent id should be contact id
123         $this->assertTrue($meeting->parent_id == $contact->id, 'Meeting parent id is not converted to contact id.');
124
125         // verification 2, parent type should be "Contacts"
126         $this->assertTrue($meeting->parent_type == 'Contacts', 'Meeting parent type is not converted to Contacts.');
127
128         // verification 3, record should be deleted from meetings_leads table
129         $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
130         $result = $GLOBALS['db']->query($sql);
131         $row = $GLOBALS['db']->fetchByAssoc($result);
132         $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
133
134         // verification 4, record should be added to meetings_contacts table
135         $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact->id}' and deleted=0";
136         $result = $GLOBALS['db']->query($sql);
137         $row = $GLOBALS['db']->fetchByAssoc($result);
138         $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
139
140         // clean up
141         unset($_REQUEST['record']);
142         $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact->id}'");
143         SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
144         SugarTestMeetingUtilities::removeMeetingContacts();
145         SugarTestMeetingUtilities::removeAllCreatedMeetings();
146         SugarTestContactUtilities::removeAllCreatedContacts();
147         SugarTestLeadUtilities::removeAllCreatedLeads();
148     }
149
150     public function testActivityCopy() {
151         // init
152         $lead = SugarTestLeadUtilities::createLead();
153         $contact = SugarTestContactUtilities::createContact();
154         $meeting = SugarTestMeetingUtilities::createMeeting();
155         SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
156         $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
157         $_REQUEST['record'] = $lead->id;
158
159         // refresh the meeting to include parent_id and parent_type
160         $meeting_id = $meeting->id;
161         $meeting = new Meeting();
162         $meeting->retrieve($meeting_id);
163
164         // action: copy meeting from lead to contact
165         $convertObj = new TestViewConvertLead();
166         $convertObj->copyActivityWrapper($meeting, $contact);
167
168         // 1. the original meeting should still have the same parent_type and parent_id
169         $meeting->retrieve($meeting_id);
170         $this->assertEquals('Leads', $meeting->parent_type, 'parent_type of the original meeting was changed from Leads to '.$meeting->parent_type);
171         $this->assertEquals($lead->id, $meeting->parent_id, 'parent_id of the original meeting was changed from '.$lead->id.' to '.$meeting->parent_id);
172
173         // 2. a newly created meeting with parent type=Contatcs and parent_id=$contact->id
174         $sql = "select id from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts' and deleted=0";
175         $result = $GLOBALS['db']->query($sql);
176         $row = $GLOBALS['db']->fetchByAssoc($result);
177         $this->assertNotNull($row, 'Could not find the newly created meeting with parent_type=Contacts and parent_id='.$contact->id);
178         $new_meeting_id = '';
179         if ($row) {
180             $new_meeting_id = $row['id'];
181         }
182
183         // 3. record should not be deleted from meetings_leads table
184         $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
185         $result = $GLOBALS['db']->query($sql);
186         $row = $GLOBALS['db']->fetchByAssoc($result);
187         $this->assertNotNull($row, "Meeting-Lead relationship was removed.");
188
189         // 4. new meeting record should be added to meetings_contacts table
190         $sql = "select id from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id='{$contact->id}' and deleted=0";
191         $result = $GLOBALS['db']->query($sql);
192         $row = $GLOBALS['db']->fetchByAssoc($result);
193         $this->assertFalse(empty($row), "Meeting-Contact relationship has not been added.");
194
195         // clean up
196         unset($_REQUEST['record']);
197         $GLOBALS['db']->query("delete from meetings where parent_id='{$contact->id}' and parent_type= 'Contacts'");
198         $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
199         $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$new_meeting_id}' and contact_id= '{$contact->id}'");
200         SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
201         SugarTestMeetingUtilities::removeMeetingContacts();
202         SugarTestMeetingUtilities::removeAllCreatedMeetings();
203         SugarTestContactUtilities::removeAllCreatedContacts();
204         SugarTestLeadUtilities::removeAllCreatedLeads();
205     }
206
207     public function testConversionAndMoveActivities() {
208         global $sugar_config;
209
210         // init
211         $lead = SugarTestLeadUtilities::createLead();
212         $account = SugarTestAccountUtilities::createAccount();
213         $meeting = SugarTestMeetingUtilities::createMeeting();
214         SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
215         $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
216         $_REQUEST['record'] = $lead->id;
217
218         // set the request/post parameters before converting the lead
219         $_REQUEST['module'] = 'Leads';
220         $_REQUEST['action'] = 'ConvertLead';
221         $_REQUEST['record'] = $lead->id;
222         $_REQUEST['handle'] = 'save';
223         $_REQUEST['selectedAccount'] = $account->id;
224         $sugar_config['lead_conv_activity_opt'] = 'move';
225         $_POST['lead_conv_ac_op_sel'] = 'Contacts';
226
227         // call display to trigger conversion
228         $vc = new ViewConvertLead();
229         $vc->display();
230
231         // refresh meeting
232         $meeting_id = $meeting->id;
233         $meeting = new Meeting();
234         $meeting->retrieve($meeting_id);
235
236         // refresh lead
237         $lead_id = $lead->id;
238         $lead = new Lead();
239         $lead->retrieve($lead_id);
240
241         // retrieve the new contact id from the conversion
242         $contact_id = $lead->contact_id;
243
244         // 1. Lead's contact_id should not be null
245         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
246
247         // 2. Lead status should be 'Converted'
248         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
249
250         // 3. new parent_type should be Contacts
251         $this->assertEquals('Contacts', $meeting->parent_type, 'Meeting parent type has not been set to Contacts');
252
253         // 4. new parent_id should be contact id
254         $this->assertEquals($contact_id, $meeting->parent_id, 'Meeting parent id has not been set to contact id.');
255
256         // 5. record should be deleted from meetings_leads table
257         $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
258         $result = $GLOBALS['db']->query($sql);
259         $row = $GLOBALS['db']->fetchByAssoc($result);
260         $this->assertFalse($row, "Meeting-Lead relationship is not removed.");
261
262         // 6. record should be added to meetings_contacts table
263         $sql = "select id from meetings_contacts where meeting_id='{$meeting->id}' and contact_id='{$contact_id}' and deleted=0";
264         $result = $GLOBALS['db']->query($sql);
265         $row = $GLOBALS['db']->fetchByAssoc($result);
266         $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
267
268         // clean up
269         unset($_REQUEST['record']);
270         $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
271         $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
272         $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
273         $GLOBALS['db']->query("delete from meetings_contacts where meeting_id='{$meeting->id}' and contact_id= '{$contact_id}'");
274         SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
275         SugarTestMeetingUtilities::removeMeetingContacts();
276         SugarTestMeetingUtilities::removeAllCreatedMeetings();
277         SugarTestAccountUtilities::removeAllCreatedAccounts();
278         SugarTestLeadUtilities::removeAllCreatedLeads();
279     }
280
281     public function testConversionAndCopyActivities() {
282         global $sugar_config;
283
284         // init
285         $lead = SugarTestLeadUtilities::createLead();
286         $account = SugarTestAccountUtilities::createAccount();
287         $meeting = SugarTestMeetingUtilities::createMeeting();
288         SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
289         $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
290         $_REQUEST['record'] = $lead->id;
291
292         // set the request/post parameters before converting the lead
293         $_REQUEST['module'] = 'Leads';
294         $_REQUEST['action'] = 'ConvertLead';
295         $_REQUEST['record'] = $lead->id;
296         $_REQUEST['handle'] = 'save';
297         $_REQUEST['selectedAccount'] = $account->id;
298         $sugar_config['lead_conv_activity_opt'] = 'copy';
299         $_POST['lead_conv_ac_op_sel'] = array('Contacts');
300
301         // call display to trigger conversion
302         $vc = new ViewConvertLead();
303         $vc->display();
304
305         // refresh meeting
306         $meeting_id = $meeting->id;
307         $meeting = new Meeting();
308         $meeting->retrieve($meeting_id);
309
310         // refresh lead
311         $lead_id = $lead->id;
312         $lead = new Lead();
313         $lead->retrieve($lead_id);
314
315         // retrieve the new contact id from the conversion
316         $contact_id = $lead->contact_id;
317
318         // 1. Lead's contact_id should not be null
319         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
320
321         // 2. Lead status should be 'Converted'
322         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
323
324         // 3. parent_type of the original meeting should be Leads
325         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
326
327         // 4. parent_id of the original meeting should be contact id
328         $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
329
330         // 5. record should NOT be deleted from meetings_leads table
331         $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
332         $result = $GLOBALS['db']->query($sql);
333         $row = $GLOBALS['db']->fetchByAssoc($result);
334         $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
335
336         // 6. record should be added to meetings_contacts table
337         $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
338         $result = $GLOBALS['db']->query($sql);
339         $row = $GLOBALS['db']->fetchByAssoc($result);
340         $this->assertFalse(empty($row), "Meeting-Contact relationship is not added.");
341
342         // 7. the parent_type of the new meeting should be Contacts
343         $new_meeting_id = $row['meeting_id'];
344         $sql = "select id, parent_type, parent_id from meetings where id='{$new_meeting_id}' and deleted=0";
345         $result = $GLOBALS['db']->query($sql);
346         $row = $GLOBALS['db']->fetchByAssoc($result);
347         $this->assertFalse(empty($row), "New meeting is not added for contact.");
348         $this->assertEquals('Contacts', $row['parent_type'], 'Parent type of the new meeting should be Contacts');
349
350         // 8. the parent_id of the new meeting should be contact id
351         $this->assertEquals($contact_id, $row['parent_id'], 'Parent id of the new meeting should be contact id.');
352
353         // clean up
354         unset($_REQUEST['record']);
355         $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
356         $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
357         $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
358         $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
359         $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
360         SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
361         SugarTestMeetingUtilities::removeMeetingContacts();
362         SugarTestMeetingUtilities::removeAllCreatedMeetings();
363         SugarTestAccountUtilities::removeAllCreatedAccounts();
364         SugarTestLeadUtilities::removeAllCreatedLeads();
365     }
366
367     public function testConversionAndDoNothing() {
368         global $sugar_config;
369
370         // init
371         $lead = SugarTestLeadUtilities::createLead();
372         $account = SugarTestAccountUtilities::createAccount();
373         $meeting = SugarTestMeetingUtilities::createMeeting();
374         SugarTestMeetingUtilities::addMeetingParent($meeting->id, $lead->id);
375         $relation_id = SugarTestMeetingUtilities::addMeetingLeadRelation($meeting->id, $lead->id);
376         $_REQUEST['record'] = $lead->id;
377
378         // set the request/post parameters before converting the lead
379         $_REQUEST['module'] = 'Leads';
380         $_REQUEST['action'] = 'ConvertLead';
381         $_REQUEST['record'] = $lead->id;
382         $_REQUEST['handle'] = 'save';
383         $_REQUEST['selectedAccount'] = $account->id;
384         $sugar_config['lead_conv_activity_opt'] = 'none';
385
386         // call display to trigger conversion
387         $vc = new ViewConvertLead();
388         $vc->display();
389
390         // refresh meeting
391         $meeting_id = $meeting->id;
392         $meeting = new Meeting();
393         $meeting->retrieve($meeting_id);
394
395         // refresh lead
396         $lead_id = $lead->id;
397         $lead = new Lead();
398         $lead->retrieve($lead_id);
399
400         // retrieve the new contact id from the conversion
401         $contact_id = $lead->contact_id;
402
403         // 1. Lead's contact_id should not be null
404         $this->assertNotNull($contact_id, 'Lead has null contact id after conversion.');
405
406         // 2. Lead status should be 'Converted'
407         $this->assertEquals('Converted', $lead->status, "Lead atatus should be 'Converted'.");
408
409         // 3. parent_type of the original meeting should be Leads
410         $this->assertEquals('Leads', $meeting->parent_type, 'Meeting parent should be Leads');
411
412         // 4. parent_id of the original meeting should be contact id
413         $this->assertEquals($lead_id, $meeting->parent_id, 'Meeting parent id should be lead id.');
414
415         // 5. record should NOT be deleted from meetings_leads table
416         $sql = "select id from meetings_leads where meeting_id='{$meeting->id}' and lead_id='{$lead->id}' and deleted=0";
417         $result = $GLOBALS['db']->query($sql);
418         $row = $GLOBALS['db']->fetchByAssoc($result);
419         $this->assertFalse(empty($row), "Meeting-Lead relationship is removed.");
420
421         // 6. record should NOT be added to meetings_contacts table
422         $sql = "select meeting_id from meetings_contacts where contact_id='{$contact_id}' and deleted=0";
423         $result = $GLOBALS['db']->query($sql);
424         $row = $GLOBALS['db']->fetchByAssoc($result);
425         $this->assertFalse($row, "Meeting-Contact relationship should not be added.");
426
427         // clean up
428         unset($_REQUEST['record']);
429         $GLOBALS['db']->query("delete from meetings where parent_id='{$lead->id}' and parent_type= 'Leads'");
430         $GLOBALS['db']->query("delete from meetings where parent_id='{$contact_id}' and parent_type= 'Contacts'");
431         $GLOBALS['db']->query("delete from contacts where id='{$contact_id}'");
432         $GLOBALS['db']->query("delete from meetings_leads where meeting_id='{$meeting->id}' and lead_id= '{$lead_id}'");
433         $GLOBALS['db']->query("delete from meetings_contacts where contact_id= '{$contact_id}'");
434         SugarTestMeetingUtilities::deleteMeetingLeadRelation($relation_id);
435         SugarTestMeetingUtilities::removeMeetingContacts();
436         SugarTestMeetingUtilities::removeAllCreatedMeetings();
437         SugarTestAccountUtilities::removeAllCreatedAccounts();
438         SugarTestLeadUtilities::removeAllCreatedLeads();
439     }
440 }
441
442 class TestViewConvertLead extends ViewConvertLead
443 {
444     public function moveActivityWrapper($activity, $bean) {
445         parent::moveActivity($activity, $bean);
446     }
447
448     public function copyActivityWrapper($activity, $bean) {
449         parent::copyActivityAndRelateToBean($activity, $bean);
450     }
451 }