2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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('include/SugarFolders/SugarFolders.php');
41 class SugarFoldersTest extends Sugar_PHPUnit_Framework_TestCase
44 var $additionalFolders = null;
49 public function setUp()
51 global $current_user, $currentModule;
53 $this->_user = SugarTestUserUtilities::createAnonymousUser();
54 $GLOBALS['current_user'] = $this->_user;
55 $this->folder = new SugarFolder();
56 $this->additionalFolders = array();
57 $this->emails = array();
60 require('include/modules.php');
61 $GLOBALS['beanList'] = $beanList;
62 $GLOBALS['beanFiles'] = $beanFiles;
63 if (empty($GLOBALS['current_language'])) {
64 $GLOBALS['current_language'] = 'en_us';
66 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
69 public function tearDown()
71 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
72 unset($GLOBALS['current_user']);
74 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
75 $this->_clearFolder($this->folder->id);
77 foreach ($this->additionalFolders as $additionalID)
78 $this->_clearFolder($additionalID);
80 foreach ($this->emails as $emailID)
81 $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
84 unset($GLOBALS['mod_strings']);
88 * Test the Set Folder method.
91 function testSetFolder()
94 $this->folder->id = create_guid();
95 $this->folder->new_with_id = TRUE;
97 $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
100 $this->folder->setFolder($fields);
102 //Retrieve newly created folder
103 $error_message = "Unable to set folder.";
104 $this->folder->retrieve($this->folder->id);
106 $this->assertEquals($fields['name'], $this->folder->name, $error_message );
107 $this->assertEquals($fields['parent_folder'], $this->folder->parent_folder, $error_message );
108 $this->assertEquals($this->_user->id, $this->folder->assign_to_id, $error_message );
110 //Check for folder subscriptions create for global user
111 $sub_ids = $this->folder->getSubscriptions($GLOBALS['current_user']);
112 $this->assertEquals(1, count($sub_ids), $error_message);
113 $this->assertEquals($this->folder->id, $sub_ids[0], $error_message);
118 * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
121 function testFolderSubscriptions()
123 $this->_createNewSugarFolder();
124 $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
126 //Clear subscriptions
127 $this->folder->clearSubscriptions();
128 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
129 $this->assertEquals(0, count($subs), $error_message);
132 $this->folder->insertFolderSubscription($this->folder->id,$GLOBALS['current_user']->id);
133 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
134 $this->assertEquals(1, count($subs), $error_message);
136 //Clear subscriptions for a paricular folder
137 $this->folder->clearSubscriptionsForFolder($this->folder->id);
138 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
139 $this->assertEquals(0, count($subs), $error_message);
143 * Test the getParentIDRecursive function which is used to find a grouping of folders.
146 function testgetParentIDRecursive()
148 $f1 = new SugarFolder();
149 $f12 = new SugarFolder();
150 $f3 = new SugarFolder();
152 $f1->id = create_guid();
153 $f1->new_with_id = TRUE;
155 $f12->id = create_guid();
156 $f12->new_with_id = TRUE;
158 $f3->id = create_guid();
159 $f3->new_with_id = TRUE;
161 $f12->parent_folder = $f1->id;
166 $this->additionalFolders[] = $f1->id;
167 $this->additionalFolders[] = $f12->id;
168 $this->additionalFolders[] = $f3->id;
171 $parentIDs = $this->folder->getParentIDRecursive($f12->id); //Includes itself in the return list.
172 $this->assertEquals(2, count($parentIDs), "Unable to retrieve parent ids recursively");
174 $parentIDs = $this->folder->getParentIDRecursive($f3->id); //Includes itself in the return list.
175 $this->assertEquals(1, count($parentIDs), "Unable to retrieve parent ids recursively");
177 //Find the children by going the other way.
178 $childrenArray = array();
179 $this->folder->findAllChildren($f1->id,$childrenArray);
180 $this->assertEquals(1, count($childrenArray), "Unable to retrieve child ids recursively");
182 $childrenArray = array();
183 $this->folder->findAllChildren($f3->id,$childrenArray);
184 $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
188 * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
191 function testGetUserFolders()
193 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
194 require_once('modules/Emails/EmailUI.php');
195 $emailUI = new EmailUI();
196 $emailUI->preflightUser($GLOBALS['current_user']);
197 $error_message = "Unable to get user folders";
198 $rootNode = new ExtNode('','');
200 $folderOpenState = "";
201 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
203 $this->assertEquals(1, count($ret), $error_message);
204 $this->assertEquals($GLOBALS['mod_strings']['LNK_MY_INBOX'], $ret[0]['text'], $error_message);
205 //Should contain 'My Drafts', 'My Sent Mail', 'My Archive'
206 $this->assertEquals(3, count($ret[0]['children']), $error_message);
211 * Test the addBean, getCountUnread,getCountItems functions.
214 function testAddBean()
216 $emailParams = array('status' => 'unread');
217 $email = $this->_createEmailObject($emailParams);
218 $this->emails[] = $email->id;
220 $this->_createNewSugarFolder();
222 $cnt = $this->folder->getCountUnread($this->folder->id);
223 $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
225 $this->folder->addBean($email,$GLOBALS['current_user']);
227 $cnt = $this->folder->getCountUnread($this->folder->id);
228 $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
230 //Create a second email obj with status read
231 $emailParams = array('status' => 'read');
232 $email = $this->_createEmailObject($emailParams);
233 $this->emails[] = $email->id;
234 $this->folder->addBean($email,$GLOBALS['current_user']);
236 $cnt = $this->folder->getCountItems($this->folder->id);
237 $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
243 * Tests sugar folder methods that deal with emails.
246 function testFolderEmailMethods()
249 $emailParams = array('status' => 'read');
250 $email = $this->_createEmailObject($emailParams);
251 $this->emails[] = $email->id;
253 $this->_createNewSugarFolder();
254 $this->folder->addBean($email,$GLOBALS['current_user']);
256 $emailExists = $this->folder->checkEmailExistForFolder($email->id);
257 $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
259 //Remove the specific email from our folder.
261 $this->folder->deleteEmailFromFolder($email->id);
262 $emailExists = $this->folder->checkEmailExistForFolder($email->id);
263 $this->assertFalse($emailExists, "Unable to check for emails with a specific folder.");
265 //Move the Email bean from one folder to another
266 $f3 = new SugarFolder();
267 $f3->id = create_guid();
268 $f3->new_with_id = TRUE;
270 $this->additionalFolders[] = $f3->id;
272 $this->folder->addBean($email,$GLOBALS['current_user']);
274 $emailExists = $f3->checkEmailExistForFolder($email->id);
275 $this->assertFalse($emailExists);
277 $this->folder->move($this->folder->id, $f3->id,$email->id);
278 $emailExists = $f3->checkEmailExistForFolder($email->id);
279 $this->assertTrue($emailExists, "Unable to move Emails bean to a different sugar folder");
284 * Test retreiving a list of emails for a particular folder.
287 function testGetListItemsForEmailXML()
289 //Create the my Emails Folder
290 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
291 require_once('modules/Emails/EmailUI.php');
292 $emailUI = new EmailUI();
293 $emailUI->preflightUser($GLOBALS['current_user']);
294 $error_message = "Unable to get list items for email.";
295 $rootNode = new ExtNode('','');
297 $folderOpenState = "";
298 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
300 $this->assertEquals(1, count($ret), $error_message);
301 $folderID = $ret[0]['id'];
303 //Create the Email Object
304 $emailParams = array('status' => 'unread','assigned_user_id' => $GLOBALS['current_user']->id);
305 $email = $this->_createEmailObject($emailParams);
306 $this->emails[] = $email->id;
308 //Add Email Object to My Email Folder
309 $my_email = new SugarFolder();
310 $my_email->retrieve($folderID);
311 $my_email->addBean($email,$GLOBALS['current_user']);
313 //Make sure the email was added to the folder.
314 $emailExists = $my_email->checkEmailExistForFolder($email->id);
315 $this->assertTrue($emailExists, $error_message);
316 //Get the list of emails.
317 $emailList = $my_email->getListItemsForEmailXML($folderID);
319 $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
324 function _createEmailObject($additionalParams = array() )
329 $em->name = 'tst_' . uniqid();
330 $em->type = 'inbound';
331 $em->intent = 'pick';
332 $em->date_sent = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ))) ; //Two days from today
334 foreach ($additionalParams as $k => $v)
342 function _createNewSugarFolder()
344 $this->folder->id = create_guid();
345 $this->folder->new_with_id = TRUE;
346 $this->folder->name = "UNIT TEST";
347 $this->folder->save();
351 private function _clearFolder($folder_id)
353 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
354 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE folder_id='{$folder_id}'");
355 $GLOBALS['db']->query("DELETE FROM folders WHERE id='{$folder_id}'");