]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarFolders/SugarFoldersTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / SugarFolders / SugarFoldersTest.php
1 <?php 
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37  
38 require_once('include/SugarFolders/SugarFolders.php');
39
40
41 class SugarFoldersTest extends Sugar_PHPUnit_Framework_TestCase
42 {
43         var $folder = null;
44         var $additionalFolders = null;
45     var $_user = null;
46     var $emails = null;
47     
48     
49         public function setUp()
50     {
51         global $current_user, $currentModule;
52
53         $this->_user = SugarTestUserUtilities::createAnonymousUser();
54         $GLOBALS['current_user'] = $this->_user;
55                 $this->folder = new SugarFolder(); 
56                 $this->additionalFolders = array();
57                 $this->emails = array();
58         }
59
60     public function tearDown()
61     {
62         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
63         unset($GLOBALS['current_user']);
64         
65         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
66         $this->_clearFolder($this->folder->id);
67         
68         foreach ($this->additionalFolders as $additionalID)
69             $this->_clearFolder($additionalID);
70         
71         foreach ($this->emails as $emailID)
72             $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
73         
74         unset($this->folder);
75         unset($GLOBALS['mod_strings']);
76     }
77
78     /**
79      * Test the Set Folder method.
80      *
81      */
82     function testSetFolder()
83     {
84         //Set folder
85         $this->folder->id = create_guid();
86         $this->folder->new_with_id = TRUE;
87         
88         $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
89                         );
90         
91         $this->folder->setFolder($fields);
92         
93         //Retrieve newly created folder
94         $error_message = "Unable to set folder.";
95         $this->folder->retrieve($this->folder->id);
96
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 ); 
100         
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);
105         
106     }
107     
108     /**
109      * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
110      *
111      */
112     function testFolderSubscriptions()
113     {
114         $this->_createNewSugarFolder();
115         $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
116         
117         //Clear subscriptions
118         $this->folder->clearSubscriptions();
119         $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
120         $this->assertEquals(0, count($subs), $error_message);
121         
122         //Add a subscription
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);
126         
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);
131     }
132     
133     /**
134      * Test the getParentIDRecursive function which is used to find a grouping of folders.
135      *
136      */
137     function testgetParentIDRecursive()
138     {
139         $f1 = new SugarFolder();
140         $f12 = new SugarFolder();
141         $f3 = new SugarFolder();
142         
143         $f1->id = create_guid();
144         $f1->new_with_id = TRUE;
145         
146         $f12->id = create_guid();
147         $f12->new_with_id = TRUE;
148         
149         $f3->id = create_guid();
150         $f3->new_with_id = TRUE;
151         
152         $f12->parent_folder = $f1->id;
153         $f1->save();
154         $f12->save();
155         $f3->save();
156         
157         $this->additionalFolders[] = $f1->id;
158         $this->additionalFolders[] = $f12->id;
159         $this->additionalFolders[] = $f3->id;
160         
161         
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");
164         
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");
167         
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");
172         
173         $childrenArray = array();
174         $this->folder->findAllChildren($f3->id,$childrenArray);
175         $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
176     }
177     
178     /**
179      * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
180      *
181      */
182     function testGetUserFolders()
183     {
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('','');
190
191         $folderOpenState = "";
192         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
193
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);
198
199     }
200     
201     /**
202      * Test the addBean, getCountUnread,getCountItems functions.
203      *
204      */
205     function testAddBean()
206     {
207         $emailParams = array('status' => 'unread');
208         $email = $this->_createEmailObject($emailParams);
209         $this->emails[] = $email->id;
210         
211         $this->_createNewSugarFolder();
212         
213         $cnt = $this->folder->getCountUnread($this->folder->id);
214         $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
215         
216         $this->folder->addBean($email,$GLOBALS['current_user']);
217         
218         $cnt = $this->folder->getCountUnread($this->folder->id);
219         $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
220         
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']);
226         
227         $cnt = $this->folder->getCountItems($this->folder->id);
228         $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
229         
230         
231     }
232     
233     /**
234      * Tests sugar folder methods that deal with emails.
235      *
236      */
237     function testFolderEmailMethods()
238     {
239         
240         $emailParams = array('status' => 'read');
241         $email = $this->_createEmailObject($emailParams);
242         $this->emails[] = $email->id;
243         
244         $this->_createNewSugarFolder();
245         $this->folder->addBean($email,$GLOBALS['current_user']);
246         
247         $emailExists = $this->folder->checkEmailExistForFolder($email->id);
248         $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
249         
250         //Remove the specific email from our folder.
251         
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.");
255         
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;
260         $f3->save();
261         $this->additionalFolders[] = $f3->id;
262         
263         $this->folder->addBean($email,$GLOBALS['current_user']);
264         
265         $emailExists = $f3->checkEmailExistForFolder($email->id);
266         $this->assertFalse($emailExists);
267         
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");
271         
272     }
273     
274     /**
275      * Test retreiving a list of emails for a particular folder.
276      *
277      */
278     function testGetListItemsForEmailXML()
279     {
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('','');
287
288         $folderOpenState = "";
289         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
290
291         $this->assertEquals(1, count($ret), $error_message);
292         $folderID = $ret[0]['id'];
293         
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;
298        
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']);
303         
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);
309         
310         $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
311
312     }
313     
314     
315     function _createEmailObject($additionalParams = array() )
316     {
317         global $timedate;
318         
319         $em = new Email();
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 
324                 
325                 foreach ($additionalParams as $k => $v)
326                   $em->$k = $v;
327                   
328                 $em->save();
329             
330                 return $em;
331     }
332     
333     function _createNewSugarFolder()
334     {
335         $this->folder->id = create_guid();
336         $this->folder->new_with_id = TRUE;
337         $this->folder->name = "UNIT TEST";
338         $this->folder->save();
339         
340     }
341     
342     private function _clearFolder($folder_id)
343     {
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}'");
347     }
348 }
349 ?>