Minor setup script tweaks, tests

This commit is contained in:
Bartek Fabiszewski 2019-05-15 12:03:55 +02:00
parent 3966c697b7
commit 7acc3df67d
9 changed files with 168 additions and 120 deletions

View File

@ -10,6 +10,12 @@ sed -i "s/^\$gkey = .*$/\$gkey = \"${ULOGGER_GKEY}\";/" /var/www/html/config.php
sed -i "s/^\$lang = .*$/\$lang = \"${ULOGGER_LANG}\";/" /var/www/html/config.php sed -i "s/^\$lang = .*$/\$lang = \"${ULOGGER_LANG}\";/" /var/www/html/config.php
sed -i "s/^\$units = .*$/\$units = \"${ULOGGER_UNITS}\";/" /var/www/html/config.php sed -i "s/^\$units = .*$/\$units = \"${ULOGGER_UNITS}\";/" /var/www/html/config.php
if [ "${ULOGGER_ENABLE_SETUP}" = "1" ]; then
sed -i "s/\$enabled = false;/\$enabled = true;/" /var/www/html/scripts/setup.php;
echo "ulogger setup script enabled"
echo "----------------------------"
fi
# show config variables # show config variables
echo "ulogger configuration" echo "ulogger configuration"
echo "---------------------" echo "---------------------"

View File

@ -1,9 +1,17 @@
<?php <?php
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase { abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase {
/**
* @var PDO $pdo
*/
static private $pdo = null; static private $pdo = null;
/**
* @var PHPUnit_Extensions_Database_DB_IDatabaseConnection $conn
*/
private $conn = null; private $conn = null;
static private $driver = "mysql"; static private $driver = "mysql";
@ -90,9 +98,21 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
self::$pdo->query("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks"); self::$pdo->query("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks");
self::$pdo->query("ALTER SEQUENCE positions_id_seq RESTART WITH $positions"); self::$pdo->query("ALTER SEQUENCE positions_id_seq RESTART WITH $positions");
} else if (self::$driver == "sqlite") { } else if (self::$driver == "sqlite") {
$retry = 1;
do {
try {
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'users'"); self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'users'");
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'"); self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'");
self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'positions'"); self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'positions'");
$retry = 0;
} catch (Exception $e) {
// sqlite raises error when db schema changes in another connection.
if (strpos($e->getMessage(), 'database schema has changed') !== false) {
self::$pdo = null;
self::setUpBeforeClass();
}
}
} while ($retry--);
} }
} }

View File

