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
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.
12 * Copyright (C) 2004-2013 SugarCRM Inc. All rights reserved.
13 ********************************************************************************/
16 require_once('include/SugarFolders/SugarFolders.php');
17 require_once('modules/Emails/EmailUI.php');
21 * upgrade from 6.4.5 to 6.5.x, does not have "My Archived Email" folder in Emails module
23 * @author bsitnikovski@sugarcrm.com
26 class Bug62883Test extends Sugar_PHPUnit_Framework_TestCase
28 private $folder = null;
30 public function setUp()
32 global $current_user, $currentModule;
34 SugarTestHelper::setUp('mod_strings', array('Emails'));
35 SugarTestHelper::setUp('beanList');
36 SugarTestHelper::setUp('beanFiles');
37 SugarTestHelper::setUp('current_user');
40 public function tearDown()
42 $GLOBALS['db']->query(
43 "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
45 $GLOBALS['db']->query(
46 "DELETE FROM folders WHERE created_by='{$GLOBALS['current_user']->id}' OR name='Bug62883Test'"
48 $GLOBALS['db']->query(
49 "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
52 SugarTestHelper::tearDown();
56 * Test to ensure that for a new user, the My Email, My Drafts, Sent Email, etc. folders can be retrieved.
59 public function testGetUserFolders()
63 $emailUI = new EmailUI();
64 $emailUI->preflightUser($GLOBALS['current_user']);
65 $rootNode = new ExtNode('', '');
67 $this->createNewSugarFolder();
68 $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
70 foreach ($ret as $childFolder) {
71 array_push($foldersId, $childFolder['id']);
74 $this->clearFolder($this->folder->id);
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'
80 $folderTypes = array();
81 foreach ($ret[0]['children'] as $p) {
82 $folderTypes[] = $p['folder_type'];
85 $this->assertContains("draft", $folderTypes);
86 $this->assertContains("sent", $folderTypes);
87 $this->assertContains("archived", $folderTypes);
91 * Test to ensure that whenever a folder is deleted, it will be created for a user
94 public function testCreationOnDeletedFolder()
96 $folder_type = "draft";
98 $this->createNewSugarFolder();
100 // Call preflightUser() to re-create all folders
101 $emailUI = new EmailUI();
102 $emailUI->preflightUser($GLOBALS['current_user']);
104 $rootNode = new ExtNode('', '');
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}'"
111 // Call preflightUser() to re-create missing folder
112 $emailUI->preflightUser($GLOBALS['current_user']);
115 $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
117 // Should contain deleted folder after preflightUser
118 $folderTypes = array();
119 foreach ($ret[0]['children'] as $p) {
120 $folderTypes[] = $p['folder_type'];
123 $this->assertContains("draft", $folderTypes);
124 $this->assertContains("sent", $folderTypes);
125 $this->assertContains("archived", $folderTypes);
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.
133 public function testCreationOnDeletedInboundFolder()
135 $this->createNewSugarFolder();
137 // Call preflightUser() to re-create all folders
138 $emailUI = new EmailUI();
139 $emailUI->preflightUser($GLOBALS['current_user']);
141 $error_message = "Unable to get user folders";
142 $rootNode = new ExtNode('', '');
144 // Delete one folder type
145 $GLOBALS['db']->query(
146 "DELETE FROM folders WHERE folder_type = 'inbound' AND created_by='{$GLOBALS['current_user']->id}'"
150 $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
152 $this->assertEquals(0, count($ret), $error_message);
154 // Call preflightUser() to re-create missing folder
155 $emailUI->preflightUser($GLOBALS['current_user']);
158 $ret = $this->folder->getUserFolders($rootNode, "", $GLOBALS['current_user'], true);
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'];
167 $this->assertContains("draft", $folderTypes);
170 private function createNewSugarFolder()
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();
179 private function clearFolder($folder_id)
181 $GLOBALS['db']->query(
182 "DELETE FROM folders_subscriptions WHERE assigned_user_id='{$GLOBALS['current_user']->id}'"
184 $GLOBALS['db']->query("DELETE FROM folders_subscriptions WHERE folder_id='{$folder_id}'");
185 $GLOBALS['db']->query("DELETE FROM folders WHERE id='{$folder_id}'");