Merge pull request #6877 from nupplaphil/task/refactor_installer

Refactoring/Optimizing Installer/Install usage
This commit is contained in:
Hypolite Petovan 2019-03-14 20:42:09 -04:00 committed by GitHub
commit 5254b361c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 468 additions and 262 deletions

View File

@ -74,6 +74,8 @@ HELP;
$installer = new Installer();
$configCache = $a->getConfigCache();
$this->out(" Complete!\n\n");
// Check Environment
@ -81,7 +83,7 @@ HELP;
$installer->resetChecks();
if (!$this->runBasicChecks($installer)) {
if (!$this->runBasicChecks($installer, $configCache)) {
$errorMessage = $this->extractErrors($installer->getChecks());
throw new RuntimeException($errorMessage);
}
@ -100,41 +102,51 @@ HELP;
}
}
$db_host = $a->getConfigCache()->get('database', 'hostname');
$db_user = $a->getConfigCache()->get('database', 'username');
$db_pass = $a->getConfigCache()->get('database', 'password');
$db_data = $a->getConfigCache()->get('database', 'database');
//reload the config cache
$loader = new Config\Cache\ConfigCacheLoader($a->getBasePath(), $a->getMode());
$loader->loadConfigFiles($configCache);
} else {
// Creating config file
$this->out("Creating config file...\n");
$save_db = $this->getOption(['s', 'savedb'], false);
$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
//$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST') ? getenv('MYSQL_HOST') : Installer::DEFAULT_HOST) : '');
$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
$db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
$db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
$db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
$db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
$url_path = $this->getOption(['u', 'urlpath'], !empty('FRIENDICA_URL_PATH') ? getenv('FRIENDICA_URL_PATH') : null);
$configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
$configCache->set('database', 'database',
$this->getOption(['d', 'dbdata'],
($save_db) ? getenv('MYSQL_DATABASE') : ''));
$configCache->set('database', 'username',
$this->getOption(['U', 'dbuser'],
($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
$configCache->set('database', 'password',
$this->getOption(['P', 'dbpass'],
($save_db) ? getenv('MYSQL_PASSWORD') : ''));
$php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
$admin_mail = $this->getOption(['A', 'admin'], !empty('FRIENDICA_ADMIN_MAIL') ? getenv('FRIENDICA_ADMIN_MAIL') : '');
$tz = $this->getOption(['T', 'tz'], !empty('FRIENDICA_TZ') ? getenv('FRIENDICA_TZ') : '');
$lang = $this->getOption(['L', 'lang'], !empty('FRIENDICA_LANG') ? getenv('FRIENDICA_LANG') : '');
if (!empty($php_path)) {
$configCache->set('config', 'php_path', $php_path);
}
$configCache->set('config', 'admin_email',
$this->getOption(['A', 'admin'],
!empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
$configCache->set('system', 'default_timezone',
$this->getOption(['T', 'tz'],
!empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
$configCache->set('system', 'language',
$this->getOption(['L', 'lang'],
!empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
if (empty($php_path)) {
$php_path = $installer->getPHPPath();
$configCache->set('config', 'php_path', $installer->getPHPPath());
}
$installer->createConfig(
$php_path,
$url_path,
(!empty($db_port) ? $db_host . ':' . $db_port : $db_host),
$db_user,
$db_pass,
$db_data,
$tz,
$lang,
$admin_mail,
$a,
$configCache,
$a->getBasePath()
);
}
@ -146,7 +158,7 @@ HELP;
$installer->resetChecks();
if (!$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $db_host, $db_user, $db_pass, $db_data)) {
if (!$installer->checkDB($configCache, $a->getProfiler())) {
$errorMessage = $this->extractErrors($installer->getChecks());
throw new RuntimeException($errorMessage);
}
@ -180,12 +192,13 @@ HELP;
}
/**
* @param Installer $installer the Installer instance
* @param Installer $installer The Installer instance
* @param Config\Cache\IConfigCache $configCache The config cache
*
* @return bool true if checks were successfully, otherwise false
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private function runBasicChecks(Installer $installer)
private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
{
$checked = true;
@ -207,8 +220,8 @@ HELP;
}
$php_path = null;
if (!empty(Config::get('config', 'php_path'))) {
$php_path = Config::get('config', 'php_path');
if ($configCache->has('config', 'php_path')) {
$php_path = $configCache->get('config', 'php_path');
}
if (!$installer->checkPHP($php_path, true)) {

View File

@ -6,6 +6,7 @@ namespace Friendica\Core;
use DOMDocument;
use Exception;
use Friendica\App;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
@ -129,33 +130,28 @@ class Installer
* - Creates `config/local.config.php`
* - Installs Database Structure
*
* @param string $phppath Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
* @param string $urlpath Path based on the URL of Friendica (e.g. '/friendica')
* @param string $dbhost Hostname/IP of the Friendica Database
* @param string $dbuser Username of the Database connection credentials
* @param string $dbpass Password of the Database connection credentials
* @param string $dbdata Name of the Database
* @param string $timezone Timezone of the Friendica Installaton (e.g. 'Europe/Berlin')
* @param string $language 2-letter ISO 639-1 code (eg. 'en')
* @param string $adminmail Mail-Adress of the administrator
* @param App $app The Friendica App
* @param IConfigCache $configCache The config cache with all config relevant information
* @param string $basepath The basepath of Friendica
*
* @return bool true if the config was created, otherwise false
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
public function createConfig(App $app, IConfigCache $configCache, $basepath)
{
$tpl = Renderer::getMarkupTemplate('local.config.tpl');
$txt = Renderer::replaceMacros($tpl, [
'$phpath' => $phppath,
'$dbhost' => $dbhost,
'$dbuser' => $dbuser,
'$dbpass' => $dbpass,
'$dbdata' => $dbdata,
'$timezone' => $timezone,
'$language' => $language,
'$urlpath' => $urlpath,
'$adminmail' => $adminmail,
'$dbhost' => $configCache->get('database', 'hostname'),
'$dbuser' => $configCache->get('database', 'username'),
'$dbpass' => $configCache->get('database', 'password'),
'$dbdata' => $configCache->get('database', 'database'),
'$phpath' => $this->getPHPPath(),
'$adminmail' => $configCache->get('config', 'admin_email'),
'$timezone' => $configCache->get('system', 'default_timezone'),
'$language' => $configCache->get('system', 'language'),
'$urlpath' => $app->getURLPath(),
]);
$result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
@ -593,16 +589,17 @@ class Installer
*
* @param IConfigCache $configCache The configuration cache
* @param Profiler $profiler The profiler of this app
* @param string $dbhost Hostname/IP of the Friendica Database
* @param string $dbuser Username of the Database connection credentials
* @param string $dbpass Password of the Database connection credentials
* @param string $dbdata Name of the Database
*
* @return bool true if the check was successful, otherwise false
* @throws Exception
*/
public function checkDB(IConfigCache $configCache, Profiler $profiler, $dbhost, $dbuser, $dbpass, $dbdata)
public function checkDB(IConfigCache $configCache, Profiler $profiler)
{
$dbhost = $configCache->get('database', 'hostname');
$dbuser = $configCache->get('database', 'username');
$dbpass = $configCache->get('database', 'password');
$dbdata = $configCache->get('database', 'database');
if (!DBA::connect($configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) {
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');

View File

@ -5,6 +5,7 @@ namespace Friendica\Module;
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Util\Strings;
@ -61,45 +62,48 @@ class Install extends BaseModule
public static function post()
{
$a = self::getApp();
$configCache = $a->getConfigCache();
switch (self::$currentWizardStep) {
case self::SYSTEM_CHECK:
case self::DATABASE_CONFIG:
// Nothing to do in these steps
self::checkSetting($configCache, $_POST, 'config', 'php_path');
break;
case self::SITE_SETTINGS:
$dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
$dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '')));
$dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '')));
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
self::checkSetting($configCache, $_POST, 'config', 'php_path');
self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
self::checkSetting($configCache, $_POST, 'database', 'username', '');
self::checkSetting($configCache, $_POST, 'database', 'password', '');
self::checkSetting($configCache, $_POST, 'database', 'database', '');
// If we cannot connect to the database, return to the previous step
if (!self::$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
self::$currentWizardStep = self::DATABASE_CONFIG;
}
break;
case self::FINISHED:
$urlpath = $a->getURLPath();
$dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
$dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '')));
$dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '')));
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
$timezone = Strings::escapeTags(trim(defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ)));
$language = Strings::escapeTags(trim(defaults($_POST, 'language', Core\Installer::DEFAULT_LANG)));
$adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
self::checkSetting($configCache, $_POST, 'config', 'php_path');
self::checkSetting($configCache, $_POST, 'database', 'hostname', Core\Installer::DEFAULT_HOST);
self::checkSetting($configCache, $_POST, 'database', 'username', '');
self::checkSetting($configCache, $_POST, 'database', 'password', '');
self::checkSetting($configCache, $_POST, 'database', 'database', '');
self::checkSetting($configCache, $_POST, 'system', 'default_timezone', Core\Installer::DEFAULT_TZ);
self::checkSetting($configCache, $_POST, 'system', 'language', Core\Installer::DEFAULT_LANG);
self::checkSetting($configCache, $_POST, 'config', 'admin_email', '');
// If we cannot connect to the database, return to the Database config wizard
if (!self::$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
if (!self::$installer->checkDB($configCache, $a->getProfiler())) {
self::$currentWizardStep = self::DATABASE_CONFIG;
return;
}
$phpath = self::$installer->getPHPPath();
if (!self::$installer->createConfig($phpath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $a->getBasePath())) {
if (!self::$installer->createConfig($a, $configCache, $a->getBasePath())) {
return;
}
@ -112,6 +116,7 @@ class Install extends BaseModule
public static function content()
{
$a = self::getApp();
$configCache = $a->getConfigCache();
$output = '';
@ -119,85 +124,62 @@ class Install extends BaseModule
switch (self::$currentWizardStep) {
case self::SYSTEM_CHECK:
$phppath = defaults($_POST, 'phpath', null);
$php_path = $configCache->get('config', 'php_path');
$status = self::$installer->checkEnvironment($a->getBaseURL(), $phppath);
$status = self::$installer->checkEnvironment($a->getBaseURL(), $php_path);
$tpl = Renderer::getMarkupTemplate('install_checks.tpl');
$output .= Renderer::replaceMacros($tpl, [
'$title' => $install_title,
'$pass' => L10n::t('System check'),
'$checks' => self::$installer->getChecks(),
'$passed' => $status,
'$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
'$next' => L10n::t('Next'),
'$reload' => L10n::t('Check again'),
'$phpath' => $phppath,
'$baseurl' => $a->getBaseURL()
'$title' => $install_title,
'$pass' => L10n::t('System check'),
'$checks' => self::$installer->getChecks(),
'$passed' => $status,
'$see_install' => L10n::t('Please see the file "INSTALL.txt".'),
'$next' => L10n::t('Next'),
'$reload' => L10n::t('Check again'),
'$php_path' => $php_path,
'$baseurl' => $a->getBaseURL()
]);
break;
case self::DATABASE_CONFIG:
$dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost' , Core\Installer::DEFAULT_HOST)));
$dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser' , '' )));
$dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass' , '' )));
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata' , '' )));
$phpath = Strings::escapeTags(trim(defaults($_POST, 'phpath' , '' )));
$adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '' )));
$tpl = Renderer::getMarkupTemplate('install_db.tpl');
$output .= Renderer::replaceMacros($tpl, [
'$title' => $install_title,
'$pass' => L10n::t('Database connection'),
'$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
'$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
'$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
'checks' => self::$installer->getChecks(),
'$dbhost' => ['dbhost',
'$title' => $install_title,
'$pass' => L10n::t('Database connection'),
'$info_01' => L10n::t('In order to install Friendica we need to know how to connect to your database.'),
'$info_02' => L10n::t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
'$info_03' => L10n::t('The database you specify below should already exist. If it does not, please create it before continuing.'),
'checks' => self::$installer->getChecks(),
'$dbhost' => ['database-hostname',
L10n::t('Database Server Name'),
$dbhost,
$configCache->get('database', 'hostname'),
'',
'required'],
'$dbuser' => ['dbuser',
'$dbuser' => ['database-username',
L10n::t('Database Login Name'),
$dbuser,
$configCache->get('database', 'username'),
'',
'required',
'autofocus'],
'$dbpass' => ['dbpass',
'$dbpass' => ['database-password',
L10n::t('Database Login Password'),
$dbpass,
$configCache->get('database', 'password'),
L10n::t("For security reasons the password must not be empty"),
'required'],
'$dbdata' => ['dbdata',
'$dbdata' => ['database-database',
L10n::t('Database Name'),
$dbdata,
$configCache->get('database', 'database'),
'',
'required'],
'$adminmail' => ['adminmail',
L10n::t('Site administrator email address'),
$adminmail,
L10n::t('Your account email address must match this in order to use the web admin panel.'),
'required',
'autofocus',
'email'],
'$lbl_10' => L10n::t('Please select a default timezone for your website'),
'$baseurl' => $a->getBaseURL(),
'$phpath' => $phpath,
'$submit' => L10n::t('Submit')
'$lbl_10' => L10n::t('Please select a default timezone for your website'),
'$baseurl' => $a->getBaseURL(),
'$php_path' => $configCache->get('config', 'php_path'),
'$submit' => L10n::t('Submit')
]);
break;
case self::SITE_SETTINGS:
$dbhost = Strings::escapeTags(trim(defaults($_POST, 'dbhost', Core\Installer::DEFAULT_HOST)));
$dbuser = Strings::escapeTags(trim(defaults($_POST, 'dbuser', '' )));
$dbpass = Strings::escapeTags(trim(defaults($_POST, 'dbpass', '' )));
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '' )));
$phpath = Strings::escapeTags(trim(defaults($_POST, 'phpath', '' )));
$adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
$timezone = defaults($_POST, 'timezone', Core\Installer::DEFAULT_TZ);
/* Installed langs */
$lang_choices = L10n::getAvailableLanguages();
@ -206,16 +188,23 @@ class Install extends BaseModule
'$title' => $install_title,
'$checks' => self::$installer->getChecks(),
'$pass' => L10n::t('Site settings'),
'$dbhost' => $dbhost,
'$dbuser' => $dbuser,
'$dbpass' => $dbpass,
'$dbdata' => $dbdata,
'$phpath' => $phpath,
'$adminmail' => ['adminmail', L10n::t('Site administrator email address'), $adminmail, L10n::t('Your account email address must match this in order to use the web admin panel.'), 'required', 'autofocus', 'email'],
'$timezone' => Temporal::getTimezoneField('timezone', L10n::t('Please select a default timezone for your website'), $timezone, ''),
'$language' => ['language',
'$dbhost' => $configCache->get('database', 'hostname'),
'$dbuser' => $configCache->get('database', 'username'),
'$dbpass' => $configCache->get('database', 'password'),
'$dbdata' => $configCache->get('database', 'database'),
'$phpath' => $configCache->get('config', 'php_path'),
'$adminmail' => ['config-admin_email',
L10n::t('Site administrator email address'),
$configCache->get('config', 'admin_email'),
L10n::t('Your account email address must match this in order to use the web admin panel.'),
'required', 'autofocus', 'email'],
'$timezone' => Temporal::getTimezoneField('system-default_timezone',
L10n::t('Please select a default timezone for your website'),
$configCache->get('system', 'default_timezone'),
''),
'$language' => ['system-language',
L10n::t('System Language:'),
Core\Installer::DEFAULT_LANG,
$configCache->get('system', 'language'),
L10n::t('Set the default language for your Friendica installation interface and to send emails.'),
$lang_choices],
'$baseurl' => $a->getBaseURL(),
@ -265,4 +254,24 @@ class Install extends BaseModule
. L10n::t('Go to your new Friendica node <a href="%s/register">registration page</a> and register as new user. Remember to use the same email you have entered as administrator email. This will allow you to enter the site admin panel.', $baseurl)
. "</p>";
}
/**
* Checks the $_POST settings and updates the config Cache for it
*
* @param IConfigCache $configCache The current config cache
* @param array $post The $_POST data
* @param string $cat The category of the setting
* @param string $key The key of the setting
* @param null|string $default The default value
*/
private static function checkSetting(IConfigCache $configCache, array $post, $cat, $key, $default = null)
{
$configCache->set($cat, $key,
Strings::escapeTags(
trim(defaults($post, sprintf('%s-%s', $cat, $key),
(!isset($default) ? $configCache->get($cat, $key) : $default))
)
)
);
}
}

View File

@ -30,19 +30,27 @@ trait AppMockTrait
*/
protected $profilerMock;
/**
* @var MockInterface|App\Mode The mocked App mode
*/
protected $mode;
/**
* Mock the App
*
* @param vfsStreamDirectory $root The root directory
* @param Config\Cache\ConfigCache $configCache
* @param bool $raw If true, no config mocking will be done
*/
public function mockApp($root)
public function mockApp(vfsStreamDirectory $root, $configCache = null, $raw = false)
{
$this->configMock = \Mockery::mock(Config\Cache\IConfigCache::class);
$this->mode = \Mockery::mock(App\Mode::class);
$configAdapterMock = \Mockery::mock(Config\Adapter\IConfigAdapter::class);
// Disable the adapter
$configAdapterMock->shouldReceive('isConnected')->andReturn(false);
$config = new Config\Configuration($this->configMock, $configAdapterMock);
$config = new Config\Configuration((isset($configCache) ? $configCache : $this->configMock), $configAdapterMock);
// Initialize empty Config
Config::init($config);
@ -52,6 +60,37 @@ trait AppMockTrait
->shouldReceive('getBasePath')
->andReturn($root->url());
$this->app
->shouldReceive('getMode')
->andReturn($this->mode);
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
$this->app
->shouldReceive('getConfigCache')
->andReturn((isset($configCache) ? $configCache : $this->configMock));
$this->app
->shouldReceive('getTemplateEngine')
->andReturn(new FriendicaSmartyEngine());
$this->app
->shouldReceive('getCurrentTheme')
->andReturn('Smarty3');
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
$this->app
->shouldReceive('getBaseUrl')
->andReturnUsing(function () {
return $this->app->getConfigCache()->get('system', 'url');
});
BaseObject::setApp($this->app);
if ($raw) {
return;
}
$this->configMock
->shouldReceive('has')
->andReturn(true);
@ -79,26 +118,5 @@ trait AppMockTrait
->shouldReceive('get')
->with('system', 'theme')
->andReturn('system_theme');
$this->profilerMock = \Mockery::mock(Profiler::class);
$this->profilerMock->shouldReceive('saveTimestamp');
$this->app
->shouldReceive('getConfigCache')
->andReturn($this->configMock);
$this->app
->shouldReceive('getTemplateEngine')
->andReturn(new FriendicaSmartyEngine());
$this->app
->shouldReceive('getCurrentTheme')
->andReturn('Smarty3');
$this->app
->shouldReceive('getBaseUrl')
->andReturn('http://friendica.local');
$this->app
->shouldReceive('getProfiler')
->andReturn($this->profilerMock);
BaseObject::setApp($this->app);
}
}

