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/OutboundEmail/OutboundEmail.php');
43 class Bug23140Test extends Sugar_PHPUnit_Framework_TestCase
45 var $outbound_id = null;
48 var $userOverideAccont = null;
50 public function setUp()
52 global $current_user, $currentModule ;
53 $this->_user = SugarTestUserUtilities::createAnonymousUser();
56 public function tearDown()
58 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
59 unset($GLOBALS['current_user']);
61 if ($this->ob != null)
62 $GLOBALS['db']->query("DELETE FROM outbound_email WHERE id= '{$this->ob->id}'");
63 if ($this->userOverideAccont != null)
64 $GLOBALS['db']->query("DELETE FROM outbound_email WHERE id= '{$this->userOverideAccont->id}'");
67 function testSystemAccountMailSettingsChangedUserAccessToUsername()
69 //User not alloweed to access system email username/password
70 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2') ");
73 $newSystemServer = "system.imap.com";
74 $newSystemUsername = "sytem_user_name";
75 $newSystemPassword = "SYSTEM_PASSWORD";
77 $userID = create_guid();
78 $ob = new OutboundEmail();
80 $ob->new_with_id = TRUE;
81 $ob->name = 'Sugar Test';
82 $ob->type = 'system-override';
83 $ob->user_id = $this->_user->id;
84 $ob->mail_sendtype = "SMTP";
85 $ob->mail_smtpuser = "Test User";
86 $ob->mail_smtppass = "User Pass";
91 $system = $ob->getSystemMailerSettings();
92 $system->new_with_id = FALSE;
93 $system->mail_smtpport = $newSystemPort;
94 $system->mail_smtpserver = $newSystemServer;
95 $system->mail_smtpuser = $newSystemUsername;
96 $system->mail_smtppass = $newSystemPassword;
98 $system->saveSystem();
100 $obRetrieved = new OutboundEmail();
101 $obRetrieved->retrieve($userID);
103 $this->assertEquals($newSystemPort, $obRetrieved->mail_smtpport, "Could not update users sytem-override accounts after system save.");
104 $this->assertEquals($newSystemServer, $obRetrieved->mail_smtpserver, "Could not update users sytem-override accounts after system save.");
105 $this->assertEquals($newSystemUsername, $obRetrieved->mail_smtpuser, "Could not update users sytem-override accounts after system save.");
106 $this->assertEquals($newSystemPassword, $obRetrieved->mail_smtppass, "Could not update users sytem-override accounts after system save.");
111 function testSystemAccountMailSettingsChangedNoUserAccessToUsername()
113 //User not alloweed to access system email username/password
114 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
116 $newSystemPort = 864;
117 $newSystemServer = "system.imap.com";
119 $userID = create_guid();
120 $ob = new OutboundEmail();
122 $ob->new_with_id = TRUE;
123 $ob->name = 'Sugar Test';
124 $ob->type = 'system-override';
125 $ob->user_id = $this->_user->id;
126 $ob->mail_sendtype = "SMTP";
127 $ob->mail_smtpuser = "Test User";
128 $ob->mail_smtppass = "User Pass";
133 $system = $ob->getSystemMailerSettings();
134 $system->new_with_id = FALSE;
135 $system->mail_smtpport = $newSystemPort;
136 $system->mail_smtpserver = $newSystemServer;
137 $system->saveSystem();
139 $obRetrieved = new OutboundEmail();
140 $obRetrieved->retrieve($userID);
142 $this->assertEquals($newSystemPort, $obRetrieved->mail_smtpport, "Could not update users sytem-override accounts after system save.");
143 $this->assertEquals($newSystemServer, $obRetrieved->mail_smtpserver, "Could not update users sytem-override accounts after system save.");
144 $this->assertEquals("Test User", $obRetrieved->mail_smtpuser, "Could not update users sytem-override accounts after system save.");
145 $this->assertEquals("User Pass", $obRetrieved->mail_smtppass, "Could not update users sytem-override accounts after system save.");
149 function testUserMailForSystemOverrideRetrieval()
151 $ob = new OutboundEmail();
152 $ob->name = 'Sugar Test';
153 $ob->type = 'system-override';
154 $ob->user_id = $this->_user->id;
155 $ob->mail_sendtype = "SMTP";
156 $ob->mail_smtpuser = "Test User";
160 $retrievedOb = $ob->getUsersMailerForSystemOverride($this->_user->id);
161 $this->assertEquals($ob->name, $retrievedOb->name, "Could not retrieve users system override outbound email account");
162 $this->assertEquals($ob->type, $retrievedOb->type, "Could not retrieve users system override outbound email account");
163 $this->assertEquals($ob->user_id, $retrievedOb->user_id, "Could not retrieve users system override outbound email account");
164 $this->assertEquals($ob->mail_sendtype, $retrievedOb->mail_sendtype, "Could not retrieve users system override outbound email account");
165 $this->assertEquals("Test User", $retrievedOb->mail_smtpuser, "Could not retrieve users system override outbound email account");
168 function testDuplicateSystemAccountForUser()
170 $oe = new OutboundEmail();
171 $userOverideAccont = $oe->createUserSystemOverrideAccount($this->_user->id, "TEST USER NAME", "TEST PASSWORD");
172 $this->userOverideAccont = $userOverideAccont;
173 $retrievedOb = $oe->getUsersMailerForSystemOverride($this->_user->id);
175 $this->assertEquals("TEST USER NAME", $retrievedOb->mail_smtpuser, "Could not duplicate systems outbound account for user");
176 $this->assertEquals($this->_user->id, $retrievedOb->user_id, "Could not duplicate systems outbound account for user");
177 $this->assertEquals("TEST PASSWORD", $retrievedOb->mail_smtppass, "Could not duplicate systems outbound account for user");
178 $this->assertEquals('system-override', $userOverideAccont->type, "Could not duplicate systems outbound account for user");
181 function testIsUserAlloweedAccessToSystemOutboundEmail()
183 $oe = new OutboundEmail();
184 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
185 $emptyTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
186 $this->assertFalse($emptyTest, "User alloweed access to system outbound email account error");
188 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2') ");
189 $allowTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
190 $this->assertTrue($allowTest, "User alloweed access to system outbound email account error");
192 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
193 $emptyTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
194 $this->assertFalse($emptyTest, "User alloweed access to system outbound email account error");
199 function testIsUserAuthRequiredForOverrideAccount()
201 $oe = new OutboundEmail();
203 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
204 $system = $oe->getSystemMailerSettings();
206 //System does not require auth, no user overide account.
207 $system->mail_smtpauth_req = 0;
208 $system->save(FALSE);
210 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
211 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");
213 //System does require auth, no user overide account.
214 $system->mail_smtpauth_req = 1;
215 $system->save(FALSE);
216 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
217 $this->assertTrue($notRequired, "Test failed for determining if user auth required.");
219 //System requires auth and users alloweed to use sys defaults.
220 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2') ");
221 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
222 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");
225 //System requires auth but user details are empty and users are not alloweed to use system details..
226 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
227 $userOverideAccont = $oe->createUserSystemOverrideAccount($this->_user->id, "", "");
228 $this->userOverideAccont = $userOverideAccont;
229 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
230 $this->assertTrue($notRequired, "Test failed for determining if user auth required.");
232 //User has provided all credentials.
233 $this->userOverideAccont->mail_smtpuser = "TEST USER NAME";
234 $this->userOverideAccont->mail_smtppass = "TEST PASSWORD";
235 $this->userOverideAccont->new_with_id = FALSE;
236 $this->userOverideAccont->save();
237 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
238 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");