]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/CampaignLog/CampaignLogTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / modules / CampaignLog / CampaignLogTest.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 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';
44
45 class CampaignLogTest extends Sugar_PHPUnit_Framework_TestCase
46 {
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');
51
52         var $target_Prospect;
53         var $target_Contact;
54         var $target_User;
55         var $target_Account;
56         var $campaign_tracker;
57         var     $campaign_log;
58
59
60     public function setup()
61     {
62                 global $current_user;
63
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
67
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;
73                 $ct->save();
74                 $this->campaign_tracker = $ct;
75
76
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'){
81                                 continue;
82                         }
83
84                         //create the new bean
85                         $bean = new $type();
86                         if ($type == 'Account'){
87                                 $bean->name = 'CampLog Unit Test Account';
88                         }else{
89                                 $bean->first_name = 'CampaignLog';
90                                 $bean->last_name = 'Test '.$type;
91                         }
92                         $type_obj = 'target_'.$type;
93                         $bean->save();
94                         $this->$type_obj = $bean;
95
96                         //save email
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]);
111
112
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;
125                         $cl->save();
126                 }
127                 //keep last created campaign log bean to be used to call functions
128                 $this->campaign_log = $cl;
129
130
131     }
132
133     public function tearDown()
134     {
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'){
140                                 continue;
141                         }
142                         //create string to reference bean by
143                         $type_obj = 'target_'.$type;
144
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);
150
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);
156
157                 }
158
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']);
165     }
166
167
168
169         public function testGetListViewData(){
170                 global $current_user;
171                 $lvd = $this->campaign_log->get_list_view_data();
172
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');
175
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 ');
179         }
180
181         public function testGetRelatedName(){
182                 global $current_user,$locale;
183
184                 foreach($this->targetObjectArray as $type){
185                         //skip campaign tracker
186                         if($type == 'CampaignTracker'){
187                                 continue;
188                         }
189                         //create string to reference bean by
190                         $type_obj = 'target_'.$type;
191
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);
194
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');
201                         }else{
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');
204                         }
205
206                 }
207
208         }
209
210         public function testRetrieveEmailAddress(){
211                 global $current_user;
212                 foreach($this->targetObjectArray as $type){
213                         //skip campaign tracker
214                         if($type == 'CampaignTracker'){
215                                 continue;
216                         }
217                         //create string to reference bean by
218                         $type_obj = 'target_'.$type;
219
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');
222                 }
223
224         }
225
226
227 }