2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 if(!defined('sugarEntry')) define('sugarEntry', true);
41 dirname(__FILE__) . PATH_SEPARATOR .
42 dirname(__FILE__) . '/..' . PATH_SEPARATOR .
46 // constant to indicate that we are running tests
47 if (!defined('SUGAR_PHPUNIT_RUNNER'))
48 define('SUGAR_PHPUNIT_RUNNER', true);
50 // initialize the various globals we use
51 global $sugar_config, $db, $fileName, $current_user, $locale, $current_language;
53 if ( !isset($_SERVER['HTTP_USER_AGENT']) )
54 // we are probably running tests from the command line
55 $_SERVER['HTTP_USER_AGENT'] = 'cli';
57 // move current working directory
58 chdir(dirname(__FILE__) . '/..');
60 require_once('include/entryPoint.php');
62 require_once('include/utils/layout_utils.php');
64 $GLOBALS['db'] = DBManagerFactory::getInstance();
66 $current_language = $sugar_config['default_language'];
67 // disable the SugarLogger
68 $sugar_config['logger']['level'] = 'fatal';
70 $GLOBALS['sugar_config']['default_permissions'] = array (
77 $GLOBALS['js_version_key'] = 'testrunner';
79 if ( !isset($_SERVER['SERVER_SOFTWARE']) )
80 $_SERVER["SERVER_SOFTWARE"] = 'PHPUnit';
82 // helps silence the license checking when running unit tests.
83 $_SESSION['VALIDATION_EXPIRES_IN'] = 'valid';
85 $GLOBALS['startTime'] = microtime(true);
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();
95 // mark that we got by the admin wizard already
96 $focus = new Administration();
97 $focus->retrieveSettings();
98 $focus->saveSetting('system','adminwizard',1);
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';
120 $GLOBALS['db']->commit();
122 // define our testcase subclass
123 class Sugar_PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase
125 protected $backupGlobals = FALSE;
127 protected $useOutputBuffering = true;
129 protected function assertPreConditions()
131 if(isset($GLOBALS['log'])) {
132 $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
134 SugarCache::instance()->flush();
137 protected function assertPostConditions() {
138 if(!empty($_REQUEST)) {
139 foreach(array_keys($_REQUEST) as $k) {
140 unset($_REQUEST[$k]);
145 foreach(array_keys($_POST) as $k) {
151 foreach(array_keys($_GET) as $k) {
155 if(isset($GLOBALS['log'])) {
156 $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
158 // reset error handler in case somebody set it
159 restore_error_handler();
162 public static function tearDownAfterClass()
164 unset($GLOBALS['disable_date_format']);
165 unset($GLOBALS['saving_relationships']);
166 unset($GLOBALS['updating_relationships']);
167 $GLOBALS['timedate']->clearCache();
171 // define output testcase subclass
172 class Sugar_PHPUnit_Framework_OutputTestCase extends PHPUnit_Extensions_OutputTestCase
174 protected $backupGlobals = FALSE;
176 protected $_notRegex;
177 protected $_outputCheck;
179 protected function assertPreConditions()
181 if(isset($GLOBALS['log'])) {
182 $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
184 SugarCache::instance()->flush();
187 protected function assertPostConditions() {
188 if(!empty($_REQUEST)) {
189 foreach(array_keys($_REQUEST) as $k) {
190 unset($_REQUEST[$k]);
195 foreach(array_keys($_POST) as $k) {
201 foreach(array_keys($_GET) as $k) {
205 if(isset($GLOBALS['log'])) {
206 $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
210 protected function NotRegexCallback($output)
212 if(empty($this->_notRegex)) {
215 $this->assertNotRegExp($this->_notRegex, $output);
219 public function setOutputCheck($callback)
221 if (!is_callable($callback)) {
222 throw new PHPUnit_Framework_Exception;
225 $this->_outputCheck = $callback;
228 protected function runTest()
230 $testResult = parent::runTest();
231 if($this->_outputCheck) {
232 $this->assertTrue(call_user_func($this->_outputCheck, $this->output));
237 public function expectOutputNotRegex($expectedRegex)
239 if (is_string($expectedRegex) || is_null($expectedRegex)) {
240 $this->_notRegex = $expectedRegex;
243 $this->setOutputCheck(array($this, "NotRegexCallback"));
248 // define a mock logger interface; used for capturing logging messages emited
250 class SugarMockLogger
252 private $_messages = array();
254 public function __call($method, $message)
256 $this->messages[] = strtoupper($method) . ': ' . $message[0];
259 public function getLastMessage()
261 return end($this->messages);
264 public function getMessageCount()
266 return count($this->messages);
271 * Own exception for SugarTestHelper class
273 * @author mgusev@sugarcrm.com
275 class SugarTestHelperException extends PHPUnit_Framework_Exception
281 * Helper for initialization of global variables of SugarCRM
283 * @author mgusev@sugarcrm.com
285 class SugarTestHelper
288 * @var array array of registered vars. It allows helper to unregister them on tearDown
290 protected static $registeredVars = array();
293 * @var array array of global vars. They are storing on init one time and restoring in global scope each tearDown
295 protected static $initVars = array(
300 * @var array of system preference of SugarCRM as theme etc. They are storing on init one time and restoring each tearDown
302 protected static $systemVars = array();
305 * @var bool is SugarTestHelper inited or not. Just to skip initialization on the second and others call of init method
307 protected static $isInited = false;
310 * All methods are static because of it we disable constructor
312 private function __construct()
317 * All methods are static because of it we disable clone
319 private function __clone()
324 * Initialization of main variables of SugarCRM in global scope
328 public static function init()
330 if (self::$isInited == true)
335 // initialization & backup of sugar_config
336 self::$initVars['GLOBALS']['sugar_config'] = null;
337 if ($GLOBALS['sugar_config'])
339 self::$initVars['GLOBALS']['sugar_config'] = $GLOBALS['sugar_config'];
341 if (self::$initVars['GLOBALS']['sugar_config'] == false)
343 global $sugar_config;
344 if (is_file('config.php'))
346 require_once('config.php');
348 if (is_file('config_override.php'))
350 require_once('config_override.php');
352 self::$initVars['GLOBALS']['sugar_config'] = $GLOBALS['sugar_config'];
355 // backup of current_language
356 self::$initVars['GLOBALS']['current_language'] = 'en_us';
357 if (isset($sugar_config['current_language']))
359 self::$initVars['GLOBALS']['current_language'] = $sugar_config['current_language'];
361 if (isset($GLOBALS['current_language']))
363 self::$initVars['GLOBALS']['current_language'] = $GLOBALS['current_language'];
365 $GLOBALS['current_language'] = self::$initVars['GLOBALS']['current_language'];
367 // backup of reload_vardefs
368 self::$initVars['GLOBALS']['reload_vardefs'] = null;
369 if (isset($GLOBALS['reload_vardefs']))
371 self::$initVars['GLOBALS']['reload_vardefs'] = $GLOBALS['reload_vardefs'];
375 self::$initVars['GLOBALS']['locale'] = null;
376 if (isset($GLOBALS['locale']))
378 self::$initVars['GLOBALS']['locale'] = $GLOBALS['locale'];
380 if (self::$initVars['GLOBALS']['locale'] == false)
382 self::$initVars['GLOBALS']['locale'] = new Localization();
385 // backup of service_object
386 self::$initVars['GLOBALS']['service_object'] = null;
387 if (isset($GLOBALS['service_object']))
389 self::$initVars['GLOBALS']['service_object'] = $GLOBALS['service_object'];
392 // backup of SugarThemeRegistry
393 self::$systemVars['SugarThemeRegistry'] = SugarThemeRegistry::current();
395 self::$isInited = true;
399 * Checking is there helper for variable or not
402 * @param string $varName name of global variable of SugarCRM
403 * @return bool is there helper for a variable or not
404 * @throws SugarTestHelperException fired when there is no implementation of helper for a variable
406 protected static function checkHelper($varName)
408 if (method_exists(__CLASS__, 'setUp_' . $varName) == false)
410 throw new SugarTestHelperException('setUp for $' . $varName . ' is not implemented. ' . __CLASS__ . '::setUp_' . $varName);
415 * Entry point for setup of global variable
418 * @param string $varName name of global variable of SugarCRM
419 * @param array $params some parameters for helper. For example for $mod_strings or $current_user
420 * @return bool is variable setuped or not
422 public static function setUp($varName, $params = array())
425 self::checkHelper($varName);
426 return call_user_func(__CLASS__ . '::setUp_' . $varName, $params);
430 * Clean up all registered variables and restore $initVars and $systemVars
432 * @return bool status of tearDown
434 public static function tearDown()
437 foreach(self::$registeredVars as $varName => $isCalled)
441 unset(self::$registeredVars[$varName]);
442 if (method_exists(__CLASS__, 'tearDown_' . $varName))
444 call_user_func(__CLASS__ . '::tearDown_' . $varName, array());
446 elseif (isset($GLOBALS[$varName]))
448 unset($GLOBALS[$varName]);
453 // Restoring of system variables
454 foreach(self::$initVars as $scope => $vars)
456 foreach ($vars as $name => $value)
458 $GLOBALS[$scope][$name] = $value;
462 // Restoring of theme
463 SugarThemeRegistry::set(self::$systemVars['SugarThemeRegistry']->dirName);
469 * Registration of $current_user in global scope
472 * @param array $params parameters for SugarTestUserUtilities::createAnonymousUser method
473 * @return bool is variable setuped or not
475 protected static function setUp_current_user(array $params)
477 self::$registeredVars['current_user'] = true;
478 $GLOBALS['current_user'] = call_user_func_array('SugarTestUserUtilities::createAnonymousUser', $params);
483 * Removal of $current_user from global scope
486 * @return bool is variable removed or not
488 protected static function tearDown_current_user()
490 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
491 unset($GLOBALS['current_user']);
496 * Registration of $beanList in global scope
499 * @return bool is variable setuped or not
501 protected static function setUp_beanList()
503 self::$registeredVars['beanList'] = true;
505 require('include/modules.php');
510 * Registration of $beanFiles in global scope
513 * @return bool is variable setuped or not
515 protected static function setUp_beanFiles()
517 self::$registeredVars['beanFiles'] = true;
519 require('include/modules.php');
524 * Registration of $moduleList in global scope
527 * @return bool is variable setuped or not
529 protected static function setUp_moduleList()
531 self::$registeredVars['moduleList'] = true;
532 self::setUp_app_list_strings();
533 $GLOBALS['moduleList'] = $GLOBALS['app_list_strings']['moduleList'];
538 * Registration of $modListHeader in global scope
541 * @return bool is variable setuped or not
543 protected static function setUp_modListHeader()
545 self::$registeredVars['modListHeader'] = true;
546 if (isset($GLOBALS['current_user']) == false)
548 self::setUp_current_user(array(
553 $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
558 * Registration of $app_strings in global scope
561 * @return bool is variable setuped or not
563 protected static function setUp_app_strings()
565 self::$registeredVars['app_strings'] = true;
566 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
571 * Registration of $app_list_strings in global scope
574 * @return bool is variable setuped or not
576 protected static function setUp_app_list_strings()
578 self::$registeredVars['app_list_strings'] = true;
579 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
584 * Registration of $timedate in global scope
587 * @return bool is variable setuped or not
589 protected static function setUp_timedate()
591 self::$registeredVars['timedate'] = true;
592 $GLOBALS['timedate'] = TimeDate::getInstance();
597 * Removal of $timedate from global scope
600 * @return bool is variable removed or not
602 protected static function tearDown_timedate()
604 $GLOBALS['timedate']->clearCache();
609 * Registration of $mod_strings in global scope
612 * @param array $params parameters for return_module_language function
613 * @return bool is variable setuped or not
615 protected static function setUp_mod_strings(array $params)
617 self::$registeredVars['mod_strings'] = true;
618 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], $params[0]);