From 6121e3145950d61e79cd73f3a60295ac7ff83b80 Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 19 Sep 2019 12:08:16 +0200 Subject: [PATCH] changes to how testactions work --- dev_mvc/controller/MVCController.php | 10 +- dev_mvc/controller/TestUtils.php | 21 -- dev_mvc/index.php | 2 - dev_mvc/model/testactions/TA_CreateDB.php | 182 ++++++++++++++++ dev_mvc/model/testactions/TA_PopulateDB.php | 63 ++++++ dev_mvc/model/testactions/TestAction.php | 36 ++++ dev_mvc/model/testactions/model_createdb.php | 196 ------------------ .../model/testactions/model_populatedb.php | 13 +- .../model/testactions/model_testDBUser.php | 11 + 9 files changed, 303 insertions(+), 231 deletions(-) delete mode 100644 dev_mvc/controller/TestUtils.php create mode 100644 dev_mvc/model/testactions/TA_CreateDB.php create mode 100644 dev_mvc/model/testactions/TA_PopulateDB.php create mode 100644 dev_mvc/model/testactions/TestAction.php delete mode 100644 dev_mvc/model/testactions/model_createdb.php diff --git a/dev_mvc/controller/MVCController.php b/dev_mvc/controller/MVCController.php index 13e4eb9..9ba4db2 100644 --- a/dev_mvc/controller/MVCController.php +++ b/dev_mvc/controller/MVCController.php @@ -33,7 +33,7 @@ class MVCController{ if(isset($_POST['testaction'])){ - $this->testaction = "./model/testactions/model_".$_POST['testaction'].".php"; + $this->testaction = "./model/testactions/TA_".$_POST['testaction'].".php"; } @@ -67,12 +67,10 @@ class MVCController{ //check if testaction is valid if(file_exists($this->testaction)){ - - echo('{"output": ['); //execute testaction - include_once($this->testaction); - TestUtils::log('End of output', "OK", false); - echo("]}"); + require_once($this->testaction); + $testactionClassname = "TA_".$_POST['testaction']; + $testactionInstance = new $testactionClassname(); } } diff --git a/dev_mvc/controller/TestUtils.php b/dev_mvc/controller/TestUtils.php deleted file mode 100644 index f73df2e..0000000 --- a/dev_mvc/controller/TestUtils.php +++ /dev/null @@ -1,21 +0,0 @@ -executeModel(); if(!isset($_POST['testaction'])){ include_once("./view/content_pagetemplate.php"); } - ?> \ No newline at end of file diff --git a/dev_mvc/model/testactions/TA_CreateDB.php b/dev_mvc/model/testactions/TA_CreateDB.php new file mode 100644 index 0000000..ac19fe4 --- /dev/null +++ b/dev_mvc/model/testactions/TA_CreateDB.php @@ -0,0 +1,182 @@ +query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db'"); + //db exists + if($query->fetchColumn() != 1){ + $query = $con->query("CREATE DATABASE $db"); + self::logMessage('db doesnt exist'); + } + //db doesn't exist + else{ + self::logMessage('db already exists, skipping'); + } + //select db + $con->exec("USE $db"); + //test if table exists + + $table = 'users'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + self::logMessage($query->fetchColumn()); + //table doesn't exist + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `users` ( + `ID` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(256) NOT NULL, + `email` varchar(256) NOT NULL, + `password` varchar(256) NOT NULL, + `reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `logMessagein_date` datetime NOT NULL, + `reg_ip` varchar(256) NOT NULL, + `permissions` int(11) NOT NULL DEFAULT '-1', + `active` tinyint(1) DEFAULT '0', + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + $table = 'usersessions'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `usersessions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `uid` int(11) NOT NULL, + `token` varchar(256) NOT NULL, + `expires` datetime NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + $table = 'email_activation_keys'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `email_activation_keys` ( + `id` int(16) NOT NULL AUTO_INCREMENT, + `users_id` int(16) NOT NULL, + `activationkey` varchar(256) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + $table = 'board'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `board` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `name` varchar(256) NOT NULL, + `description` text NOT NULL, + `permLevel` int(16) NOT NULL DEFAULT '0', + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + + $table = 'thread'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `thread` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `users_ID` int(16) NOT NULL, + `board_ID` int(16) NOT NULL, + `title` varchar(256) NOT NULL, + `text` text NOT NULL, + `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + $table = 'reply'; + $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); + if($query->fetchColumn() != 4){ + self::logMessage('table doesnt exist'); + $query = $con->query( + " CREATE TABLE `reply` ( + `ID` int(16) NOT NULL AUTO_INCREMENT, + `thread_ID` int(16) NOT NULL, + `users_ID` int(16) NOT NULL, + `content` text NOT NULL, + `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); + self::logMessage("created table $table"); + } + //table exists + else{ + self::logMessage("table $table already exists, skipping"); + } + + + + } + catch(PDOException $e){ + self::logMessage('PDO ERROR', "FAILURE"); + die("pdo exception, cannot connect to sql:
$e"); + } + } +} + + + + + + + + ?> \ No newline at end of file diff --git a/dev_mvc/model/testactions/TA_PopulateDB.php b/dev_mvc/model/testactions/TA_PopulateDB.php new file mode 100644 index 0000000..dbfacf5 --- /dev/null +++ b/dev_mvc/model/testactions/TA_PopulateDB.php @@ -0,0 +1,63 @@ +exec("USE $db"); + + + + self::logMessage('table doesnt exist', "OK"); + $query = $con->query("INSERT INTO users (username, email, password, login_date, reg_ip, active) VALUES ( 'andreas', 'andreas@andreas.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.2', 1), + ( 'bram', 'bram@bram.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.1', 1)"); + self::logMessage("created test users", "OK"); + $query = $con->query("INSERT INTO `board` (`name`, `description`, `permLevel`) VALUES ('General Discussion', 'Plek om algemene discussie te voeren.', '0'), + ('Off Topic', 'Voor alle irrelevante zooi.', '0')"); + self::logMessage("created test boards", "OK"); + $query = $con->query("INSERT INTO `thread` (`users_ID`, `board_ID`, `title`, `text`, `date_created`) VALUES ('1', '1', 'Test thread', 'Deze thread is een test.', '2019-06-20 13:55:37'), + ('1', '2', 'Waa', 'Frist niffo', '2019-06-20 13:56:42')"); + self::logMessage("created test threads", "OK"); + $query = $con->query("INSERT INTO `reply` (`thread_ID`, `users_ID`, `content`, `date_created`) VALUES ('1', '1', 'heehee eks dee', '2019-06-21 11:01:57'), + ('1', '1', 'hoi\r\n', '2019-06-21 11:07:25'), + ('2', '2', 'fristi niBBa', '2019-06-21 11:08:08'), + ('1', '1', 'was jouw prebleem', '2019-06-21 14:41:00'), + ('1', '2', 'Mijn naam is bram', '2019-06-21 17:58:12'), + ('1', '2', 'huh wuddufuq', '2019-06-21 17:58:29'), + ('1', '1', 'huts a neef', '2019-06-21 17:59:27')"); + self::logMessage("created test replies", "OK"); + } + catch(PDOException $e){ + self::logMessage("created test replies", "FAILURE"); + die("pdo exception, cannot connect to sql:
$e"); + } + } +} + + + + + + + + ?> \ No newline at end of file diff --git a/dev_mvc/model/testactions/TestAction.php b/dev_mvc/model/testactions/TestAction.php new file mode 100644 index 0000000..96a82ca --- /dev/null +++ b/dev_mvc/model/testactions/TestAction.php @@ -0,0 +1,36 @@ +query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db'"); - //db exists - if($query->fetchColumn() != 1){ - $query = $con->query("CREATE DATABASE $db"); - TestUtils::log('db doesnt exist'); - } - //db doesn't exist - else{ - TestUtils::log('db already exists, skipping'); - } - //select db - $con->exec("USE $db"); - //test if table exists - - $table = 'users'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - TestUtils::log($query->fetchColumn()); - //table doesn't exist - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `users` ( - `ID` int(11) NOT NULL AUTO_INCREMENT, - `username` varchar(256) NOT NULL, - `email` varchar(256) NOT NULL, - `password` varchar(256) NOT NULL, - `reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - `login_date` datetime NOT NULL, - `reg_ip` varchar(256) NOT NULL, - `permissions` int(11) NOT NULL DEFAULT '-1', - `active` tinyint(1) DEFAULT '0', - PRIMARY KEY (`ID`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - $table = 'usersessions'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `usersessions` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `uid` int(11) NOT NULL, - `token` varchar(256) NOT NULL, - `expires` datetime NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - $table = 'email_activation_keys'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `email_activation_keys` ( - `id` int(16) NOT NULL AUTO_INCREMENT, - `users_id` int(16) NOT NULL, - `activationkey` varchar(256) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - $table = 'board'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `board` ( - `ID` int(16) NOT NULL AUTO_INCREMENT, - `name` varchar(256) NOT NULL, - `description` text NOT NULL, - `permLevel` int(16) NOT NULL DEFAULT '0', - PRIMARY KEY (`ID`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - - $table = 'thread'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `thread` ( - `ID` int(16) NOT NULL AUTO_INCREMENT, - `users_ID` int(16) NOT NULL, - `board_ID` int(16) NOT NULL, - `title` varchar(256) NOT NULL, - `text` text NOT NULL, - `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`ID`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - $table = 'reply'; - $query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); - if($query->fetchColumn() != 4){ - TestUtils::log('table doesnt exist'); - $query = $con->query( - " CREATE TABLE `reply` ( - `ID` int(16) NOT NULL AUTO_INCREMENT, - `thread_ID` int(16) NOT NULL, - `users_ID` int(16) NOT NULL, - `content` text NOT NULL, - `date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`ID`) - ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"); - TestUtils::log("created table $table"); - } - //table exists - else{ - TestUtils::log("table $table already exists, skipping"); - } - - - - } - catch(PDOException $e){ - TestUtils::log('PDO ERROR', "FAILURE"); - die("pdo exception, cannot connect to sql:
$e"); - } -} - - - - - - - ?> \ No newline at end of file diff --git a/dev_mvc/model/testactions/model_populatedb.php b/dev_mvc/model/testactions/model_populatedb.php index a68f649..a6f3b14 100644 --- a/dev_mvc/model/testactions/model_populatedb.php +++ b/dev_mvc/model/testactions/model_populatedb.php @@ -4,7 +4,7 @@ if(isset($_POST['auth'])){ populateDB(); } }else{ - echol('you have no authorization to do that'); + self::logMessage('you have no authorization to do that', "OK"); } function populateDB(){ @@ -32,16 +32,16 @@ function populateDB(){ - echol('table doesnt exist'); + self::logMessage('table doesnt exist', "OK"); $query = $con->query("INSERT INTO users (username, email, password, login_date, reg_ip, active) VALUES ( 'andreas', 'andreas@andreas.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.2', 1), ( 'bram', 'bram@bram.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.1', 1)"); - echol("created test users"); + self::logMessage("created test users", "OK"); $query = $con->query("INSERT INTO `board` (`name`, `description`, `permLevel`) VALUES ('General Discussion', 'Plek om algemene discussie te voeren.', '0'), ('Off Topic', 'Voor alle irrelevante zooi.', '0')"); - echol("created test boards"); + self::logMessage("created test boards", "OK"); $query = $con->query("INSERT INTO `thread` (`users_ID`, `board_ID`, `title`, `text`, `date_created`) VALUES ('1', '1', 'Test thread', 'Deze thread is een test.', '2019-06-20 13:55:37'), ('1', '2', 'Waa', 'Frist niffo', '2019-06-20 13:56:42')"); - echol("created test threads"); + self::logMessage("created test threads", "OK"); $query = $con->query("INSERT INTO `reply` (`thread_ID`, `users_ID`, `content`, `date_created`) VALUES ('1', '1', 'heehee eks dee', '2019-06-21 11:01:57'), ('1', '1', 'hoi\r\n', '2019-06-21 11:07:25'), ('2', '2', 'fristi niBBa', '2019-06-21 11:08:08'), @@ -49,9 +49,10 @@ function populateDB(){ ('1', '2', 'Mijn naam is bram', '2019-06-21 17:58:12'), ('1', '2', 'huh wuddufuq', '2019-06-21 17:58:29'), ('1', '1', 'huts a neef', '2019-06-21 17:59:27')"); - echol("created test replies"); + self::logMessage("created test replies", "OK"); } catch(PDOException $e){ + self::logMessage("created test replies", "FAILURE"); die("pdo exception, cannot connect to sql:
$e"); } } \ No newline at end of file diff --git a/dev_mvc/model/testactions/model_testDBUser.php b/dev_mvc/model/testactions/model_testDBUser.php index e69de29..29196f9 100644 --- a/dev_mvc/model/testactions/model_testDBUser.php +++ b/dev_mvc/model/testactions/model_testDBUser.php @@ -0,0 +1,11 @@ +