]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Campaigns/Bug40233Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / Campaigns / Bug40233Test.php
1 <?php 
2 require_once('modules/Contacts/Contact.php');
3 require_once('modules/Accounts/Account.php');
4 require_once('modules/Campaigns/Campaign.php');
5 require_once('modules/CampaignLog/CampaignLog.php');
6 require_once('modules/Campaigns/utils.php');
7 require_once('modules/EmailMarketing/EmailMarketing.php');
8 require_once('include/ListView/ListView.php');
9 require_once('SugarTestContactUtilities.php');
10 require_once('SugarTestLeadUtilities.php');
11
12 class Bug40233Test extends Sugar_PHPUnit_Framework_TestCase
13 {
14         
15         var $campaign = null;
16         var $prospectlist = null;
17         var $prospectlist2 = null;
18         var $emailmarketing = null;
19         var $emailmarketing2 = null;
20         var $saved_current_user = null;
21         var $clear_database = true;
22         var $remove_beans = true;       
23         
24         public function setUp()
25     {
26         $this->markTestSkipped('Marking this skipped until we figure out why it is causing the SQL server connection to go away.');
27         $this->clear_database = true;
28         $this->remove_beans = true;
29         $this->saved_current_user = $GLOBALS['current_user'];
30         $user = new User();
31         $user->retrieve('1');
32         $GLOBALS['current_user'] = $user;
33         
34         $this->campaign = new Campaign();
35         $this->campaign->name = 'Bug39665Test ' . time();
36         $this->campaign->campaign_type = 'Email';
37         $this->campaign->status = 'Active';
38         $timeDate = new TimeDate();
39         $this->campaign->end_date = $timeDate->to_display_date(date('Y')+1 .'-01-01');
40         $this->campaign->assigned_id = $user->id;
41         $this->campaign->team_id = '1';
42         $this->campaign->team_set_id = '1';
43         $this->campaign->save();
44         
45         $this->emailmarketing = new EmailMarketing();
46         $this->emailmarketing->name = $this->campaign->name . ' Email1';
47         $this->emailmarketing->campaign_id = $this->campaign->id;
48         $this->emailmarketing->from_name = 'SugarCRM';
49         $this->emailmarketing->from_addr = 'from@exmaple.com';
50         $this->emailmarketing->reply_to_name = 'SugarCRM';
51         $this->emailmarketing->reply_to_addr = 'reply@exmaple.com';
52         $this->emailmarketing->status = 'active';
53         $this->emailmarketing->all_prospect_lists = 1;
54         $this->emailmarketing->date_start = $timeDate->to_display_date(date('Y')+1 .'-01-01') . ' 00:00:00';
55         
56         $this->emailmarketing2 = new EmailMarketing();
57         $this->emailmarketing2->name = $this->campaign->name . ' Email2';
58         $this->emailmarketing2->campaign_id = $this->campaign->id;
59         $this->emailmarketing2->from_name = 'SugarCRM';
60         $this->emailmarketing2->from_addr = 'do_not_reply@exmaple.com';
61         $this->emailmarketing2->reply_to_name = 'SugarCRM';
62         $this->emailmarketing2->reply_to_addr = 'reply@exmaple.com';            
63         $this->emailmarketing2->status = 'active';
64         $this->emailmarketing2->all_prospect_lists = 1;
65         $this->emailmarketing2->date_start = $timeDate->to_display_date(date('Y')+1 .'-01-01') . ' 00:00:00';           
66         
67         $query = 'SELECT id FROM inbound_email WHERE deleted=0';
68         $result = $GLOBALS['db']->query($query);
69         while($row = $GLOBALS['db']->fetchByAssoc($result))
70         {
71                           $this->emailmarketing->inbound_email_id = $row['id'];
72                           $this->emailmarketing2->inbound_email_id = $row['id'];
73                           break;
74                 }       
75         
76                 $query = 'SELECT id FROM email_templates WHERE deleted=0';
77         while($row = $GLOBALS['db']->fetchByAssoc($result))
78         {
79                           $this->emailmarketing->template_id = $row['id'];
80                           $this->emailmarketing2->template_id = $row['id'];
81                           break;
82                 }               
83                 
84         $this->emailmarketing->save();
85         $this->emailmarketing2->save();
86         
87         $this->campaign->load_relationship('prospectlists');
88                 $this->prospectlist = new ProspectList();
89         $this->prospectlist->name = $this->campaign->name.' Prospect List1';
90         $this->prospectlist->assigned_user_id= $GLOBALS['current_user']->id;
91         $this->prospectlist->list_type = "default";
92         $this->prospectlist->save();
93         $this->campaign->prospectlists->add($this->prospectlist->id);
94         
95         $this->campaign->load_relationship('prospectlists');
96                 $this->prospectlist2 = new ProspectList();
97         $this->prospectlist2->name = $this->campaign->name.' Prospect List2';
98         $this->prospectlist2->assigned_user_id= $GLOBALS['current_user']->id;
99         $this->prospectlist2->list_type = "default";
100         $this->prospectlist2->save();       
101         $this->campaign->prospectlists->add($this->prospectlist2->id);         
102         
103         $campaign_log_states = array(0=>'viewed', 1=>'link', 2=>'invalid email', 3=>'send error', 4=>'removed', 5=>'blocked', 6=>'lead', 7=>'contact');
104         
105         for($i=0; $i < 10; $i++)
106         {
107                 $contact = SugarTestContactUtilities::createContact();
108                 $contact->campaign_id = $this->campaign->id;
109                 $contact->email2 = 'contact'. mt_rand() . '@sugar.com'; //Simulate a secondary email
110                 $contact->save();
111             $contact->load_relationship('prospect_lists');
112                 $contact->prospect_lists->add($this->prospectlist->id);
113                 $contact->prospect_lists->add($this->prospectlist2->id);
114                 
115                 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing, $this->prospectlist, 'targeted');
116                 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
117
118                 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing2, $this->prospectlist, 'targeted');
119                 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing2, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);                
120         }
121
122         for($i=0; $i < 10; $i++)
123         {
124                 $lead = SugarTestLeadUtilities::createLead();
125                 $lead->campaign_id = $this->campaign->id;
126                 $lead->email2 = 'lead2' . mt_rand() . '@sugar.com'; //Simulate a secondary email
127                 $lead->save();
128                         $lead->load_relationship('prospect_lists');
129                 $lead->prospect_lists->add($this->prospectlist->id);
130                 $lead->prospect_lists->add($this->prospectlist2->id);
131                 
132                 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing, $this->prospectlist, 'targeted');
133                 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
134       
135                 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing2, $this->prospectlist, 'targeted');
136                 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing2, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);        
137        }        
138   
139        
140         //But wait, there's more... now we email the friggin thing to place it in the queue
141
142
143                 if ($this->campaign->db->dbType=='oci8') {
144                 } else {
145                         $current_date= "'".gmdate($GLOBALS['timedate']->get_db_date_time_format())."'";
146                 }
147                 
148                 //start scheduling now.....
149                 $emailmarketing_beans = array($this->emailmarketing, $this->emailmarketing2);
150                 foreach ($emailmarketing_beans as $marketing) {
151                 
152                         $mergedvalue=$GLOBALS['timedate']->merge_date_time($marketing->date_start,$marketing->time_start);
153                         
154                         if ($this->campaign->db->dbType=='oci8') {
155                         } else {
156                                         $send_date_time= "'".$GLOBALS['timedate']->to_db_date($mergedvalue) . ' ' .$GLOBALS['timedate']->to_db_time($mergedvalue)."'";  
157                         }
158                 
159                         //find all prospect lists associated with this email marketing message.
160                         if ($marketing->all_prospect_lists == 1) {
161                                 $query="SELECT prospect_lists.id prospect_list_id from prospect_lists ";
162                                 $query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
163                                 $query.=" WHERE plc.campaign_id='{$this->campaign->id}'"; 
164                                 $query.=" AND prospect_lists.deleted=0";
165                                 $query.=" AND plc.deleted=0";
166                                 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";                                        
167                         } else {
168                                 $query="select email_marketing_prospect_lists.* FROM email_marketing_prospect_lists ";
169                                 $query.=" inner join prospect_lists on prospect_lists.id = email_marketing_prospect_lists.prospect_list_id";
170                                 $query.=" WHERE prospect_lists.deleted=0 and email_marketing_id = '{$marketing->id}' and email_marketing_prospect_lists.deleted=0";
171                                 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";                                        
172                         }
173                         $result=$this->campaign->db->query($query);
174                         while (($row=$this->campaign->db->fetchByAssoc($result))!=null ) {
175                                 $prospect_list_id=$row['prospect_list_id'];
176                                 //delete all messages for the current campaign and current email marketing message.
177                                 $delete_emailman_query="delete from emailman where campaign_id='{$this->campaign->id}' and marketing_id='{$marketing->id}' and list_id='{$prospect_list_id}'";
178                                 $this->campaign->db->query($delete_emailman_query);
179                                 
180                                 $insert_query= "INSERT INTO emailman (date_entered, user_id, campaign_id, marketing_id,list_id, related_id, related_type, send_date_time";
181                                 if ($this->campaign->db->dbType=='oci8') {
182                                 }
183                                 $insert_query.=')'; 
184                                 $insert_query.= " SELECT $current_date,'{$GLOBALS['current_user']->id}',plc.campaign_id,'{$marketing->id}',plp.prospect_list_id, plp.related_id, plp.related_type,{$send_date_time} ";  
185                                 if ($this->campaign->db->dbType=='oci8') {
186                                 }
187                                 $insert_query.= "FROM prospect_lists_prospects plp ";
188                                 $insert_query.= "INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = plp.prospect_list_id "; 
189                                 $insert_query.= "WHERE plp.prospect_list_id = '{$prospect_list_id}' ";
190                                 $insert_query.= "AND plp.deleted=0 ";
191                                 $insert_query.= "AND plc.deleted=0 ";
192                                 $insert_query.= "AND plc.campaign_id='{$this->campaign->id}'";
193                 
194                                 if ($this->campaign->db->dbType=='oci8') {
195                                 }
196                                 $this->campaign->db->query($insert_query);
197                         }
198                 }
199         }
200         
201     public function tearDown()
202     {
203         $GLOBALS['current_user'] = $this->saved_current_user;
204         
205         if($this->remove_beans)
206         {
207                         $this->campaign->mark_deleted($this->campaign->id);
208                         $this->prospectlist->mark_deleted($this->prospectlist->id);
209                         
210                         SugarTestContactUtilities::removeAllCreatedContacts();
211                         SugarTestLeadUtilities::removeAllCreatedLeads();
212         }
213                 
214                 if($this->clear_database)
215                 {
216                         $sql = 'DELETE FROM email_marketing WHERE campaign_id = \'' . $this->campaign->id . '\'';
217                         $GLOBALS['db']->query($sql);
218                         
219                         $sql = 'DELETE FROM emailman WHERE campaign_id = \'' . $this->campaign->id . '\'';
220                         $GLOBALS['db']->query($sql);                            
221                         
222                         $sql = 'DELETE FROM campaign_log WHERE campaign_id = \'' . $this->campaign->id . '\'';
223                         $GLOBALS['db']->query($sql);
224                         
225                         $sql = 'DELETE FROM prospect_lists_prospects WHERE prospect_list_id=\'' . $this->prospectlist->id . '\'';
226                         $GLOBALS['db']->query($sql);
227                         
228                         $sql = 'DELETE FROM prospect_lists_prospects WHERE prospect_list_id=\'' . $this->prospectlist2->id . '\'';
229                         $GLOBALS['db']->query($sql);                    
230                         
231                         $sql = 'DELETE FROM prospect_lists WHERE id = \'' . $this->prospectlist->id . '\'';
232                         $GLOBALS['db']->query($sql);                            
233
234                         $sql = 'DELETE FROM prospect_lists WHERE id = \'' . $this->prospectlist2->id . '\'';
235                         $GLOBALS['db']->query($sql);                            
236                         
237                         $sql = 'DELETE FROM prospect_list_campaigns WHERE campaign_id = \'' . $this->campaign->id . '\'';
238                         $GLOBALS['db']->query($sql);                            
239                         
240                         $sql = 'DELETE FROM campaigns WHERE id = \'' . $this->campaign->id . '\'';
241                         $GLOBALS['db']->query($sql);    
242                 }
243                 
244     }   
245         
246         function test_viewed_message()
247         {
248                 $this->markTestSkipped('Skip for now');
249                 $this->assertTrue(true);
250     }
251     
252     protected function create_campaign_log($campaign, $target, $marketing, $prospectlist, $activity_type, $target_tracker_key='')
253     {
254                         $campaign_log = new CampaignLog();
255                         $campaign_log->campaign_id=$campaign->id;
256                         $campaign_log->target_tracker_key=$target_tracker_key;
257                         $campaign_log->target_id= $target->id;
258                         $campaign_log->target_type=$target->module_dir;
259             $campaign_log->marketing_id=$marketing->id;
260                         $campaign_log->more_information=$target->email1;
261                         $campaign_log->activity_type=$activity_type;
262                         $campaign_log->activity_date=$GLOBALS['timedate']->to_display_date_time(gmdate($GLOBALS['timedate']->get_db_date_time_format()));
263                         $campaign_log->list_id=$prospectlist->id;
264                         $campaign_log->related_type='Emails';
265             $campaign_log->save();
266     }    
267     
268 }
269 ?>