2 require_once('include/SugarFolders/SugarFolders.php');
5 class SugarFoldersTest extends Sugar_PHPUnit_Framework_TestCase
8 var $additionalFolders = null;
13 public function setUp()
15 global $current_user, $currentModule;
17 $this->_user = SugarTestUserUtilities::createAnonymousUser();
18 $GLOBALS['current_user'] = $this->_user;
19 $this->folder = new SugarFolder();
20 $this->additionalFolders = array();
21 $this->emails = array();
24 public function tearDown()
26 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
27 unset($GLOBALS['current_user']);
29 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
30 $this->_clearFolder($this->folder->id);
32 foreach ($this->additionalFolders as $additionalID)
33 $this->_clearFolder($additionalID);
35 foreach ($this->emails as $emailID)
36 $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
42 * Test the Set Folder method.
45 function testSetFolder()
48 $this->folder->id = create_guid();
49 $this->folder->new_with_id = TRUE;
51 $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
54 $this->folder->setFolder($fields);
56 //Retrieve newly created folder
57 $error_message = "Unable to set folder.";
58 $this->folder->retrieve($this->folder->id);
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 );
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);
72 * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
75 function testFolderSubscriptions()
77 $this->_createNewSugarFolder();
78 $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
81 $this->folder->clearSubscriptions();
82 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
83 $this->assertEquals(0, count($subs), $error_message);
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);
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);
97 * Test the getParentIDRecursive function which is used to find a grouping of folders.
100 function testgetParentIDRecursive()
102 $f1 = new SugarFolder();
103 $f12 = new SugarFolder();
104 $f3 = new SugarFolder();
106 $f1->id = create_guid();
107 $f1->new_with_id = TRUE;
109 $f12->id = create_guid();
110 $f12->new_with_id = TRUE;
112 $f3->id = create_guid();
113 $f3->new_with_id = TRUE;
115 $f12->parent_folder = $f1->id;
120 $this->additionalFolders[] = $f1->id;
121 $this->additionalFolders[] = $f12->id;
122 $this->additionalFolders[] = $f3->id;
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");
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");
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");
136 $childrenArray = array();
137 $this->folder->findAllChildren($f3->id,$childrenArray);
138 $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
142 * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
145 function testGetUserFolders()
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('','');
154 $folderOpenState = "";
155 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
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);
165 * Test the addBean, getCountUnread,getCountItems functions.
168 function testAddBean()
170 $emailParams = array('status' => 'unread');
171 $email = $this->_createEmailObject($emailParams);
172 $this->emails[] = $email->id;
174 $this->_createNewSugarFolder();
176 $cnt = $this->folder->getCountUnread($this->folder->id);
177 $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
179 $this->folder->addBean($email,$GLOBALS['current_user']);
181 $cnt = $this->folder->getCountUnread($this->folder->id);
182 $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
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']);
190 $cnt = $this->folder->getCountItems($this->folder->id);
191 $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
197 * Tests sugar folder methods that deal with emails.
200 function testFolderEmailMethods()
203 $emailParams = array('status' => 'read');
204 $email = $this->_createEmailObject($emailParams);
205 $this->emails[] = $email->id;
207 $this->_createNewSugarFolder();
208 $this->folder->addBean($email,$GLOBALS['current_user']);
210 $emailExists = $this->folder->checkEmailExistForFolder($email->id);
211 $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
213 //Remove the specific email from our folder.
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.");
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;
224 $this->additionalFolders[] = $f3->id;
226 $this->folder->addBean($email,$GLOBALS['current_user']);
228 $emailExists = $f3->checkEmailExistForFolder($email->id);
229 $this->assertFalse($emailExists);
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");
238 * Test retreiving a list of emails for a particular folder.
241 function testGetListItemsForEmailXML()
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('','');
251 $folderOpenState = "";
252 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
254 $this->assertEquals(1, count($ret), $error_message);
255 $folderID = $ret[0]['id'];
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;
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']);
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);
273 $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
278 function _createEmailObject($additionalParams = array() )
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
288 foreach ($additionalParams as $k => $v)
296 function _createNewSugarFolder()
298 $this->folder->id = create_guid();
299 $this->folder->new_with_id = TRUE;
300 $this->folder->name = "UNIT TEST";
301 $this->folder->save();
305 private function _clearFolder($folder_id)
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}'");