fetch primary domain from smtp config for tests
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 11 Jan 2016 16:02:35 +0000 (17:02 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 11 Jan 2016 16:19:48 +0000 (17:19 +0100)
* normalizes mail domain fetching (method of TestServer)
* get domain for demo users from smtp config, too

Change-Id: If609a5998481d40cfe61efb619652742a7e4c2fe
Reviewed-on: http://gerrit.tine20.com/customers/2570
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Admin/CliTest.php
tests/tine20/Courses/JsonTest.php
tests/tine20/Felamimail/Controller/MessageTest.php
tests/tine20/Felamimail/Frontend/JsonTest.php
tests/tine20/TestCase.php
tests/tine20/TestServer.php
tests/tine20/Tinebase/User/EmailUser/Imap/DovecotTest.php
tests/tine20/Tinebase/User/EmailUser/Smtp/PostfixTest.php
tests/tine20/Tinebase/User/LdapTest.php
tests/tine20/Tinebase/User/SqlTest.php
tine20/Admin/Setup/DemoData.php

index 92ef111..e557bdc 100644 (file)
@@ -340,8 +340,7 @@ class Admin_CliTest extends TestCase
         if ($username == 'hmoster') {
             $this->assertEquals('Hins', $user->accountFirstName);
         }
-        $config = TestServer::getInstance()->getConfig();
-        $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $maildomain = TestServer::getPrimaryMailDomain();
         $this->assertEquals($username . '@' . $maildomain, $user->accountEmailAddress);
     }
 
