2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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();
54 $current_user = $this->_user;
57 public function tearDown()
59 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
60 //unset($GLOBALS['current_user']);
61 $GLOBALS['db']->query("DELETE FROM outbound_email WHERE user_id= '{$this->_user->id}'");
62 if ($this->ob != null)
63 $GLOBALS['db']->query("DELETE FROM outbound_email WHERE id= '{$this->ob->id}'");
64 if ($this->userOverideAccont != null)
65 $GLOBALS['db']->query("DELETE FROM outbound_email WHERE id= '{$this->userOverideAccont->id}'");
68 function testSystemAccountMailSettingsChangedUserAccessToUsername()
70 //User not alloweed to access system email username/password
71 $GLOBALS['db']->query("DELETE FROM config where name='allow_default_outbound' AND category='notify'");
72 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2')");
75 $newSystemServer = "system.imap.com";
76 $newSystemUsername = "system_user_name";
77 $newSystemPassword = "SYSTEM_PASSWORD";
79 $userID = create_guid();
80 $ob = new OutboundEmail();
82 $ob->new_with_id = TRUE;
83 $ob->name = 'Sugar Test 1';
84 $ob->type = 'system-override';
85 $ob->user_id = $this->_user->id;
86 $ob->mail_sendtype = "SMTP";
87 $ob->mail_smtpuser = "Test User";
88 $ob->mail_smtppass = "User Pass";
93 $system = $ob->getSystemMailerSettings();
94 $system->new_with_id = FALSE;
95 $system->mail_smtpport = $newSystemPort;
96 $system->mail_smtpserver = $newSystemServer;
97 $system->mail_smtpuser = $newSystemUsername;
98 $system->mail_smtppass = $newSystemPassword;
100 $system->saveSystem();
102 $obRetrieved = new OutboundEmail();
103 $obRetrieved->retrieve($userID);
105 $this->assertEquals($newSystemPort, $obRetrieved->mail_smtpport, "Could not update users port system-override accounts after system save.");
106 $this->assertEquals($newSystemServer, $obRetrieved->mail_smtpserver, "Could not update users server system-override accounts after system save.");
107 $this->assertEquals($newSystemUsername, $obRetrieved->mail_smtpuser, "Could not update users username system-override accounts after system save.");
108 $this->assertEquals($newSystemPassword, $obRetrieved->mail_smtppass, "Could not update users password system-override accounts after system save.");
113 function testSystemAccountMailSettingsChangedNoUserAccessToUsername()
115 //User not alloweed to access system email username/password
116 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
118 $newSystemPort = 864;
119 $newSystemServer = "system.imap.com";
121 $userID = create_guid();
122 $ob = new OutboundEmail();
124 $ob->new_with_id = TRUE;
125 $ob->name = 'Sugar Test 2';
126 $ob->type = 'system-override';
127 $ob->user_id = $this->_user->id;
128 $ob->mail_sendtype = "SMTP";
129 $ob->mail_smtpuser = "Test User";
130 $ob->mail_smtppass = "User Pass";
135 $system = $ob->getSystemMailerSettings();
136 $system->new_with_id = FALSE;
137 $system->mail_smtpport = $newSystemPort;
138 $system->mail_smtpserver = $newSystemServer;
139 $system->saveSystem();
141 $obRetrieved = new OutboundEmail();
142 $obRetrieved->retrieve($userID);
144 $this->assertEquals($newSystemPort, $obRetrieved->mail_smtpport, "Could not update users system-override accounts after system save.");
145 $this->assertEquals($newSystemServer, $obRetrieved->mail_smtpserver, "Could not update users system-override accounts after system save.");
146 $this->assertEquals("Test User", $obRetrieved->mail_smtpuser, "Could not update users system-override accounts after system save.");
147 $this->assertEquals("User Pass", $obRetrieved->mail_smtppass, "Could not update users system-override accounts after system save.");
151 function testUserMailForSystemOverrideRetrieval()
153 $ob = new OutboundEmail();
154 $ob->name = 'Sugar Test 3';
155 $ob->type = 'system-override';
156 $ob->user_id = $this->_user->id;
157 $ob->mail_sendtype = "SMTP";
158 $ob->mail_smtpuser = "Test User";
162 $retrievedOb = $ob->getUsersMailerForSystemOverride($this->_user->id);
163 $this->assertEquals($ob->name, $retrievedOb->name, "Could not retrieve users system override outbound email account");
164 $this->assertEquals($ob->type, $retrievedOb->type, "Could not retrieve users system override outbound email account");
165 $this->assertEquals($ob->user_id, $retrievedOb->user_id, "Could not retrieve users system override outbound email account");
166 $this->assertEquals($ob->mail_sendtype, $retrievedOb->mail_sendtype, "Could not retrieve users system override outbound email account");
167 $this->assertEquals("Test User", $retrievedOb->mail_smtpuser, "Could not retrieve users system override outbound email account");
170 function testDuplicateSystemAccountForUser()
172 $oe = new OutboundEmail();
173 $userOverideAccont = $oe->createUserSystemOverrideAccount($this->_user->id, "TEST USER NAME", "TEST PASSWORD");
174 $this->userOverideAccont = $userOverideAccont;
175 $retrievedOb = $oe->getUsersMailerForSystemOverride($this->_user->id);
177 $this->assertEquals("TEST USER NAME", $retrievedOb->mail_smtpuser, "Could not duplicate systems outbound account for user");
178 $this->assertEquals($this->_user->id, $retrievedOb->user_id, "Could not duplicate systems outbound account for user");
179 $this->assertEquals("TEST PASSWORD", $retrievedOb->mail_smtppass, "Could not duplicate systems outbound account for user");
180 $this->assertEquals('system-override', $userOverideAccont->type, "Could not duplicate systems outbound account for user");
183 function testIsUserAlloweedAccessToSystemOutboundEmail()
185 $oe = new OutboundEmail();
186 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
187 $emptyTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
188 $this->assertFalse($emptyTest, "User alloweed access to system outbound email account error");
190 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2') ");
191 $allowTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
192 $this->assertTrue($allowTest, "User alloweed access to system outbound email account error");
194 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
195 $emptyTest = $oe->isAllowUserAccessToSystemDefaultOutbound();
196 $this->assertFalse($emptyTest, "User alloweed access to system outbound email account error");
201 function testIsUserAuthRequiredForOverrideAccount()
203 $oe = new OutboundEmail();
205 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
206 $system = $oe->getSystemMailerSettings();
208 //System does not require auth, no user overide account.
209 $system->mail_smtpauth_req = 0;
210 $system->save(FALSE);
212 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
213 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");
215 //System does require auth, no user overide account.
216 $system->mail_smtpauth_req = 1;
217 $system->save(FALSE);
218 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
219 $this->assertTrue($notRequired, "Test failed for determining if user auth required.");
221 //System requires auth and users alloweed to use sys defaults.
222 $GLOBALS['db']->query("INSERT INTO config (category,name,value) VALUES ('notify','allow_default_outbound','2') ");
223 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
224 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");
227 //System requires auth but user details are empty and users are not alloweed to use system details..
228 $GLOBALS['db']->query("DELETE FROM config WHERE category='notify' AND name='allow_default_outbound' ");
229 $userOverideAccont = $oe->createUserSystemOverrideAccount($this->_user->id, "", "");
230 $this->userOverideAccont = $userOverideAccont;
231 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
232 $this->assertTrue($notRequired, "Test failed for determining if user auth required.");
234 //User has provided all credentials.
235 $this->userOverideAccont->mail_smtpuser = "TEST USER NAME";
236 $this->userOverideAccont->mail_smtppass = "TEST PASSWORD";
237 $this->userOverideAccont->new_with_id = FALSE;
238 $this->userOverideAccont->save();
239 $notRequired = $oe->doesUserOverrideAccountRequireCredentials($this->_user->id);
240 $this->assertFalse($notRequired, "Test failed for determining if user auth required.");