[CI] Add pager test + MySQL service

This commit is contained in:
ruan 2020-08-07 19:39:12 +02:00
parent e975ab8001
commit 2b7bc8fe2e
6 changed files with 152 additions and 73 deletions

View File

@ -24,19 +24,20 @@ before_script:
# Install and run Composer # Install and run Composer
- curl -sS https://getcomposer.org/installer | php - curl -sS https://getcomposer.org/installer | php
- php composer.phar install - php composer.phar install
- php composer.phar require --dev phpunit/phpunit ^9
# Bring in any services we need http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service # Bring in any services we need http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
# See http://docs.gitlab.com/ce/ci/services/README.html for examples. # See http://docs.gitlab.com/ce/ci/services/README.html for examples.
services: [] services:
# - mysql:5.7 - mariadb:latest
# Set any variables we need # Set any variables we need
variables: variables:
COMPOSER: 'inc/test/composer.json' COMPOSER: 'inc/test/composer.json'
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/) # Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
MYSQL_DATABASE: mysql_database MYSQL_DATABASE: mysql_database
MYSQL_ROOT_PASSWORD: mysql_strong_password MYSQL_USERNAME: root
MYSQL_PASSWORD: mysql_strong_password
LITEBANS_TABLE_PREFIX: test_
# Run our tests # Run our tests
# If Xdebug was installed you can generate a coverage report and see code coverage metrics. # If Xdebug was installed you can generate a coverage report and see code coverage metrics.

View File

