use configured locale for unittests
[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
68         Tinebase_Core::initFramework();
69
70         // set default test mailer
71         Tinebase_Smtp::setDefaultTransport(new Zend_Mail_Transport_Array());
72
73         // set max execution time
74         Tinebase_Core::setExecutionLifeTime(1200);
75
76         if ($config->locale) {
77             Tinebase_Core::setupUserLocale($config->locale);
78         }
79         
80         // this is needed for session handling in unittests (deactivate Zend_Session::writeClose and others)
81         Zend_Session::$_unitTestEnabled = TRUE;
82     }
83     
84     /**
85      * Set white / black lists
86      */
87     public function setWhiteAndBlacklists()
88     {
89         if ($this->isPhpunitVersionGreaterOrEquals("3.6.0")) {
90             $filter = new PHP_CodeCoverage_Filter();
91             $filter->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
92             $filter->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
93             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
94             $filter->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
95         } else if ($this->isPhpunitVersionGreaterOrEquals("3.5.0")) {
96             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TEST_DIR);
97             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_TINE_LIBRARY);
98             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Setup');
99             PHP_CodeCoverage_Filter::getInstance()->addDirectoryToBlacklist(PATH_TO_REAL_DIR.'/Zend');
100         } else {
101             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TEST_DIR);
102             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_TINE_LIBRARY);
103             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Setup');
104             PHPUnit_Util_Filter::addDirectoryToFilter(PATH_TO_REAL_DIR.'/Zend');
105         }
106     }
107
108     /**
109      * inits (adds) some test users
110      */
111     public function initTestUsers()
112     {
113         $personas = $this->_getPersonas();
114         if (count($personas) !== 5) {
115             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(false);
116             Admin_Setup_DemoData::getInstance()->createDemoData(array('en'));
117             Addressbook_Controller_Contact::getInstance()->setGeoDataForContacts(true);
118             $personas = $this->_getPersonas();
119         }
120         
121         Zend_Registry::set('personas', $personas);
122     }
123     
124     /**
125      * fetch persona user accounts
126      * 
127      * @return array loginname => useraccount
128      */
129     protected function _getPersonas()
130     {
131         $personas = array();
132         $personaLoginNames = array('sclever', 'rwright', 'pwulf', 'jmcblack', 'jsmith');
133         foreach ($personaLoginNames as $loginName) {
134             try {
135                 $personas[$loginName] = Tinebase_User::getInstance()->getFullUserByLoginName($loginName);
136             } catch (Tinebase_Exception_NotFound $tenf) {
137             }
138         }
139         return $personas;
140     }
141
142     /**
143      * set test user email address if in config
144      */
145     public function setTestUserEmail()
146     {
147         if (Zend_Registry::get('testConfig')->email) {
148             // set email of test user contact
149             $testUserContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
150             $testUserContact->email = Zend_Registry::get('testConfig')->email;
151             Addressbook_Controller_Contact::getInstance()->update($testUserContact, FALSE);
152         }
153     }
154
155     /**
156      * get test config
157      * 
158      * @return Zend_Config
159      */
160     public function getConfig()
161     {
162         return Zend_Registry::get('testConfig');
163     }
164     
165     /**
166      * assemble CLI command line call (tine20.php)
167      * 
168      * @param string $command
169      * @param bool   $addCredentials
170      * @return string
171      */
172     public static function assembleCliCommand($command = "", $addCredentials = FALSE)
173     {
174         //$backtrace = debug_backtrace();
175         //return $backtrace[1]['class'];
176  
177         $cmd = implode(' ', $_SERVER['argv']);
178         
179         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Original commmand: ' . $cmd);
180         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Script name: ' . $_SERVER['SCRIPT_NAME']);
181         
182         $cmd = preg_replace(array(
183             '@' . preg_quote($_SERVER['SCRIPT_NAME']) . '@',
184             '/--stderr /',
185             '/--colors /',
186             '/--verbose /',
187             '/--stop-on-failure /',
188             '/[\S]+\.php$/',
189             '/ \S+Tests{0,1}/',
190             '/--debug\s*/',
191             '/--filter [\S]+\D/',
192             '/--configuration [\S]+\D/',
193             '/--exclude-group [\S]+\D/',
194             '/-c [\S]+\D/',
195             '/--log-junit [\S]+\D/'
196         ), array(
197             'php',
198             '',
199             '',
200             '',
201             '',
202             '',
203             '',
204             '',
205             '',
206             '',
207             '',
208             '',
209             '',
210         ), $cmd);
211         
212         $cmd .= $command;
213         
214         if ($addCredentials) {
215             $config = TestServer::getInstance()->getConfig();
216             $cmd .= " --username {$config->username} --password {$config->password}";
217         }
218         
219         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Assembled commmand: ' . $cmd);
220         
221         return $cmd;
222     }
223
224     /**
225      * replace maildomain in input file
226      * 
227      * @param string $filename
228      * @return string filename
229      */
230     public static function replaceEmailDomainInFile($filename)
231     {
232         $config = TestServer::getInstance()->getConfig();
233         $maildomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
234         $tempPath = Tinebase_TempFile::getTempPath();
235         $contents = file_get_contents($filename);
236         $contents = preg_replace('/tine20.org/', $maildomain, $contents);
237         file_put_contents($tempPath, $contents);
238         
239         return $tempPath;
240     }
241
242     /**
243      * isPhpunitVersionGreaterOrEquals
244      * 
245      * @param String $version for example '3.6.0'
246      */
247     public function isPhpunitVersionGreaterOrEquals($version)
248     {
249         $phpUnitVersion = explode(' ',PHPUnit_Runner_Version::getVersionString());
250         return (version_compare($phpUnitVersion[1], $version) >= 0);
251     }
252 }