Merge branch '2015.11-develop' into 2016.11
[tine20] / tests / tine20 / Tinebase / User / EmailUser / Smtp / PostfixTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @subpackage  User
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2009-2013 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Tinebase_PostfixTest
19  */
20 class Tinebase_User_EmailUser_Smtp_PostfixTest extends PHPUnit_Framework_TestCase
21 {
22     /**
23      * user backend
24      *
25      * @var Tinebase_User
26      */
27     protected $_backend = NULL;
28     
29     /**
30      * @var array test objects
31      */
32     protected $_objects = array();
33
34     /**
35      * mailserver domain
36      * 
37      * @var string
38      */
39     protected $_mailDomain = 'tine20.org';
40     
41     /**
42      * Sets up the fixture.
43      * This method is called before a test is executed.
44      *
45      * @access protected
46      */
47     protected function setUp()
48     {
49         $this->_backend = Tinebase_User::getInstance();
50         
51         if (! array_key_exists('Tinebase_EmailUser_Smtp_Postfix', $this->_backend->getPlugins())) {
52             $this->markTestSkipped('Postfix SQL plugin not enabled');
53         }
54
55         if (Tinebase_User::getConfiguredBackend() === Tinebase_User::ACTIVEDIRECTORY) {
56             // error: Zend_Ldap_Exception: 0x44 (Already exists; 00002071: samldb: Account name (sAMAccountName)
57             // 'tine20phpunituser' already in use!): adding: cn=PHPUnit User Tine 2.0,cn=Users,dc=example,dc=org
58             $this->markTestSkipped('skipped for ad backends as it does not allow duplicate CNs');
59         }
60
61         $this->objects['users'] = array();
62         
63         $this->_mailDomain = TestServer::getPrimaryMailDomain();
64     }
65
66     /**
67      * Tears down the fixture
68      * This method is called after a test is executed.
69      *
70      * @access protected
71      */
72     protected function tearDown()
73     {
74         foreach ($this->objects['users'] as $user) {
75             $this->_backend->deleteUser($user);
76         }
77     }
78     
79     /**
80      * try to add an user
81      * 
82      * @return Tinebase_Model_FullUser
83      */
84     public function testAddUser()
85     {
86         $user = Tinebase_User_LdapTest::getTestRecord();
87         $user->smtpUser = new Tinebase_Model_EmailUser(array(
88             'emailAddress'     => $user->accountEmailAddress,
89             'emailForwardOnly' => true,
90             'emailForwards'    => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
91             'emailAliases'     => array('bla@' . $this->_mailDomain, 'blubb@' . $this->_mailDomain)
92         ));
93         
94         $testUser = $this->_backend->addUser($user);
95         $this->objects['users']['testUser'] = $testUser;
96         
97         $this->assertTrue($testUser instanceof Tinebase_Model_FullUser);
98         $this->assertTrue(isset($testUser->smtpUser), 'no smtpUser data found in ' . print_r($testUser->toArray(), TRUE));
99         $this->assertEquals(array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain), $testUser->smtpUser->emailForwards, 'forwards not found');
100         $this->assertEquals(array('bla@' . $this->_mailDomain, 'blubb@' . $this->_mailDomain),     $testUser->smtpUser->emailAliases, 'aliases not found');
101         $this->assertEquals(true,                                            $testUser->smtpUser->emailForwardOnly);
102         $this->assertEquals($user->accountEmailAddress,                      $testUser->smtpUser->emailAddress);
103         
104         return $testUser;
105     }
106     
107     /**
108      * try to update an email account
109      */
110     public function testUpdateUser()
111     {
112         // add smtp user
113         $user = $this->testAddUser();
114         
115         // update user
116         $user->smtpUser->emailForwardOnly = 1;
117         $user->smtpUser->emailAliases = array('bla@' . $this->_mailDomain);
118         $user->smtpUser->emailForwards = array();
119         $user->accountEmailAddress = 'j.smith@' . $this->_mailDomain;
120         
121         $testUser = $this->_backend->updateUser($user);
122         
123         $this->assertEquals(array(),                            $testUser->smtpUser->emailForwards, 'forwards mismatch');
124         $this->assertEquals(array('bla@' . $this->_mailDomain), $testUser->smtpUser->emailAliases,  'aliases mismatch');
125         $this->assertEquals(false,                              $testUser->smtpUser->emailForwardOnly);
126         $this->assertEquals('j.smith@' . $this->_mailDomain,    $testUser->smtpUser->emailAddress);
127         $this->assertEquals($testUser->smtpUser->emailAliases,  $testUser->emailUser->emailAliases,
128             'smtp user data needs to be merged in email user: ' . print_r($testUser->emailUser->toArray(), TRUE));
129     }
130     
131     /**
132      * try to enable an account
133      *
134      */
135     public function testSetStatus()
136     {
137         $user = $this->testAddUser();
138
139         
140         $this->_backend->setStatus($user, Tinebase_User::STATUS_DISABLED);
141         
142         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
143         
144         $this->assertEquals(Tinebase_User::STATUS_DISABLED, $testUser->accountStatus);
145
146         
147         $this->_backend->setStatus($user, Tinebase_User::STATUS_ENABLED);
148         
149         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
150         
151         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
152     }
153     
154     /**
155      * try to update an email account
156      */
157     public function testSetPassword()
158     {
159         // add smtp user
160         $user = $this->testAddUser();
161         
162         $newPassword = Tinebase_Record_Abstract::generateUID();
163         $this->_backend->setPassword($user->getId(), $newPassword);
164         
165         // fetch email pw from db
166         $db = Tinebase_EmailUser::getInstance(Tinebase_Config::SMTP)->getDb();
167         $select = $db->select()
168             ->from(array('smtp_users'))
169             ->where($db->quoteIdentifier('userid') . ' = ?', $user->getId());
170         $stmt = $db->query($select);
171         $queryResult = $stmt->fetch();
172         $stmt->closeCursor();
173         
174         $this->assertTrue(isset($queryResult['passwd']), 'no password in result: ' . print_r($queryResult, TRUE));
175         $hashPw = new Hash_Password();
176         $this->assertTrue($hashPw->validate($queryResult['passwd'], $newPassword), 'password mismatch: ' . print_r($queryResult, TRUE));
177     }
178     
179     /**
180      * testForwardedAlias
181      * 
182      * @see 0007066: postfix email user: allow wildcard alias forwarding
183      */
184     public function testForwardedAlias()
185     {
186         $user = $this->testAddUser();
187         
188         // check destinations
189         $db = Tinebase_EmailUser::getInstance(Tinebase_Config::SMTP)->getDb();
190         $select = $db->select()
191             ->from(array('smtp_destinations'))
192             ->where($db->quoteIdentifier('userid') . ' = ?', $user->getId());
193         $stmt = $db->query($select);
194         $queryResult = $stmt->fetchAll();
195         $stmt->closeCursor();
196         
197         $this->assertEquals(6, count($queryResult), print_r($queryResult, TRUE));
198         $expectedDestinations = array(
199             'bla@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
200             'blubb@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
201             'phpunit@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
202         );
203         foreach ($expectedDestinations as $source => $destinations) {
204             $foundDestinations = array();
205             foreach ($queryResult as $row) {
206                 if ($row['source'] === $source) {
207                     $foundDestinations[] = $row['destination'];
208                 }
209             }
210             $this->assertEquals(2, count($foundDestinations));
211             $this->assertTrue($foundDestinations == $destinations, print_r($destinations, TRUE));
212         }
213     }
214     
215     /**
216      * testLotsOfAliasesAndForwards
217      * 
218      * @see 0007194: alias table in user admin dialog truncated
219      */
220     public function testLotsOfAliasesAndForwards()
221     {
222         $user = $this->testAddUser();
223         $aliases = $forwards = array();
224         for ($i = 0; $i < 100; $i++) {
225             $aliases[] = 'alias_blablablablablablablablalbalbbl' . $i . '@' . $this->_mailDomain;
226         }
227         $user->smtpUser->emailAliases = $aliases;
228         for ($i = 0; $i < 100; $i++) {
229             $forwards[] = 'forward_blablablablablablablablalbalbbl' . $i . '@' . $this->_mailDomain;
230         }
231         $user->smtpUser->emailForwards = $forwards;
232         $testUser = $this->_backend->updateUser($user);
233         
234         $testUser = Tinebase_User::getInstance()->getUserById($testUser->getId(), 'Tinebase_Model_FullUser');
235         $this->assertEquals(100, count($testUser->smtpUser->emailAliases));
236         $this->assertEquals(100, count($testUser->smtpUser->emailForwards));
237     }
238 }