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';
119 require_once 'SugarTestOpportunityUtilities.php';
120 require_once 'SugarTestRelationshipUtilities.php';
122 $GLOBALS['db']->commit();
124 // define our testcase subclass
125 class Sugar_PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase
127 protected $backupGlobals = FALSE;
129 protected $useOutputBuffering = true;
131 protected function assertPreConditions()
133 if(isset($GLOBALS['log'])) {
134 $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
136 SugarCache::instance()->flush();
139 protected function assertPostConditions() {
140 if(!empty($_REQUEST)) {
141 foreach(array_keys($_REQUEST) as $k) {
142 unset($_REQUEST[$k]);
147 foreach(array_keys($_POST) as $k) {
153 foreach(array_keys($_GET) as $k) {
157 if(isset($GLOBALS['log'])) {
158 $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
160 // reset error handler in case somebody set it
161 restore_error_handler();
164 public static function tearDownAfterClass()
166 unset($GLOBALS['disable_date_format']);
167 unset($GLOBALS['saving_relationships']);
168 unset($GLOBALS['updating_relationships']);
169 $GLOBALS['timedate']->clearCache();
173 // define output testcase subclass
174 class Sugar_PHPUnit_Framework_OutputTestCase extends PHPUnit_Extensions_OutputTestCase
176 protected $backupGlobals = FALSE;
178 protected $_notRegex;
179 protected $_outputCheck;
181 protected function assertPreConditions()
183 if(isset($GLOBALS['log'])) {
184 $GLOBALS['log']->info("START TEST: {$this->getName(false)}");
186 SugarCache::instance()->flush();
189 protected function assertPostConditions() {
190 if(!empty($_REQUEST)) {
191 foreach(array_keys($_REQUEST) as $k) {
192 unset($_REQUEST[$k]);
197 foreach(array_keys($_POST) as $k) {
203 foreach(array_keys($_GET) as $k) {
207 if(isset($GLOBALS['log'])) {
208 $GLOBALS['log']->info("DONE TEST: {$this->getName(false)}");
212 protected function NotRegexCallback($output)
214 if(empty($this->_notRegex)) {
217 $this->assertNotRegExp($this->_notRegex, $output);
221 public function setOutputCheck($callback)
223 if (!is_callable($callback)) {
224 throw new PHPUnit_Framework_Exception;
227 $this->_outputCheck = $callback;
230 protected function runTest()
232 $testResult = parent::runTest();
233 if($this->_outputCheck) {
234 $this->assertTrue(call_user_func($this->_outputCheck, $this->output));
239 public function expectOutputNotRegex($expectedRegex)
241 if (is_string($expectedRegex) || is_null($expectedRegex)) {
242 $this->_notRegex = $expectedRegex;
245 $this->setOutputCheck(array($this, "NotRegexCallback"));
250 // define a mock logger interface; used for capturing logging messages emited
252 class SugarMockLogger
254 private $_messages = array();
256 public function __call($method, $message)
258 $this->messages[] = strtoupper($method) . ': ' . $message[0];
261 public function getLastMessage()
263 return end($this->messages);
266 public function getMessageCount()
268 return count($this->messages);
272 require_once('ModuleInstall/ModuleInstaller.php');
275 * Own exception for SugarTestHelper class
277 * @author mgusev@sugarcrm.com
279 class SugarTestHelperException extends PHPUnit_Framework_Exception
285 * Helper for initialization of global variables of SugarCRM
287 * @author mgusev@sugarcrm.com
289 class SugarTestHelper
292 * @var array array of registered vars. It allows helper to unregister them on tearDown
294 protected static $registeredVars = array();
297 * @var array array of global vars. They are storing on init one time and restoring in global scope each tearDown
299 protected static $initVars = array(
304 * @var array of system preference of SugarCRM as theme etc. They are storing on init one time and restoring each tearDown
306 protected static $systemVars = array();
309 * @var array of modules which we should refresh on tearDown.
311 protected static $cleanModules = array();
314 * @var bool is SugarTestHelper inited or not. Just to skip initialization on the second and others call of init method
316 protected static $isInited = false;
319 * All methods are static because of it we disable constructor
321 private function __construct()
326 * All methods are static because of it we disable clone
328 private function __clone()
333 * Initialization of main variables of SugarCRM in global scope
337 public static function init()
339 if (self::$isInited == true)
344 // initialization & backup of sugar_config
345 self::$initVars['GLOBALS']['sugar_config'] = null;
346 if ($GLOBALS['sugar_config'])
348 self::$initVars['GLOBALS']['sugar_config'] = $GLOBALS['sugar_config'];
350 if (self::$initVars['GLOBALS']['sugar_config'] == false)
352 global $sugar_config;
353 if (is_file('config.php'))
355 require_once('config.php');
357 if (is_file('config_override.php'))
359 require_once('config_override.php');
361 self::$initVars['GLOBALS']['sugar_config'] = $GLOBALS['sugar_config'];
364 // backup of current_language
365 self::$initVars['GLOBALS']['current_language'] = 'en_us';
366 if (isset($sugar_config['current_language']))
368 self::$initVars['GLOBALS']['current_language'] = $sugar_config['current_language'];
370 if (isset($GLOBALS['current_language']))
372 self::$initVars['GLOBALS']['current_language'] = $GLOBALS['current_language'];
374 $GLOBALS['current_language'] = self::$initVars['GLOBALS']['current_language'];
376 // backup of reload_vardefs
377 self::$initVars['GLOBALS']['reload_vardefs'] = null;
378 if (isset($GLOBALS['reload_vardefs']))
380 self::$initVars['GLOBALS']['reload_vardefs'] = $GLOBALS['reload_vardefs'];
384 self::$initVars['GLOBALS']['locale'] = null;
385 if (isset($GLOBALS['locale']))
387 self::$initVars['GLOBALS']['locale'] = $GLOBALS['locale'];
389 if (self::$initVars['GLOBALS']['locale'] == false)
391 self::$initVars['GLOBALS']['locale'] = new Localization();
394 // backup of service_object
395 self::$initVars['GLOBALS']['service_object'] = null;
396 if (isset($GLOBALS['service_object']))
398 self::$initVars['GLOBALS']['service_object'] = $GLOBALS['service_object'];
401 // backup of SugarThemeRegistry
402 self::$systemVars['SugarThemeRegistry'] = SugarThemeRegistry::current();
404 self::$isInited = true;
408 * Checking is there helper for variable or not
411 * @param string $varName name of global variable of SugarCRM
412 * @return bool is there helper for a variable or not
413 * @throws SugarTestHelperException fired when there is no implementation of helper for a variable
415 protected static function checkHelper($varName)
417 if (method_exists(__CLASS__, 'setUp_' . $varName) == false)
419 throw new SugarTestHelperException('setUp for $' . $varName . ' is not implemented. ' . __CLASS__ . '::setUp_' . $varName);
424 * Entry point for setup of global variable
427 * @param string $varName name of global variable of SugarCRM
428 * @param array $params some parameters for helper. For example for $mod_strings or $current_user
429 * @return bool is variable setuped or not
431 public static function setUp($varName, $params = array())
434 self::checkHelper($varName);
435 return call_user_func(__CLASS__ . '::setUp_' . $varName, $params);
439 * Clean up all registered variables and restore $initVars and $systemVars
441 * @return bool status of tearDown
443 public static function tearDown()
446 foreach(self::$registeredVars as $varName => $isCalled)
450 unset(self::$registeredVars[$varName]);
451 if (method_exists(__CLASS__, 'tearDown_' . $varName))
453 call_user_func(__CLASS__ . '::tearDown_' . $varName, array());
455 elseif (isset($GLOBALS[$varName]))
457 unset($GLOBALS[$varName]);
462 // Restoring of system variables
463 foreach(self::$initVars as $scope => $vars)
465 foreach ($vars as $name => $value)
467 $GLOBALS[$scope][$name] = $value;
471 // Restoring of theme
472 SugarThemeRegistry::set(self::$systemVars['SugarThemeRegistry']->dirName);
473 SugarCache::$isCacheReset = false;
478 * Registration of $current_user in global scope
481 * @param array $params parameters for SugarTestUserUtilities::createAnonymousUser method
482 * @return bool is variable setuped or not
484 protected static function setUp_current_user(array $params)
486 self::$registeredVars['current_user'] = true;
487 $GLOBALS['current_user'] = call_user_func_array('SugarTestUserUtilities::createAnonymousUser', $params);
492 * Removal of $current_user from global scope
495 * @return bool is variable removed or not
497 protected static function tearDown_current_user()
499 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
500 unset($GLOBALS['current_user']);
505 * Registration of $beanList in global scope
508 * @return bool is variable setuped or not
510 protected static function setUp_beanList()
512 self::$registeredVars['beanList'] = true;
514 require('include/modules.php');
519 * Registration of $beanFiles in global scope
522 * @return bool is variable setuped or not
524 protected static function setUp_beanFiles()
526 self::$registeredVars['beanFiles'] = true;
528 require('include/modules.php');
533 * Registration of $moduleList in global scope
536 * @return bool is variable setuped or not
538 protected static function setUp_moduleList()
540 self::$registeredVars['moduleList'] = true;
542 require('include/modules.php');
547 * Reinitialization of $moduleList in global scope because we can't unset that variable
550 * @return bool is variable setuped or not
552 protected static function tearDown_moduleList()
554 return self::setUp_moduleList();
558 * Registration of $modListHeader in global scope
561 * @return bool is variable setuped or not
563 protected static function setUp_modListHeader()
565 self::$registeredVars['modListHeader'] = true;
566 if (isset($GLOBALS['current_user']) == false)
568 self::setUp_current_user(array(
573 $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
578 * Registration of $app_strings in global scope
581 * @return bool is variable setuped or not
583 protected static function setUp_app_strings()
585 self::$registeredVars['app_strings'] = true;
586 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
591 * Registration of $app_list_strings in global scope
594 * @return bool is variable setuped or not
596 protected static function setUp_app_list_strings()
598 self::$registeredVars['app_list_strings'] = true;
599 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
604 * Registration of $timedate in global scope
607 * @return bool is variable setuped or not
609 protected static function setUp_timedate()
611 self::$registeredVars['timedate'] = true;
612 $GLOBALS['timedate'] = TimeDate::getInstance();
617 * Removal of $timedate from global scope
620 * @return bool is variable removed or not
622 protected static function tearDown_timedate()
624 $GLOBALS['timedate']->clearCache();
629 * Registration of $mod_strings in global scope
632 * @param array $params parameters for return_module_language function
633 * @return bool is variable setuped or not
635 protected static function setUp_mod_strings(array $params)
637 self::$registeredVars['mod_strings'] = true;
638 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], $params[0]);
643 * Registration of $dictionary in global scope
646 * @return bool is variable setuped or not
648 protected static function setUp_dictionary()
650 self::setUp('beanFiles');
651 self::setUp('beanList');
652 self::$registeredVars['dictionary'] = true;
655 $dictionary = array();
656 $moduleInstaller = new ModuleInstaller();
657 $moduleInstaller->silent = true;
658 $moduleInstaller->rebuild_tabledictionary();
659 require 'modules/TableDictionary.php';
661 foreach($GLOBALS['beanList'] as $k => $v)
663 VardefManager::loadVardef($k, $v);
669 * Reinitialization of $dictionary in global scope because we can't unset that variable
672 * @return bool is variable setuped or not
674 protected static function tearDown_dictionary()
676 return self::setUp_dictionary();
680 * Cleaning caches and refreshing vardefs
683 * @param string $lhs_module left module from relation
684 * @param string $rhs_module right module from relation
685 * @return bool are caches refreshed or not
687 protected static function setUp_relation(array $params)
689 if (empty($params[0]) || empty($params[1]))
691 throw new SugarTestHelperException('setUp("relation") requires two parameters');
693 list($lhs_module, $rhs_module) = $params;
694 self::$registeredVars['relation'] = true;
695 self::$cleanModules[] = $lhs_module;
697 LanguageManager::clearLanguageCache($lhs_module);
698 if ($lhs_module != $rhs_module)
700 self::$cleanModules[] = $rhs_module;
701 LanguageManager::clearLanguageCache($rhs_module);
704 self::setUp('dictionary');
706 VardefManager::$linkFields = array();
707 VardefManager::clearVardef();
708 VardefManager::refreshVardefs($lhs_module, BeanFactory::getObjectName($lhs_module));
709 if ($lhs_module != $rhs_module)
711 VardefManager::refreshVardefs($rhs_module, BeanFactory::getObjectName($rhs_module));
713 SugarRelationshipFactory::rebuildCache();
719 * Doing the same things like setUp but for initialized list of modules
722 * @return bool are caches refreshed or not
724 protected static function tearDown_relation()
726 SugarRelationshipFactory::deleteCache();
728 $modules = array_unique(self::$cleanModules);
729 foreach ($modules as $module)
731 LanguageManager::clearLanguageCache($module);
734 self::tearDown('dictionary');
736 VardefManager::$linkFields = array();
737 VardefManager::clearVardef();
738 foreach($modules as $module)
740 VardefManager::refreshVardefs($module, BeanFactory::getBeanName($module));
742 SugarRelationshipFactory::rebuildCache();
744 self::$cleanModules = array();