@ -5,6 +5,9 @@ require_once("BaseDatabaseTestCase.php");
class UloggerAPITestCase extends BaseDatabaseTestCase { class UloggerAPITestCase extends BaseDatabaseTestCase {
/**
* @var null|GuzzleHttp\Client $http
*/
protected $http = null; protected $http = null;
public function setUp() { public function setUp() {

View File

@ -6,6 +6,9 @@ require_once(__DIR__ . "/../../helpers/db.php");
class UloggerDatabaseTestCase extends BaseDatabaseTestCase { class UloggerDatabaseTestCase extends BaseDatabaseTestCase {
/**
* @var uDb $udb
*/
static private $udb = null; static private $udb = null;
public static function setUpBeforeClass() { public static function setUpBeforeClass() {

View File

@ -25,11 +25,11 @@ before_install:
case "$DB_DSN" in case "$DB_DSN" in
mysql*) mysql*)
docker build -t ulogger --build-arg DB_DRIVER=mysql . docker build -t ulogger --build-arg DB_DRIVER=mysql .
docker run -d --name ulogger -p 8080:80 -p 8081:3306 --expose 3306 ulogger docker run -d --name ulogger -p 8080:80 -p 8081:3306 --expose 3306 -e ULOGGER_ENABLE_SETUP=1 ulogger
;; ;;
pgsql*) pgsql*)
docker build -t ulogger --build-arg DB_DRIVER=pgsql . docker build -t ulogger --build-arg DB_DRIVER=pgsql .
docker run -d --name ulogger -p 8080:80 -p 8081:5432 --expose 5432 ulogger docker run -d --name ulogger -p 8080:80 -p 8081:5432 --expose 5432 -e ULOGGER_ENABLE_SETUP=1 ulogger
;; ;;
sqlite*) sqlite*)
sudo apt-get update -qq sudo apt-get update -qq
@ -39,7 +39,7 @@ before_install:
sudo chown -R travis:travis /tmp/data sudo chown -R travis:travis /tmp/data
sudo chmod -R 777 /tmp/data sudo chmod -R 777 /tmp/data
docker build -t ulogger --build-arg DB_DRIVER=sqlite . docker build -t ulogger --build-arg DB_DRIVER=sqlite .
docker run -d --name ulogger -p 8080:80 -v /tmp/data:/data ulogger docker run -d --name ulogger -p 8080:80 -v /tmp/data:/data -e ULOGGER_ENABLE_SETUP=1 ulogger
;; ;;
esac esac
- composer install - composer install

View File

@ -16,6 +16,7 @@ ENV ULOGGER_GKEY ""
ENV ULOGGER_LANG en ENV ULOGGER_LANG en
ENV ULOGGER_UNITS metric ENV ULOGGER_UNITS metric
ENV ULOGGER_DB_DRIVER ${DB_DRIVER} ENV ULOGGER_DB_DRIVER ${DB_DRIVER}
ENV ULOGGER_ENABLE_SETUP 0
ENV LANG=en_US.utf-8 ENV LANG=en_US.utf-8

View File

@ -1,6 +1,9 @@
{ {
"require": { "require": {
"ulrichsg/getopt-php": "^3.2" "ulrichsg/getopt-php": "^3.2",
"ext-json": "*",
"ext-pdo": "*",
"ext-xmlwriter": "*"
}, },
"scripts": { "scripts": {
"test": "./vendor/bin/phpunit" "test": "./vendor/bin/phpunit"

View File

@ -43,6 +43,8 @@ $langSetup["scriptdesc"] = "This script will set up tables needed for µlogger (
$langSetup["scriptdesc2"] = "When done the script will ask you to provide user name and password for your µlogger user."; $langSetup["scriptdesc2"] = "When done the script will ask you to provide user name and password for your µlogger user.";
$langSetup["startbutton"] = "Press to start"; $langSetup["startbutton"] = "Press to start";
$langSetup["restartbutton"] = "Restart"; $langSetup["restartbutton"] = "Restart";
$langSetup["optionwarn"] = "PHP configuration option %s must be set to %s."; // substitutes option name and value
$langSetup["extensionwarn"] = "Required PHP extension %s is not available."; // substitutes extension name
// application strings // application strings

View File

@ -54,14 +54,14 @@ switch ($command) {
$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ];
$pdo = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass, $options); $pdo = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass, $options);
$dbDriver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); $dbDriver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
} catch (PDOException $e ) { } catch (PDOException $e) {
$messages[] = "<span class=\"warn\">{$langSetup["dbconnectfailed"]}</span>"; $messages[] = "<span class=\"warn\">{$langSetup["dbconnectfailed"]}</span>";
$messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>"); $messages[] = sprintf($langSetup["serversaid"], "<b>" . $e->getMessage() . "</b>");
$messages[] = $langSetup["checkdbsettings"]; $messages[] = $langSetup["checkdbsettings"];
break; break;
} }
try { try {
$queries = getQueries($pdo); $queries = getQueries();
foreach ($queries as $query) { foreach ($queries as $query) {
$pdo->query($query); $pdo->query($query);
} }
@ -99,7 +99,7 @@ switch ($command) {
break; break;
default: default:
$messages[] = "<img src=\"../icons/favicon-32x32.png\">" . $langSetup["welcome"]; $messages[] = "<img src=\"../icons/favicon-32x32.png\" alt=\"µLogger\">" . $langSetup["welcome"];
if (!isset($enabled) || $enabled === false) { if (!isset($enabled) || $enabled === false) {
$messages[] = sprintf($langSetup["disabledwarn"], "<b>\$enabled</b>", "<b>true</b>"); $messages[] = sprintf($langSetup["disabledwarn"], "<b>\$enabled</b>", "<b>true</b>");
$messages[] = sprintf($langSetup["lineshouldread"], "<br><span class=\"warn\">\$enabled = false;</span><br>", "<br><span class=\"ok\">\$enabled = true;</span>"); $messages[] = sprintf($langSetup["lineshouldread"], "<br><span class=\"warn\">\$enabled = false;</span><br>", "<br><span class=\"ok\">\$enabled = true;</span>");
@ -132,20 +132,34 @@ switch ($command) {
$messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>"; $messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>";
break; break;
} }
if (ini_get("session.auto_start") == '1') {
$messages[] = sprintf($langSetup["optionwarn"], "session.auto_start", "0 (off)");
$messages[] = $langSetup["dorestart"];
$messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>";
break;
}
if (!extension_loaded("pdo")) {
$messages[] = sprintf($langSetup["extensionwarn"], "PDO");
$messages[] = $langSetup["dorestart"];
$messages[] = "<form method=\"post\" action=\"setup.php\"><button>{$langSetup["restartbutton"]}</button></form>";
break;
}
$messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "<b>" . getDbname(uConfig::$dbdsn) . "</b>"); $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "<b>" . getDbname(uConfig::$dbdsn) . "</b>");
$messages[] = $langSetup["scriptdesc2"]; $messages[] = $langSetup["scriptdesc2"];
$messages[] = "<form method=\"post\" action=\"setup.php\"><input type=\"hidden\" name=\"command\" value=\"setup\"><button>{$langSetup["startbutton"]}</button></form>"; $messages[] = "<form method=\"post\" action=\"setup.php\"><input type=\"hidden\" name=\"command\" value=\"setup\"><button>{$langSetup["startbutton"]}</button></form>";
break; break;
} }
function getQueries($pdo) { function getQueries() {
global $tPositions, $tUsers, $tTracks, $dbDriver; global $tPositions, $tUsers, $tTracks, $dbDriver;
$queries = []; $queries = [];
switch($dbDriver) { switch ($dbDriver) {
case "mysql": case "mysql":
// users $queries[] = "DROP TABLE IF EXISTS `$tPositions`";
$queries[] = "DROP TABLE IF EXISTS `$tTracks`";
$queries[] = "DROP TABLE IF EXISTS `$tUsers`"; $queries[] = "DROP TABLE IF EXISTS `$tUsers`";
$queries[] = "CREATE TABLE `$tUsers` ( $queries[] = "CREATE TABLE `$tUsers` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`login` varchar(15) CHARACTER SET latin1 NOT NULL UNIQUE, `login` varchar(15) CHARACTER SET latin1 NOT NULL UNIQUE,
@ -153,8 +167,6 @@ function getQueries($pdo) {
) ENGINE=InnoDB DEFAULT CHARSET=utf8"; ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
// tracks
$queries[] = "DROP TABLE IF EXISTS `$tTracks`";
$queries[] = "CREATE TABLE `$tTracks` ( $queries[] = "CREATE TABLE `$tTracks` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) NOT NULL, `user_id` int(11) NOT NULL,
@ -164,8 +176,6 @@ function getQueries($pdo) {
FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`) FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"; ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
// positions
$queries[] = "DROP TABLE IF EXISTS `$tPositions`";
$queries[] = "CREATE TABLE `$tPositions` ( $queries[] = "CREATE TABLE `$tPositions` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -188,16 +198,16 @@ function getQueries($pdo) {
break; break;
case "pgsql": case "pgsql":
// users $queries[] = "DROP TABLE IF EXISTS $tPositions";
$queries[] = "DROP TABLE IF EXISTS $tTracks";
$queries[] = "DROP TABLE IF EXISTS $tUsers"; $queries[] = "DROP TABLE IF EXISTS $tUsers";
$queries[] = "CREATE TABLE $tUsers ( $queries[] = "CREATE TABLE $tUsers (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
login VARCHAR(15) NOT NULL UNIQUE, login VARCHAR(15) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL DEFAULT '' password VARCHAR(255) NOT NULL DEFAULT ''
)"; )";
// tracks
$queries[] = "DROP TABLE IF EXISTS $tTracks";
$queries[] = "CREATE TABLE $tTracks ( $queries[] = "CREATE TABLE $tTracks (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
user_id INT NOT NULL, user_id INT NOT NULL,
@ -207,8 +217,6 @@ function getQueries($pdo) {
)"; )";
$queries[] = "CREATE INDEX idx_user_id ON $tTracks(user_id)"; $queries[] = "CREATE INDEX idx_user_id ON $tTracks(user_id)";
// positions
$queries[] = "DROP TABLE IF EXISTS $tPositions";
$queries[] = "CREATE TABLE $tPositions ( $queries[] = "CREATE TABLE $tPositions (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -231,15 +239,15 @@ function getQueries($pdo) {
break; break;
case "sqlite": case "sqlite":
// users $queries[] = "DROP TABLE IF EXISTS `$tPositions`";
$queries[] = "DROP TABLE IF EXISTS `$tTracks`";
$queries[] = "DROP TABLE IF EXISTS `$tUsers`"; $queries[] = "DROP TABLE IF EXISTS `$tUsers`";
$queries[] = "CREATE TABLE `$tUsers` ( $queries[] = "CREATE TABLE `$tUsers` (
`id` integer PRIMARY KEY AUTOINCREMENT, `id` integer PRIMARY KEY AUTOINCREMENT,
`login` varchar(15) NOT NULL UNIQUE, `login` varchar(15) NOT NULL UNIQUE,
`password` varchar(255) NOT NULL DEFAULT '' `password` varchar(255) NOT NULL DEFAULT ''
)"; )";
// tracks
$queries[] = "DROP TABLE IF EXISTS `$tTracks`";
$queries[] = "CREATE TABLE `$tTracks` ( $queries[] = "CREATE TABLE `$tTracks` (
`id` integer PRIMARY KEY AUTOINCREMENT, `id` integer PRIMARY KEY AUTOINCREMENT,
`user_id` integer NOT NULL, `user_id` integer NOT NULL,
@ -249,8 +257,6 @@ function getQueries($pdo) {
)"; )";
$queries[] = "CREATE INDEX `idx_user_id` ON `$tTracks`(`user_id`)"; $queries[] = "CREATE INDEX `idx_user_id` ON `$tTracks`(`user_id`)";
// positions
$queries[] = "DROP TABLE IF EXISTS `$tPositions`";
$queries[] = "CREATE TABLE `$tPositions` ( $queries[] = "CREATE TABLE `$tPositions` (
`id` integer PRIMARY KEY AUTOINCREMENT, `id` integer PRIMARY KEY AUTOINCREMENT,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -273,7 +279,7 @@ function getQueries($pdo) {
break; break;
default: default:
throw InvalidArgumentException("Driver not supported"); throw new InvalidArgumentException("Driver not supported");
} }
return $queries; return $queries;
} }
@ -303,13 +309,13 @@ function getDbname($dsn) {
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="<?= uConfig::$lang ?>">
<head> <head>
<title><?= $lang["title"] ?></title> <title><?= $lang["title"] ?></title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"> <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i&amp;subset=cyrillic" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i&amp;subset=cyrillic" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../css/main.css"> <link href="../css/main.css" type="text/css" rel="stylesheet">
<style> <style>
#message { #message {
font-family: 'Open Sans', Verdana, sans-serif; font-family: 'Open Sans', Verdana, sans-serif;
@ -317,9 +323,11 @@ function getDbname($dsn) {
color: #f8f5f7; color: #f8f5f7;
padding: 10%; padding: 10%;
} }
#message img { #message img {
vertical-align: bottom; vertical-align: bottom;
} }
#message input[type=text], #message input[type=password] { #message input[type=text], #message input[type=password] {
width: 40em; width: 40em;
padding: 0.4em; padding: 0.4em;
@ -331,14 +339,16 @@ function getDbname($dsn) {
-moz-border-radius: 5px; -moz-border-radius: 5px;
-webkit-border-radius: 5px; -webkit-border-radius: 5px;
} }
.warn { .warn {
color: #ffc747; color: #ffc747;
} }
.ok { .ok {
color: #00e700; color: #00e700;
} }
</style> </style>
<script type="text/javascript"> <script>
var lang = <?= json_encode($lang) ?>; var lang = <?= json_encode($lang) ?>;
var pass_regex = <?= uConfig::passRegex() ?>; var pass_regex = <?= uConfig::passRegex() ?>;
@ -351,7 +361,7 @@ function getDbname($dsn) {
alert(lang['allrequired']); alert(lang['allrequired']);
return false; return false;
} }
if (pass != pass2) { if (pass !== pass2) {
alert(lang['passnotmatch']); alert(lang['passnotmatch']);
return false; return false;
} }
@ -362,13 +372,13 @@ function getDbname($dsn) {
return true; return true;
} }
</script> </script>
</head> </head>
<body> <body>
<div id="message"> <div id="message">
<?php foreach ($messages as $message): ?> <?php foreach ($messages as $message): ?>
<p><?= $message ?></p> <p><?= $message ?></p>
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
</body> </body>
</html> </html>