]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/InboundEmail/Bug49784Test.php
Release 6.5.10
[Github/sugarcrm.git] / tests / modules / InboundEmail / Bug49784Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2013 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
38
39 require_once('include/utils/LogicHook.php');
40 require_once('modules/InboundEmail/InboundEmail.php');
41 require_once('modules/Accounts/Account.php');
42 require_once('modules/Cases/Case.php');
43 require_once('modules/Emails/Email.php');
44
45
46 /*
47  * This simulates the processing of an inbound email that is to be linked to a case.
48  * We create a logic hook attached to after_relationship_add, which should only run once.
49  * Logic is borrowed heavily from Bug46122Test.php
50  * @ticket 49784
51  */
52 class Bug49784Test extends Sugar_PHPUnit_Framework_TestCase
53 {
54     var $hasCustomCasesLogicHookFile = false;
55     var $casesHookFile = 'custom/modules/Cases/logic_hooks.php';
56     var $casesCountFile = 'custom/modules/Cases/count.php';
57     var $user ='';
58     var $case ='';
59     var $account='';
60     var $email ='';
61
62     public function setUp()
63     {
64         $this->user = SugarTestUserUtilities::createAnonymousUser();
65         $GLOBALS['current_user'] = $this->user;
66
67         //Setup logichook files
68         if(file_exists($this->casesHookFile))
69         {
70             $this->hasCustomCasesLogicHookFile = true;
71             copy($this->casesHookFile, $this->casesHookFile.'.bak');
72         }
73         $hook_array['after_relationship_add'][] = Array(1, 'Cases increment count', $this->casesCountFile,'CaseCount', 'countMe');
74         write_array_to_file("hook_array", $hook_array, $this->casesHookFile);
75         $this->useOutputBuffering = false;
76         LogicHook::refreshHooks();
77
78         //now  write out the script that the logichook executes.  This will keep track of times called
79         global $hookRunCount;
80         $hookRunCount = 0;
81         $fileCont = '<?php class CaseCount {
82             function countMe($bean, $event, $arguments){
83                 global $hookRunCount;
84                 if($event =="after_relationship_add" && $arguments["module"]=="Cases" && $arguments["related_module"]=="Emails")
85                     $hookRunCount++;
86                 }}?>';
87         file_put_contents($this->casesCountFile, $fileCont);
88
89
90         //setup test account for case
91                 $this->account = new Account();
92         $this->account->name = 'test account for bug 39855';
93         $this->account->assigned_user_id = 'SugarUser';
94         $this->account->save();
95
96         //create case
97         $this->case = new aCase();
98         $this->case->name = 'test case for unitTest 49784';
99         $this->case->account_id = $this->account->id;
100         $this->case->status = 'New';
101         $this->case->save();
102         //retrieve so we have latest info (case number)
103         $this->case->retrieve($this->case->id);
104
105
106         //create email with case in subject
107         $this->email = new Email();
108         $this->email->type = 'inbound';
109         $this->email->status = 'unread';
110         $this->email->from_addr_name = $this->email->cleanEmails("sender@domain.eu");
111         $this->email->to_addrs_names = $this->email->cleanEmails("to@domain.eu");
112         $this->email->cc_addrs_names = $this->email->cleanEmails("cc@domain.eu");
113         $this->email->name = 'RE: [CASE:'.$this->case->case_number.'] '.$this->case->name;
114         $this->email->save();
115
116     }
117
118     public function tearDown()
119     {
120         //Remove the custom logic hook files
121         if($this->hasCustomCasesLogicHookFile && file_exists($this->casesHookFile.'.bak'))
122         {
123             copy($this->casesHookFile.'.bak', $this->casesHookFile);
124             unlink($this->casesHookFile.'.bak');
125         } else if(file_exists($this->casesHookFile)) {
126             unlink($this->casesHookFile);
127         }
128         unlink($this->casesCountFile);
129         $GLOBALS['db']->query("delete from emails where id='{$this->email->id}'");
130         $GLOBALS['db']->query("delete from accounts where id='{$this->account->id}'");
131         $GLOBALS['db']->query("delete from cases where id='{$this->case->id}'");
132
133
134         unset($GLOBALS['logic_hook']);
135         unset($this->account);
136         unset($this->case);
137         unset($this->email);
138         unset($this->user);
139         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
140     }
141
142     public function testCaseHandlingOfInboundEmail()
143     {
144         //lets make sure the file exists
145         $this->assertFileExists($this->casesCountFile, 'file to be run from logic hook that keeps track of execution count was not written');
146
147         //create a new inbound email and call process that links to case
148         $i = new InboundEmail();
149         $i->handleCaseAssignment($this->email);
150
151         //make sure the logic hook only ran once
152         $this->assertEquals(1, $GLOBALS['hookRunCount'], 'Logic hook should only have run once during the inbound email processing.');
153     }
154
155 }
156
157 ?>