2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 ********************************************************************************/
38 require_once('modules/InboundEmail/InboundEmail.php');
39 require_once('include/TimeDate.php');
41 * This class is meant to test everything for InboundEmail
44 class InboundEmailTest extends Sugar_PHPUnit_Framework_TestCase
51 public function setUp()
53 global $inbound_account_id;
55 // the email server is down, so this test doesn't work
56 $this->markTestSkipped('Connection to mail server is down.');
58 if (empty($inbound_account_id)) {
59 $this->_setupTestUser();
60 $this->_createInboundAccount();
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;
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";
86 $optimum = $focus->findOptimumSettings($useSsl);
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();
101 * retrieve an inbound account.
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.');
115 * Create a folder in inbound account.
118 function testCreateFolder() {
119 $focus = $this->_retrieveInboundAccount();
120 $status = $focus->saveNewFolder("unittest1", "INBOX");
121 $this->assertTrue($status,"INBOX.unittest1 can not be created = " . $status);
125 * Delete a folder in inbound account.
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);
136 $this->assertTrue($statusArray['status'],"INBOX.unittest1 can not be deleted");
139 public function testIdWithSingleQuotesCanBeInsertedIntoCacheTable()
141 $focus = new InboundEmail();
142 $focus->id = create_guid();
143 $focus->setCacheTimestamp("John's House");
145 $r = $focus->db->getOne('select id from inbound_email_cache_ts where id = \''.
146 $focus->db->quote("{$focus->id}_John's House").'\'');
148 $this->assertTrue($r !== false,"Could not find id \"{$focus->id}_John's House\" in inbound_email_cache_ts");
150 $focus->db->query('delete from inbound_email_cache_ts where id = \''.
151 $focus->db->quote("{$focus->id}_John's House").'\'');
155 * Remove anything that was used during this test
158 function tearDown() {
159 global $inbound_account_id;
160 $this->_tearDownTestUser();
164 * Delete this inbound account.
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}'");
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();
189 * Remove user created for test
192 function _tearDownTestUser() {
193 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
194 unset($GLOBALS['current_user']);