2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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 ********************************************************************************/
37 require_once 'modules/Accounts/Account.php';
38 require_once 'modules/Campaigns/Campaign.php';
39 require_once 'modules/CampaignLog/CampaignLog.php';
40 require_once 'modules/CampaignTrackers/CampaignTracker.php';
41 require_once 'modules/Contacts/Contact.php';
42 require_once 'modules/Users/User.php';
43 require_once 'include/SugarEmailAddress/SugarEmailAddress.php';
45 class CampaignLogTest extends Sugar_PHPUnit_Framework_TestCase
47 var $campaign_id = 'campaignforcamplogunittest';
48 var $prospect_list_id = 'prospectlistforcamplogunittest';
49 var $email_marketing_id = 'marketingforcamplogunittest';
50 var $targetObjectArray = array('User','Contact','Lead','Prospect','Account','CampaignTracker');
56 var $campaign_tracker;
60 public function setup()
64 $current_user = SugarTestUserUtilities::createAnonymousUser();
65 //for the purpose of this test, we need to create some records with fake campaign and prospect list data,
66 //however we do need to create some targets for the prospect list
68 //create campaign tracker
69 $ct = new CampaignTracker();
70 $ct->tracker_name ='Campaign Log Unit Test Tracker';
71 $ct->tracker_url = 'sugarcrm.com';
72 $ct->campaign_id = $this->campaign_id;
74 $this->campaign_tracker = $ct;
77 //for each type, create an object and populate the campaignLog list
78 foreach($this->targetObjectArray as $type){
79 //skip campaign tracker
80 if($type == 'CampaignTracker'){
86 if ($type == 'Account'){
87 $bean->name = 'CampLog Unit Test Account';
89 $bean->first_name = 'CampaignLog';
90 $bean->last_name = 'Test '.$type;
92 $type_obj = 'target_'.$type;
94 $this->$type_obj = $bean;
97 $sea = new SugarEmailAddress();
98 $id = $this->$type_obj->id;
99 $module = $this->$type_obj->module_dir;
100 $new_addrs=array();$primary='';$replyTo='';$invalid='';$optOut='';$in_workflow=false;
101 $_REQUEST[$module.'_email_widget_id'] = 0;
102 $_REQUEST[$module.'0emailAddress0'] = $type.'UnitTest@example.com';
103 $_REQUEST[$module.'emailAddressPrimaryFlag'] = '0emailAddress0';
104 $_REQUEST[$module.'emailAddressVerifiedFlag0'] = 'true';
105 $_REQUEST[$module.'emailAddressVerifiedValue0'] = 'unitTest@sugarcrm.com';
106 $requestVariablesSet = array('0emailAddress0','emailAddressPrimaryFlag','emailAddressVerifiedFlag0','emailAddressVerifiedValue0');
107 $sea->save($id, $module, $new_addrs, $primary, $replyTo, $invalid, $optOut, $in_workflow);
108 //unset email request values for next run
109 foreach ($requestVariablesSet as $k)
110 unset($_REQUEST[$k]);
113 //now create the campaign log
114 $cl = new CampaignLog();
115 $cl->campaign_id = $this->campaign_id;
116 $cl->tracker_key = $ct->tracker_key;
117 $cl->target_id = $bean->id;
118 $cl->target_type = $bean->module_dir;
119 $cl->activity_type = 'targeted';//options are targeted (user was sent an email), link (user clicked on link), removed (user opted out) and viewed (viewed)
120 $cl->activity_date = date('Y-m-d H:i:s');
121 $cl->related_id = 'somebogusemailid'.date('His'); // this means link will not really work, but we are not testing email
122 $cl->related_type = 'Emails';
123 $cl->list_id = $this->prospect_list_id;
124 $cl->marketing_id = $this->email_marketing_id;
127 //keep last created campaign log bean to be used to call functions
128 $this->campaign_log = $cl;
133 public function tearDown()
135 global $current_user;
136 //for each type, delete the object and it's email
137 foreach($this->targetObjectArray as $type){
138 //skip campaign tracker
139 if($type == 'CampaignTracker'){
142 //create string to reference bean by
143 $type_obj = 'target_'.$type;
145 //remove the email address and relationship
146 $query = 'delete from email_addresses where email_address = \''.$type.'UnitTest@example.com\'';
147 $GLOBALS['db']->query($query);
148 $query = 'delete from email_addr_bean_rel where bean_id = \''.$this->$type_obj->id.'\'';
149 $GLOBALS['db']->query($query);
151 //remove the bean and delete record
152 $this->$type_obj->deleted = 1;
153 $this->$type_obj->save();
154 $GLOBALS['db']->query('DELETE FROM '.$this->$type_obj->table_name.' WHERE id = \''.$this->$type_obj->id.'\' ');
155 unset($this->$type_obj);
159 //delete the campaign logs and campaign tracker
160 $GLOBALS['db']->query('DELETE FROM campaign_log WHERE campaign_id = \''.$this->campaign_id.'\' ');
161 $GLOBALS['db']->query('DELETE FROM campaign_trkrs WHERE id = \''.$this->campaign_tracker->id.'\' ');
162 unset($this->campaign_tracker);
163 unset($this->campaign_log );SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
164 unset($GLOBALS['current_user']);
169 public function testGetListViewData(){
170 global $current_user;
171 $lvd = $this->campaign_log->get_list_view_data();
173 //make sure the returned value is an array
174 $this->assertTrue(is_array($lvd), 'CampaignLog->get_list_view_data should return an object of array type');
176 //make sure some of the expected values exist
177 $this->assertFalse(empty($lvd['CAMPAIGN_ID']), 'array element CAMPAIGN_ID is expected to exist when calling CampaignLog->get_list_view_data ');
178 $this->assertFalse(empty($lvd['TARGET_ID']), 'array element TARGET_ID is expected to exist when calling CampaignLog->get_list_view_data ');
181 public function testGetRelatedName(){
182 global $current_user,$locale;
184 foreach($this->targetObjectArray as $type){
185 //skip campaign tracker
186 if($type == 'CampaignTracker'){
189 //create string to reference bean by
190 $type_obj = 'target_'.$type;
192 //make sure the related name is coming in from the correct related type
193 $related_name = $this->campaign_log->get_related_name($this->$type_obj->id,$this->$type_obj->module_dir);
195 //make sure the returned name is formatted as expected
196 if ($type == 'Account'){
197 $this->assertSame($related_name, $this->$type_obj->name, 'name retrieved from campaign log does not match the expected name of '.$formatted_name.' for the related '.$type.' object');
198 }elseif ($type == 'User'){
199 $formatted_name = $this->$type_obj->id.$this->$type_obj->module_dir;
200 $this->assertSame($related_name, $formatted_name, 'name retrieved from campaign log does not match the expected formatted name of '.$formatted_name.' for the related '.$type.' object');
202 $formatted_name = $locale->getLocaleFormattedName($this->$type_obj->first_name, $this->$type_obj->last_name);
203 $this->assertSame($related_name, $formatted_name, 'name retrieved from campaign log does not match the expected formatted name of '.$formatted_name.' for the related '.$type.' object');
210 public function testRetrieveEmailAddress(){
211 global $current_user;
212 foreach($this->targetObjectArray as $type){
213 //skip campaign tracker
214 if($type == 'CampaignTracker'){
217 //create string to reference bean by
218 $type_obj = 'target_'.$type;
220 $eastring = $this->campaign_log->retrieve_email_address($this->$type_obj->id);
221 $this->assertSame($eastring, $type.'UnitTest@example.com', 'email retrieved from campaign log object type '.$type.'does not match the expected email of '.$type.'UnitTest@example.com');