]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarFolders/SugarFoldersTest.php
Release 6.4.1
[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-2012 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         $beanList = array();
59         $beanFiles = 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';
65         }
66         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
67     }
68
69     public function tearDown()
70     {
71         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
72         unset($GLOBALS['current_user']);
73         
74         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE assigned_user_id='{$this->_user->id}'");
75         $this->_clearFolder($this->folder->id);
76         
77         foreach ($this->additionalFolders as $additionalID)
78             $this->_clearFolder($additionalID);
79         
80         foreach ($this->emails as $emailID)
81             $GLOBALS['db']->query("DELETE FROM emails WHERE id='$emailID'");
82         
83         unset($this->folder);
84         unset($GLOBALS['mod_strings']);
85     }
86
87     /**
88      * Test the Set Folder method.
89      *
90      */
91     function testSetFolder()
92     {
93         //Set folder
94         $this->folder->id = create_guid();
95         $this->folder->new_with_id = TRUE;
96         
97         $fields = array('name' => 'TEST_FOLDER','parent_folder' => 'PRNT_FOLDER',
98                         );
99         
100         $this->folder->setFolder($fields);
101         
102         //Retrieve newly created folder
103         $error_message = "Unable to set folder.";
104         $this->folder->retrieve($this->folder->id);
105
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 ); 
109         
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);
114         
115     }
116     
117     /**
118      * Test sugar folder subscriptions: create, clear, insert, clear specific folder.
119      *
120      */
121     function testFolderSubscriptions()
122     {
123         $this->_createNewSugarFolder();
124         $error_message = "Unable to create|insert|delete sugar folder subscriptions.";
125         
126         //Clear subscriptions
127         $this->folder->clearSubscriptions();
128         $subs = $this->folder->getSubscriptions($GLOBALS['current_user']);
129         $this->assertEquals(0, count($subs), $error_message);
130         
131         //Add a subscription
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);
135         
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);
140     }
141     
142     /**
143      * Test the getParentIDRecursive function which is used to find a grouping of folders.
144      *
145      */
146     function testgetParentIDRecursive()
147     {
148         $f1 = new SugarFolder();
149         $f12 = new SugarFolder();
150         $f3 = new SugarFolder();
151         
152         $f1->id = create_guid();
153         $f1->new_with_id = TRUE;
154         
155         $f12->id = create_guid();
156         $f12->new_with_id = TRUE;
157         
158         $f3->id = create_guid();
159         $f3->new_with_id = TRUE;
160         
161         $f12->parent_folder = $f1->id;
162         $f1->save();
163         $f12->save();
164         $f3->save();
165         
166         $this->additionalFolders[] = $f1->id;
167         $this->additionalFolders[] = $f12->id;
168         $this->additionalFolders[] = $f3->id;
169         
170         
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");
173         
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");
176         
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");
181         
182         $childrenArray = array();
183         $this->folder->findAllChildren($f3->id,$childrenArray);
184         $this->assertEquals(0, count($childrenArray), "Unable to retrieve child ids recursively");
185     }
186     
187     /**
188      * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
189      *
190      */
191     function testGetUserFolders()
192     {
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('','');
199
200         $folderOpenState = "";
201         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
202
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' and 'My Sent Mail'
206         $this->assertEquals(2, count($ret[0]['children']), $error_message);
207
208     }
209     
210     /**
211      * Test the addBean, getCountUnread,getCountItems functions.
212      *
213      */
214     function testAddBean()
215     {
216         $emailParams = array('status' => 'unread');
217         $email = $this->_createEmailObject($emailParams);
218         $this->emails[] = $email->id;
219         
220         $this->_createNewSugarFolder();
221         
222         $cnt = $this->folder->getCountUnread($this->folder->id);
223         $this->assertEquals(0, $cnt, "Unable to execute addBean function properly.");
224         
225         $this->folder->addBean($email,$GLOBALS['current_user']);
226         
227         $cnt = $this->folder->getCountUnread($this->folder->id);
228         $this->assertEquals(1, $cnt, "Unable to execute addBean function properly.");
229         
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']);
235         
236         $cnt = $this->folder->getCountItems($this->folder->id);
237         $this->assertEquals(2, $cnt, "Unable to execute getCountItems function properly.");
238         
239         
240     }
241     
242     /**
243      * Tests sugar folder methods that deal with emails.
244      *
245      */
246     function testFolderEmailMethods()
247     {
248         
249         $emailParams = array('status' => 'read');
250         $email = $this->_createEmailObject($emailParams);
251         $this->emails[] = $email->id;
252         
253         $this->_createNewSugarFolder();
254         $this->folder->addBean($email,$GLOBALS['current_user']);
255         
256         $emailExists = $this->folder->checkEmailExistForFolder($email->id);
257         $this->assertTrue($emailExists, "Unable to check for emails with a specific folder");
258         
259         //Remove the specific email from our folder.
260         
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.");
264         
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;
269         $f3->save();
270         $this->additionalFolders[] = $f3->id;
271         
272         $this->folder->addBean($email,$GLOBALS['current_user']);
273         
274         $emailExists = $f3->checkEmailExistForFolder($email->id);
275         $this->assertFalse($emailExists);
276         
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");
280         
281     }
282     
283     /**
284      * Test retreiving a list of emails for a particular folder.
285      *
286      */
287     function testGetListItemsForEmailXML()
288     {
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('','');
296
297         $folderOpenState = "";
298         $ret = $this->folder->getUserFolders($rootNode, $folderOpenState, $GLOBALS['current_user'], true);
299
300         $this->assertEquals(1, count($ret), $error_message);
301         $folderID = $ret[0]['id'];
302         
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;
307        
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']);
312         
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);
318         
319         $this->assertEquals($email->id,$emailList['out'][0]['uid'],$error_message );
320
321     }
322     
323     
324     function _createEmailObject($additionalParams = array() )
325     {
326         global $timedate;
327         
328         $em = new Email();
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 
333                 
334                 foreach ($additionalParams as $k => $v)
335                   $em->$k = $v;
336                   
337                 $em->save();
338             
339                 return $em;
340     }
341     
342     function _createNewSugarFolder()
343     {
344         $this->folder->id = create_guid();
345         $this->folder->new_with_id = TRUE;
346         $this->folder->name = "UNIT TEST";
347         $this->folder->save();
348         
349     }
350     
351     private function _clearFolder($folder_id)
352     {
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}'");
356     }
357 }
358 ?>