View File

@ -35,16 +35,20 @@ trait RendererMockTrait
* Mocking the method 'Renderer::replaceMacros()'
*
* @param string $template The template to use (normally, it is the mock result of 'mockGetMarkupTemplate()'
* @param array $args The arguments to pass to the macro
* @param array|\Closure|null $args The arguments to pass to the macro
* @param string $return the return value of the mock
* @param null|int $times How often the method will get used
*/
public function mockReplaceMacros($template, $args = [], $return = '', $times = null)
public function mockReplaceMacros($template, $args = null, $return = '', $times = null)
{
if (!isset($this->rendererMock)) {
$this->rendererMock = \Mockery::mock('alias:Friendica\Core\Renderer');
}
if (!isset($args)) {
$args = [];
}
$this->rendererMock
->shouldReceive('replaceMacros')
->with($template, $args)

View File

@ -25,7 +25,7 @@ trait VFSTrait
];
// create a virtual directory and copy all needed files and folders to it
$this->root = vfsStream::setup('friendica', null, $structure);
$this->root = vfsStream::setup('friendica', 0777, $structure);
$this->setConfigFile('defaults.config.php');
$this->setConfigFile('settings.config.php');

View File

@ -2,11 +2,15 @@
namespace Friendica\Test\src\Core\Console;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Console\AutomaticInstallation;
use Friendica\Core\Installer;
use Friendica\Core\Logger;
use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\DBStructureMockTrait;
use Friendica\Test\Util\L10nMockTrait;
use Friendica\Test\Util\RendererMockTrait;
use Friendica\Util\Logger\VoidLogger;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;
@ -22,12 +26,6 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
use DBStructureMockTrait;
use RendererMockTrait;
private $db_host;
private $db_port;
private $db_data;
private $db_user;
private $db_pass;
/**
* @var vfsStreamFile Assert file without DB credentials
*/
@ -37,6 +35,11 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
*/
private $assertFileDb;
/**
* @var ConfigCache The configuration cache to check after each test
*/
private $configCache;
public function setUp()
{
parent::setUp();
@ -46,42 +49,103 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
->removeChild('local.config.php');
}
$this->db_host = getenv('MYSQL_HOST');
$this->db_port = !empty(getenv('MYSQL_PORT')) ? getenv('MYSQL_PORT') : null;
$this->db_data = getenv('MYSQL_DATABASE');
$this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
$this->db_pass = getenv('MYSQL_PASSWORD');
$this->configMock
->shouldReceive('get')
->with('config', 'php_path')
->andReturn(false);
$this->mockL10nT();
$this->configCache = new ConfigCache();
$this->configCache->set('system', 'basepath', $this->root->url());
$this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
$this->mockApp($this->root, null, true);
$this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
if ($key !== 'basepath') {
return $this->configCache->set($cat, $key, $value);
} else {
return true;
}
});
$this->configMock->shouldReceive('has')->andReturn(true);
$this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
return $this->configCache->get($cat, $key);
});
$this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
return $this->configCache->load($config, $overwrite);
});
$this->mode->shouldReceive('isInstall')->andReturn(true);
Logger::init(new VoidLogger());
}
/**
* Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
* Returns the dataset for each automatic installation test
*
* @param bool $withDb if true, DB will get saved too
*
* @return array The arguments to pass to the mock for 'replaceMacros()'
* @return array the dataset
*/
private function createArgumentsForMacro($withDb)
public function dataInstaller()
{
$args = [
'$phpath' => trim(shell_exec('which php')),
'$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''),
'$dbuser' => (($withDb) ? $this->db_user : ''),
'$dbpass' => (($withDb) ? $this->db_pass : ''),
'$dbdata' => (($withDb) ? $this->db_data : ''),
'$timezone' => 'Europe/Berlin',
'$language' => 'de',
'$urlpath' => '/friendica',
'$adminmail' => 'admin@friendica.local'
return [
'empty' => [
'data' => [
'database' => [
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'port' => '',
],
'config' => [
'php_path' => '',
'admin_email' => '',
],
'system' => [
'urlpath' => '',
'default_timezone' => '',
'language' => '',
],
],
],
'normal' => [
'data' => [
'database' => [
'hostname' => 'testhost',
'port' => 3306,
'username' => 'friendica',
'password' => 'a password',
'database' => 'database',
],
'config' => [
'php_path' => '',
'admin_email' => 'admin@philipp.info',
],
'system' => [
'urlpath' => 'test/it',
'default_timezone' => 'en',
'language' => 'Europe/Berlin',
],
],
],
'special' => [
'data' => [
'database' => [
'hostname' => 'testhost.new.domain',
'port' => 3341,
'username' => 'fr"§%ica',
'password' => '$%\"gse',
'database' => 'db',
],
'config' => [
'php_path' => '',
'admin_email' => 'admin@philipp.info',
],
'system' => [
'urlpath' => 'test/it',
'default_timezone' => 'en',
'language' => 'Europe/Berlin',
],
],
],
];
return $args;
}
private function assertFinished($txt, $withconfig = false, $copyfile = false)
@ -177,15 +241,95 @@ FIN;
}
/**
* @medium
* Asserts one config entry
*
* @param string $cat The category to test
* @param string $key The key to test
* @param null|array $assertion The asserted value (null = empty, or array/string)
* @param string $default_value The default value
*/
public function testWithConfig()
public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
{
if (!empty($assertion[$cat][$key])) {
$this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
} elseif (!empty($assertion) && !is_array($assertion)) {
$this->assertEquals($assertion, $this->configCache->get($cat, $key));
} elseif (!empty($default_value)) {
$this->assertEquals($default_value, $this->configCache->get($cat, $key));
} else {
$this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
}
}
/**
* Asserts all config entries
*
* @param null|array $assertion The optional assertion array
* @param boolean $saveDb True, if the db credentials should get saved to the file
* @param boolean $default True, if we use the default values
* @param boolean $defaultDb True, if we use the default value for the DB
*/
public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true)
{
if (!empty($assertion['database']['hostname'])) {
$assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
}
$this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
$this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
$this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
$this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
$this->assertConfigEntry('config', 'admin_email', $assertion);
$this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
$this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
$this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
}
/**
* Test the automatic installation without any parameter/setting
*/
public function testEmpty()
{
$this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true, false);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertConfig();
}
/**
* Test the automatic installation with a prepared config file
* @dataProvider dataInstaller
*/
public function testWithConfig(array $data)
{
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$conf = function ($cat, $key) use ($data) {
if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
return $data[$cat][$key] . ':' . $data['database']['port'];
}
return $data[$cat][$key];
};
$config = <<<CONF
<?php
@ -196,10 +340,10 @@ FIN;
return [
'database' => [
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'hostname' => '{$conf('database', 'hostname')}',
'username' => '{$conf('database', 'username')}',
'password' => '{$conf('database', 'password')}',
'database' => '{$conf('database', 'database')}',
'charset' => 'utf8mb4',
],
@ -210,14 +354,15 @@ return [
// ****************************************************************
'config' => [
'admin_email' => '',
'admin_email' => '{$conf('config', 'admin_email')}',
'sitename' => 'Friendica Social Network',
'register_policy' => \Friendica\Module\Register::OPEN,
'register_text' => '',
],
'system' => [
'default_timezone' => 'UTC',
'language' => 'en',
'urlpath' => '{$conf('system', 'urlpath')}',
'default_timezone' => '{$conf('system', 'default_timezone')}',
'language' => '{$conf('system', 'language')}',
],
];
CONF;
@ -234,25 +379,39 @@ CONF;
$this->assertFinished($txt, false, true);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
$this->assertConfig($data, true, false, false);
}
/**
* @medium
* Test the automatic installation with environment variables
* Includes saving the DB credentials to the file
* @dataProvider dataInstaller
*/
public function testWithEnvironmentAndSave()
public function testWithEnvironmentAndSave(array $data)
{
$this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
$this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
$this->assertTrue(putenv('FRIENDICA_LANG=de'));
$this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
$this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
$this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
$this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
$this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
$this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
$this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
$this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
$this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
$this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('savedb', true);
@ -260,90 +419,105 @@ CONF;
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true);
$this->assertConfig($data, true, true, false);
}
/**
* @medium
* Test the automatic installation with environment variables
* Don't save the db credentials to the file
* @dataProvider dataInstaller
*/
public function testWithEnvironmentWithoutSave()
public function testWithEnvironmentWithoutSave(array $data)
{
$this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
$this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
$this->assertTrue(putenv('FRIENDICA_LANG=de'));
$this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
$this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
$this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
$this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
$this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
$this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
$this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
$this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
$this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
$this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
$console = new AutomaticInstallation($this->consoleArgv);
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true);
$this->assertConfig($data, false, true);
}
/**
* @medium
* Test the automatic installation with arguments
* @dataProvider dataInstaller
*/
public function testWithArguments()
public function testWithArguments(array $data)
{
$this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
$this->mockUpdate([$this->root->url(), false, true, true], null, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('dbhost', $this->db_host);
$console->setOption('dbuser', $this->db_user);
if (!empty($this->db_pass)) {
$console->setOption('dbpass', $this->db_pass);
}
if (!empty($this->db_port)) {
$console->setOption('dbport', $this->db_port);
}
$console->setOption('dbdata', $this->db_data);
$console->setOption('admin', 'admin@friendica.local');
$console->setOption('tz', 'Europe/Berlin');
$console->setOption('lang', 'de');
$console->setOption('urlpath', '/friendica');
$option = function($var, $cat, $key) use ($data, $console) {
if (!empty($data[$cat][$key])) {
$console->setOption($var, $data[$cat][$key]);
}
};
$option('dbhost' , 'database', 'hostname');
$option('dbport' , 'database', 'port');
$option('dbuser' , 'database', 'username');
$option('dbpass' , 'database', 'password');
$option('dbdata' , 'database', 'database');
$option('urlpath' , 'system' , 'urlpath');
$option('phppath' , 'config' , 'php_path');
$option('admin' , 'config' , 'admin_email');
$option('tz' , 'system' , 'default_timezone');
$option('lang' , 'system' , 'language');
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true);
$this->assertConfig($data, true, true, true);
}
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* Test the automatic installation with a wrong database connection
*/
public function testNoDatabaseConnection()
{
$this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
$this->mockConnect(false, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
$this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
$this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
$this->assertTrue(putenv('FRIENDICA_LANG=de'));
$this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
$this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$txt = $this->dumpExecute($console);
$this->assertStuckDB($txt);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertConfig(null, false, true, false);
}
public function testGetHelp()
@ -406,6 +580,6 @@ HELP;
$txt = $this->dumpExecute($console);
$this->assertEquals($txt, $theHelp);
$this->assertEquals($theHelp, $txt);
}
}

