Postfix test: order should not be important
[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->assertTrue(in_array('unittest@' . $this->_mailDomain, $testUser->smtpUser->emailForwards), 'forwards not found');
100         $this->assertTrue(in_array('test@' . $this->_mailDomain, $testUser->smtpUser->emailForwards), 'forwards not found');
101         $this->assertTrue(in_array('bla@' . $this->_mailDomain, $testUser->smtpUser->emailAliases), 'aliases not found');
102         $this->assertTrue(in_array('blubb@' . $this->_mailDomain, $testUser->smtpUser->emailAliases), 'aliases not found');
103         $this->assertEquals(true,                                            $testUser->smtpUser->emailForwardOnly);
104         $this->assertEquals($user->accountEmailAddress,                      $testUser->smtpUser->emailAddress);
105         
106         return $testUser;
107     }
108     
109     /**
110      * try to update an email account
111      */
112     public function testUpdateUser()
113     {
114         // add smtp user
115         $user = $this->testAddUser();
116         
117         // update user
118         $user->smtpUser->emailForwardOnly = 1;
119         $user->smtpUser->emailAliases = array('bla@' . $this->_mailDomain);
120         $user->smtpUser->emailForwards = array();
121         $user->accountEmailAddress = 'j.smith@' . $this->_mailDomain;
122         
123         $testUser = $this->_backend->updateUser($user);
124         
125         $this->assertEquals(array(),                            $testUser->smtpUser->emailForwards, 'forwards mismatch');
126         $this->assertEquals(array('bla@' . $this->_mailDomain), $testUser->smtpUser->emailAliases,  'aliases mismatch');
127         $this->assertEquals(false,                              $testUser->smtpUser->emailForwardOnly);
128         $this->assertEquals('j.smith@' . $this->_mailDomain,    $testUser->smtpUser->emailAddress);
129         $this->assertEquals($testUser->smtpUser->emailAliases,  $testUser->emailUser->emailAliases,
130             'smtp user data needs to be merged in email user: ' . print_r($testUser->emailUser->toArray(), TRUE));
131     }
132     
133     /**
134      * try to enable an account
135      *
136      */
137     public function testSetStatus()
138     {
139         $user = $this->testAddUser();
140
141         
142         $this->_backend->setStatus($user, Tinebase_User::STATUS_DISABLED);
143         
144         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
145         
146         $this->assertEquals(Tinebase_User::STATUS_DISABLED, $testUser->accountStatus);
147
148         
149         $this->_backend->setStatus($user, Tinebase_User::STATUS_ENABLED);
150         
151         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
152         
153         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
154     }
155     
156     /**
157      * try to update an email account
158      */
159     public function testSetPassword()
160     {
161         // add smtp user
162         $user = $this->testAddUser();
163         
164         $newPassword = Tinebase_Record_Abstract::generateUID();
165         $this->_backend->setPassword($user->getId(), $newPassword);
166         
167         // fetch email pw from db
168         $db = Tinebase_EmailUser::getInstance(Tinebase_Config::SMTP)->getDb();
169         $select = $db->select()
170             ->from(array('smtp_users'))
171             ->where($db->quoteIdentifier('userid') . ' = ?', $user->getId());
172         $stmt = $db->query($select);
173         $queryResult = $stmt->fetch();
174         $stmt->closeCursor();
175         
176         $this->assertTrue(isset($queryResult['passwd']), 'no password in result: ' . print_r($queryResult, TRUE));
177         $hashPw = new Hash_Password();
178         $this->assertTrue($hashPw->validate($queryResult['passwd'], $newPassword), 'password mismatch: ' . print_r($queryResult, TRUE));
179     }
180     
181     /**
182      * testForwardedAlias
183      * 
184      * @see 0007066: postfix email user: allow wildcard alias forwarding
185      */
186     public function testForwardedAlias()
187     {
188         $user = $this->testAddUser();
189         
190         // check destinations
191         $db = Tinebase_EmailUser::getInstance(Tinebase_Config::SMTP)->getDb();
192         $select = $db->select()
193             ->from(array('smtp_destinations'))
194             ->where($db->quoteIdentifier('userid') . ' = ?', $user->getId());
195         $stmt = $db->query($select);
196         $queryResult = $stmt->fetchAll();
197         $stmt->closeCursor();
198         
199         $this->assertEquals(6, count($queryResult), print_r($queryResult, TRUE));
200         $expectedDestinations = array(
201             'bla@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
202             'blubb@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
203             'phpunit@' . $this->_mailDomain => array('unittest@' . $this->_mailDomain, 'test@' . $this->_mailDomain),
204         );
205         foreach ($expectedDestinations as $source => $destinations) {
206             $foundDestinations = array();
207             foreach ($queryResult as $row) {
208                 if ($row['source'] === $source) {
209                     $foundDestinations[] = $row['destination'];
210                 }
211             }
212             $this->assertEquals(2, count($foundDestinations));
213             $this->assertTrue($foundDestinations == $destinations, print_r($destinations, TRUE));
214         }
215     }
216     
217     /**
218      * testLotsOfAliasesAndForwards
219      * 
220      * @see 0007194: alias table in user admin dialog truncated
221      */
222     public function testLotsOfAliasesAndForwards()
223     {
224         $user = $this->testAddUser();
225         $aliases = $forwards = array();
226         for ($i = 0; $i < 100; $i++) {
227             $aliases[] = 'alias_blablablablablablablablalbalbbl' . $i . '@' . $this->_mailDomain;
228         }
229         $user->smtpUser->emailAliases = $aliases;
230         for ($i = 0; $i < 100; $i++) {
231             $forwards[] = 'forward_blablablablablablablablalbalbbl' . $i . '@' . $this->_mailDomain;
232         }
233         $user->smtpUser->emailForwards = $forwards;
234         $testUser = $this->_backend->updateUser($user);
235         
236         $testUser = Tinebase_User::getInstance()->getUserById($testUser->getId(), 'Tinebase_Model_FullUser');
237         $this->assertEquals(100, count($testUser->smtpUser->emailAliases));
238         $this->assertEquals(100, count($testUser->smtpUser->emailForwards));
239     }
240 }