]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestHelper.php
Release 6.4.0
[Github/sugarcrm.git] / tests / SugarTestHelper.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 if(!defined('sugarEntry')) define('sugarEntry', true);
39
40 set_include_path(
41     dirname(__FILE__) . PATH_SEPARATOR .
42     dirname(__FILE__) . '/..' . PATH_SEPARATOR .
43     get_include_path()
44 );
45
46 // constant to indicate that we are running tests
47 if (!defined('SUGAR_PHPUNIT_RUNNER'))
48     define('SUGAR_PHPUNIT_RUNNER', true);
49
50 // initialize the various globals we use
51 global $sugar_config, $db, $fileName, $current_user, $locale, $current_language;
52
53 if ( !isset($_SERVER['HTTP_USER_AGENT']) )
54     // we are probably running tests from the command line
55     $_SERVER['HTTP_USER_AGENT'] = 'cli';
56
57 // move current working directory
58 chdir(dirname(__FILE__) . '/..');
59
60 require_once('include/entryPoint.php');
61
62 require_once('include/utils/layout_utils.php');
63
64 $GLOBALS['db'] = DBManagerFactory::getInstance();
65
66 $current_language = $sugar_config['default_language'];
67 // disable the SugarLogger
68 $sugar_config['logger']['level'] = 'fatal';
69
70 $GLOBALS['sugar_config']['default_permissions'] = array (
71                 'dir_mode' => 02770,
72                 'file_mode' => 0777,
73                 'chown' => '',
74                 'chgrp' => '',
75         );
76
77 $GLOBALS['js_version_key'] = 'testrunner';
78
79 if ( !isset($_SERVER['SERVER_SOFTWARE']) )
80     $_SERVER["SERVER_SOFTWARE"] = 'PHPUnit';
81
82 // helps silence the license checking when running unit tests.
83 $_SESSION['VALIDATION_EXPIRES_IN'] = 'valid';
84
85 $GLOBALS['startTime'] = microtime(true);
86
87 // clean out the cache directory
88 require_once('modules/Administration/QuickRepairAndRebuild.php');
89 $repair = new RepairAndClear();
90 $repair->module_list = array();
91 $repair->show_output = false;
92 $repair->clearJsLangFiles();
93 $repair->clearJsFiles();
94
95 // mark that we got by the admin wizard already
96 $focus = new Administration();
97 $focus->retrieveSettings();
98 $focus->saveSetting('system','adminwizard',1);
99
100 // include the other test tools
101 require_once 'SugarTestObjectUtilities.php';
102 require_once 'SugarTestProjectUtilities.php';
103 require_once 'SugarTestProjectTaskUtilities.php';
104 require_once 'SugarTestUserUtilities.php';
105 require_once 'SugarTestLangPackCreator.php';
106 require_once 'SugarTestThemeUtilities.php';
107 require_once 'SugarTestContactUtilities.php';
108 require_once 'SugarTestEmailUtilities.php';
109 require_once 'SugarTestCampaignUtilities.php';
110 require_once 'SugarTestLeadUtilities.php';
111 require_once 'SugarTestStudioUtilities.php';
112 require_once 'SugarTestMeetingUtilities.php';
113 require_once 'SugarTestCallUtilities.php';
114 require_once 'SugarTestAccountUtilities.php';
115 require_once 'SugarTestTrackerUtility.php';
116 require_once 'SugarTestImportUtilities.php';
117 require_once 'SugarTestMergeUtilities.php';
118 require_once 'SugarTestTaskUtilities.php';
119
120 $GLOBALS['db']->commit();
121
122 // define our testcase subclass
123 class Sugar_PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase
124 {
125     protected $backupGlobals = FALSE;
126
127     protected $useOutputBuffering = true;
128
129     protected function assertPreConditions()
130     {
131         if(isset($GLOBALS['log'])) {
132             $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
133         }
134         SugarCache::instance()->flush();
135     }
136
137     protected function assertPostConditions() {
138         if(!empty($_REQUEST)) {
139             foreach(array_keys($_REQUEST) as $k) {
140                         unset($_REQUEST[$k]);
141                     }
142         }
143
144         if(!empty($_POST)) {
145             foreach(array_keys($_POST) as $k) {
146                         unset($_POST[$k]);
147                     }
148         }
149
150         if(!empty($_GET)) {
151             foreach(array_keys($_GET) as $k) {
152                         unset($_GET[$k]);
153                     }
154         }
155         if(isset($GLOBALS['log'])) {
156             $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
157         }
158         // reset error handler in case somebody set it
159         restore_error_handler();
160     }
161
162     public static function tearDownAfterClass()
163     {
164         unset($GLOBALS['disable_date_format']);
165         unset($GLOBALS['saving_relationships']);
166         unset($GLOBALS['updating_relationships']);
167         $GLOBALS['timedate']->clearCache();
168     }
169 }
170
171 // define output testcase subclass
172 class Sugar_PHPUnit_Framework_OutputTestCase extends PHPUnit_Extensions_OutputTestCase
173 {
174     protected $backupGlobals = FALSE;
175
176     protected $_notRegex;
177     protected $_outputCheck;
178
179     protected function assertPreConditions()
180     {
181         if(isset($GLOBALS['log'])) {
182             $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
183         }
184         SugarCache::instance()->flush();
185     }
186
187     protected function assertPostConditions() {
188         if(!empty($_REQUEST)) {
189             foreach(array_keys($_REQUEST) as $k) {
190                         unset($_REQUEST[$k]);
191                     }
192         }
193
194         if(!empty($_POST)) {
195             foreach(array_keys($_POST) as $k) {
196                         unset($_POST[$k]);
197                     }
198         }
199
200         if(!empty($_GET)) {
201             foreach(array_keys($_GET) as $k) {
202                         unset($_GET[$k]);
203                     }
204         }
205         if(isset($GLOBALS['log'])) {
206             $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
207         }
208     }
209
210     protected function NotRegexCallback($output)
211     {
212         if(empty($this->_notRegex)) {
213             return true;
214         }
215         $this->assertNotRegExp($this->_notRegex, $output);
216         return true;
217     }
218
219     public function setOutputCheck($callback)
220     {
221         if (!is_callable($callback)) {
222             throw new PHPUnit_Framework_Exception;
223         }
224
225         $this->_outputCheck = $callback;
226     }
227
228     protected function runTest()
229     {
230                 $testResult = parent::runTest();
231         if($this->_outputCheck) {
232             $this->assertTrue(call_user_func($this->_outputCheck, $this->output));
233         }
234         return $testResult;
235     }
236
237     public function expectOutputNotRegex($expectedRegex)
238     {
239         if (is_string($expectedRegex) || is_null($expectedRegex)) {
240             $this->_notRegex = $expectedRegex;
241         }
242
243         $this->setOutputCheck(array($this, "NotRegexCallback"));
244     }
245
246 }
247
248 // define a mock logger interface; used for capturing logging messages emited
249 // the test suite
250 class SugarMockLogger
251 {
252         private $_messages = array();
253
254         public function __call($method, $message)
255         {
256                 $this->messages[] = strtoupper($method) . ': ' . $message[0];
257         }
258
259         public function getLastMessage()
260         {
261                 return end($this->messages);
262         }
263
264         public function getMessageCount()
265         {
266                 return count($this->messages);
267         }
268 }