fix missing extraFreetime Icons
[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  * @todo invent common bootstrap for ServerTests and normal Tests to avoid code duplication
12  */
13
14 /**
15  * helper class
16  */
17 class TestServer
18 {
19     /**
20      * holdes the instance of the singleton
21      *
22      * @var TestServer
23      */
24     private static $instance = NULL;
25
26     /**
27      * the constructor
28      *
29      */
30     public function __construct()
31     {
32     }
33
34     /**
35      * the singleton pattern
36      *
37      * @return TestServer
38      */
39     public static function getInstance()
40     {
41         if (self::$instance === NULL) {
42             self::$instance = new TestServer;
43         }
44
45         return self::$instance;
46     }
47
48     /**
49      * init the test framework
50      */
51     public function initFramework()
52     {
53         $this->setWhiteAndBlacklists();
54         
55         // get config
56         $configData = @include('phpunitconfig.inc.php');
57         if ($configData === false) {
58             $configData = include('config.inc.php');
59         }
60         if ($configData === false) {
61             die ('central configuration file config.inc.php not found in includepath: ' . get_include_path());
62         }
63         $config = new Zend_Config($configData);
64
65         Zend_Registry::set('testConfig', $config);
66
67         $_SERVER['DOCUMENT_ROOT'] = $config->docroot;
68         $_SERVER['REQUEST_URI'] = '';
69         
70         Tinebase_Core::startCoreSession();
71         
72         Tinebase_Core::initFramework();
73
74         // set default test mailer
75         Tinebase_Smtp::setDefaultTransport(new Zend_Mail_Transport_Array());
76
77         // set max execution time
78         Tinebase_Core::setExecutionLifeTime(1200);
79
80         if ($config->locale) {
81             Tinebase_Core::setupUserLocale($config->locale);
82         }
83         
84         // this is needed for session handling in unittests (deactivate Zend_Session::writeClose and others)
85         Zend_Session::$_unitTestEnabled = TRUE;
86     }
87     
88     /**
89      * Set white / black lists
90      */
91     public function setWhiteAndBlacklists()
92     {
93         if ($this->isPhpunitVersionGreaterOrEquals("3.6.0")) {
94             $filter = new PHP_CodeCoverage_Filter();
95             $filter->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
96             $filter->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
97             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
98             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
99         } else if ($this->isPhpunitVersionGreaterOrEquals("3.5.0")) {
100             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
101             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
102             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
103             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
104         } else {
105             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TEST_DIR);
106             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TINE_LIBRARY);
107             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Setup');
108             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Zend');
109         }
110     }
111
112     /**
113      * inits (adds) some test users
114      */
115     public function initTestUsers()
116     {
117         $personas = $this->_getPersonas();
118         if (count($personas) !== 5) {
119             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(false);
120             Admin_Setup_DemoData::getInstance()->createDemoData(array('en'));
121             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(true);
122             $personas = $this->_getPersonas();
123         }
124         
125         Zend_Registry::set('personas', $personas);
126     }
127     
128     /**
129      * fetch persona user accounts
130      * 
131      * @return array loginname => useraccount
132      */
133     protected function _getPersonas()
134     {
135         $personas = array();
136         $personaLoginNames = array('sclever', 'rwright', 'pwulf', 'jmcblack', 'jsmith');
137         foreach ($personaLoginNames as $loginName) {
138             try {
139                 $personas[$loginName] = Tinebase_User::getInstance()->getFullUserByLoginName($loginName);
140             } catch (Tinebase_Exception_NotFound $tenf) {
141             }
142         }
143         return $personas;
144     }
145
146     /**
147      * set test user email address if in config
148      */
149     public function setTestUserEmail()
150     {
151         if (Zend_Registry::get('testConfig')->email) {
152             // set email of test user contact
153             $testUserContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
154             $testUserContact->email = Zend_Registry::get('testConfig')->email;
155             Addressbook_Controller_Contact::getInstance()->update($testUserContact, FALSE);
156         }
157     }
158
159     /**
160      * get test config
161      * 
162      * @return Zend_Config
163      */
164     public function getConfig()
165     {
166         return Zend_Registry::get('testConfig');
167     }
168     
169     /**
170      * assemble CLI command line call (tine20.php)
171      * 
172      * @param string $command
173      * @param bool   $addCredentials
174      * @return string
175      */
176     public static function assembleCliCommand($command = "", $addCredentials = FALSE)
177     {
178         //$backtrace = debug_backtrace();
179         //return $backtrace[1]['class'];
180  
181         $cmd = implode(' ', $_SERVER['argv']);
182         
183         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Original commmand: ' . $cmd);
184         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Script name: ' . $_SERVER['SCRIPT_NAME']);
185         
186         $cmd = preg_replace(array(
187             '@' . preg_quote($_SERVER['SCRIPT_NAME']) . '@',
188             '/--stderr /',
189             '/--colors{0,1} /',
190             '/--verbose /',
191             '/--stop-on-failure /',
192             '/[\S]+\.php$/',
193             '/ \S+Tests{0,1}/',
194             '/--debug\s*/',
195             '/--filter [\S]+\D/',
196             '/--configuration [\S]+\D/',
197             '/--exclude-group [\S]+\D/',
198             '/-c [\S]+\D/',
199             '/--log-junit [\S]+\D/'
200         ), array(
201             'php',
202             '',
203             '',
204             '',
205             '',
206             '',
207             '',
208             '',
209             '',
210             '',
211             '',
212             '',
213             '',
214         ), $cmd);
215         
216         $cmd .= $command;
217         
218         if ($addCredentials) {
219             $credentials = TestServer::getInstance()->getTestCredentials();
220             $cmd .= " --username {$credentials['username']} --password {$credentials['password']}";
221         }
222         
223         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Assembled commmand: ' . $cmd);
224         
225         return $cmd;
226     }
227
228     /**
229      * replace maildomain in input file
230      * 
231      * @param string $filename
232      * @return string filename
233      */
234     public static function replaceEmailDomainInFile($filename)
235     {
236         $maildomain = self::getPrimaryMailDomain();
237         $tempPath = Tinebase_TempFile::getTempPath();
238         $contents = file_get_contents($filename);
239         $contents = preg_replace('/tine20.org/', $maildomain, $contents);
240         file_put_contents($tempPath, $contents);
241         
242         return $tempPath;
243     }
244
245     /**
246      * returns configured primary mail domain
247      *
248      * phpunit.config.inc > smtp config primary domain > current user mail domain > tine20.org
249      *
250      * @return mixed|string
251      */
252     public static function getPrimaryMailDomain()
253     {
254         $config = TestServer::getInstance()->getConfig();
255         if ($config->maildomain) {
256             return $config->maildomain;
257         } else {
258             $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
259             if (isset($smtpConfig['primarydomain'])) {
260                 return $smtpConfig['primarydomain'];
261             }
262
263             if (!empty(Tinebase_Core::getUser()->accountEmailAddress)) {
264                 list($user, $domain) = explode('@', Tinebase_Core::getUser()->accountEmailAddress, 2);
265                 return $domain;
266             }
267
268         }
269
270         return 'tine20.org';
271     }
272
273     /**
274      * isPhpunitVersionGreaterOrEquals
275      * 
276      * @param String $version for example '3.6.0'
277      */
278     public function isPhpunitVersionGreaterOrEquals($version)
279     {
280         $phpUnitVersion = explode(' ',PHPUnit_Runner_Version::getVersionString());
281         return (version_compare($phpUnitVersion[1], $version) >= 0);
282     }
283
284     /**
285      * login user
286      *
287      * @throws Exception
288      */
289     public function login()
290     {
291         $tinebaseController = Tinebase_Controller::getInstance();
292         $credentials = $this->getTestCredentials();
293         
294         $config = $this->getConfig();
295         $_SERVER['REMOTE_ADDR']     = $config->ip ? $config->ip : '127.0.0.1';
296         $_SERVER['HTTP_USER_AGENT'] = 'Unit Test Client';
297         if (! $tinebaseController->login($credentials['username'], $credentials['password'], new \Zend\Http\PhpEnvironment\Request(), 'TineUnittest')){
298             throw new Exception("Couldn't login, user session required for tests! \n");
299         }
300     }
301     
302     /**
303      * fetch test user credentials
304      * 
305      * @return array
306      * 
307      * @todo DRY: should be moved to abstract TestCase and used in ServerTestCase
308      */
309     public function getTestCredentials()
310     {
311         $config = $this->getConfig();
312         $username = isset($config->login->username) ? $config->login->username : $config->username;
313         $password = isset($config->login->password) ? $config->login->password : $config->password;
314         
315         return array(
316             'username' => $username,
317             'password' => $password
318         );
319     }
320 }