Commit b5b420b1 authored by Philippe Pary's avatar Philippe Pary
Browse files

Revert "Remove global variable from Installer, clean some functions and update test"

This reverts commit 30ba8104.
parent 30ba8104
......@@ -23,25 +23,19 @@ namespace Pasteque;
* initializing database connectivity. */
class Installer {
static function sanitize_n_default($arguments) {
// Default values
$sanitized = array(
'dbtype' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'pasteque',
'dbuser' => 'pasteque',
'dbpassword' => substr(md5(time()),strlen(time())-10,10),
'log_level' => Log::LEVEL_ERROR,
'ptuser' => 'pasteque',
'ptpassword' => 'pasteque');
// Merge and trim
foreach (array_keys($sanitized) as $param) {
if (!empty($arguments[$param]))
$sanitized[$param] = trim($arguments[$param]);
}
return $sanitized;
foreach ($arguments as $argument) {
$argument = trim($argument);
}
$arguments["dbtype"] = ($arguments["dbtype"]=="")?"mysql":$arguments["dbtype"]; // this is a troll
$arguments["dbhost"] = ($arguments["dbhost"]=="")?"localhost":$arguments["dbhost"];
$arguments["dbname"] = ($arguments["dbname"]=="")?"pasteque":$arguments["dbname"];
$arguments["username"] = ($arguments["username"]=="")?"admin":$arguments["username"];
$arguments["userpassword"] = ($arguments["userpassword"]=="")?substr(md5(time()),strlen(time())-10,10):$arguments["userpassword"];
return $arguments;
}
static function perform_checks($arguments) {
static function perform_checks() {
global $arguments;
if (!is_writable(PT::$ABSPATH)
|| !is_writable(PT::$ABSPATH."/core_modules/ident/inifile")
|| !is_writable(PT::$ABSPATH."/core_modules/database/inifile")) {
......@@ -66,7 +60,8 @@ class Installer {
return 1;
}
static function database_setup($arguments) {
static function database_setup() {
global $arguments;
// DB log will get displayed if we meet an problem
$dblog = array();
......@@ -143,7 +138,8 @@ class Installer {
}
/* For user we need 2 files to write : ident and database */
static function userconfig_write($arguments) {
static function userconfig_write() {
global $arguments;
// First, check if auth dir exists
if (!file_exists("auth")) {
mkdir("auth");
......@@ -155,11 +151,11 @@ class Installer {
return "<em>auth</em> folder must be writable";
}
// Then, write ident file
$fhandle=fopen(sprintf(PT::$ABSPATH . '/auth/%s_id.ini',$arguments["ptuser"]),"w");
fwrite($fhandle,sprintf("password=%s",password_hash($arguments["ptpassword"])));
$fhandle=fopen(sprintf(PT::$ABSPATH . '/auth/%s_id.ini',$arguments["username"]),"w");
fwrite($fhandle,sprintf("password=%s",password_hash($arguments["userpassword"],PASSWORD_DEFAULT)));
fclose($fhandle);
// Then, write database file
$fhandle=fopen(sprintf(PT::$ABSPATH . '/auth/%s_db.ini',$arguments["ptuser"]),"w");
$fhandle=fopen(sprintf(PT::$ABSPATH . '/auth/%s_db.ini',$arguments["username"]),"w");
fprintf($fhandle,"type = %s\n",$arguments["dbtype"]);
fprintf($fhandle,"host = %s\n",$arguments["dbhost"]);
fprintf($fhandle,"port = %s\n",($arguments["dbtype"]=="mysql")?"3306":"5432");
......@@ -176,7 +172,8 @@ class Installer {
* 3. database config file
* We will assume directories exist, if not, we're in hell
*/
static function configfile_write($arguments) {
static function configfile_write() {
global $arguments;
// Main config file
$fhandle=fopen(PT::$ABSPATH . "/config.php","w");
fprintf($fhandle,"<?php\n");
......@@ -218,7 +215,7 @@ class Installer {
fprintf($fhandle,"];\n");
fprintf($fhandle,"\$config[\"debug\"] = true;\n");
fprintf($fhandle,"\$config[\"no_auth\"] = false;\n");
fprintf($fhandle,"\$config[\"log_level\"] = \"%s\";\n",Log::strLevel($arguments['log_level']));
fprintf($fhandle,"\$config[\"log_level\"] = \"%s\";\n",$arguments["debug"]);
fprintf($fhandle,"function getConfig() {\n");
fprintf($fhandle," global \$config;\n");
fprintf($fhandle," return \$config;\n");
......
......@@ -57,7 +57,7 @@ elseif (isset($_POST["dbuser"])) {
// First step : system checks (FS, DB server)
if(!isset($error)) {
$checks_result = Installer::perform_checks($arguments);
$checks_result = Installer::perform_checks();
if($checks_result !== 1) {
$error = $checks_result;
echo \Pasteque\errorDiv($error);
......@@ -66,7 +66,7 @@ elseif (isset($_POST["dbuser"])) {
// Second step : try to install database
if(!isset($error)) {
$dbsetup_result = Installer::database_setup($arguments);
$dbsetup_result = Installer::database_setup();
if ($dbsetup_result !== 1) {
$error = $dbsetup_result;
echo \Pasteque\errorDiv($error);
......@@ -75,7 +75,7 @@ elseif (isset($_POST["dbuser"])) {
// Third step : try to create user config files
if(!isset($error)) {
$userconfig_result = Installer::userconfig_write($arguments);
$userconfig_result = Installer::userconfig_write();
if ($userconfig_result !== 1) {
$error = $userconfig_result;
echo \Pasteque\errorDiv($error);
......@@ -84,7 +84,7 @@ elseif (isset($_POST["dbuser"])) {
// Last step : try to create config.php
if(!isset($error)) {
$configfile_result = Installer::configfile_write($arguments);
$configfile_result = Installer::configfile_write();
if ($configfile_result !== 1) {
$error = $configfile_result;
echo \Pasteque\errorDiv($error);
......
......@@ -55,7 +55,7 @@ namespace Pasteque;
<div class="form-group">
<label class="col-md-4 control-label" for="username">Username</label>
<div class="col-md-4">
<input id="username" name="ptname" placeholder="Username" class="form-control input-md" type="text">
<input id="username" name="username" placeholder="Username" class="form-control input-md" type="text">
<span class="help-block">username used for logging in Pastèque</span>
</div>
</div>
......@@ -63,7 +63,7 @@ namespace Pasteque;
<div class="form-group">
<label class="col-md-4 control-label" for="userpassword"></label>
<div class="col-md-4">
<input id="userpassword" name="ptpassword" placeholder="Password" class="form-control input-md" type="password">
<input id="userpassword" name="userpassword" placeholder="Password" class="form-control input-md" type="password">
<span class="help-block">password used for logging in Pastèque</span>
</div>
</div>
......@@ -72,15 +72,15 @@ namespace Pasteque;
<div id="advanced_options" style="display:none;">
<div class="form-group">
<label class="col-md-4 control-label" for="log_level">Debug Level</label>
<label class="col-md-4 control-label" for="debug">Debug Level</label>
<div class="col-md-4">
<select id="log_level" name="log_level" class="form-control">
<option value="<?php echo Log::LEVEL_DISABLE ?>">None</option>
<option value="<?php echo Log::LEVEL_DEBUG ?>">DEBUG</option>
<option value="<?php echo Log::LEVEL_INFO ?>">INFO</option>
<option value="<?php echo Log::LEVEL_WARNING ?>">WARNING</option>
<option value="<?php echo Log::LEVEL_ERROR ?>">ERROR</option>
<option value="<?php echo Log::LEVEL_WTF ?>">WTF</option>
<select id="debug" name="debug" class="form-control">
<option value="NONE">None</option>
<option value="DEBUG">DEBUG</option>
<option value="INFO">INFO</option>
<option value="WARNING">WARNING</option>
<option value="ERROR">ERROR</option>
<option value="WTF">WTF</option>
</select>
</div>
</div>
......
......@@ -41,62 +41,44 @@ require_once(PT::$ABSPATH . "/inc/jwt.php");
require_once(PT::$ABSPATH . "/inc/login.php");
require_once(PT::$ABSPATH . '/inc/installer.php');
Class InstallTest extends \PHPUnit_Framework_TestCase {
private $arguments;
protected function setUp() {
global $config;
$this->arguments = array (
'dbtype' => $config['db_type'],
'dbuser' => $config['db_user'],
'dbpassword' => $config['db_password'],
'dbhost' => $config['db_host'],
'dbname' => $config['db_name'],
'dbuser' => 'admin ',
'dbpassword' => 't0ps3cr3tp4ssw0rD!',
'log_level' => Log::LEVEL_WARNING,
'unused' => 'lol');
}
$arguments["dbtype"] = $config["db_type"];
$arguments["dbuser"] = $config["db_user"];
$arguments["dbpassword"] = $config["db_password"];
$arguments["dbhost"] = $config["db_host"];
$arguments["dbname"] = $config["db_name"];
$arguments["username"] = "admin";
$arguments["userpassword"] = "t0ps3cr3tp4ssw0rD!";
$arguments["debug"] = "999";
function tearDown() {
}
Class InstallTest extends \PHPUnit_Framework_TestCase {
public function testSanitize_n_default() {
$this->arguments['dbtye'] = ''; // Test default
$sanitizedArgs = Installer::sanitize_n_default($this->arguments);
$this->assertEquals('mysql', $sanitizedArgs['dbtype'], 'Default dbtype failed');
$this->assertEquals('admin', $sanitizedArgs['dbuser'], 'Trim failed');
$this->assertEquals($this->arguments['dbpassword'], $sanitizedArgs['dbpassword'], 'Unexpected dbpassword modification');
$this->assertEquals($this->arguments['log_level'], $sanitizedArgs['log_level'], 'Unexpected log_level modification');
$this->assertEquals(false, array_key_exists('unused', $sanitizedArgs), "Unused parameter wasn't deleted");
public function testSanitite_n_default() {
global $arguments;
$arguments = Installer::sanitize_n_default($arguments);
$this->assertEquals("mysql",$arguments["dbtype"],"Sanitize and default failed");
}
/** @depends testSanitize_n_default */
public function testPerform_checks() {
$sanitizedArgs = Installer::sanitize_n_default($this->arguments);
$this->assertNotEquals(1, Installer::perform_checks($sanitizedArgs),"Perform checks failed");
global $arguments;
$this->assertNotEquals(1, Installer::perform_checks(),"Perform checks failed");
}
/** @depends testSanitize_n_default */
public function testDatabase_setup() {
$sanitizedArgs = Installer::sanitize_n_default($this->arguments);
$this->assertNotEquals(1, Installer::database_setup($sanitizedArgs),"Database setup failed with good credentials");
global $arguments;
$this->assertNotEquals(1, Installer::database_setup(),"Database setup failed with good credentials");
$arguments["dbpassword"] = "thisisnotthepasswordIhope";
$this->assertNotEquals(1, Installer::database_setup($sanitizedArgs),"Database setup failed with wrong credentials");
$this->assertNotEquals(1, Installer::database_setup(),"Database setup failed with wrong credentials");
dropDatabase();
$this->assertNotEquals(1, Installer::database_setup($sanitizedArgs),"Database creation failed");
$this->assertNotEquals(1, Installer::database_setup(),"Database creation failed");
}
/** @depends testSanitize_n_default */
public function testUserconfig_write() {
$sanitizedArgs = Installer::sanitize_n_default($this->arguments);
$this->assertNotEquals(1, Installer::userconfig_write($sanitizedArgs),"Userconfiguration failed to be written");
global $arguments;
$this->assertNotEquals(1, Installer::userconfig_write(),"Userconfiguration failed to be written");
}
/** @depends testSanitize_n_default */
public function testConfigfile_write() {
$sanitizedArgs = Installer::sanitize_n_default($this->arguments);
$this->assertNotEquals(1, Installer::configfile_write($sanitizedArgs),"Configuration file writing failed");
global $arguments;
$this->assertNotEquals(1, Installer::configfile_write(),"Configuration file writing failed");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment