Move Console namespace one level up

This commit is contained in:
Philipp Holzer 2019-05-02 23:17:35 +02:00
parent 2628da422a
commit d716a3326f
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
23 changed files with 46 additions and 44 deletions

View file

@ -0,0 +1,646 @@
<?php
namespace Friendica\Test\src\Console;
use Friendica\Console\AutomaticInstallation;
use Friendica\Core\Config\Cache\ConfigCache;
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\BaseURL;
use Friendica\Util\Logger\VoidLogger;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @requires PHP 7.0
*/
class AutomaticInstallationConsoleTest extends ConsoleTest
{
use L10nMockTrait;
use DBAMockTrait;
use DBStructureMockTrait;
use RendererMockTrait;
/**
* @var vfsStreamFile Assert file without DB credentials
*/
private $assertFile;
/**
* @var vfsStreamFile Assert file with DB credentials
*/
private $assertFileDb;
/**
* @var ConfigCache The configuration cache to check after each test
*/
private $configCache;
public function setUp()
{
parent::setUp();
if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
$this->root->getChild('config')
->removeChild('local.config.php');
}
$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->configCache->set('system', 'theme', 'smarty3');
$this->mockApp($this->root, 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());
}
/**
* Returns the dataset for each automatic installation test
*
* @return array the dataset
*/
public function dataInstaller()
{
return [
'empty' => [
'data' => [
'database' => [
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'port' => '',
],
'config' => [
'php_path' => '',
'hostname' => 'friendica.local',
'admin_email' => '',
],
'system' => [
'basepath' => '',
'urlpath' => '',
'url' => 'http://friendica.local',
'ssl_policy' => 0,
'default_timezone' => '',
'language' => '',
],
],
],
'normal' => [
'data' => [
'database' => [
'hostname' => 'testhost',
'port' => 3306,
'username' => 'friendica',
'password' => 'a password',
'database' => 'database',
],
'config' => [
'php_path' => '',
'hostname' => 'friendica.local',
'admin_email' => 'admin@philipp.info',
],
'system' => [
'urlpath' => 'test/it',
'url' => 'http://friendica.local/test/it',
'basepath' => '',
'ssl_policy' => '2',
'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' => '',
'hostname' => 'friendica.local',
'admin_email' => 'admin@philipp.info',
],
'system' => [
'urlpath' => 'test/it',
'url' => 'https://friendica.local/test/it',
'basepath' => '',
'ssl_policy' => '1',
'default_timezone' => 'en',
'language' => 'Europe/Berlin',
],
],
],
];
}
private function assertFinished($txt, $withconfig = false, $copyfile = false)
{
$cfg = '';
if ($withconfig) {
$cfg = <<<CFG
Creating config file...
Complete!
CFG;
}
if ($copyfile) {
$cfg = <<<CFG
Copying config file...
Complete!
CFG;
}
$finished = <<<FIN
Initializing setup...
Complete!
Checking environment...
NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
Complete!
{$cfg}
Checking database...
Complete!
Inserting data into database...
Complete!
Installing theme
Complete
Installation is finished
FIN;
$this->assertEquals($finished, $txt);
}
private function assertStuckDB($txt)
{
$finished = <<<FIN
Initializing setup...
Complete!
Checking environment...
NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
Complete!
Creating config file...
Complete!
Checking database...
[Error] --------
Could not connect to database.:
FIN;
$this->assertEquals($finished, $txt);
}
private function assertStuckURL($txt)
{
$finished = <<<FIN
Initializing setup...
Complete!
Checking environment...
NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
Complete!
Creating config file...
The Friendica URL has to be set during CLI installation.
FIN;
$this->assertEquals($finished, $txt);
}
/**
* 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
* @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
*/
public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
{
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('config', 'hostname', $assertion);
$this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
$this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
$this->assertConfigEntry('system', 'url', $assertion);
$this->assertConfigEntry('system', 'urlpath', $assertion);
$this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
$this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
}
/**
* Test the automatic installation without any parameter/setting
* Should stuck because of missing hostname
*/
public function testEmpty()
{
$console = new AutomaticInstallation($this->consoleArgv);
$txt = $this->dumpExecute($console);
$this->assertStuckURL($txt);
}
/**
* Test the automatic installation without any parameter/setting
* except URL
*/
public function testEmptyWithURL()
{
$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(), false, '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('url', 'http://friendica.local');
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true, false);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
}
/**
* 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
// Local configuration
// If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
// documentation of their data type and behavior.
return [
'database' => [
'hostname' => '{$conf('database', 'hostname')}',
'username' => '{$conf('database', 'username')}',
'password' => '{$conf('database', 'password')}',
'database' => '{$conf('database', 'database')}',
'charset' => 'utf8mb4',
],
// ****************************************************************
// The configuration below will be overruled by the admin panel.
// Changes made below will only have an effect if the database does
// not contain any configuration for the friendica system.
// ****************************************************************
'config' => [
'admin_email' => '{$conf('config', 'admin_email')}',
'hostname' => '{$conf('config', 'hostname')}',
'sitename' => 'Friendica Social Network',
'register_policy' => \Friendica\Module\Register::OPEN,
'register_text' => '',
],
'system' => [
'basepath' => '{$conf('system', 'basepath')}',
'urlpath' => '{$conf('system', 'urlpath')}',
'url' => '{$conf('system', 'url')}',
'ssl_policy' => '{$conf('system', 'ssl_policy')}',
'default_timezone' => '{$conf('system', 'default_timezone')}',
'language' => '{$conf('system', 'language')}',
],
];
CONF;
vfsStream::newFile('prepared.config.php')
->at($this->root)
->setContent($config);
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('f', 'prepared.config.php');
$txt = $this->dumpExecute($console);
$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);
}
/**
* Test the automatic installation with environment variables
* Includes saving the DB credentials to the file
* @dataProvider dataInstaller
*/
public function testWithEnvironmentAndSave(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);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 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_HOSTNAME=' . $data['config']['hostname']));
$this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
$this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
$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, true);
}
/**
* Test the automatic installation with environment variables
* Don't save the db credentials to the file
* @dataProvider dataInstaller
*/
public function testWithEnvironmentWithoutSave(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);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 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_HOSTNAME=' . $data['config']['hostname']));
$this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
$this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
$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, false, true);
}
/**
* Test the automatic installation with arguments
* @dataProvider dataInstaller
*/
public function testWithArguments(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);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$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('url' , 'system' , 'url');
$option('phppath' , 'config' , 'php_path');
$option('admin' , 'config' , 'admin_email');
$option('tz' , 'system' , 'default_timezone');
$option('lang' , 'system' , 'language');
$option('basepath' , 'system' , 'basepath');
$txt = $this->dumpExecute($console);
$this->assertFinished($txt, true);
$this->assertConfig($data, true, true, true, true);
}
/**
* Test the automatic installation with a wrong database connection
*/
public function testNoDatabaseConnection()
{
$this->mockConnect(false, 1);
$this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
$this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('url', 'http://friendica.local');
$txt = $this->dumpExecute($console);
$this->assertStuckDB($txt);
$this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
$this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
}
public function testGetHelp()
{
// Usable to purposely fail if new commands are added without taking tests into account
$theHelp = <<<HELP
Installation - Install Friendica automatically
Synopsis
bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
Description
Installs Friendica with data based on the local.config.php file or environment variables
Notes
Not checking .htaccess/URL-Rewrite during CLI installation.
Options
-h|--help|-? Show help information
-v Show more debug information.
-a All setup checks are required (except .htaccess)
-f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
-s|--savedb Save the DB credentials to the file (if environment variables is used)
-H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
-p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
-d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
-U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
-P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
-U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
-B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
-b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
-t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
-L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
Environment variables
MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
MYSQL_PORT The port of the mysql/mariadb database
MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
MYSQL_PASSWORD The password of the mysql/mariadb database login
MYSQL_DATABASE The name of the mysql/mariadb database
FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
FRIENDICA_TZ The timezone of Friendica
FRIENDICA_LANG The langauge of Friendica
Examples
bin/console autoinstall -f 'input.config.php
Installs Friendica with the prepared 'input.config.php' file
bin/console autoinstall --savedb
Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
Installs Friendica with a local mysql database with credentials
HELP;
$console = new AutomaticInstallation($this->consoleArgv);
$console->setOption('help', true);
$txt = $this->dumpExecute($console);
$this->assertEquals($theHelp, $txt);
}
}

