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;
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
*/
*/
private $assertFileDb;
+ /**
+ * @var ConfigCache The configuration cache to check after each test
+ */
+ private $configCache;
+
public function setUp()
{
parent::setUp();
->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->mockConfigGet('config', 'php_path', 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
- *
- * @param bool $withDb if true, DB will get saved too
+ * Returns the dataset for each automatic installation test
*
- * @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)
}
/**
- * @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 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()
+ public function testWithConfig(array $data)
{
$this->mockConnect(true, 1);
$this->mockConnected(true, 1);
$this->mockExistsTable('user', false, 1);
- $this->mockUpdate([false, true, true], null, 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
return [
'database' => [
- 'hostname' => '',
- 'username' => '',
- 'password' => '',
- 'database' => '',
+ 'hostname' => '{$conf('database', 'hostname')}',
+ 'username' => '{$conf('database', 'username')}',
+ 'password' => '{$conf('database', 'password')}',
+ 'database' => '{$conf('database', 'database')}',
'charset' => 'utf8mb4',
],
// ****************************************************************
'config' => [
- 'admin_email' => '',
+ 'admin_email' => '{$conf('config', 'admin_email')}',
'sitename' => 'Friendica Social Network',
- 'register_policy' => REGISTER_OPEN,
+ '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;
$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([false, true, true], null, 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('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_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('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);
$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([false, true, true], null, 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('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_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('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([false, true, true], null, 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()
$txt = $this->dumpExecute($console);
- $this->assertEquals($txt, $theHelp);
+ $this->assertEquals($theHelp, $txt);
}
}