@ -2,10 +2,14 @@
require_once './inc/init.php'; require_once './inc/init.php';
class Page { class Page {
public function __construct($name, $header = true) { public function __construct($name, $header = true, $connect = true) {
ini_set('default_charset', 'utf-8'); ini_set('default_charset', 'utf-8');
require_once './inc/settings.php'; require_once './inc/settings.php';
$settings = new Settings(); if (class_exists("EnvSettings")) {
$settings = new EnvSettings($connect);
} else {
$settings = new Settings($connect);
}
setlocale(LC_ALL, $settings->lang); setlocale(LC_ALL, $settings->lang);
require_once './lang/en_US.utf8.php'; require_once './lang/en_US.utf8.php';
@ -25,7 +29,7 @@ class Page {
} }
$this->conn = $settings->conn; $this->conn = $settings->conn;
$this->settings = $settings; $this->settings = $settings;
$this->uuid_name_cache = array(); $this->uuid_name_cache = [];
$this->name = $name; $this->name = $name;
@ -36,30 +40,30 @@ class Page {
$info = $this->type_info($name); $info = $this->type_info($name);
$this->set_info($info); $this->set_info($info);
$this->permanent = array( $this->permanent = [
'ban' => $this->t("generic.permanent.ban"), 'ban' => $this->t("generic.permanent.ban"),
'mute' => $this->t("generic.permanent.mute"), 'mute' => $this->t("generic.permanent.mute"),
'warn' => $this->t("generic.permanent"), 'warn' => $this->t("generic.permanent"),
'kick' => null, 'kick' => null,
); ];
$this->expired = array( $this->expired = [
'ban' => $this->t("page.expired.ban"), 'ban' => $this->t("page.expired.ban"),
'mute' => $this->t("page.expired.mute"), 'mute' => $this->t("page.expired.mute"),
'warn' => $this->t("page.expired.warning"), 'warn' => $this->t("page.expired.warning"),
'kick' => null, 'kick' => null,
); ];
$this->expired_by = array( $this->expired_by = [
'ban' => $this->t("page.expired.ban-by"), 'ban' => $this->t("page.expired.ban-by"),
'mute' => $this->t("page.expired.mute-by"), 'mute' => $this->t("page.expired.mute-by"),
'warn' => $this->t("page.expired.warning"), 'warn' => $this->t("page.expired.warning"),
'kick' => null, 'kick' => null,
); ];
$this->punished_by = array( $this->punished_by = [
'ban' => $this->t("generic.banned.by"), 'ban' => $this->t("generic.banned.by"),
'mute' => $this->t("generic.muted.by"), 'mute' => $this->t("generic.muted.by"),
'warn' => $this->t("generic.warned.by"), 'warn' => $this->t("generic.warned.by"),
'kick' => $this->t("generic.kicked.by"), 'kick' => $this->t("generic.kicked.by"),
); ];
$this->table_headers_printed = false; $this->table_headers_printed = false;
$this->args = array_values($_GET); $this->args = array_values($_GET);
@ -121,43 +125,43 @@ class Page {
switch ($type) { switch ($type) {
case "ban": case "ban":
case "bans": case "bans":
return array( return [
"type" => "ban", "type" => "ban",
"table" => $settings->table['bans'], "table" => $settings->table['bans'],
"title" => $this->t("title.bans"), "title" => $this->t("title.bans"),
"page" => "bans.php", "page" => "bans.php",
); ];
case "mute": case "mute":
case "mutes": case "mutes":
return array( return [
"type" => "mute", "type" => "mute",
"table" => $settings->table['mutes'], "table" => $settings->table['mutes'],
"title" => $this->t("title.mutes"), "title" => $this->t("title.mutes"),
"page" => "mutes.php", "page" => "mutes.php",
); ];
case "warn": case "warn":
case "warnings": case "warnings":
return array( return [
"type" => "warn", "type" => "warn",
"table" => $settings->table['warnings'], "table" => $settings->table['warnings'],
"title" => $this->t("title.warnings"), "title" => $this->t("title.warnings"),
"page" => "warnings.php", "page" => "warnings.php",
); ];
case "kick": case "kick":
case "kicks": case "kicks":
return array( return [
"type" => "kick", "type" => "kick",
"table" => $settings->table['kicks'], "table" => $settings->table['kicks'],
"title" => $this->t("title.kicks"), "title" => $this->t("title.kicks"),
"page" => "kicks.php", "page" => "kicks.php",
); ];
default: default:
return array( return [
"type" => null, "type" => null,
"table" => null, "table" => null,
"title" => null, "title" => null,
"page" => null, "page" => null,
); ];
} }
} }
@ -199,13 +203,13 @@ class Page {
return $rows; return $rows;
} catch (PDOException $ex) { } catch (PDOException $ex) {
Settings::handle_error($this->settings, $ex); Settings::handle_error($this->settings, $ex);
return array(); return [];
} }
} }
function get_selection($table, $phpIsBroken = true) { function get_selection($table, $phpIsBroken = true) {
$columns = array("id", "uuid", "reason", "banned_by_name", "banned_by_uuid", "time", "until", "server_origin", "server_scope", "active", "ipban"); $columns = ["id", "uuid", "reason", "banned_by_name", "banned_by_uuid", "time", "until", "server_origin", "server_scope", "active", "ipban"];
$bitColumns = array("active", "ipban"); $bitColumns = ["active", "ipban"];
if ($table === $this->settings->table['warnings']) { if ($table === $this->settings->table['warnings']) {
array_push($columns, "warned"); array_push($columns, "warned");
@ -482,7 +486,7 @@ class Page {
} }
if ($print_headers && !$this->table_headers_printed) { if ($print_headers && !$this->table_headers_printed) {
$headers = array_keys($array); $headers = array_keys($array);
$headers_translated = array(); $headers_translated = [];
foreach ($headers as $header) { foreach ($headers as $header) {
if ($header === "executor" && $this->name !== "history") { if ($header === "executor" && $this->name !== "history") {
$header = $this->punished_by[$type]; $header = $this->punished_by[$type];
@ -554,6 +558,10 @@ class Page {
function print_pager($total = -1, $args = "", $prevargs = "", $page = null, $simple = true) { function print_pager($total = -1, $args = "", $prevargs = "", $page = null, $simple = true) {
if (!$this->settings->show_pager) return; if (!$this->settings->show_pager) return;
echo implode($this->generate_pager($total, $args, $prevargs, $page, $simple));
}
function generate_pager($total = -1, $args = "", $prevargs = "", $page = null, $simple = true) {
$table = $this->table; $table = $this->table;
if ($page === null) { if ($page === null) {
$page = $this->name . ".php"; $page = $this->name . ".php";
@ -599,7 +607,11 @@ class Page {
$pager_next = "<a href=\"$pager_next_href\">$pager_next</a>"; $pager_next = "<a href=\"$pager_next_href\">$pager_next</a>";
} }
$pager_count = '<div><div class="litebans-pager-number">' . $this->t("table.pager.number") . ' ' . $cur . '/' . $pages . '</div></div>'; $pager_count = '<div><div class="litebans-pager-number">' . $this->t("table.pager.number") . ' ' . $cur . '/' . $pages . '</div></div>';
echo "$pager_prev $pager_next $pager_count"; return [
"prev" => $pager_prev,
"next" => $pager_next,
"count" => $pager_count,
];
} }
function print_footer($container_end = true) { function print_footer($container_end = true) {

View File

@ -1,6 +1,7 @@
<?php <?php
if (class_exists("Settings")) return;
final class Settings { class Settings {
public static $TRUE = "1", $FALSE = "0"; public static $TRUE = "1", $FALSE = "0";
public function __construct($connect = true) { public function __construct($connect = true) {
@ -161,54 +162,49 @@ final class Settings {
date_default_timezone_set($timezone); // set configured timezone date_default_timezone_set($timezone); // set configured timezone
$table_prefix = $this->table_prefix; $this->init_tables();
// Internal table names, do not translate.
$this->table = array(
'bans' => "${table_prefix}bans",
'mutes' => "${table_prefix}mutes",
'warnings' => "${table_prefix}warnings",
'kicks' => "${table_prefix}kicks",
'history' => "${table_prefix}history",
'servers' => "${table_prefix}servers",
'config' => "${table_prefix}config",
);
if ($connect) { if ($connect) {
$driver = $this->driver; $this->connect();
$host = $this->host; } else {
$port = $this->port; $this->conn = null;
$database = $this->database; }
$username = $this->username; }
$password = $this->password;
if ($username === "" && $password === "") {
redirect("error/unconfigured.php");
}
$dsn = "$driver:dbname=$database;host=$host;port=$port"; protected function connect() {
if ($driver === 'mysql') { $driver = $this->driver;
$dsn .= ';charset=utf8'; $host = $this->host;
} $port = $this->port;
$database = $this->database;
$username = $this->username;
$password = $this->password;
if ($username === "" && $password === "") {
redirect("error/unconfigured.php");
}
$options = array( $dsn = "$driver:dbname=$database;host=$host;port=$port";
PDO::ATTR_TIMEOUT => 5, if ($driver === 'mysql') {
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, $dsn .= ';charset=utf8';
PDO::ATTR_EMULATE_PREPARES => false, }
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
);
try { $options = array(
$this->conn = new PDO($dsn, $username, $password, $options); PDO::ATTR_TIMEOUT => 5,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
);
$st = $this->conn->query("SELECT * FROM " . $this->table['config'] . " LIMIT 1;"); try {
$st->fetch(); $this->conn = new PDO($dsn, $username, $password, $options);
$st->closeCursor();
} catch (PDOException $e) { $st = $this->conn->query("SELECT * FROM " . $this->table['config'] . " LIMIT 1;");
Settings::handle_error($this, $e); $st->fetch();
} $st->closeCursor();
if ($driver === 'pgsql') { } catch (PDOException $e) {
$this->conn->query("SET NAMES 'UTF8';"); Settings::handle_error($this, $e);
} }
if ($driver === 'pgsql') {
$this->conn->query("SET NAMES 'UTF8';");
} }
} }
@ -275,4 +271,18 @@ final class Settings {
die("Assertion failed: gmstrftime(\"%Y-%m-%d %H:%M\",0) != \"1970-01-01 00:00\"<br>Actual result: $testdump"); die("Assertion failed: gmstrftime(\"%Y-%m-%d %H:%M\",0) != \"1970-01-01 00:00\"<br>Actual result: $testdump");
} }
} }
protected function init_tables() {
$table_prefix = $this->table_prefix;
// Internal table names, do not translate.
$this->table = [
'bans' => "${table_prefix}bans",
'mutes' => "${table_prefix}mutes",
'warnings' => "${table_prefix}warnings",
'kicks' => "${table_prefix}kicks",
'history' => "${table_prefix}history",
'servers' => "${table_prefix}servers",
'config' => "${table_prefix}config",
];
}
} }

View File

@ -0,0 +1,15 @@
<?php
class EnvSettings extends Settings {
public function __construct($connect = true) {
parent::__construct(false);
$this->database = getenv("MYSQL_DATABASE");
$this->username = getenv("MYSQL_USERNAME");
$this->password = getenv("MYSQL_PASSWORD");
$this->table_prefix = getenv("LITEBANS_TABLE_PREFIX");
$this->init_tables();
if ($connect) $this->connect();
}
}

33
inc/test/php/PageTest.php Normal file
View File

@ -0,0 +1,33 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
include_once './inc/page.php';
final class PageTest extends TestCase {
public function testBansPagerHTML(): void {
$page = new Page("bans", false, false);
$pager = $page->generate_pager(10);
$this->assertIsArray($pager);
$this->assertCount(3, $pager);
$this->assertEquals('<div class="litebans-pager litebans-pager-left litebans-pager-inactive">«</div>', $pager["prev"]);
$this->assertEquals('<a href="bans.php?page=2"><div class="litebans-pager litebans-pager-right litebans-pager-active">»</div></a>', $pager["next"]);
$this->assertEquals('<div><div class="litebans-pager-number">Page 1/2</div></div>', $pager["count"]);
}
public function testHistoryPagerHTML(): void {
$page = new Page("test", false);
foreach (explode("\n", file_get_contents("./inc/test/php/test_setup.sql")) as $query) {
if (strlen($query) > 0) {
$page->conn->query($query);
}
}
$_GET = ["uuid" => "2ccd0bb281214361803a945b8f0644ab"];
ob_start();
require_once './history.php';
$output = ob_get_clean();
$historyPagerAssertion = '<div class="litebans-pager litebans-pager-left litebans-pager-inactive">«</div><div class="litebans-pager litebans-pager-right litebans-pager-inactive">»</div><div><div class="litebans-pager-number">Page 1/1</div></div>';
$this->assertStringContainsString($historyPagerAssertion, $output);
}
}

View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS test_bans(id SERIAL,uuid VARCHAR(36),ip VARCHAR(45),reason varchar(2048) NOT NULL,banned_by_uuid VARCHAR(36),banned_by_name VARCHAR(128),removed_by_uuid VARCHAR(36),removed_by_name VARCHAR(128),removed_by_date TIMESTAMP,time BIGINT NOT NULL,until BIGINT NOT NULL,server_scope VARCHAR(32),server_origin VARCHAR(32),silent BIT NOT NULL,ipban BIT NOT NULL,ipban_wildcard BIT NOT NULL,active BIT NOT NULL ,PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS test_mutes(id SERIAL,uuid VARCHAR(36),ip VARCHAR(45),reason varchar(2048) NOT NULL,banned_by_uuid VARCHAR(36),banned_by_name VARCHAR(128),removed_by_uuid VARCHAR(36),removed_by_name VARCHAR(128),removed_by_date TIMESTAMP,time BIGINT NOT NULL,until BIGINT NOT NULL,server_scope VARCHAR(32),server_origin VARCHAR(32),silent BIT NOT NULL,ipban BIT NOT NULL,ipban_wildcard BIT NOT NULL,active BIT NOT NULL ,PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS test_warnings(id SERIAL,uuid VARCHAR(36),ip VARCHAR(45),reason varchar(2048) NOT NULL,banned_by_uuid VARCHAR(36),banned_by_name VARCHAR(128),removed_by_uuid VARCHAR(36),removed_by_name VARCHAR(128),removed_by_date TIMESTAMP,time BIGINT NOT NULL,until BIGINT NOT NULL,server_scope VARCHAR(32),server_origin VARCHAR(32),silent BIT NOT NULL,ipban BIT NOT NULL,ipban_wildcard BIT NOT NULL,active BIT NOT NULL ,warned BIT NOT NULL ,PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS test_kicks(id SERIAL,uuid VARCHAR(36),ip VARCHAR(45),reason varchar(2048) NOT NULL,banned_by_uuid VARCHAR(36),banned_by_name VARCHAR(128),time BIGINT NOT NULL,until BIGINT NOT NULL,server_scope VARCHAR(32),server_origin VARCHAR(32),silent BIT NOT NULL,ipban BIT NOT NULL,ipban_wildcard BIT NOT NULL,active BIT NOT NULL ,PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS test_history(id SERIAL,date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,name varchar(16) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,uuid varchar(36) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,ip varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS test_config(id SERIAL,version varchar(128)CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,build varchar(128)CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,timezone varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT '+00:00',PRIMARY KEY (id))ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_unicode_ci;
INSERT INTO test_history(name,uuid,ip)VALUES('Ruan','2ccd0bb2-8121-4361-803a-945b8f0644ab','#');
INSERT INTO test_bans(uuid,ip,reason,banned_by_uuid,banned_by_name,time,until,server_scope,server_origin,silent,ipban,ipban_wildcard,active)VALUES('2ccd0bb2-8121-4361-803a-945b8f0644ab','#','.','CONSOLE','Console',1596821141202,-1,'*',NULL,0,0,0,1);