adds generic method for getting the hostname
[tine20] / tests / tine20 / TestServer.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     tests
6  * @subpackage  test root
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL3
8  * @copyright   Copyright (c) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Cornelius Weiss <c.weiss@metaways.de>
10  * 
11  */
12
13 /**
14  * helper class
15  */
16 class TestServer
17 {
18     /**
19      * holdes the instance of the singleton
20      *
21      * @var TestServer
22      */
23     private static $instance = NULL;
24
25     /**
26      * the constructor
27      *
28      */
29     public function __construct()
30     {
31     }
32
33     /**
34      * the singleton pattern
35      *
36      * @return TestServer
37      */
38     public static function getInstance()
39     {
40         if (self::$instance === NULL) {
41             self::$instance = new TestServer;
42         }
43
44         return self::$instance;
45     }
46
47     /**
48      * init the test framework
49      */
50     public function initFramework()
51     {
52         $this->setWhiteAndBlacklists();
53         
54         // get config
55         $configData = @include('phpunitconfig.inc.php');
56         if ($configData === false) {
57             $configData = include('config.inc.php');
58         }
59         if ($configData === false) {
60             die ('central configuration file config.inc.php not found in includepath: ' . get_include_path());
61         }
62         $config = new Zend_Config($configData);
63
64         Zend_Registry::set('testConfig', $config);
65
66         $_SERVER['DOCUMENT_ROOT'] = $config->docroot;
67         $_SERVER['REQUEST_URI'] = '';
68
69         Tinebase_Core::initFramework();
70
71         // set default test mailer
72         Tinebase_Smtp::setDefaultTransport(new Zend_Mail_Transport_Array());
73
74         // set max execution time
75         Tinebase_Core::setExecutionLifeTime(1200);
76
77         if ($config->locale) {
78             Tinebase_Core::setupUserLocale($config->locale);
79         }
80         
81         // this is needed for session handling in unittests (deactivate Zend_Session::writeClose and others)
82         Zend_Session::$_unitTestEnabled = TRUE;
83     }
84     
85     /**
86      * Set white / black lists
87      */
88     public function setWhiteAndBlacklists()
89     {
90         if ($this->isPhpunitVersionGreaterOrEquals("3.6.0")) {
91             $filter = new PHP_CodeCoverage_Filter();
92             $filter->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
93             $filter->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
94             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
95             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
96         } else if ($this->isPhpunitVersionGreaterOrEquals("3.5.0")) {
97             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
98             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
99             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
100             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
101         } else {
102             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TEST_DIR);
103             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TINE_LIBRARY);
104             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Setup');
105             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Zend');
106         }
107     }
108
109     /**
110      * inits (adds) some test users
111      */
112     public function initTestUsers()
113     {
114         $personas = $this->_getPersonas();
115         if (count($personas) !== 5) {
116             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(false);
117             Admin_Setup_DemoData::getInstance()->createDemoData(array('en'));
118             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(true);
119             $personas = $this->_getPersonas();
120         }
121         
122         Zend_Registry::set('personas', $personas);
123     }
124     
125     /**
126      * fetch persona user accounts
127      * 
128      * @return array loginname => useraccount
129      */
130     protected function _getPersonas()
131     {
132         $personas = array();
133         $personaLoginNames = array('sclever', 'rwright', 'pwulf', 'jmcblack', 'jsmith');
134         foreach ($personaLoginNames as $loginName) {
135             try {
136                 $personas[$loginName] = Tinebase_User::getInstance()->getFullUserByLoginName($loginName);
137             } catch (Tinebase_Exception_NotFound $tenf) {
138             }
139         }
140         return $personas;
141     }
142
143     /**
144      * set test user email address if in config
145      */
146     public function setTestUserEmail()
147     {
148         if (Zend_Registry::get('testConfig')->email) {
149             // set email of test user contact
150             $testUserContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
151             $testUserContact->email = Zend_Registry::get('testConfig')->email;
152             Addressbook_Controller_Contact::getInstance()->update($testUserContact, FALSE);
153         }
154     }
155
156     /**
157      * get test config
158      * 
159      * @return Zend_Config
160      */
161     public function getConfig()
162     {
163         return Zend_Registry::get('testConfig');
164     }
165     
166     /**
167      * assemble CLI command line call (tine20.php)
168      * 
169      * @param string $command
170      * @param bool   $addCredentials
171      * @return string
172      */
173     public static function assembleCliCommand($command = "", $addCredentials = FALSE)
174     {
175         //$backtrace = debug_backtrace();
176         //return $backtrace[1]['class'];
177  
178         $cmd = implode(' ', $_SERVER['argv']);
179         
180         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Original commmand: ' . $cmd);
181         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Script name: ' . $_SERVER['SCRIPT_NAME']);
182         
183         $cmd = preg_replace(array(
184             '@' . preg_quote($_SERVER['SCRIPT_NAME']) . '@',
185             '/--stderr /',
186             '/--colors /',
187             '/--verbose /',
188             '/--stop-on-failure /',
189             '/[\S]+\.php$/',
190             '/ \S+Tests{0,1}/',
191             '/--debug\s*/',
192             '/--filter [\S]+\D/',
193             '/--configuration [\S]+\D/',
194             '/--exclude-group [\S]+\D/',
195             '/-c [\S]+\D/',
196             '/--log-junit [\S]+\D/'
197         ), array(
198             'php',
199             '',
200             '',
201             '',
202             '',
203             '',
204             '',
205             '',
206             '',
207             '',
208             '',
209             '',
210             '',
211         ), $cmd);
212         
213         $cmd .= $command;
214         
215         if ($addCredentials) {
216             $config = TestServer::getInstance()->getConfig();
217             $cmd .= " --username {$config->username} --password {$config->password}";
218         }
219         
220         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Assembled commmand: ' . $cmd);
221         
222         return $cmd;
223     }
224
225     /**
226      * replace maildomain in input file
227      * 
228      * @param string $filename
229      * @return string filename
230      */
231     public static function replaceEmailDomainInFile($filename)
232     {
233         $config = TestServer::getInstance()->getConfig();
234         $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
235         $tempPath = Tinebase_TempFile::getTempPath();
236         $contents = file_get_contents($filename);
237         $contents = preg_replace('/tine20.org/', $maildomain, $contents);
238         file_put_contents($tempPath, $contents);
239         
240         return $tempPath;
241     }
242
243     /**
244      * isPhpunitVersionGreaterOrEquals
245      * 
246      * @param String $version for example '3.6.0'
247      */
248     public function isPhpunitVersionGreaterOrEquals($version)
249     {
250         $phpUnitVersion = explode(' ',PHPUnit_Runner_Version::getVersionString());
251         return (version_compare($phpUnitVersion[1], $version) >= 0);
252     }
253 }