]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/InboundEmail/InboundEmailTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / InboundEmail / InboundEmailTest.php
1 <?php
2 require_once('modules/InboundEmail/InboundEmail.php');
3 require_once('include/TimeDate.php');
4 /**
5  * This class is meant to test everything for InboundEmail
6  *
7  */
8 class InboundEmailTest extends Sugar_PHPUnit_Framework_TestCase
9 {
10         var $_user = null;
11     /**
12      * Create test user
13      *
14      */
15         public function setUp() 
16         {
17         global $inbound_account_id;
18         
19         // the email server is down, so this test doesn't work
20         $this->markTestSkipped('Connection to mail server is down.');
21         
22         if (empty($inbound_account_id)) {
23                 $this->_setupTestUser();
24                 $this->_createInboundAccount();
25         } // IF
26     }
27
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;
39                 
40         $useSsl = false;
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";
48         $focus->port = "143";
49         
50                 $optimum = $focus->findOptimumSettings($useSsl);
51         
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();
62     } // fn
63     
64         /**
65          * retrieve an inbound account.
66          *
67          */
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.');
75                 return $focus;
76     } // fn
77     
78         /**
79          * Create a folder in inbound account.
80          *
81          */
82     function testCreateFolder() {
83         $focus = $this->_retrieveInboundAccount();
84         $status = $focus->saveNewFolder("unittest1", "INBOX");
85         $this->assertTrue($status,"INBOX.unittest1 can not be created = " . $status);
86     } // fn
87     
88         /**
89          * Delete a folder in inbound account.
90          *
91          */
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);
99         }
100         $this->assertTrue($statusArray['status'],"INBOX.unittest1 can not be deleted");
101     }
102     
103         public function testIdWithSingleQuotesCanBeInsertedIntoCacheTable()
104     {
105         $focus = new InboundEmail();
106         $focus->id = create_guid();
107         $focus->setCacheTimestamp("John's House");
108         
109         $r = $focus->db->getOne('select id from inbound_email_cache_ts where id = \''.
110             $focus->db->quote("{$focus->id}_John's House").'\'');
111         
112         $this->assertTrue($r !== false,"Could not find id \"{$focus->id}_John's House\" in inbound_email_cache_ts");
113         
114         $focus->db->query('delete from inbound_email_cache_ts where id = \''.
115             $focus->db->quote("{$focus->id}_John's House").'\'');
116     }
117     
118     /**
119      * Remove anything that was used during this test
120      *
121      */
122     function tearDown() {
123         global $inbound_account_id;
124         $this->_tearDownTestUser();
125     }
126
127         /**
128          * Delete this inbound account.
129          *
130          */
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}'");
136     }
137     
138     /**
139      * Create a test user
140      *
141      */
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();
150     }
151         
152     /**
153      * Remove user created for test
154      *
155      */
156         function _tearDownTestUser() {
157        SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
158        unset($GLOBALS['current_user']);
159     }
160     
161 }
162 ?>