PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modules/CampaignLog/CampaignLogTest.php

https://github.com/item/sugarcrm_dev
PHP | 194 lines | 135 code | 35 blank | 24 comment | 13 complexity | 47716749c59e16b1c839e7986d762f02 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'modules/Accounts/Account.php';
  3. require_once 'modules/Campaigns/Campaign.php';
  4. require_once 'modules/CampaignLog/CampaignLog.php';
  5. require_once 'modules/CampaignTrackers/CampaignTracker.php';
  6. require_once 'modules/Contacts/Contact.php';
  7. require_once 'modules/Users/User.php';
  8. require_once 'include/SugarEmailAddress/SugarEmailAddress.php';
  9. class CampaignLogTest extends Sugar_PHPUnit_Framework_TestCase
  10. {
  11. var $campaign_id = 'campaignforcamplogunittest';
  12. var $prospect_list_id = 'prospectlistforcamplogunittest';
  13. var $email_marketing_id = 'marketingforcamplogunittest';
  14. var $targetObjectArray = array('User','Contact','Lead','Prospect','Account','CampaignTracker');
  15. var $target_Prospect;
  16. var $target_Contact;
  17. var $target_User;
  18. var $target_Account;
  19. var $campaign_tracker;
  20. var $campaign_log;
  21. public function setup()
  22. {
  23. global $current_user;
  24. $current_user = SugarTestUserUtilities::createAnonymousUser();
  25. //for the purpose of this test, we need to create some records with fake campaign and prospect list data,
  26. //however we do need to create some targets for the prospect list
  27. //create campaign tracker
  28. $ct = new CampaignTracker();
  29. $ct->tracker_name ='Campaign Log Unit Test Tracker';
  30. $ct->tracker_url = 'sugarcrm.com';
  31. $ct->campaign_id = $this->campaign_id;
  32. $ct->save();
  33. $this->campaign_tracker = $ct;
  34. //for each type, create an object and populate the campaignLog list
  35. foreach($this->targetObjectArray as $type){
  36. //skip campaign tracker
  37. if($type == 'CampaignTracker'){
  38. continue;
  39. }
  40. //create the new bean
  41. $bean = new $type();
  42. if ($type == 'Account'){
  43. $bean->name = 'CampLog Unit Test Account';
  44. }else{
  45. $bean->first_name = 'CampaignLog';
  46. $bean->last_name = 'Test '.$type;
  47. }
  48. $type_obj = 'target_'.$type;
  49. $bean->save();
  50. $this->$type_obj = $bean;
  51. //save email
  52. $sea = new SugarEmailAddress();
  53. $id = $this->$type_obj->id;
  54. $module = $this->$type_obj->module_dir;
  55. $new_addrs=array();$primary='';$replyTo='';$invalid='';$optOut='';$in_workflow=false;
  56. $_REQUEST[$module.'_email_widget_id'] = 0;
  57. $_REQUEST[$module.'0emailAddress0'] = $type.'UnitTest@example.com';
  58. $_REQUEST[$module.'emailAddressPrimaryFlag'] = '0emailAddress0';
  59. $_REQUEST[$module.'emailAddressVerifiedFlag0'] = 'true';
  60. $_REQUEST[$module.'emailAddressVerifiedValue0'] = 'unitTest@sugarcrm.com';
  61. $requestVariablesSet = array('0emailAddress0','emailAddressPrimaryFlag','emailAddressVerifiedFlag0','emailAddressVerifiedValue0');
  62. $sea->save($id, $module, $new_addrs, $primary, $replyTo, $invalid, $optOut, $in_workflow);
  63. //unset email request values for next run
  64. foreach ($requestVariablesSet as $k)
  65. unset($_REQUEST[$k]);
  66. //now create the campaign log
  67. $cl = new CampaignLog();
  68. $cl->campaign_id = $this->campaign_id;
  69. $cl->tracker_key = $ct->tracker_key;
  70. $cl->target_id = $bean->id;
  71. $cl->target_type = $bean->module_dir;
  72. $cl->activity_type = 'targeted';//options are targeted (user was sent an email), link (user clicked on link), removed (user opted out) and viewed (viewed)
  73. $cl->activity_date = date('Y-m-d H:i:s');
  74. $cl->related_id = 'somebogusemailid'.date('His'); // this means link will not really work, but we are not testing email
  75. $cl->related_type = 'Emails';
  76. $cl->list_id = $this->prospect_list_id;
  77. $cl->marketing_id = $this->email_marketing_id;
  78. $cl->save();
  79. }
  80. //keep last created campaign log bean to be used to call functions
  81. $this->campaign_log = $cl;
  82. }
  83. public function tearDown()
  84. {
  85. global $current_user;
  86. //for each type, delete the object and it's email
  87. foreach($this->targetObjectArray as $type){
  88. //skip campaign tracker
  89. if($type == 'CampaignTracker'){
  90. continue;
  91. }
  92. //create string to reference bean by
  93. $type_obj = 'target_'.$type;
  94. //remove the email address and relationship
  95. $query = 'delete from email_addresses where email_address = \''.$type.'UnitTest@example.com\';';
  96. $GLOBALS['db']->query($query);
  97. $query = 'delete from email_addr_bean_rel where bean_id = \''.$this->$type_obj->id.'\';';
  98. $GLOBALS['db']->query($query);
  99. //remove the bean and delete record
  100. $this->$type_obj->deleted = 1;
  101. $this->$type_obj->save();
  102. $GLOBALS['db']->query('DELETE FROM '.$this->$type_obj->table_name.' WHERE id = \''.$this->$type_obj->id.'\' ');
  103. unset($this->$type_obj);
  104. }
  105. //delete the campaign logs and campaign tracker
  106. $GLOBALS['db']->query('DELETE FROM campaign_log WHERE campaign_id = \''.$this->campaign_id.'\' ');
  107. $GLOBALS['db']->query('DELETE FROM campaign_tracker WHERE id = \''.$this->campaign_tracker->id.'\' ');
  108. unset($this->campaign_tracker);
  109. unset($this->campaign_log );SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
  110. unset($GLOBALS['current_user']);
  111. }
  112. public function testGetListViewData(){
  113. global $current_user;
  114. $lvd = $this->campaign_log->get_list_view_data();
  115. //make sure the returned value is an array
  116. $this->assertTrue(is_array($lvd), 'CampaignLog->get_list_view_data should return an object of array type');
  117. //make sure some of the expected values exist
  118. $this->assertFalse(empty($lvd['CAMPAIGN_ID']), 'array element CAMPAIGN_ID is expected to exist when calling CampaignLog->get_list_view_data ');
  119. $this->assertFalse(empty($lvd['TARGET_ID']), 'array element TARGET_ID is expected to exist when calling CampaignLog->get_list_view_data ');
  120. }
  121. public function testGetRelatedName(){
  122. global $current_user,$locale;
  123. foreach($this->targetObjectArray as $type){
  124. //skip campaign tracker
  125. if($type == 'CampaignTracker'){
  126. continue;
  127. }
  128. //create string to reference bean by
  129. $type_obj = 'target_'.$type;
  130. //make sure the related name is coming in from the correct related type
  131. $related_name = $this->campaign_log->get_related_name($this->$type_obj->id,$this->$type_obj->module_dir);
  132. //make sure the returned name is formatted as expected
  133. if ($type == 'Account'){
  134. $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');
  135. }elseif ($type == 'User'){
  136. $formatted_name = $this->$type_obj->id.$this->$type_obj->module_dir;
  137. $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');
  138. }else{
  139. $bean->first_name = 'CampaignLog';
  140. $bean->last_name = 'Test '.$type;
  141. $formatted_name = $locale->getLocaleFormattedName($this->$type_obj->first_name, $this->$type_obj->last_name);
  142. $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');
  143. }
  144. }
  145. }
  146. public function testRetrieveEmailAddress(){
  147. global $current_user;
  148. foreach($this->targetObjectArray as $type){
  149. //skip campaign tracker
  150. if($type == 'CampaignTracker'){
  151. continue;
  152. }
  153. //create string to reference bean by
  154. $type_obj = 'target_'.$type;
  155. $eastring = $this->campaign_log->retrieve_email_address($this->$type_obj->id);
  156. $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');
  157. }
  158. }
  159. }