2 require_once('modules/InboundEmail/InboundEmail.php');
3 require_once('include/TimeDate.php');
5 * This class is meant to test everything for InboundEmail
8 class InboundEmailTest extends Sugar_PHPUnit_Framework_TestCase
15 public function setUp()
17 global $inbound_account_id;
19 // the email server is down, so this test doesn't work
20 $this->markTestSkipped('Connection to mail server is down.');
22 if (empty($inbound_account_id)) {
23 $this->_setupTestUser();
24 $this->_createInboundAccount();
28 function _createInboundAccount() {
29 global $inbound_account_id, $current_user;
30 $stored_options = array();
31 $stored_options['from_name'] = "UnitTest";
32 $stored_options['from_addr'] = "ajaysales@sugarcrm.com";
33 $stored_options['reply_to_name'] = "UnitTest";
34 $stored_options['reply_to_addr'] = "ajaysales@sugarcrm.com";
35 $stored_options['only_since'] = false;
36 $stored_options['filter_domain'] = "";
37 $stored_options['trashFolder'] = "INBOX.Trash";
38 $stored_options['leaveMessagesOnMailServer'] = 1;
41 $focus = new InboundEmail();
42 $focus->name = "Ajay Sales Personal Unittest";
43 $focus->email_user = "ajaysales@sugarcrm.com";
44 $focus->email_password = "f00f004";
45 $focus->server_url = "mail.sugarcrm.com";
46 $focus->protocol = "imap";
47 $focus->mailbox = "INBOX";
50 $optimum = $focus->findOptimumSettings($useSsl);
52 $focus->service = $optimum['serial'];
53 $focus->is_personal = 1;
54 $focus->status = "Active";
55 $focus->mailbox_type = 'pick';
56 $focus->group_id = $current_user->id;
57 $teamId = User::getPrivateTeam($current_user->id);
58 $focus->team_id = $teamId;
59 $focus->team_set_id = $focus->getTeamSetIdForTeams($teamId);
60 $focus->stored_options = base64_encode(serialize($stored_options));
61 $inbound_account_id = $focus->save();
65 * retrieve an inbound account.
68 function _retrieveInboundAccount() {
69 global $inbound_account_id;
70 $focus = new InboundEmail();
71 $focus->retrieve($inbound_account_id);
72 $result = $focus->connectMailserver();
73 if ( $result == 'false' )
74 $this->markTestSkipped('Connection to mail server is down.');
79 * Create a folder in inbound account.
82 function testCreateFolder() {
83 $focus = $this->_retrieveInboundAccount();
84 $status = $focus->saveNewFolder("unittest1", "INBOX");
85 $this->assertTrue($status,"INBOX.unittest1 can not be created = " . $status);
89 * Delete a folder in inbound account.
92 function testDeleteFolder() {
93 global $inbound_account_id;
94 $focus = $this->_retrieveInboundAccount();
95 $statusArray = $focus->deleteFolder("INBOX.unittest1");
96 if ($statusArray['status']) {
97 $this->_tearDownInboundAccount($inbound_account_id);
98 unset($inbound_account_id);
100 $this->assertTrue($statusArray['status'],"INBOX.unittest1 can not be deleted");
103 public function testIdWithSingleQuotesCanBeInsertedIntoCacheTable()
105 $focus = new InboundEmail();
106 $focus->id = create_guid();
107 $focus->setCacheTimestamp("John's House");
109 $r = $focus->db->getOne('select id from inbound_email_cache_ts where id = \''.
110 $focus->db->quote("{$focus->id}_John's House").'\'');
112 $this->assertTrue($r !== false,"Could not find id \"{$focus->id}_John's House\" in inbound_email_cache_ts");
114 $focus->db->query('delete from inbound_email_cache_ts where id = \''.
115 $focus->db->quote("{$focus->id}_John's House").'\'');
119 * Remove anything that was used during this test
122 function tearDown() {
123 global $inbound_account_id;
124 $this->_tearDownTestUser();
128 * Delete this inbound account.
131 function _tearDownInboundAccount($inbound_account_id) {
132 $focus = new InboundEmail();
133 $focus->retrieve($inbound_account_id);
134 $focus->mark_deleted($inbound_account_id);
135 $focus->db->query("delete from inbound_email WHERE id = '{$inbound_account_id}'");
142 function _setupTestUser() {
143 global $current_user;
144 $this->_user = SugarTestUserUtilities::createAnonymousUser();
145 $GLOBALS['current_user'] = $this->_user;
146 $current_user = $this->_user;
147 $this->_user->status = 'Active';
148 $this->_user->is_admin = 1;
149 $this->_user->save();
153 * Remove user created for test
156 function _tearDownTestUser() {
157 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
158 unset($GLOBALS['current_user']);