2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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/Contacts/Contact.php');
39 require_once('modules/Accounts/Account.php');
40 require_once('modules/Campaigns/Campaign.php');
41 require_once('modules/CampaignLog/CampaignLog.php');
42 require_once('modules/Campaigns/utils.php');
43 require_once('modules/EmailMarketing/EmailMarketing.php');
44 require_once('include/ListView/ListView.php');
45 require_once('SugarTestContactUtilities.php');
46 require_once('SugarTestLeadUtilities.php');
48 class Bug40233Test extends Sugar_PHPUnit_Framework_TestCase
52 var $prospectlist = null;
53 var $prospectlist2 = null;
54 var $emailmarketing = null;
55 var $emailmarketing2 = null;
56 var $saved_current_user = null;
57 var $clear_database = true;
58 var $remove_beans = true;
60 public function setUp()
62 $this->markTestSkipped('Marking this skipped until we figure out why it is causing the SQL server connection to go away.');
63 $this->clear_database = true;
64 $this->remove_beans = true;
65 $this->saved_current_user = $GLOBALS['current_user'];
68 $GLOBALS['current_user'] = $user;
70 $this->campaign = new Campaign();
71 $this->campaign->name = 'Bug39665Test ' . time();
72 $this->campaign->campaign_type = 'Email';
73 $this->campaign->status = 'Active';
74 $timeDate = new TimeDate();
75 $this->campaign->end_date = $timeDate->to_display_date(date('Y')+1 .'-01-01');
76 $this->campaign->assigned_id = $user->id;
77 $this->campaign->team_id = '1';
78 $this->campaign->team_set_id = '1';
79 $this->campaign->save();
81 $this->emailmarketing = new EmailMarketing();
82 $this->emailmarketing->name = $this->campaign->name . ' Email1';
83 $this->emailmarketing->campaign_id = $this->campaign->id;
84 $this->emailmarketing->from_name = 'SugarCRM';
85 $this->emailmarketing->from_addr = 'from@exmaple.com';
86 $this->emailmarketing->reply_to_name = 'SugarCRM';
87 $this->emailmarketing->reply_to_addr = 'reply@exmaple.com';
88 $this->emailmarketing->status = 'active';
89 $this->emailmarketing->all_prospect_lists = 1;
90 $this->emailmarketing->date_start = $timeDate->to_display_date(date('Y')+1 .'-01-01') . ' 00:00:00';
92 $this->emailmarketing2 = new EmailMarketing();
93 $this->emailmarketing2->name = $this->campaign->name . ' Email2';
94 $this->emailmarketing2->campaign_id = $this->campaign->id;
95 $this->emailmarketing2->from_name = 'SugarCRM';
96 $this->emailmarketing2->from_addr = 'do_not_reply@exmaple.com';
97 $this->emailmarketing2->reply_to_name = 'SugarCRM';
98 $this->emailmarketing2->reply_to_addr = 'reply@exmaple.com';
99 $this->emailmarketing2->status = 'active';
100 $this->emailmarketing2->all_prospect_lists = 1;
101 $this->emailmarketing2->date_start = $timeDate->to_display_date(date('Y')+1 .'-01-01') . ' 00:00:00';
103 $query = 'SELECT id FROM inbound_email WHERE deleted=0';
104 $result = $GLOBALS['db']->query($query);
105 while($row = $GLOBALS['db']->fetchByAssoc($result))
107 $this->emailmarketing->inbound_email_id = $row['id'];
108 $this->emailmarketing2->inbound_email_id = $row['id'];
112 $query = 'SELECT id FROM email_templates WHERE deleted=0';
113 while($row = $GLOBALS['db']->fetchByAssoc($result))
115 $this->emailmarketing->template_id = $row['id'];
116 $this->emailmarketing2->template_id = $row['id'];
120 $this->emailmarketing->save();
121 $this->emailmarketing2->save();
123 $this->campaign->load_relationship('prospectlists');
124 $this->prospectlist = new ProspectList();
125 $this->prospectlist->name = $this->campaign->name.' Prospect List1';
126 $this->prospectlist->assigned_user_id= $GLOBALS['current_user']->id;
127 $this->prospectlist->list_type = "default";
128 $this->prospectlist->save();
129 $this->campaign->prospectlists->add($this->prospectlist->id);
131 $this->campaign->load_relationship('prospectlists');
132 $this->prospectlist2 = new ProspectList();
133 $this->prospectlist2->name = $this->campaign->name.' Prospect List2';
134 $this->prospectlist2->assigned_user_id= $GLOBALS['current_user']->id;
135 $this->prospectlist2->list_type = "default";
136 $this->prospectlist2->save();
137 $this->campaign->prospectlists->add($this->prospectlist2->id);
139 $campaign_log_states = array(0=>'viewed', 1=>'link', 2=>'invalid email', 3=>'send error', 4=>'removed', 5=>'blocked', 6=>'lead', 7=>'contact');
141 for($i=0; $i < 10; $i++)
143 $contact = SugarTestContactUtilities::createContact();
144 $contact->campaign_id = $this->campaign->id;
145 $contact->email2 = 'contact'. mt_rand() . '@sugar.com'; //Simulate a secondary email
147 $contact->load_relationship('prospect_lists');
148 $contact->prospect_lists->add($this->prospectlist->id);
149 $contact->prospect_lists->add($this->prospectlist2->id);
151 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing, $this->prospectlist, 'targeted');
152 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
154 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing2, $this->prospectlist, 'targeted');
155 $this->create_campaign_log($this->campaign, $contact, $this->emailmarketing2, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
158 for($i=0; $i < 10; $i++)
160 $lead = SugarTestLeadUtilities::createLead();
161 $lead->campaign_id = $this->campaign->id;
162 $lead->email2 = 'lead2' . mt_rand() . '@sugar.com'; //Simulate a secondary email
164 $lead->load_relationship('prospect_lists');
165 $lead->prospect_lists->add($this->prospectlist->id);
166 $lead->prospect_lists->add($this->prospectlist2->id);
168 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing, $this->prospectlist, 'targeted');
169 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
171 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing2, $this->prospectlist, 'targeted');
172 $this->create_campaign_log($this->campaign, $lead, $this->emailmarketing2, $this->prospectlist, $campaign_log_states[mt_rand(0, 7)]);
176 //But wait, there's more... now we email the friggin thing to place it in the queue
179 if ($this->campaign->db->dbType=='oci8') {
181 $current_date= "'".gmdate($GLOBALS['timedate']->get_db_date_time_format())."'";
184 //start scheduling now.....
185 $emailmarketing_beans = array($this->emailmarketing, $this->emailmarketing2);
186 foreach ($emailmarketing_beans as $marketing) {
188 $mergedvalue=$GLOBALS['timedate']->merge_date_time($marketing->date_start,$marketing->time_start);
190 if ($this->campaign->db->dbType=='oci8') {
192 $send_date_time= "'".$GLOBALS['timedate']->to_db_date($mergedvalue) . ' ' .$GLOBALS['timedate']->to_db_time($mergedvalue)."'";
195 //find all prospect lists associated with this email marketing message.
196 if ($marketing->all_prospect_lists == 1) {
197 $query="SELECT prospect_lists.id prospect_list_id from prospect_lists ";
198 $query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
199 $query.=" WHERE plc.campaign_id='{$this->campaign->id}'";
200 $query.=" AND prospect_lists.deleted=0";
201 $query.=" AND plc.deleted=0";
202 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
204 $query="select email_marketing_prospect_lists.* FROM email_marketing_prospect_lists ";
205 $query.=" inner join prospect_lists on prospect_lists.id = email_marketing_prospect_lists.prospect_list_id";
206 $query.=" WHERE prospect_lists.deleted=0 and email_marketing_id = '{$marketing->id}' and email_marketing_prospect_lists.deleted=0";
207 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
209 $result=$this->campaign->db->query($query);
210 while (($row=$this->campaign->db->fetchByAssoc($result))!=null ) {
211 $prospect_list_id=$row['prospect_list_id'];
212 //delete all messages for the current campaign and current email marketing message.
213 $delete_emailman_query="delete from emailman where campaign_id='{$this->campaign->id}' and marketing_id='{$marketing->id}' and list_id='{$prospect_list_id}'";
214 $this->campaign->db->query($delete_emailman_query);
216 $insert_query= "INSERT INTO emailman (date_entered, user_id, campaign_id, marketing_id,list_id, related_id, related_type, send_date_time";
217 if ($this->campaign->db->dbType=='oci8') {
220 $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} ";
221 if ($this->campaign->db->dbType=='oci8') {
223 $insert_query.= "FROM prospect_lists_prospects plp ";
224 $insert_query.= "INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = plp.prospect_list_id ";
225 $insert_query.= "WHERE plp.prospect_list_id = '{$prospect_list_id}' ";
226 $insert_query.= "AND plp.deleted=0 ";
227 $insert_query.= "AND plc.deleted=0 ";
228 $insert_query.= "AND plc.campaign_id='{$this->campaign->id}'";
230 if ($this->campaign->db->dbType=='oci8') {
232 $this->campaign->db->query($insert_query);
237 public function tearDown()
240 $GLOBALS['current_user'] = $this->saved_current_user;
242 if($this->remove_beans)
244 $this->campaign->mark_deleted($this->campaign->id);
245 $this->prospectlist->mark_deleted($this->prospectlist->id);
247 SugarTestContactUtilities::removeAllCreatedContacts();
248 SugarTestLeadUtilities::removeAllCreatedLeads();
251 if($this->clear_database)
253 $sql = 'DELETE FROM email_marketing WHERE campaign_id = \'' . $this->campaign->id . '\'';
254 $GLOBALS['db']->query($sql);
256 $sql = 'DELETE FROM emailman WHERE campaign_id = \'' . $this->campaign->id . '\'';
257 $GLOBALS['db']->query($sql);
259 $sql = 'DELETE FROM campaign_log WHERE campaign_id = \'' . $this->campaign->id . '\'';
260 $GLOBALS['db']->query($sql);
262 $sql = 'DELETE FROM prospect_lists_prospects WHERE prospect_list_id=\'' . $this->prospectlist->id . '\'';
263 $GLOBALS['db']->query($sql);
265 $sql = 'DELETE FROM prospect_lists_prospects WHERE prospect_list_id=\'' . $this->prospectlist2->id . '\'';
266 $GLOBALS['db']->query($sql);
268 $sql = 'DELETE FROM prospect_lists WHERE id = \'' . $this->prospectlist->id . '\'';
269 $GLOBALS['db']->query($sql);
271 $sql = 'DELETE FROM prospect_lists WHERE id = \'' . $this->prospectlist2->id . '\'';
272 $GLOBALS['db']->query($sql);
274 $sql = 'DELETE FROM prospect_list_campaigns WHERE campaign_id = \'' . $this->campaign->id . '\'';
275 $GLOBALS['db']->query($sql);
277 $sql = 'DELETE FROM campaigns WHERE id = \'' . $this->campaign->id . '\'';
278 $GLOBALS['db']->query($sql);
283 function test_viewed_message()
285 $this->markTestSkipped('Skip for now');
286 $this->assertTrue(true);
289 protected function create_campaign_log($campaign, $target, $marketing, $prospectlist, $activity_type, $target_tracker_key='')
291 $this->markTestSkipped('Marking this skipped until we figure out why it is causing the SQL server connection to go away.');
292 $campaign_log = new CampaignLog();
293 $campaign_log->campaign_id=$campaign->id;
294 $campaign_log->target_tracker_key=$target_tracker_key;
295 $campaign_log->target_id= $target->id;
296 $campaign_log->target_type=$target->module_dir;
297 $campaign_log->marketing_id=$marketing->id;
298 $campaign_log->more_information=$target->email1;
299 $campaign_log->activity_type=$activity_type;
300 $campaign_log->activity_date=$GLOBALS['timedate']->to_display_date_time(gmdate($GLOBALS['timedate']->get_db_date_time_format()));
301 $campaign_log->list_id=$prospectlist->id;
302 $campaign_log->related_type='Emails';
303 $campaign_log->save();