diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 241168a..d66bf2d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -24,19 +24,20 @@ before_script: # Install and run Composer - curl -sS https://getcomposer.org/installer | php - 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 # See http://docs.gitlab.com/ce/ci/services/README.html for examples. -services: [] -# - mysql:5.7 +services: + - mariadb:latest # Set any variables we need variables: COMPOSER: 'inc/test/composer.json' # Configure mysql environment variables (https://hub.docker.com/r/_/mysql/) 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 # If Xdebug was installed you can generate a coverage report and see code coverage metrics. diff --git a/inc/page.php b/inc/page.php index bf1e5ee..a181e27 100644 --- a/inc/page.php +++ b/inc/page.php @@ -2,10 +2,14 @@ require_once './inc/init.php'; class Page { - public function __construct($name, $header = true) { + public function __construct($name, $header = true, $connect = true) { ini_set('default_charset', 'utf-8'); 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); require_once './lang/en_US.utf8.php'; @@ -25,7 +29,7 @@ class Page { } $this->conn = $settings->conn; $this->settings = $settings; - $this->uuid_name_cache = array(); + $this->uuid_name_cache = []; $this->name = $name; @@ -36,30 +40,30 @@ class Page { $info = $this->type_info($name); $this->set_info($info); - $this->permanent = array( + $this->permanent = [ 'ban' => $this->t("generic.permanent.ban"), 'mute' => $this->t("generic.permanent.mute"), 'warn' => $this->t("generic.permanent"), 'kick' => null, - ); - $this->expired = array( + ]; + $this->expired = [ 'ban' => $this->t("page.expired.ban"), 'mute' => $this->t("page.expired.mute"), 'warn' => $this->t("page.expired.warning"), 'kick' => null, - ); - $this->expired_by = array( + ]; + $this->expired_by = [ 'ban' => $this->t("page.expired.ban-by"), 'mute' => $this->t("page.expired.mute-by"), 'warn' => $this->t("page.expired.warning"), 'kick' => null, - ); - $this->punished_by = array( + ]; + $this->punished_by = [ 'ban' => $this->t("generic.banned.by"), 'mute' => $this->t("generic.muted.by"), 'warn' => $this->t("generic.warned.by"), 'kick' => $this->t("generic.kicked.by"), - ); + ]; $this->table_headers_printed = false; $this->args = array_values($_GET); @@ -121,43 +125,43 @@ class Page { switch ($type) { case "ban": case "bans": - return array( + return [ "type" => "ban", "table" => $settings->table['bans'], "title" => $this->t("title.bans"), "page" => "bans.php", - ); + ]; case "mute": case "mutes": - return array( + return [ "type" => "mute", "table" => $settings->table['mutes'], "title" => $this->t("title.mutes"), "page" => "mutes.php", - ); + ]; case "warn": case "warnings": - return array( + return [ "type" => "warn", "table" => $settings->table['warnings'], "title" => $this->t("title.warnings"), "page" => "warnings.php", - ); + ]; case "kick": case "kicks": - return array( + return [ "type" => "kick", "table" => $settings->table['kicks'], "title" => $this->t("title.kicks"), "page" => "kicks.php", - ); + ]; default: - return array( + return [ "type" => null, "table" => null, "title" => null, "page" => null, - ); + ]; } } @@ -199,13 +203,13 @@ class Page { return $rows; } catch (PDOException $ex) { Settings::handle_error($this->settings, $ex); - return array(); + return []; } } 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"); - $bitColumns = array("active", "ipban"); + $columns = ["id", "uuid", "reason", "banned_by_name", "banned_by_uuid", "time", "until", "server_origin", "server_scope", "active", "ipban"]; + $bitColumns = ["active", "ipban"]; if ($table === $this->settings->table['warnings']) { array_push($columns, "warned"); @@ -482,7 +486,7 @@ class Page { } if ($print_headers && !$this->table_headers_printed) { $headers = array_keys($array); - $headers_translated = array(); + $headers_translated = []; foreach ($headers as $header) { if ($header === "executor" && $this->name !== "history") { $header = $this->punished_by[$type]; @@ -554,6 +558,10 @@ class Page { function print_pager($total = -1, $args = "", $prevargs = "", $page = null, $simple = true) { 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; if ($page === null) { $page = $this->name . ".php"; @@ -599,7 +607,11 @@ class Page { $pager_next = "$pager_next"; } $pager_count = '
' . $this->t("table.pager.number") . ' ' . $cur . '/' . $pages . '
'; - echo "$pager_prev $pager_next $pager_count"; + return [ + "prev" => $pager_prev, + "next" => $pager_next, + "count" => $pager_count, + ]; } function print_footer($container_end = true) { diff --git a/inc/settings.php b/inc/settings.php index 6530d46..3c0821a 100644 --- a/inc/settings.php +++ b/inc/settings.php @@ -1,6 +1,7 @@ table_prefix; - - // 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", - ); + $this->init_tables(); if ($connect) { - $driver = $this->driver; - $host = $this->host; - $port = $this->port; - $database = $this->database; - $username = $this->username; - $password = $this->password; - if ($username === "" && $password === "") { - redirect("error/unconfigured.php"); - } + $this->connect(); + } else { + $this->conn = null; + } + } - $dsn = "$driver:dbname=$database;host=$host;port=$port"; - if ($driver === 'mysql') { - $dsn .= ';charset=utf8'; - } + protected function connect() { + $driver = $this->driver; + $host = $this->host; + $port = $this->port; + $database = $this->database; + $username = $this->username; + $password = $this->password; + if ($username === "" && $password === "") { + redirect("error/unconfigured.php"); + } - $options = array( - PDO::ATTR_TIMEOUT => 5, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_EMULATE_PREPARES => false, - PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", - ); + $dsn = "$driver:dbname=$database;host=$host;port=$port"; + if ($driver === 'mysql') { + $dsn .= ';charset=utf8'; + } - try { - $this->conn = new PDO($dsn, $username, $password, $options); + $options = array( + 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;"); - $st->fetch(); - $st->closeCursor(); - } catch (PDOException $e) { - Settings::handle_error($this, $e); - } - if ($driver === 'pgsql') { - $this->conn->query("SET NAMES 'UTF8';"); - } + try { + $this->conn = new PDO($dsn, $username, $password, $options); + + $st = $this->conn->query("SELECT * FROM " . $this->table['config'] . " LIMIT 1;"); + $st->fetch(); + $st->closeCursor(); + } catch (PDOException $e) { + 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\"
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", + ]; + } } diff --git a/inc/test/php/EnvSettings.php b/inc/test/php/EnvSettings.php new file mode 100644 index 0000000..612edb7 --- /dev/null +++ b/inc/test/php/EnvSettings.php @@ -0,0 +1,15 @@ +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(); + } +} diff --git a/inc/test/php/PageTest.php b/inc/test/php/PageTest.php new file mode 100644 index 0000000..37f9156 --- /dev/null +++ b/inc/test/php/PageTest.php @@ -0,0 +1,33 @@ +generate_pager(10); + $this->assertIsArray($pager); + $this->assertCount(3, $pager); + $this->assertEquals('
«
', $pager["prev"]); + $this->assertEquals('
»
', $pager["next"]); + $this->assertEquals('
Page 1/2
', $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 = '
«
»
Page 1/1
'; + $this->assertStringContainsString($historyPagerAssertion, $output); + } +} diff --git a/inc/test/php/test_setup.sql b/inc/test/php/test_setup.sql new file mode 100644 index 0000000..2cfc837 --- /dev/null +++ b/inc/test/php/test_setup.sql @@ -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);