2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 public function tearDown()
62 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
63 unset($GLOBALS['current_user']);
65 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
66 $this->_clearFolder($this->folder->id);
68 foreach ($this->additionalFolders as $additionalID)
69 $this->_clearFolder($additionalID);
71 foreach ($this->emails as $emailID)
72 $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
75 unset($GLOBALS['mod_strings']);
79 * Test the Set Folder method.
82 function testSetFolder()
85 $this->folder->id = create_guid();
86 $this->folder->new_with_id = TRUE;
88 $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
91 $this->folder->setFolder($fields);
93 //Retrieve newly created folder
94 $error_message = "Unable to set folder.";
95 $this->folder->retrieve($this->folder->id);
97 $this->assertEquals($fields['name'], $this->folder->name, $error_message );
98 $this->assertEquals($fields['parent_folder'], $this->folder->parent_folder, $error_message );
99 $this->assertEquals($this->_user->id, $this->folder->assign_to_id, $error_message );
101 //Check for folder subscriptions create for global user
102 $sub_ids = $this->folder->getSubscriptions($GLOBALS['current_user']);
103 $this->assertEquals(1, count($sub_ids), $error_message);
104 $this->assertEquals($this->folder->id, $sub_ids[0], $error_message);
109 * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
112 function testFolderSubscriptions()
114 $this->_createNewSugarFolder();
115 $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
117 //Clear subscriptions
118 $this->folder->clearSubscriptions();
119 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
120 $this->assertEquals(0, count($subs), $error_message);
123 $this->folder->insertFolderSubscription($this->folder->id,$GLOBALS['current_user']->id);
124 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
125 $this->assertEquals(1, count($subs), $error_message);
127 //Clear subscriptions for a paricular folder
128 $this->folder->clearSubscriptionsForFolder($this->folder->id);
129 $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
130 $this->assertEquals(0, count($subs), $error_message);
134 * Test the getParentIDRecursive function which is used to find a grouping of folders.
137 function testgetParentIDRecursive()
139 $f1 = new SugarFolder();
140 $f12 = new SugarFolder();
141 $f3 = new SugarFolder();
143 $f1->id = create_guid();
144 $f1->new_with_id = TRUE;
146 $f12->id = create_guid();
147 $f12->new_with_id = TRUE;
149 $f3->id = create_guid();
150 $f3->new_with_id = TRUE;
152 $f12->parent_folder = $f1->id;
157 $this->additionalFolders[] = $f1->id;
158 $this->additionalFolders[] = $f12->id;
159 $this->additionalFolders[] = $f3->id;
162 $parentIDs = $this->folder->getParentIDRecursive($f12->id); //Includes itself in the return list.
163 $this->assertEquals(2, count($parentIDs), "Unable to retrieve parent ids recursively");
165 $parentIDs = $this->folder->getParentIDRecursive($f3->id); //Includes itself in the return list.
166 $this->assertEquals(1, count($parentIDs), "Unable to retrieve parent ids recursively");
168 //Find the children by going the other way.
169 $childrenArray = array();
170 $this->folder->findAllChildren($f1->id,$childrenArray);
171 $this->assertEquals(1, count($childrenArray), "Unable to retrieve child ids recursively");
173 $childrenArray = array();
174 $this->folder->findAllChildren($f3->id,$childrenArray);
175 $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
179 * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
182 function testGetUserFolders()
184 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
185 require_once('modules/Emails/EmailUI.php');
186 $emailUI = new EmailUI();
187 $emailUI->preflightUser($GLOBALS['current_user']);
188 $error_message = "Unable to get user folders";
189 $rootNode = new ExtNode('','');
191 $folderOpenState = "";
192 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
194 $this->assertEquals(1, count($ret), $error_message);
195 $this->assertEquals($GLOBALS['mod_strings']['LNK_MY_INBOX'], $ret[0]['text'], $error_message);
196 //Should contain 'My Drafts' and 'My Sent Mail'
197 $this->assertEquals(2, count($ret[0]['children']), $error_message);
202 * Test the addBean, getCountUnread,getCountItems functions.
205 function testAddBean()
207 $emailParams = array('status' => 'unread');
208 $email = $this->_createEmailObject($emailParams);
209 $this->emails[] = $email->id;
211 $this->_createNewSugarFolder();
213 $cnt = $this->folder->getCountUnread($this->folder->id);
214 $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
216 $this->folder->addBean($email,$GLOBALS['current_user']);
218 $cnt = $this->folder->getCountUnread($this->folder->id);
219 $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
221 //Create a second email obj with status read
222 $emailParams = array('status' => 'read');
223 $email = $this->_createEmailObject($emailParams);
224 $this->emails[] = $email->id;
225 $this->folder->addBean($email,$GLOBALS['current_user']);
227 $cnt = $this->folder->getCountItems($this->folder->id);
228 $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
234 * Tests sugar folder methods that deal with emails.
237 function testFolderEmailMethods()
240 $emailParams = array('status' => 'read');
241 $email = $this->_createEmailObject($emailParams);
242 $this->emails[] = $email->id;
244 $this->_createNewSugarFolder();
245 $this->folder->addBean($email,$GLOBALS['current_user']);
247 $emailExists = $this->folder->checkEmailExistForFolder($email->id);
248 $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
250 //Remove the specific email from our folder.
252 $this->folder->deleteEmailFromFolder($email->id);
253 $emailExists = $this->folder->checkEmailExistForFolder($email->id);
254 $this->assertFalse($emailExists, "Unable to check for emails with a specific folder.");
256 //Move the Email bean from one folder to another
257 $f3 = new SugarFolder();
258 $f3->id = create_guid();
259 $f3->new_with_id = TRUE;
261 $this->additionalFolders[] = $f3->id;
263 $this->folder->addBean($email,$GLOBALS['current_user']);
265 $emailExists = $f3->checkEmailExistForFolder($email->id);
266 $this->assertFalse($emailExists);
268 $this->folder->move($this->folder->id, $f3->id,$email->id);
269 $emailExists = $f3->checkEmailExistForFolder($email->id);
270 $this->assertTrue($emailExists, "Unable to move Emails bean to a different sugar folder");
275 * Test retreiving a list of emails for a particular folder.
278 function testGetListItemsForEmailXML()
280 //Create the my Emails Folder
281 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], "Emails");
282 require_once('modules/Emails/EmailUI.php');
283 $emailUI = new EmailUI();
284 $emailUI->preflightUser($GLOBALS['current_user']);
285 $error_message = "Unable to get list items for email.";
286 $rootNode = new ExtNode('','');
288 $folderOpenState = "";
289 $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
291 $this->assertEquals(1, count($ret), $error_message);
292 $folderID = $ret[0]['id'];
294 //Create the Email Object
295 $emailParams = array('status' => 'unread','assigned_user_id' => $GLOBALS['current_user']->id);
296 $email = $this->_createEmailObject($emailParams);
297 $this->emails[] = $email->id;
299 //Add Email Object to My Email Folder
300 $my_email = new SugarFolder();
301 $my_email->retrieve($folderID);
302 $my_email->addBean($email,$GLOBALS['current_user']);
304 //Make sure the email was added to the folder.
305 $emailExists = $my_email->checkEmailExistForFolder($email->id);
306 $this->assertTrue($emailExists, $error_message);
307 //Get the list of emails.
308 $emailList = $my_email->getListItemsForEmailXML($folderID);
310 $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
315 function _createEmailObject($additionalParams = array() )
320 $em->name = 'tst_' . uniqid();
321 $em->type = 'inbound';
322 $em->intent = 'pick';
323 $em->date_sent = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ))) ; //Two days from today
325 foreach ($additionalParams as $k => $v)
333 function _createNewSugarFolder()
335 $this->folder->id = create_guid();
336 $this->folder->new_with_id = TRUE;
337 $this->folder->name = "UNIT TEST";
338 $this->folder->save();
342 private function _clearFolder($folder_id)
344 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
345 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE folder_id='{$folder_id}'");
346 $GLOBALS['db']->query("DELETE FROM folders WHERE id='{$folder_id}'");