]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarFolders/Bug62883Test.php
Release 6.5.16
[Github/sugarcrm.git] / tests / include / SugarFolders / Bug62883Test.php
1 <?php
2 /*********************************************************************************
3  * By installing or using this file, you are confirming on behalf of the entity
4  * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
5  * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
6  * http://www.sugarcrm.com/master-subscription-agreement
7  *
8  * If Company is not bound by the MSA, then by installing or using this file
9  * you are agreeing unconditionally that Company will be bound by the MSA and
10  * certifying that you have authority to bind Company accordingly.
11  *
12  * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
13  ********************************************************************************/
14
15
16 require_once('include/SugarFolders/SugarFolders.php');
17 require_once('modules/Emails/EmailUI.php');
18
19 /**
20  * Bug #62883
21  * upgrade from 6.4.5 to 6.5.x, does not have "My Archived Email" folder in Emails module
22  *
23  * @author bsitnikovski@sugarcrm.com
24  * @ticket 62883
25  */
26 class Bug62883Test extends Sugar_PHPUnit_Framework_TestCase
27 {
28     private $folder = null;
29
30     public function setUp()
31     {
32         global $current_user, $currentModule;
33
34         SugarTestHelper::setUp('mod_strings', array('Emails'));
35         SugarTestHelper::setUp('beanList');
36         SugarTestHelper::setUp('beanFiles');
37         SugarTestHelper::setUp('current_user');
38     }
39
40     public function tearDown()
41     {
42         $GLOBALS['db']->query(
43             "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
44         );
45         $GLOBALS['db']->query(
46             "DELETE FROM folders WHERE created_by='{$GLOBALS['current_user']->id}' OR name='Bug62883Test'"
47         );
48         $GLOBALS['db']->query(
49             "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
50         );
51
52         SugarTestHelper::tearDown();
53     }
54
55     /**
56      * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
57      *
58      */
59     public function testGetUserFolders()
60     {
61         $foldersId = array();
62
63         $emailUI = new EmailUI();
64         $emailUI->preflightUser($GLOBALS['current_user']);
65         $rootNode = new ExtNode('', '');
66
67         $this->createNewSugarFolder();
68         $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
69
70         foreach ($ret as $childFolder) {
71             array_push($foldersId, $childFolder['id']);
72         }
73
74         $this->clearFolder($this->folder->id);
75
76         $this->assertEquals(1, count($ret));
77         $this->assertEquals($GLOBALS['mod_strings']['LNK_MY_INBOX'], $ret[0]['text']);
78         //Should contain 'My Drafts', 'My Sent Mail', 'My Archive'
79
80         $folderTypes = array();
81         foreach ($ret[0]['children'] as $p) {
82             $folderTypes[] = $p['folder_type'];
83         }
84
85         $this->assertContains("draft", $folderTypes);
86         $this->assertContains("sent", $folderTypes);
87         $this->assertContains("archived", $folderTypes);
88     }
89
90     /**
91      * Test to ensure that whenever a folder is deleted, it will be created for a user
92      *
93      */
94     public function testCreationOnDeletedFolder()
95     {
96         $folder_type = "draft";
97
98         $this->createNewSugarFolder();
99
100         // Call preflightUser() to re-create all folders
101         $emailUI = new EmailUI();
102         $emailUI->preflightUser($GLOBALS['current_user']);
103
104         $rootNode = new ExtNode('', '');
105
106         // Delete one folder type
107         $GLOBALS['db']->query(
108             "DELETE FROM folders WHERE folder_type='{$folder_type}' AND created_by='{$GLOBALS['current_user']->id}'"
109         );
110
111         // Call preflightUser() to re-create missing folder
112         $emailUI->preflightUser($GLOBALS['current_user']);
113
114         // Retrieve folders
115         $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
116
117         // Should contain deleted folder after preflightUser
118         $folderTypes = array();
119         foreach ($ret[0]['children'] as $p) {
120             $folderTypes[] = $p['folder_type'];
121         }
122
123         $this->assertContains("draft", $folderTypes);
124         $this->assertContains("sent", $folderTypes);
125         $this->assertContains("archived", $folderTypes);
126     }
127
128     /**
129      * Test to ensure that whenever the inboud folder is deleted, it will be created for a user
130      * and the parent_id for the other folders will be updated accordingly.
131      *
132      */
133     public function testCreationOnDeletedInboundFolder()
134     {
135         $this->createNewSugarFolder();
136
137         // Call preflightUser() to re-create all folders
138         $emailUI = new EmailUI();
139         $emailUI->preflightUser($GLOBALS['current_user']);
140
141         $error_message = "Unable to get user folders";
142         $rootNode = new ExtNode('', '');
143
144         // Delete one folder type
145         $GLOBALS['db']->query(
146             "DELETE FROM folders WHERE folder_type = 'inbound' AND created_by='{$GLOBALS['current_user']->id}'"
147         );
148
149         // Retrieve folders
150         $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
151
152         $this->assertEquals(0, count($ret), $error_message);
153
154         // Call preflightUser() to re-create missing folder
155         $emailUI->preflightUser($GLOBALS['current_user']);
156
157         // Retrieve folders
158         $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
159
160         $this->assertEquals(1, count($ret));
161         // Should contain all folders after preflightUser
162         $folderTypes = array();
163         foreach ($ret[0]['children'] as $p) {
164             $folderTypes[] = $p['folder_type'];
165         }
166
167         $this->assertContains("draft", $folderTypes);
168     }
169
170     private function createNewSugarFolder()
171     {
172         $this->folder = new SugarFolder();
173         $this->folder->new_with_id = true;
174         $this->folder->name = "Bug62883Test";
175         $this->folder->created_by = $this->folder->modified_by = $GLOBALS['current_user']->id;
176         $this->folder->save();
177     }
178
179     private function clearFolder($folder_id)
180     {
181         $GLOBALS['db']->query(
182             "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
183         );
184         $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE folder_id='{$folder_id}'");
185         $GLOBALS['db']->query("DELETE FROM folders WHERE id='{$folder_id}'");
186     }
187 }