View File

@ -2,6 +2,7 @@
namespace Friendica\Test\src\Core\Console;
use Friendica\App;
use Friendica\Core\Console\Config;
/**
@ -15,20 +16,15 @@ class ConfigConsoleTest extends ConsoleTest
{
parent::setUp();
$this->mockApp($this->root);
\Mockery::getConfiguration()->setConstantsMap([
'Friendica\App\Mode' => [
App\Mode::class => [
'DBCONFIGAVAILABLE' => 0
]
]);
$mode = \Mockery::mock('Friendica\App\Mode');
$mode
->shouldReceive('has')
->andReturn(true);
$this->app
->shouldReceive('getMode')
->andReturn($mode);
$this->mode->shouldReceive('has')->andReturn(true);
}
function testSetGetKeyValue() {

View File

@ -22,14 +22,9 @@ abstract class ConsoleTest extends MockedTest
{
parent::setUp();
if (!getenv('MYSQL_DATABASE')) {
$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
}
Intercept::setUp();
$this->setUpVfsDir();
$this->mockApp($this->root);
}
/**

View File

@ -28,7 +28,7 @@
</table>
{{if $phpath}}
<input type="hidden" name="phpath" value="{{$phpath}}">
<input type="hidden" name="config-php_path" value="{{$php_path}}">
{{/if}}
{{if $passed}}

View File

@ -18,7 +18,7 @@
<form id="install-form" action="{{$baseurl}}/install" method="post">
<input type="hidden" name="phpath" value="{{$phpath}}" />
<input type="hidden" name="config-php_path" value="{{$php_path}}" />
<input type="hidden" name="pass" value="3" />
{{include file="field_input.tpl" field=$dbhost}}

View File

@ -6,11 +6,11 @@
<form id="install-form" action="{{$baseurl}}/install" method="post">
<input type="hidden" name="phpath" value="{{$phpath}}" />
<input type="hidden" name="dbhost" value="{{$dbhost}}" />
<input type="hidden" name="dbuser" value="{{$dbuser}}" />
<input type="hidden" name="dbpass" value="{{$dbpass}}" />
<input type="hidden" name="dbdata" value="{{$dbdata}}" />
<input type="hidden" name="config-php_path" value="{{$php_path}}" />
<input type="hidden" name="database-hostname" value="{{$dbhost}}" />
<input type="hidden" name="database-username" value="{{$dbuser}}" />
<input type="hidden" name="database-password" value="{{$dbpass}}" />
<input type="hidden" name="database-database" value="{{$dbdata}}" />
<input type="hidden" name="pass" value="4" />
{{include file="field_input.tpl" field=$adminmail}}