Просмотр исходного кода

#35 - refactoring for using in unit tests

Alex Trofimov 12 лет назад
Родитель
Сommit
f914d48b32

+ 1 - 1
inc/classes/BxDolDb.php

@@ -750,7 +750,7 @@ EOJ;
     function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true) {
 
         if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
-            return array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied');
+            return array(array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied'));
 
         $sQuery = "";
         $sDelimiter = ';';

+ 4 - 0
install/classes/BxDolInstallController.php

@@ -26,6 +26,10 @@ class BxDolInstallController
             $this->actionInitial ();
     }
    
+    function actionEmpty () 
+    {
+    }
+
     function actionAudit () 
     {
         $this->_oView->pageStart();

+ 74 - 34
install/classes/BxDolInstallSiteConfig.php

@@ -11,16 +11,31 @@ define('BX_INSTALL_ERR_GENERAL', 'general');
 
 class BxDolInstallSiteConfig
 {
-    protected $_sSqlDb = 'sql/v70.sql';
-    protected $_sSqlAddon = 'sql/addon.sql';
-
-    protected $_sPatternHeader = 'patterns/header.inc.php';
+    protected $_sSqlDb;
+    protected $_sSqlAddon;
+    protected $_sPatternHeader;
 
     protected $_aDbErrorMap;
     protected $_aConfig;
 
-    public function __construct()
+    protected $_sServerHttpHost;
+    protected $_sServerPhpSelf;
+    protected $_sServerDocumentRoot;
+
+    protected $_isAutologin;
+
+    public function __construct($sServerHttpHost = false, $sServerPhpSelf = false, $sServerDocumentRoot = false, $isAutologin = true)
     {
+        $this->_sServerHttpHost = false === $sServerHttpHost ? $_SERVER['HTTP_HOST'] : $sServerHttpHost;
+        $this->_sServerPhpSelf = false === $sServerPhpSelf ? $_SERVER['PHP_SELF'] : $sServerPhpSelf;
+        $this->_sServerDocumentRoot = false === $sServerDocumentRoot ? $_SERVER['DOCUMENT_ROOT'] : $sServerDocumentRoot;
+
+        $this->_isAutologin = $isAutologin;
+
+        $this->_sSqlDb = BX_INSTALL_DIR . 'sql/v70.sql';
+        $this->_sSqlAddon = BX_INSTALL_DIR . 'sql/addon.sql';
+        $this->_sPatternHeader = BX_INSTALL_DIR . 'patterns/header.inc.php';
+
         $this->_aDbErrorMap = array (
             'Database connect failed' => array ('fields' => array('db_host', 'db_user', 'db_password'), 'msg' => _t('_sys_inst_msg_db_err_connect')),
             'Database select failed' => array ('fields' => array('db_name'), 'msg' => _t('_sys_inst_msg_db_err_select')),
@@ -201,25 +216,45 @@ class BxDolInstallSiteConfig
         );
     }
 
-    public function getFormHtml() 
+    public function getAutoValues($aData = array()) 
     {
+        $aRet = array();
+        foreach($this->_aConfig as $sKey => $a) {
+            $sAutoMessage = '';
+            $aRet[$sKey] = $this->def ($aData, $sKey, $a, $sAutoMessage);
+        }
+        return $aRet;
+    }
+
+    public function getFormHtml($aData = false, $bRedirectOnSuccess = true, &$sOutputErrorMessage = null)
+    {
+        if (false === $aData)
+            $aData = $_POST;
+
         $aErrorFields = array();
-        if (isset($_POST['site_config'])) {
-            $aErrorFields = $this->processConfigData($this->processInputData($_POST));
+        if (isset($aData['site_config'])) {
+            $aErrorFields = $this->processConfigData($this->processInputData($aData));
             if (empty($aErrorFields)) {
-                $sHost = $_SERVER['HTTP_HOST'];
-                $sUri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
-                $sPage = 'index.php?action=finish';
-                header("Location: http://{$sHost}{$sUri}/{$sPage}");
-                exit;
+                if ($bRedirectOnSuccess) {
+                    $sHost = $this->_sServerHttpHost;
+                    $sUri = rtrim(dirname($this->_sServerPhpSelf), '/\\');
+                    $sPage = 'index.php?action=finish';
+                    header("Location: http://{$sHost}{$sUri}/{$sPage}");
+                    exit;
+                } else {
+                    return true;
+                }
             }
         }
 
         $sErrorMessage = '';
-        if (isset($aErrorFields[BX_INSTALL_ERR_GENERAL]) && $aErrorFields[BX_INSTALL_ERR_GENERAL])
+        if (isset($aErrorFields[BX_INSTALL_ERR_GENERAL]) && $aErrorFields[BX_INSTALL_ERR_GENERAL]) {
             $sErrorMessage = '<div class="bx-install-error-message bx-def-padding bx-def-margin-bottom">' . $aErrorFields[BX_INSTALL_ERR_GENERAL] . '</div>';
+            if (null !== $sOutputErrorMessage)
+                $sOutputErrorMessage = $aErrorFields[BX_INSTALL_ERR_GENERAL] . (empty($aErrorFields) ? '' : ' / Fields: ' . join(',', array_keys($aErrorFields)));
+        }
 
-        $sRows = $this->getFormFields($aErrorFields);
+        $sRows = $this->getFormFields($aErrorFields, $aData);
         $sSubmitTitle = _t('_Submit');
         return <<<EOF
             {$sErrorMessage}
@@ -346,7 +381,12 @@ EOF;
         } 
 
         bx_import('BxDolAccount');
-        bx_login(BxDolAccount::getInstance($a['admin_email'])->id());
+        $oAccount = BxDolAccount::getInstance($a['admin_email']);
+        if (!$oAccount)
+            return array(BX_INSTALL_ERR_GENERAL => _t('_sys_inst_msg_admin_account_not_found', $a['admin_email']));
+
+        if ($this->_isAutologin)
+            bx_login($oAccount->id());
 
         return array();
     }
@@ -354,7 +394,7 @@ EOF;
     protected function dbErrors2ErrorFields ($a) 
     {
         $s = '';
-        foreach ($a as $r) 
+        foreach ($a as $r)
             $s = $r['error'] . ': <br />' . $r['query'] . '<br />';
         return array(BX_INSTALL_ERR_GENERAL => $s);
     }
@@ -401,37 +441,37 @@ EOF;
         return $aMarkers;
     }
 
-    protected function getFormFields($aErrorFields) 
+    protected function getFormFields($aErrorFields, $aData) 
     {
         $s = '';
         foreach($this->_aConfig as $sKey => $a) {
             $sFunc = isset($a['func']) ? $a['func'] : 'rowInput';
-            $s .= $this->$sFunc($sKey, $a, isset($aErrorFields[$sKey]) ? $aErrorFields[$sKey] : false);
+            $s .= $this->$sFunc($aData, $sKey, $a, isset($aErrorFields[$sKey]) ? $aErrorFields[$sKey] : false);
         }
 
         return $s;
     }
-    
-    protected function rowInput ($sKey, $a, $isError = false) 
+
+    protected function rowInput ($aData, $sKey, $a, $isError = false) 
     {
         $sAutoMessage = '';
-        $sValue = bx_html_attribute($this->def ($sKey, $a, $sAutoMessage));
+        $sValue = bx_html_attribute($this->def ($aData, $sKey, $a, $sAutoMessage));
         $sInput = '<input type="text" name="' . $sKey. '" value="' . $sValue . '" class="bx-def-font-inputs bx-form-input-text" />';
-        return $this->rowWrapper ($sInput, $sAutoMessage, 'text', $sKey, $a, $isError);
+        return $this->rowWrapper ($aData, $sInput, $sAutoMessage, 'text', $sKey, $a, $isError);
     }
 
-    protected function rowSelect ($sKey, $a, $isError = false) 
+    protected function rowSelect ($aData, $sKey, $a, $isError = false) 
     {
         $sAutoMessage = '';
-        $sValue = bx_html_attribute($this->def ($sKey, $a, $sAutoMessage));
+        $sValue = bx_html_attribute($this->def ($aData, $sKey, $a, $sAutoMessage));
         $sValues = '';
         foreach ($a['vals'] as $sVal => $sTitle)
             $sValues .= '<option value="' . $sVal . '" ' . ($sVal == $sValue ? 'selected="selected"' : '') . '>' . $sTitle . '</option>';
         $sInput = '<select name="' . $sKey . '" class="bx-def-font-inputs bx-form-input-select">' . $sValues . '</select>';
-        return $this->rowWrapper ($sInput, $sAutoMessage, 'select', $sKey, $a, $isError);
+        return $this->rowWrapper ($aData, $sInput, $sAutoMessage, 'select', $sKey, $a, $isError);
     }
 
-    protected function rowWrapper ($sInput, $sAutoMessage, $sType, $sKey, $a, $isError = false)
+    protected function rowWrapper ($aData, $sInput, $sAutoMessage, $sType, $sKey, $a, $isError = false)
     {
         $sDesc = _t('_sys_inst_conf_desc', $sAutoMessage, $a['desc'], isset($a['ex']) ? $a['ex'] : _t('_sys_inst_conf_no_example'));
 
@@ -461,7 +501,7 @@ EOF;
 EOF;
     }
 
-    protected function rowSectionOpen ($sKey, $a) 
+    protected function rowSectionOpen ($aData, $sKey, $a) 
     {
         return <<<EOF
                     <div class="bx-form-section-wrapper bx-def-margin-top">
@@ -471,7 +511,7 @@ EOF;
 EOF;
     }
 
-    protected function rowSectionClose ($sKey, $a) 
+    protected function rowSectionClose ($aData, $sKey, $a) 
     {
         return <<<EOF
                             </div>
@@ -497,10 +537,10 @@ EOF;
         return mb_strlen($s) > $i && false !== strpos($s, '@') ? true : false;
     }
 
-    protected function def ($sKey, $a, &$sAutoMessage) 
+    protected function def ($aData, $sKey, $a, &$sAutoMessage) 
     {
-        if (isset($_POST[$sKey]))
-            return bx_process_pass($_POST[$sKey]);
+        if (isset($aData[$sKey]))
+            return bx_process_pass($aData[$sKey]);
         if (!empty($a['def_exp'])) {
             $s = $this->{$a['def_exp'][0]}($a['def_exp'][1]);
             if ($s) {
@@ -515,13 +555,13 @@ EOF;
 
     protected function defUrl ($foo) 
     {
-        $s = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
+        $s = "http://" . $this->_sServerHttpHost . $this->_sServerPhpSelf;
         return preg_replace("/install\/(index\.php$)/", '', $s);
     }
 
     protected function defPath ($foo) 
     {
-        $s = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . $_SERVER['PHP_SELF'];
+        $s = rtrim($this->_sServerDocumentRoot, '/') . $this->_sServerPhpSelf;
         return preg_replace("/install\/(index\.php$)/", '', $s);
     }
 

+ 15 - 13
install/index.php

@@ -17,11 +17,13 @@ define ('BX_INSTALL_DEFAULT_LANGUAGE', 'en');
 define ('BX_INSTALL_URL_ROOT', '../');
 define ('BX_INSTALL_URL_MODULES', '../modules/');
 
-define ('BX_INSTALL_DIR_ROOT', '../');
-define ('BX_INSTALL_DIR_MODULES', '../modules/');
-define ('BX_INSTALL_DIR_PLUGINS', '../plugins/');
-define ('BX_INSTALL_DIR_TEMPLATES', './templates/');
-define ('BX_INSTALL_DIR_CLASSES', './classes/');
+$aPathInfo = pathinfo(__FILE__);
+define ('BX_INSTALL_DIR', $aPathInfo['dirname'] . '/');
+define ('BX_INSTALL_DIR_ROOT', BX_INSTALL_DIR . '../');
+define ('BX_INSTALL_DIR_MODULES', BX_INSTALL_DIR_ROOT . 'modules/');
+define ('BX_INSTALL_DIR_PLUGINS', BX_INSTALL_DIR_ROOT . 'plugins/');
+define ('BX_INSTALL_DIR_TEMPLATES', BX_INSTALL_DIR . 'templates/');
+define ('BX_INSTALL_DIR_CLASSES', BX_INSTALL_DIR . 'classes/');
 
 define ('BX_INSTALL_PATH_HEADER', '../inc/header.inc.php');
 
@@ -31,14 +33,14 @@ if (!defined('BX_DOL_MODULE_TYPE_MODULE')) {
     define('BX_DOL_MODULE_TYPE_TEMPLATE', 'template');
 }
 
-require_once('../inc/utils.inc.php');
-require_once('../inc/classes/BxDol.php');
-require_once('../inc/classes/BxDolIO.php');
-require_once('../inc/classes/BxDolDb.php');
-require_once('../inc/classes/BxDolXmlParser.php');
-require_once('../template/scripts/BxBaseConfig.php');
-require_once('../studio/classes/BxDolStudioTools.php');
-require_once('../studio/classes/BxDolStudioToolsAudit.php');
+require_once(BX_INSTALL_DIR_ROOT . 'inc/utils.inc.php');
+require_once(BX_INSTALL_DIR_ROOT . 'inc/classes/BxDol.php');
+require_once(BX_INSTALL_DIR_ROOT . 'inc/classes/BxDolIO.php');
+require_once(BX_INSTALL_DIR_ROOT . 'inc/classes/BxDolDb.php');
+require_once(BX_INSTALL_DIR_ROOT . 'inc/classes/BxDolXmlParser.php');
+require_once(BX_INSTALL_DIR_ROOT . 'template/scripts/BxBaseConfig.php');
+require_once(BX_INSTALL_DIR_ROOT . 'studio/classes/BxDolStudioTools.php');
+require_once(BX_INSTALL_DIR_ROOT . 'studio/classes/BxDolStudioToolsAudit.php');
 
 require_once(BX_INSTALL_DIR_CLASSES . 'BxDolInstallController.php');
 require_once(BX_INSTALL_DIR_CLASSES . 'BxDolInstallView.php');

+ 1 - 0
modules/boonex/english/data/langs/system.xml

@@ -3822,6 +3822,7 @@ If you don't know how to do it by yourself you can submit <a target="_blank" hre
     <string name="_sys_inst_msg_finish_delete_install_folder"><![CDATA[Remove <b>/install/</b> folder from Dolphin root folder.]]></string>
     <string name="_sys_inst_msg_finish_goto_studio"><![CDATA[<a href="{0}studio/">Go to Studio</a> to add additional modules and customize the site.]]></string>
     <string name="_sys_inst_msg_remove_install"><![CDATA[Please, remove INSTALL directory from your server and reload this page to activate your site.<br />After deletion you can safely go <a href="../studio/">to the site Studio</a> to add additional modules and customize the site.]]></string>
+    <string name="_sys_inst_msg_admin_account_not_found"><![CDATA[Admin account wasn't found by email - {0}]]></string>
 
 	<string name="_sys_inst_finish"><![CDATA[Installation has been successfully completed]]></string>
 	<string name="_sys_inst_site_config"><![CDATA[Site configuration]]></string>

+ 1 - 1
studio/classes/BxDolStudioInstaller.php

@@ -834,7 +834,7 @@ class BxDolStudioInstaller extends BxDolInstallerUtils {
 
         if($bUseFtp) {
         	bx_import('BxDolFtp');
-			$oFile = new BxDolFtp($_SERVER['HTTP_HOST'], $this->oDb->getParam('sys_ftp_login'), $this->oDb->getParam('sys_ftp_password'), $this->oDb->getParam('sys_ftp_dir'));
+			$oFile = new BxDolFtp(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', $this->oDb->getParam('sys_ftp_login'), $this->oDb->getParam('sys_ftp_password'), $this->oDb->getParam('sys_ftp_dir'));
 
 			if(!$oFile->connect())
 				return null;