]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarFolders/SugarFoldersTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / SugarFolders / SugarFoldersTest.php
1 <?php 
2 require_once('include/SugarFolders/SugarFolders.php');
3
4
5 class SugarFoldersTest extends Sugar_PHPUnit_Framework_TestCase
6 {
7         var $folder = null;
8         var $additionalFolders = null;
9     var $_user = null;
10     var $emails = null;
11     
12     
13         public function setUp()
14     {
15         global $current_user, $currentModule;
16
17         $this->_user = SugarTestUserUtilities::createAnonymousUser();
18         $GLOBALS['current_user'] = $this->_user;
19                 $this->folder = new SugarFolder(); 
20                 $this->additionalFolders = array();
21                 $this->emails = array();
22         }
23
24     public function tearDown()
25     {
26         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
27         unset($GLOBALS['current_user']);
28         
29         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
30         $this->_clearFolder($this->folder->id);
31         
32         foreach ($this->additionalFolders as $additionalID)
33             $this->_clearFolder($additionalID);
34         
35         foreach ($this->emails as $emailID)
36             $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
37         
38         unset($this->folder);
39     }
40
41     /**
42      * Test the Set Folder method.
43      *
44      */
45     function testSetFolder()
46     {
47         //Set folder
48         $this->folder->id = create_guid();
49         $this->folder->new_with_id = TRUE;
50         
51         $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
52                         );
53         
54         $this->folder->setFolder($fields);
55         
56         //Retrieve newly created folder
57         $error_message = "Unable to set folder.";
58         $this->folder->retrieve($this->folder->id);
59
60         $this->assertEquals($fields['name'], $this->folder->name, $error_message );
61         $this->assertEquals($fields['parent_folder'], $this->folder->parent_folder, $error_message );
62         $this->assertEquals($this->_user->id, $this->folder->assign_to_id, $error_message ); 
63         
64         //Check for folder subscriptions create for global user
65         $sub_ids = $this->folder->getSubscriptions($GLOBALS['current_user']);
66         $this->assertEquals(1, count($sub_ids), $error_message);
67         $this->assertEquals($this->folder->id, $sub_ids[0], $error_message);
68         
69     }
70     
71     /**
72      * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
73      *
74      */
75     function testFolderSubscriptions()
76     {
77         $this->_createNewSugarFolder();
78         $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
79         
80         //Clear subscriptions
81         $this->folder->clearSubscriptions();
82         $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
83         $this->assertEquals(0, count($subs), $error_message);
84         
85         //Add a subscription
86         $this->folder->insertFolderSubscription($this->folder->id,$GLOBALS['current_user']->id);
87         $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
88         $this->assertEquals(1, count($subs), $error_message);
89         
90         //Clear subscriptions for a paricular folder
91         $this->folder->clearSubscriptionsForFolder($this->folder->id);
92         $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
93         $this->assertEquals(0, count($subs), $error_message);
94     }
95     
96     /**
97      * Test the getParentIDRecursive function which is used to find a grouping of folders.
98      *
99      */
100     function testgetParentIDRecursive()
101     {
102         $f1 = new SugarFolder();
103         $f12 = new SugarFolder();
104         $f3 = new SugarFolder();
105         
106         $f1->id = create_guid();
107         $f1->new_with_id = TRUE;
108         
109         $f12->id = create_guid();
110         $f12->new_with_id = TRUE;
111         
112         $f3->id = create_guid();
113         $f3->new_with_id = TRUE;
114         
115         $f12->parent_folder = $f1->id;
116         $f1->save();
117         $f12->save();
118         $f3->save();
119         
120         $this->additionalFolders[] = $f1->id;
121         $this->additionalFolders[] = $f12->id;
122         $this->additionalFolders[] = $f3->id;
123         
124         
125         $parentIDs = $this->folder->getParentIDRecursive($f12->id); //Includes itself in the return list.
126         $this->assertEquals(2, count($parentIDs), "Unable to retrieve parent ids recursively");
127         
128         $parentIDs = $this->folder->getParentIDRecursive($f3->id); //Includes itself in the return list.
129         $this->assertEquals(1, count($parentIDs), "Unable to retrieve parent ids recursively");
130         
131         //Find the children by going the other way.
132         $childrenArray = array();
133         $this->folder->findAllChildren($f1->id,$childrenArray);
134         $this->assertEquals(1, count($childrenArray), "Unable to retrieve child ids recursively");
135         
136         $childrenArray = array();
137         $this->folder->findAllChildren($f3->id,$childrenArray);
138         $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
139     }
140     
141     /**
142      * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
143      *
144      */
145     function testGetUserFolders()
146     {
147         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
148         require_once('modules/Emails/EmailUI.php');
149         $emailUI = new EmailUI();
150         $emailUI->preflightUser($GLOBALS['current_user']);
151         $error_message = "Unable to get user folders";
152         $rootNode = new ExtNode('','');
153
154         $folderOpenState = "";
155         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
156
157         $this->assertEquals(1, count($ret), $error_message);
158         $this->assertEquals($GLOBALS['mod_strings']['LNK_MY_INBOX'], $ret[0]['text'], $error_message);
159         //Should contain 'My Drafts' and 'My Sent Mail'
160         $this->assertEquals(2, count($ret[0]['children']), $error_message);
161
162     }
163     
164     /**
165      * Test the addBean, getCountUnread,getCountItems functions.
166      *
167      */
168     function testAddBean()
169     {
170         $emailParams = array('status' => 'unread');
171         $email = $this->_createEmailObject($emailParams);
172         $this->emails[] = $email->id;
173         
174         $this->_createNewSugarFolder();
175         
176         $cnt = $this->folder->getCountUnread($this->folder->id);
177         $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
178         
179         $this->folder->addBean($email,$GLOBALS['current_user']);
180         
181         $cnt = $this->folder->getCountUnread($this->folder->id);
182         $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
183         
184         //Create a second email obj with status read
185         $emailParams = array('status' => 'read');
186         $email = $this->_createEmailObject($emailParams);
187         $this->emails[] = $email->id;
188         $this->folder->addBean($email,$GLOBALS['current_user']);
189         
190         $cnt = $this->folder->getCountItems($this->folder->id);
191         $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
192         
193         
194     }
195     
196     /**
197      * Tests sugar folder methods that deal with emails.
198      *
199      */
200     function testFolderEmailMethods()
201     {
202         
203         $emailParams = array('status' => 'read');
204         $email = $this->_createEmailObject($emailParams);
205         $this->emails[] = $email->id;
206         
207         $this->_createNewSugarFolder();
208         $this->folder->addBean($email,$GLOBALS['current_user']);
209         
210         $emailExists = $this->folder->checkEmailExistForFolder($email->id);
211         $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
212         
213         //Remove the specific email from our folder.
214         
215         $this->folder->deleteEmailFromFolder($email->id);
216         $emailExists = $this->folder->checkEmailExistForFolder($email->id);
217         $this->assertFalse($emailExists, "Unable to check for emails with a specific folder.");
218         
219         //Move the Email bean from one folder to another
220         $f3 = new SugarFolder();
221         $f3->id = create_guid();
222         $f3->new_with_id = TRUE;
223         $f3->save();
224         $this->additionalFolders[] = $f3->id;
225         
226         $this->folder->addBean($email,$GLOBALS['current_user']);
227         
228         $emailExists = $f3->checkEmailExistForFolder($email->id);
229         $this->assertFalse($emailExists);
230         
231         $this->folder->move($this->folder->id, $f3->id,$email->id);
232         $emailExists = $f3->checkEmailExistForFolder($email->id);
233         $this->assertTrue($emailExists, "Unable to move Emails bean to a different sugar folder");
234         
235     }
236     
237     /**
238      * Test retreiving a list of emails for a particular folder.
239      *
240      */
241     function testGetListItemsForEmailXML()
242     {
243         //Create the my Emails Folder
244         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
245         require_once('modules/Emails/EmailUI.php');
246         $emailUI = new EmailUI();
247         $emailUI->preflightUser($GLOBALS['current_user']);
248         $error_message = "Unable to get list items for email.";
249         $rootNode = new ExtNode('','');
250
251         $folderOpenState = "";
252         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
253
254         $this->assertEquals(1, count($ret), $error_message);
255         $folderID = $ret[0]['id'];
256         
257         //Create the Email Object
258         $emailParams = array('status' => 'unread','assigned_user_id' => $GLOBALS['current_user']->id);
259         $email = $this->_createEmailObject($emailParams);
260         $this->emails[] = $email->id;
261        
262         //Add Email Object to My Email Folder
263         $my_email = new SugarFolder();
264         $my_email->retrieve($folderID);
265         $my_email->addBean($email,$GLOBALS['current_user']);
266         
267         //Make sure the email was added to the folder.
268         $emailExists = $my_email->checkEmailExistForFolder($email->id);
269         $this->assertTrue($emailExists, $error_message);
270         //Get the list of emails.
271         $emailList = $my_email->getListItemsForEmailXML($folderID);
272         
273         $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
274
275     }
276     
277     
278     function _createEmailObject($additionalParams = array() )
279     {
280         global $timedate;
281         
282         $em = new Email();
283                 $em->name = 'tst_' . uniqid();
284                 $em->type = 'inbound';
285                 $em->intent = 'pick';
286                 $em->date_sent = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ))) ; //Two days from today 
287                 
288                 foreach ($additionalParams as $k => $v)
289                   $em->$k = $v;
290                   
291                 $em->save();
292             
293                 return $em;
294     }
295     
296     function _createNewSugarFolder()
297     {
298         $this->folder->id = create_guid();
299         $this->folder->new_with_id = TRUE;
300         $this->folder->name = "UNIT TEST";
301         $this->folder->save();
302         
303     }
304     
305     private function _clearFolder($folder_id)
306     {
307         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
308         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE folder_id='{$folder_id}'");
309         $GLOBALS['db']->query("DELETE FROM folders WHERE id='{$folder_id}'");
310     }
311 }
312 ?>