]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/InboundEmail/InboundEmailTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / InboundEmail / InboundEmailTest.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  
38 require_once('modules/InboundEmail/InboundEmail.php');
39 require_once('include/TimeDate.php');
40 /**
41  * This class is meant to test everything for InboundEmail
42  *
43  */
44 class InboundEmailTest extends Sugar_PHPUnit_Framework_TestCase
45 {
46         var $_user = null;
47     /**
48      * Create test user
49      *
50      */
51         public function setUp() 
52         {
53         global $inbound_account_id;
54         
55         // the email server is down, so this test doesn't work
56         $this->markTestSkipped('Connection to mail server is down.');
57         
58         if (empty($inbound_account_id)) {
59                 $this->_setupTestUser();
60                 $this->_createInboundAccount();
61         } // IF
62     }
63
64     function _createInboundAccount() { 
65         global $inbound_account_id, $current_user;      
66                 $stored_options = array();
67                 $stored_options['from_name'] = "UnitTest";
68                 $stored_options['from_addr'] = "ajaysales@sugarcrm.com";
69                 $stored_options['reply_to_name'] = "UnitTest";
70                 $stored_options['reply_to_addr'] = "ajaysales@sugarcrm.com";
71                 $stored_options['only_since'] = false;
72                 $stored_options['filter_domain'] = "";
73         $stored_options['trashFolder'] = "INBOX.Trash";
74                 $stored_options['leaveMessagesOnMailServer'] = 1;
75                 
76         $useSsl = false;
77                 $focus = new InboundEmail();
78         $focus->name = "Ajay Sales Personal Unittest";
79         $focus->email_user = "ajaysales@sugarcrm.com";
80         $focus->email_password = "f00f004";
81         $focus->server_url = "mail.sugarcrm.com";
82         $focus->protocol = "imap";
83         $focus->mailbox = "INBOX";
84         $focus->port = "143";
85         
86                 $optimum = $focus->findOptimumSettings($useSsl);
87         
88                 $focus->service = $optimum['serial'];
89                 $focus->is_personal = 1;
90                 $focus->status = "Active";
91                 $focus->mailbox_type = 'pick';
92                 $focus->group_id = $current_user->id;
93                 $teamId = User::getPrivateTeam($current_user->id);
94                 $focus->team_id = $teamId;
95                 $focus->team_set_id = $focus->getTeamSetIdForTeams($teamId);
96                 $focus->stored_options = base64_encode(serialize($stored_options));
97                 $inbound_account_id = $focus->save();
98     } // fn
99     
100         /**
101          * retrieve an inbound account.
102          *
103          */
104     function _retrieveInboundAccount() {
105         global $inbound_account_id;
106                 $focus = new InboundEmail();
107                 $focus->retrieve($inbound_account_id);
108                 $result = $focus->connectMailserver();
109                 if ( $result == 'false' )
110                     $this->markTestSkipped('Connection to mail server is down.');
111                 return $focus;
112     } // fn
113     
114         /**
115          * Create a folder in inbound account.
116          *
117          */
118     function testCreateFolder() {
119         $focus = $this->_retrieveInboundAccount();
120         $status = $focus->saveNewFolder("unittest1", "INBOX");
121         $this->assertTrue($status,"INBOX.unittest1 can not be created = " . $status);
122     } // fn
123     
124         /**
125          * Delete a folder in inbound account.
126          *
127          */
128     function testDeleteFolder() {
129         global $inbound_account_id;
130                 $focus = $this->_retrieveInboundAccount();
131                 $statusArray = $focus->deleteFolder("INBOX.unittest1");
132         if ($statusArray['status']) {
133                 $this->_tearDownInboundAccount($inbound_account_id);
134                 unset($inbound_account_id);
135         }
136         $this->assertTrue($statusArray['status'],"INBOX.unittest1 can not be deleted");
137     }
138     
139         public function testIdWithSingleQuotesCanBeInsertedIntoCacheTable()
140     {
141         $focus = new InboundEmail();
142         $focus->id = create_guid();
143         $focus->setCacheTimestamp("John's House");
144         
145         $r = $focus->db->getOne('select id from inbound_email_cache_ts where id = \''.
146             $focus->db->quote("{$focus->id}_John's House").'\'');
147         
148         $this->assertTrue($r !== false,"Could not find id \"{$focus->id}_John's House\" in inbound_email_cache_ts");
149         
150         $focus->db->query('delete from inbound_email_cache_ts where id = \''.
151             $focus->db->quote("{$focus->id}_John's House").'\'');
152     }
153     
154     /**
155      * Remove anything that was used during this test
156      *
157      */
158     function tearDown() {
159         global $inbound_account_id;
160         $this->_tearDownTestUser();
161     }
162
163         /**
164          * Delete this inbound account.
165          *
166          */
167     function _tearDownInboundAccount($inbound_account_id) {
168                 $focus = new InboundEmail();
169                 $focus->retrieve($inbound_account_id);
170                 $focus->mark_deleted($inbound_account_id);
171                 $focus->db->query("delete from inbound_email WHERE id = '{$inbound_account_id}'");
172     }
173     
174     /**
175      * Create a test user
176      *
177      */
178         function _setupTestUser() {
179                 global $current_user;
180         $this->_user = SugarTestUserUtilities::createAnonymousUser();
181         $GLOBALS['current_user'] = $this->_user;
182         $current_user = $this->_user;
183         $this->_user->status = 'Active';
184         $this->_user->is_admin = 1;
185         $this->_user->save();
186     }
187         
188     /**
189      * Remove user created for test
190      *
191      */
192         function _tearDownTestUser() {
193        SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
194        unset($GLOBALS['current_user']);
195     }
196     
197 }
198 ?>