View file

@ -0,0 +1,193 @@
<?php
namespace Friendica\Test\src\Console;
use Friendica\App\Mode;
use Friendica\Console\Config;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
* @requires PHP 7.0
*/
class ConfigConsoleTest extends ConsoleTest
{
protected function setUp()
{
parent::setUp();
$this->mockApp($this->root);
\Mockery::getConfiguration()->setConstantsMap([
Mode::class => [
'DBCONFIGAVAILABLE' => 0
]
]);
$this->mode
->shouldReceive('has')
->andReturn(true);
}
function testSetGetKeyValue() {
$this->configMock
->shouldReceive('set')
->with('config', 'test', 'now')
->andReturn(true)
->once();
$this->configMock
->shouldReceive('get')
->with('config', 'test')
->andReturn('now')
->twice();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$console->setArgument(2, 'now');
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test <= now\n", $txt);
$this->configMock
->shouldReceive('get')
->with('config', 'test')
->andReturn('now')
->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test => now\n", $txt);
$this->configMock
->shouldReceive('get')
->with('config', 'test')
->andReturn(null)
->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("config.test => \n", $txt);
}
function testSetArrayValue() {
$testArray = [1, 2, 3];
$this->configMock
->shouldReceive('get')
->with('config', 'test')
->andReturn($testArray)
->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$console->setArgument(2, 'now');
$txt = $this->dumpExecute($console);
$this->assertEquals("[Error] config.test is an array and can't be set using this command.\n", $txt);
}
function testTooManyArguments() {
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'config');
$console->setArgument(1, 'test');
$console->setArgument(2, 'it');
$console->setArgument(3, 'now');
$txt = $this->dumpExecute($console);
$assertion = '[Warning] Too many arguments';
$firstline = substr($txt, 0, strlen($assertion));
$this->assertEquals($assertion, $firstline);
}
function testVerbose() {
$this->configMock
->shouldReceive('get')
->with('test', 'it')
->andReturn('now')
->once();
$console = new Config($this->consoleArgv);
$console->setArgument(0, 'test');
$console->setArgument(1, 'it');
$console->setOption('v', 1);
$executable = $this->consoleArgv[0];
$assertion = <<<CONF
Executable: {$executable}
Class: Friendica\Console\Config
Arguments: array (
0 => 'test',
1 => 'it',
)
Options: array (
'v' => 1,
)
test.it => now
CONF;
$txt = $this->dumpExecute($console);
$this->assertEquals($assertion, $txt);
}
function testUnableToSet() {
$this->configMock
->shouldReceive('set')
->with('test', 'it', 'now')
->andReturn(false)
->once();
$this->configMock
->shouldReceive('get')
->with('test', 'it')
->andReturn(NULL)
->once();
$console = new Config();
$console->setArgument(0, 'test');
$console->setArgument(1, 'it');
$console->setArgument(2, 'now');
$txt = $this->dumpExecute($console);
$this->assertSame("Unable to set test.it\n", $txt);
}
public function testGetHelp()
{
// Usable to purposely fail if new commands are added without taking tests into account
$theHelp = <<<HELP
console config - Manage site configuration
Synopsis
bin/console config [-h|--help|-?] [-v]
bin/console config <category> [-h|--help|-?] [-v]
bin/console config <category> <key> [-h|--help|-?] [-v]
bin/console config <category> <key> <value> [-h|--help|-?] [-v]
Description
bin/console config
Lists all config values
bin/console config <category>
Lists all config values in the provided category
bin/console config <category> <key>
Shows the value of the provided key in the category
bin/console config <category> <key> <value>
Sets the value of the provided key in the category
Notes:
Setting config entries which are manually set in config/local.config.php may result in
conflict between database settings and the manual startup settings.
Options
-h|--help|-? Show help information
-v Show more debug information.
HELP;
$console = new Config($this->consoleArgv);
$console->setOption('help', true);
$txt = $this->dumpExecute($console);
$this->assertEquals($txt, $theHelp);
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Friendica\Test\src\Console;
use Asika\SimpleConsole\Console;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\AppMockTrait;
use Friendica\Test\Util\Intercept;
use Friendica\Test\Util\VFSTrait;
abstract class ConsoleTest extends MockedTest
{
use VFSTrait;
use AppMockTrait;
/**
* @var array The default argv for a Console Instance
*/
protected $consoleArgv = [ 'consoleTest.php' ];
protected function setUp()
{
parent::setUp();
Intercept::setUp();
$this->setUpVfsDir();
}
/**
* Dumps the execution of an console output to a string and returns it
*
* @param Console $console The current console instance
*
* @return string the output of the execution
*/
protected function dumpExecute($console)
{
Intercept::reset();
$console->execute();
$returnStr = Intercept::$cache;
Intercept::reset();
return $returnStr;
}
}

View file

@ -0,0 +1,337 @@
<?php
namespace Friendica\Test\src\Console;
use Friendica\Console\ServerBlock;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class ServerBlockConsoleTest extends ConsoleTest
{
protected $defaultBlockList = [
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
[
'domain' => 'pod.ordoevangelistarum.com',
'reason' => 'Illegal content',
]
];
protected function setUp()
{
parent::setUp();
$this->mockApp($this->root);
}
/**
* Test to list the default blocked servers
*/
public function testBlockedServersList()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$console = new ServerBlock($this->consoleArgv);
$txt = $this->dumpExecute($console);
$output = <<<CONS
+----------------------------+-----------------+
| Domain | Reason |
+----------------------------+-----------------+
| social.nobodyhasthe.biz | Illegal content |
| pod.ordoevangelistarum.com | Illegal content |
+----------------------------+-----------------+
CONS;
$this->assertEquals($output, $txt);
}
/**
* Test blockedservers add command
*/
public function testAddBlockedServer()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = $this->defaultBlockList;
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => 'I like it!',
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now');
$console->setArgument(2, 'I like it!');
$txt = $this->dumpExecute($console);
$this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
}
/**
* Test blockedservers add command with the default reason
*/
public function testAddBlockedServerWithDefaultReason()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = $this->defaultBlockList;
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => ServerBlock::DEFAULT_REASON,
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now');
$txt = $this->dumpExecute($console);
$this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
}
/**
* Test blockedservers add command on existed domain
*/
public function testUpdateBlockedServer()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = [
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
[
'domain' => 'pod.ordoevangelistarum.com',
'reason' => 'Other reason',
]
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'add');
$console->setArgument(1, 'pod.ordoevangelistarum.com');
$console->setArgument(2, 'Other reason');
$txt = $this->dumpExecute($console);
$this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
}
/**
* Test blockedservers remove command
*/
public function testRemoveBlockedServer()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = [
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(true)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'remove');
$console->setArgument(1, 'pod.ordoevangelistarum.com');
$txt = $this->dumpExecute($console);
$this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
}
/**
* Test blockedservers with a wrong command
*/
public function testBlockedServersWrongCommand()
{
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'wrongcommand');
$txt = $this->dumpExecute($console);
$this->assertStringStartsWith('[Warning] Unknown command', $txt);
}
/**
* Test blockedservers remove with not existing domain
*/
public function testRemoveBlockedServerNotExist()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'remove');
$console->setArgument(1, 'not.exiting');
$txt = $this->dumpExecute($console);
$this->assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
}
/**
* Test blockedservers add command without argument
*/
public function testAddBlockedServerMissingArgument()
{
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'add');
$txt = $this->dumpExecute($console);
$this->assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
}
/**
* Test blockedservers add command without save
*/
public function testAddBlockedServerNoSave()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = $this->defaultBlockList;
$newBlockList[] = [
'domain' => 'testme.now',
'reason' => ServerBlock::DEFAULT_REASON,
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(false)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'add');
$console->setArgument(1, 'testme.now');
$txt = $this->dumpExecute($console);
$this->assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
}
/**
* Test blockedservers remove command without save
*/
public function testRemoveBlockedServerNoSave()
{
$this->configMock
->shouldReceive('get')
->with('system', 'blocklist')
->andReturn($this->defaultBlockList)
->once();
$newBlockList = [
[
'domain' => 'social.nobodyhasthe.biz',
'reason' => 'Illegal content',
],
];
$this->configMock
->shouldReceive('set')
->with('system', 'blocklist', $newBlockList)
->andReturn(false)
->once();
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'remove');
$console->setArgument(1, 'pod.ordoevangelistarum.com');
$txt = $this->dumpExecute($console);
$this->assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
}
/**
* Test blockedservers remove command without argument
*/
public function testRemoveBlockedServerMissingArgument()
{
$console = new ServerBlock($this->consoleArgv);
$console->setArgument(0, 'remove');
$txt = $this->dumpExecute($console);
$this->assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
}
/**
* Test the blockedservers help
*/
public function testBlockedServersHelp()
{
$console = new ServerBlock($this->consoleArgv);
$console->setOption('help', true);
$txt = $this->dumpExecute($console);
$help = <<<HELP
console serverblock - Manage blocked servers
Usage
bin/console serverblock [-h|--help|-?] [-v]
bin/console serverblock add <server> <reason> [-h|--help|-?] [-v]
bin/console serverblock remove <server> [-h|--help|-?] [-v]
Description
With this tool, you can list the current blocked servers
or you can add / remove a blocked server from the list
Options
-h|--help|-? Show help information
-v Show more debug information.
HELP;
$this->assertEquals($help, $txt);
}
}