@@ -371,8 +370,7 @@ class Admin_CliTest extends TestCase
     public function testImportUsersWithEmailUser()
     {
         $userBackend = Tinebase_User::getInstance();
-        $config = TestServer::getInstance()->getConfig();
-        $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $maildomain = TestServer::getPrimaryMailDomain();
 
         $readFile = fopen(dirname(__FILE__) . '/files/tine_user5.csv', 'r');
         $writeFile = fopen('test.csv', 'w');
index 96edeeb..9684308 100644 (file)
@@ -253,8 +253,7 @@ class Courses_JsonTest extends TestCase
         $this->assertEquals('lahmph', $lahm['data']);
         
         // get user and check email
-        $testConfig = Zend_Registry::get('testConfig');
-        $maildomain = ($testConfig->maildomain) ? $testConfig->maildomain : 'school.org';
+        $maildomain = TestServer::getPrimaryMailDomain();
         $user = Tinebase_User::getInstance()->getFullUserById($lahm['id']);
         $this->assertEquals('lahmph', $user->accountLoginName);
         $this->assertEquals('lahmph@' . $maildomain, $user->accountEmailAddress);
@@ -646,8 +645,7 @@ class Courses_JsonTest extends TestCase
             $this->assertGreaterThan(0, $result['results']);
             
         } else {
-            $testConfig = Zend_Registry::get('testConfig');
-            $maildomain = ($testConfig->maildomain) ? $testConfig->maildomain : 'school.org';
+            $maildomain = TestServer::getPrimaryMailDomain();
             
             $importer = call_user_func($definition->plugin . '::createFromDefinition', $definition, array(
                     'group_id'                  => $courseData['group_id'],
index 8fc7958..628c449 100644 (file)
@@ -1119,8 +1119,7 @@ class Felamimail_Controller_MessageTest extends PHPUnit_Framework_TestCase
      */
     public function testSendWithWrongLineEnd()
     {
-        $config = TestServer::getInstance()->getConfig();
-        $mailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $mailDomain = TestServer::getPrimaryMailDomain();
         
         // build message with wrong line end rfc822 part
         $mail = new Tinebase_Mail('utf-8');
index 4765855..4dc587a 100644 (file)
@@ -164,8 +164,7 @@ class Felamimail_Frontend_JsonTest extends PHPUnit_Framework_TestCase
             $this->_getFolder($folderToCreate);
         }
         
-        $config = TestServer::getInstance()->getConfig();
-        $this->_mailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $this->_mailDomain = TestServer::getPrimaryMailDomain();
 
         $this->_frontend = new Tinebase_Frontend_Json();
     }
index 17f34d1..71bad22 100644 (file)
@@ -298,19 +298,7 @@ abstract class TestCase extends PHPUnit_Framework_TestCase
      */
     protected function _getMailDomain()
     {
-        $testconfig = Zend_Registry::get('testConfig');
-        
-        if ($testconfig && isset($testconfig->maildomain)) {
-            return $testconfig->maildomain;
-        }
-        
-        if (!empty(Tinebase_Core::getUser()->accountEmailAddress)) {
-            list($user, $domain) = explode('@', Tinebase_Core::getUser()->accountEmailAddress, 2);
-            
-            return $domain;
-        }
-        
-        return 'tine20.org';
+        return TestServer::getPrimaryMailDomain();
     }
     
     /**
index a9b9dab..fa999f7 100644 (file)
@@ -233,8 +233,7 @@ class TestServer
      */
     public static function replaceEmailDomainInFile($filename)
     {
-        $config = TestServer::getInstance()->getConfig();
-        $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $maildomain = self::getPrimaryMailDomain();
         $tempPath = Tinebase_TempFile::getTempPath();
         $contents = file_get_contents($filename);
         $contents = preg_replace('/tine20.org/', $maildomain, $contents);
@@ -244,6 +243,34 @@ class TestServer
     }
 
     /**
+     * returns configured primary mail domain
+     *
+     * phpunit.config.inc > smtp config primary domain > current user mail domain > tine20.org
+     *
+     * @return mixed|string
+     */
+    public static function getPrimaryMailDomain()
+    {
+        $config = TestServer::getInstance()->getConfig();
+        if ($config->maildomain) {
+            return $config->maildomain;
+        } else {
+            $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
+            if (isset($smtpConfig['primarydomain'])) {
+                return $smtpConfig['primarydomain'];
+            }
+
+            if (!empty(Tinebase_Core::getUser()->accountEmailAddress)) {
+                list($user, $domain) = explode('@', Tinebase_Core::getUser()->accountEmailAddress, 2);
+                return $domain;
+            }
+
+        }
+
+        return 'tine20.org';
+    }
+
+    /**
      * isPhpunitVersionGreaterOrEquals
      * 
      * @param String $version for example '3.6.0'
index 048283a..9dba2a6 100644 (file)
@@ -244,8 +244,7 @@ class Tinebase_User_EmailUser_Imap_DovecotTest extends PHPUnit_Framework_TestCas
      */
     public function testDuplicateUserId()
     {
-        $config = Zend_Registry::get('testConfig');
-        $emailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $emailDomain = TestServer::getPrimaryMailDomain();
         $user = $this->_addUser('testuser@' . $emailDomain);
         
         // update user loginname
index 0ad2669..fc6da3e 100644 (file)
@@ -66,10 +66,7 @@ class Tinebase_User_EmailUser_Smtp_PostfixTest extends PHPUnit_Framework_TestCas
         
         $this->objects['users'] = array();
         
-        $config = TestServer::getInstance()->getConfig();
-        if ($config->maildomain) {
-            $this->_mailDomain = $config->maildomain;
-        }
+        $this->_mailDomain = TestServer::getPrimaryMailDomain();
     }
 
     /**
index 2d8d2e6..a8930a9 100644 (file)
@@ -228,8 +228,7 @@ class Tinebase_User_LdapTest extends TestCase
      */
     public static function getTestRecord()
     {
-        $config = Zend_Registry::get('testConfig');
-        $emailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
+        $emailDomain = TestServer::getPrimaryMailDomain();
         
         $user  = new Tinebase_Model_FullUser(array(
             'accountLoginName'      => 'tine20phpunituser',
index 07a312c..f8baef8 100644 (file)
@@ -389,16 +389,8 @@ class Tinebase_User_SqlTest extends TestCase
      */
     public static function getTestRecord()
     {
-        $testconfig = Zend_Registry::get('testConfig');
-        
-        if ($testconfig && isset($testconfig->maildomain)) {
-            $domain = $testconfig->maildomain;
-        } else if (!empty(Tinebase_Core::getUser()->accountEmailAddress)) {
-            list($user, $domain) = explode('@', Tinebase_Core::getUser()->accountEmailAddress, 2);
-        } else {
-             $domain = 'tine20.org';
-        }
-        
+        $domain = TestServer::getPrimaryMailDomain();
+
         $user  = new Tinebase_Model_FullUser(array(
             'accountLoginName'      => 'tine20phpunituser',
             'accountStatus'         => 'enabled',
@@ -411,4 +403,4 @@ class Tinebase_User_SqlTest extends TestCase
         
         return $user;
     }
-}
\ No newline at end of file
+}
index 6196c22..b6d8cca 100644 (file)
@@ -174,13 +174,7 @@ class Admin_Setup_DemoData extends Tinebase_Setup_DemoData_Abstract
                 $group   = Tinebase_Group::getInstance()->getGroupByName('Users');
                 $groupId = $group->getId();
                 
-                try {
-                    $testconfig = Zend_Registry::get('testConfig');
-                } catch (Zend_Exception $e) {
-                    $testconfig = NULL;
-                }
-                // TODO think about fetching this from IMAP config
-                $emailDomain = ($testconfig && isset($testconfig->maildomain)) ? $testconfig->maildomain : 'tine20.org';
+                $emailDomain = $this->_getMailDomain();
 
                 $user = new Tinebase_Model_FullUser(array(
                     'accountLoginName'      => $login,
@@ -212,7 +206,7 @@ class Admin_Setup_DemoData extends Tinebase_Setup_DemoData_Abstract
                     $listBackend->addListMember($group->list_id, $user->contact_id);
                 }
 
-                $this->_setUserPassword($user, $testconfig);
+                $this->_setUserPassword($user);
             }
             
             if (Tinebase_Application::getInstance()->isInstalled('Addressbook') === true) {
@@ -239,15 +233,49 @@ class Admin_Setup_DemoData extends Tinebase_Setup_DemoData_Abstract
         $this->_createRoles();
         $this->_createSharedTags();
     }
-    
+
+    /**
+     * get mail domain
+     *
+     * @return string
+     */
+    protected function _getMailDomain()
+    {
+        $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
+        if (isset($smtpConfig['primarydomain'])) {
+            return $smtpConfig['primarydomain'];
+        }
+
+        $testconfig = $this->_getTestConfig();
+        $emailDomain = ($testconfig && isset($testconfig->maildomain)) ? $testconfig->maildomain : 'tine20.org';
+
+        return $emailDomain;
+    }
+
+    /**
+     * get testconfig
+     *
+     * @return mixed|null
+     */
+    protected function _getTestConfig()
+    {
+        try {
+            $testconfig = Zend_Registry::get('testConfig');
+        } catch (Zend_Exception $e) {
+            $testconfig = NULL;
+        }
+
+        return $testconfig;
+    }
+
     /**
      * give additional testusers a password
      * 
      * @param Tinebase_Model_User $user
-     * @param Zend_Config|null $testconfig
      */
-    protected function _setUserPassword($user, $testconfig)
+    protected function _setUserPassword($user)
     {
+        $testconfig = $this->_getTestConfig();
         $password =
             // use password of the primary test account 
             ($testconfig && isset($testconfig->password)) ? $testconfig->password :