added forgotten files
authorSven Hecht <sven@sven-laptop.(none)>
Fri, 19 Jun 2009 23:45:26 +0000 (01:45 +0200)
committerSven Hecht <sven@sven-laptop.(none)>
Fri, 19 Jun 2009 23:45:26 +0000 (01:45 +0200)
web/php/Communication.php [new file with mode: 0644]
web/php/Controller.php [new file with mode: 0644]
web/php/Database.php [new file with mode: 0644]
web/php/Plant.php [new file with mode: 0644]
web/php/Response.php [new file with mode: 0644]
web/php/Server.php [new file with mode: 0644]
web/php/User.php [new file with mode: 0644]
web/php/tests/BuisnessLogicTest.php [new file with mode: 0644]
web/php/tests/DatabaseTest.php [new file with mode: 0644]
web/php/tests/all_tests.php [new file with mode: 0644]

diff --git a/web/php/Communication.php b/web/php/Communication.php
new file mode 100644 (file)
index 0000000..b150df4
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+       header("Content-type: text/html; charset=utf-8");
+
+       session_start();
+       if (get_magic_quotes_gpc()) { $_POST = array_map( 'stripslashes', $_POST ); }
+       
+       include("Plant.php");
+       include("User.php");
+       include("Controller.php");
+       include("Database.php");
+
+       function main()
+       {
+               $controller = new Controller();
+
+               $response = null;
+               
+               switch ($_POST["cmd"]) {
+                       case "RPC":
+                               $username = $_POST["user"];
+                               $pw = $_POST["pw"];
+                               $plantname = $_POST["plant"];
+                               $code = $_POST["code"];
+                               $response = $controller->HandleRemoteProcedureCall($_POST["func"], $username, $pw, $plantname, $code);
+                               break;
+
+                       case "ContentRequest":
+                               if ($controller->IsLoggedIn() != "false") {
+                                       $response = new ContentMessage($_POST["content"]);
+                               } else {
+                                       $func = "function() { this.showLoginDialog(); this.showMessage('Sie sind nicht eingeloggt bitte einloggen', 'error'); }";
+                                       $response = new RemoteProcedureCall($func);
+                               }
+                               break;
+                       
+                       default:
+                               $response = new Message('error', 'unknown Command');
+                               break;
+               }
+               
+               if ($response != null) {
+                       $response->send();
+               } else {
+                       echo "Error! no response was generated";
+               }
+       }
+
+       main();
+
+?>
diff --git a/web/php/Controller.php b/web/php/Controller.php
new file mode 100644 (file)
index 0000000..9474942
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+       include("Response.php");
+       include("Server.php");
+       
+       class Controller
+       {
+               protected $m_Database;
+               protected $m_Server;
+               protected $m_User;
+               protected $m_IsLoggedIn;
+
+               public function __construct() {
+                       $this->m_Database = new Database();
+                       $this->m_Server = new Server($this);
+                       
+                       if (isset($_SESSION['user'])) {
+                               $this->m_User = $this->m_Database->GetUser($_SESSION['user']);
+                               $this->m_IsLoggedIn = $this->m_Server->IsLoggedIn();
+                       }
+               }
+               
+               public function GetDatabase() { return $this->m_Database; }
+               public function GetUser() { return $this->m_User; }
+               public function SetUser($user) { $this->m_User = $user; }
+               public function IsLoggedIn() { $this->m_Server->IsLoggedIn(); }
+
+               private function CreatePlant($plantname, $code) {
+                       $res = "{ success: false, msg: 'Unbekannter Fehler' }";
+                       
+                       if (isset($this->m_User)) {
+                               if ($plantname != null) {
+                                       if ($this->m_User->CreatePlant($plantname, $code) == "true") {
+                                               $res = "{ success: true, msg: 'Es wurde eine neue Pflanze für sie angelegt.' }";
+                                       } else {
+                                               $res = "{ success: false, msg: 'Pflanze konnte nicht erstellt werden.' }";
+                                       }
+                               } else {
+                                       $res = "{ success: false, msg: 'Kein Name wurde übergeben:' }";
+                               }
+                       } else {
+                               $res = "{ success: false, msg: 'Sie sind nicht angemeldet' }";
+                       }
+                       
+                       return $res;
+               }
+
+               public function HandleRemoteProcedureCall($func, $username, $pw, $plantname, $code) {
+                       $plant = null;
+                       if ($this->m_User == null) {
+                               $this->m_User = $this->m_Database->GetUser($username);
+                       }
+                       if ($this->m_User != null) {
+                               $plant = $this->m_User->GetPlant($plantname);
+                       }
+                       
+                       $res = "";
+
+                       switch ($func) {
+                               case "IsLoggedIn":
+                                       $res = $this->m_Server->IsLoggedIn();
+                                       break;
+
+                               case "Auth":
+                                       $res = $this->m_Server->LogIn($username, $pw);
+                                       break;
+
+                               case "Logout":
+                                       $res = $this->m_Server->LogOut();
+                                       break;
+
+                               case "Register":
+                                       $res = $this->m_Server->Register($username, $pw);
+                                       break;
+
+                               case "SavePlant":
+                                       if ($plant != null) {
+                                               $res = $plant->Save($code);
+                                       } else {
+                                       $res = $this->CreatePlant($plantname, $code);
+                                       }
+                                       break;
+
+                               case "CreatePlant":
+                                       $res = $this->CreatePlant($plantname, "");
+                                       break;
+
+                               case "GetPlantList":
+                                       if (isset($this->m_User)) {
+                                               $res = $this->m_User->GetPlantList();
+                                       } else {
+                                               $res = "{ success: false, msg: 'Sie sind nicht angemeldet' }";
+                                       }
+                                       break;
+
+                               case "DeletePlant":
+                                       if ($plant != null) {
+                                               $res = $plant->Delete();
+                                       } else {
+                                               $res = "{ success: false, msg: \"Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.\" }";
+                                       }
+                                       break;
+
+                               case "TestPlant":
+                                       break;
+
+                               case "ValidatePlant":
+                                       if ($plant != null) {
+                                               $res = $plant->Validate();
+                                       } else {
+                                               $res = "{ success: false, msg: 'Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.' }";
+                                       }
+                                       break;
+                       }
+               
+                       return new RPCAnswer($func, $res);
+               }
+       }
+?>
diff --git a/web/php/Database.php b/web/php/Database.php
new file mode 100644 (file)
index 0000000..578e21c
--- /dev/null
@@ -0,0 +1,203 @@
+<?php
+       
+       class DataBase
+       {
+               public $m_Connection;
+
+               public function __construct() {
+                       $this->m_Connection = @new mysqli( 'localhost', 'lseed', 'GCp:rtAaN8nwTFMP', 'lseed' );
+                       if (mysqli_connect_errno() != 0) {
+                               die( "Argh what did you do?->SERVERERR=0, ".mysqli_connect_error());
+                       }
+               }
+               
+               public function Clear() {
+                       $res1 = $this->m_Connection->query("DELETE FROM user");
+                       $res2 = $this->m_Connection->query("DELETE FROM plant");
+                       return $res1 && $res2;
+               }
+               public function Close() {
+                       $this->m_Connection->close();
+               }
+
+               public function CreateUser($user, $md5pw) {
+                       $result = false;
+
+                       $stmt = $this->m_Connection->prepare("INSERT INTO user (Name, Password, IsAdmin) VALUES (?, ?, ?)");
+                       
+                       if ($stmt) {
+                               $isadmin = false;
+                               $stmt->bind_param("ssb", $user, $md5pw, $isadmin);
+                               $stmt->execute();
+
+                               if ($stmt->affected_rows == 1) {
+                                       $result = true;
+                               }
+                               
+                               $stmt->close();
+                       } else {
+                               die("You little Nerd. How could you?");
+                       }
+
+                       return $result;
+               }
+               public function GetUser($user) {
+                       $result = null;
+                       
+                       $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin FROM user WHERE Name=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("s", $user);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $name, $pw, $isadmin);
+
+                               if ($stmt->fetch()) {
+                                       $result = new User($id, $name, $pw, $isadmin, $this);
+                               }
+                               $stmt->close();
+                       } else {
+                               die("You silly bastard. Not again!!!!");
+                       }
+
+                       return $result;
+               }
+               public function GetUserByID($userid) {
+                       $result = null;
+                       
+                       $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin FROM user WHERE ID=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("d", $userid);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $name, $pw, $isadmin);
+
+                               if ($stmt->fetch()) {
+                                       $result = new User($id, $name, $pw, $isadmin, $this);
+                               }
+                               $stmt->close();
+                       } else {
+                               die("AAARRRRRRR!!!!");
+                       }
+
+                       return $result;
+               }
+               public function InsertNewPlant($userid, $name, $code) {
+                       $result = false;
+                       
+                       $stmt = $this->m_Connection->prepare("INSERT INTO plant (UserID, Name, Code) VALUES (?, ?, ?)");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("dss", $userid, $name, $code);
+                               $stmt->execute();
+                               
+                               if ($stmt->affected_rows == 1) {
+                                       $result = true;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("DAAAAAAMN.");
+                       }
+                       
+                       return $result;
+               }
+               public function GetPlant($userid, $name) {
+                       $result = null;
+
+                       $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=? AND Name=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("ds", $userid, $name);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $theuserid, $thename, $code);
+                               
+                               if ($stmt->fetch()) {
+                                       $result = new Plant($id, $theuserid, $thename, $code, $this);
+                               } else {
+                                       //echo "nope no plant like that found.";
+                               }
+                               $stmt->close();
+                       } else {
+                               die("the server you requested is currently unavailable. Please engage in nose picking...");
+                       }
+                       return $result;
+               }
+               public function GetPlantByID($userid, $plantid) {
+                       $result = null;
+
+                       $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=? AND ID=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("ds", $userid, $plantid);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $theuserid, $thename, $code);
+                               
+                               if ($stmt->fetch()) {
+                                       $result = new Plant($id, $theuserid, $thename, $code, $this);
+                               } else {
+                                       //echo "nope no plant like that found.";
+                               }
+                               $stmt->close();
+                       } else {
+                               die("the server you requested is currently unavailable. Please engage in nose picking...");
+                       }
+                       return $result;
+               }
+               public function UpdatePlant($plant) {
+                       $result = false;
+                       
+                       $stmt = $this->m_Connection->prepare("UPDATE plant SET Code=? WHERE ID=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("sd", $plant->Code, $plant->ID);
+                               $stmt->execute();
+
+                               if ($stmt->affected_rows == 1) {
+                                       $result = true;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("Look there, behind you. A three-headed monkey!");
+                       }
+                       
+                       return $result;
+               }
+               public function DropPlant($plant) {
+                       $result = false;
+                       
+                       $stmt = $this->m_Connection->prepare("DELETE FROM plant WHERE ID=?");
+                       if ($stmt) {
+                               $stmt->bind_param("d", $plant->ID);
+                               $stmt->execute();
+
+                               if ($stmt->affected_rows == 1) {
+                                       $result = true;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("Au man, that hurt.");
+                       }
+
+                       return $result;
+               }
+               public function GetPlantsForUser($userid) {
+                       $result = array();
+                       
+                       $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=?");
+                       if ($stmt) {
+                               $stmt->bind_param("d", $userid);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $userid, $name, $code);
+
+                               while ($stmt->fetch()) {
+                                       $plant = new Plant($id, $userid, $name, $code, $this);
+                                       $result[] = $plant;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("None of that young lady!");
+                       }
+                       
+                       return $result;
+               }
+       }
+?>
diff --git a/web/php/Plant.php b/web/php/Plant.php
new file mode 100644 (file)
index 0000000..d149be9
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+       
+       class Plant
+       {
+           public $ID;
+           public $UserID;
+           public $Name;
+           public $Code;
+               public $m_Database;
+
+               public function __construct($id, $userid, $name, $code, $database) {
+                       $this->ID = $id;
+                       $this->UserID = $userid;
+                       $this->Name = $name;
+                       $this->Code = $code;
+                       $this->m_Database = $database;
+               }
+
+               public function ToJson() {
+                       return "{ ID: " . $this->ID . ", Name: '" . $this->Name . "', Code: '" . $this->Code . "' }";
+               }
+
+               public function ToJsonArray() {
+                       return "[ " . $this->ID . ", '" . $this->Name . "', '" . $this->Code . "' ]";
+               }
+
+               public function Save($code) {
+                       $result = "{ success: true, msg: '' }";
+                       
+                       $this->Code = $code;
+
+                       $user = $this->m_Database->GetUserByID($this->UserID);
+                       if ($user) {
+                               $plant = $this->m_Database->GetPlant($user->ID, $this->Name);
+                               
+                               if ($plant == null) {
+                                       if (!$this->m_Database->InsertNewPlant($user->ID, $this->Name, $this->Code)) {
+                                               $result = "{ success: false, msg: 'Pflanze konnte nicht erstellt werden.' }";
+                                       }
+                               } else {
+                                       $plant->Code = $code;
+                                       if (!$this->m_Database->UpdatePlant($plant)) {
+                                               $result = "{ success: false, msg: 'Pflanze konnte nicht aktualisiert werden.' }";
+                                       }
+                               }
+                       } else {
+                               $result = "{ success: false, msg: 'User (id: \'".$this->UserID."\' could not be found.' }";
+                       }
+
+                       return $result;
+               }
+
+               public function Delete() {
+                       $result = "{ success: false, msg: 'Pflanze konnte nicht gelöscht werden.' }";
+
+                       if ($this->m_Database->DropPlant($this)) {
+                               $result = "{ success: true, msg: '' }";
+                       }
+
+                       return $result;
+               }
+               
+               public function Validate() {
+                       return $this->ValidateCode();
+               }
+               
+               public function ValidateCode() {
+                       $result = "{valid: false, line: 0, column: 0, msg: 'Interner Server Fehler'}";
+                       
+                       $descriptorspec = array(
+                          0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
+                          1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
+                          2 => array("pipe", "w")   // STDERR
+                       );
+
+                       $cwd = realpath("..\\cgi");
+
+
+                       $process = proc_open('validate.exe', $descriptorspec, $pipes, $cwd, array());
+
+                       if (is_resource($process)) {
+                               // $pipes sieht nun so aus:
+                               // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
+                               // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
+                               // Jedwede Fehlerausgaben werden an /tmp/error-output.txt angefügt
+
+                               fwrite($pipes[0], $this->Code);
+                               fclose($pipes[0]);
+
+                               $output = stream_get_contents($pipes[1]);
+                               fclose($pipes[1]);
+
+                               //echo stream_get_contents($pipes[2]);
+                               fclose($pipes[2]);
+
+                               $return_value = proc_close($process);
+                               
+                               //echo $return_value;
+                               if ($return_value == 0) {
+                                       $result = $output;
+                               }
+                       }
+
+                       return $result;
+               }
+       }
+?>
diff --git a/web/php/Response.php b/web/php/Response.php
new file mode 100644 (file)
index 0000000..18809c8
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+       
+       class Response
+       {
+               public $m_Command;
+               
+               public function __construct($cmd) {
+                       $this->m_Command = $cmd;
+               }
+               
+               public function send() {
+                       echo "{ cmd: '".$this->m_Command."' }";
+               }
+       }
+       
+       class Message extends Response
+       {
+               public $m_Type;
+               public $m_Message;
+               
+               public function __construct($type, $msg) {
+                       parent::__construct("Message");
+                       $this->m_Type = $type;
+                       $this->m_Message = $msg;
+               }
+               
+               public function send() {
+                       echo "{ cmd: '".$this->m_Command."', type: '".$this->m_Type."', msg: '".$this->m_Message."' }";
+               }
+       }
+       
+       class ContentMessage extends Response
+       {
+               public $m_Type;
+               public $m_ContentName;
+               
+               public function __construct($name) {
+                       parent::__construct("Content");
+                       $this->m_ContentName = $name;
+               }
+               
+               public function send() {
+                       echo "{ cmd: '".$this->m_Command."', contentname: '".$this->m_ContentName."', content: ";
+                       readfile( "page/".$this->m_ContentName.".pg" );
+                       echo "}";
+               }
+       }
+       
+       class RemoteProcedureCall extends Response
+       {
+               public $m_Function;
+               
+               public function __construct($func) {
+                       parent::__construct("RPC");
+                       $this->m_Function = $func;
+               }
+               
+               public function send() {
+                       echo "{ cmd: '".$this->m_Command."', func: '".$this->m_Function."' }";
+               }
+       }
+       
+       class RPCAnswer extends Response
+       {
+               public $m_CalledFunction;
+               public $m_Data;
+               
+               public function __construct($calledFunc, $data) {
+                       parent::__construct("RPC-Response");
+                       $this->m_CalledFunction = $calledFunc;
+                       $data = str_replace("\r", "\\r", $data);
+                       $data = str_replace("\n", "\\n", $data);
+                       $data = str_replace("\t", "\\t", $data);
+                       //TODO escape further
+                       $this->m_Data = $data;
+               }
+               
+               public function send() {
+                       echo "{ cmd: '".$this->m_Command."', calledFunc: '".$this->m_CalledFunction."', data: ".$this->m_Data."}";
+               }
+       }
+
+?>
diff --git a/web/php/Server.php b/web/php/Server.php
new file mode 100644 (file)
index 0000000..462545b
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+       class Server
+       {
+               protected $m_Controller;
+               
+               public function __construct($controller) {
+                       $this->m_Controller = $controller;
+               }
+
+               public function ReturnContent($content) {
+                       switch ($content) {
+                               case "myplants":
+                               case "createplant":
+                               case "testplant":
+                                       new ContentMessage($content).send();
+                                       break;
+                               case "nav":
+                                       if ($this->IsAdmin() != "false") {
+                                               new ContentMessage("adminnav").send();
+                                       } else {
+                                               new ContentMessage($content).send();
+                                       }
+                               break;
+                               case "debug":
+                                       if ($this->IsAdmin() != "false") {
+                                               new ContentMessage($content).send();
+                                       }
+                               break;
+                       }
+               }
+
+               public function IsLoggedIn() {
+                       $result = "false";
+                       
+                       if (isset($_SESSION['user']) and isset($_SESSION['pw'])) {
+                               $user = $_SESSION['user'];
+                               $md5pw = $_SESSION['pw'];
+
+                               $userobj = $this->m_Controller->GetDatabase()->GetUser($user);
+                               if ($userobj != null and $userobj->Password == $md5pw) {
+                                       $result = "true";
+                               }
+                       }
+
+                       return $result;
+               }
+
+               public function IsAdmin() {
+                       $result = "false";
+                       
+                       $user = $controller->GetUser();
+                       if ($user != null) {
+                               if ($user->IsAdmin) {
+                                       $result = "true";
+                               }
+                       }
+
+                       return $result;
+               }
+
+               public function LogIn($username, $md5pw) {
+                       $result = "false";
+                       $user = $this->m_Controller->GetDatabase()->GetUser($username);
+                       
+                       if ($user != null and $user->Name == $username and $user->Password == $md5pw) {
+                               $_SESSION['user'] = $user->Name;
+                               $_SESSION['pw'] = $md5pw;
+
+                               $result = "true";
+                       }
+
+                       return $result;
+               }
+
+               public function LogOut() {
+                       $_SESSION['user'] = "";
+                       $_SESSION['pw'] = "";
+                       $user = $this->m_Controller->SetUser(null);
+                       return "true";
+               }
+
+               public function Register($username, $md5pw) {
+                       $result = "{ success: true, msg: '' }";
+                       
+                       $userobj = $this->m_Controller->GetDatabase()->GetUser($username);
+
+                       if ($userobj == null) {
+                               if (!$this->m_Controller->GetDatabase()->CreateUser($username, $md5pw)) {
+                                       $result = "{ success: false, msg: 'Benutzer konnte nicht erstellt werden.' }";
+                               } else {
+                                       if ($this->LogIn($username, $md5pw) == "true") {
+                                               $result = "{ success: true, msg: 'Benutzer erstellt und erfolgreich angemeldet.' }";
+                                       } else {
+                                               $result = "{ success: true, msg: 'Benutzer erstellt, die Anmeldung schlug jedoch fehl.' }";
+                                       }
+                               }
+                       } else {
+                               $result = "{ success: false, msg: 'Benutzername bereits vergeben.' }";
+                       }
+
+                       return $result;
+               }
+       }
+?>
diff --git a/web/php/User.php b/web/php/User.php
new file mode 100644 (file)
index 0000000..c370be2
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+       
+       class User
+       {
+           public $ID;
+           public $Name;
+           public $Password;
+           public $IsAdmin;
+           public $m_Database;
+
+               public function __construct($id, $user, $md5pw, $isadmin, $database) {
+                       $this->ID = $id;
+                       $this->Name = $user;
+                       $this->Password = $md5pw;
+                       $this->IsAdmin = $isadmin;
+                       $this->m_Database = $database;
+               }
+               
+               public function GetPlantList() {
+                       $result = "{ list: [] }";
+
+                       $list = $this->m_Database->GetPlantsForUser($this->ID);
+
+                       if (count($list) > 0) {
+                               $result = "{ list: [";
+                               $first = true;
+                               foreach ($list as $plant) {
+                                       if (!$first) {
+                                               $result .= ", ";
+                                       }
+                                       //$result .= "{ ID: " . $plant->ID . ", Name: '" . $plant->Name . "', Code: '" . $plant->Code . "' }";
+                                       $result .= $plant->ToJson();
+                                       $first = false;
+                               }
+                               $result .= "] }";
+                       }
+
+                       return $result;
+               }
+               
+               public function GetPlantById($id) {
+                       $result = null;
+                       
+                       $list = $this->m_Database->GetPlantsForUser($this->ID);
+
+                       foreach ($list as $plant) {
+                               if ($plant->ID == $id) {
+                                       $result = $plant;
+                                       break;
+                               }
+                       }
+                       
+                       return $result;
+               }
+               
+               public function CreatePlant($name, $code) {
+                       $result = "false";
+                       
+                       $res = $this->m_Database->InsertNewPlant($this->ID, $name, $code);
+                       if ($res) {
+                               $result = "true";
+                       }
+                       
+                       return $result;
+               }
+               
+               public function GetPlant($name) {
+                       return $this->m_Database->GetPlant($this->ID, $name);
+               }
+       }
+?>
diff --git a/web/php/tests/BuisnessLogicTest.php b/web/php/tests/BuisnessLogicTest.php
new file mode 100644 (file)
index 0000000..5edf43b
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+       include("../User.php");
+       include("../Plant.php");
+       include("../Controller.php");
+
+       class TestOfBuisnessLogic extends UnitTestCase
+       {
+               private $m_Controller;
+               
+               function __construct() {
+               }
+               
+               function setUp() {
+                       $this->m_Controller = new Controller();
+                       $db = new DataBase();
+                       $db->Clear();
+                       $db->Close();
+               }
+               
+               function tearDown() {
+                       $db = new DataBase();
+                       $db->Clear();
+                       $db->Close();
+                       unset($_SESSION['user']);
+                       unset($_SESSION['pw']);
+               }
+               
+               function validateRPCResponseWithoutData($res, $func) {
+                       $this->assertTrue($res != null, "No Result given");
+                       $this->assertTrue($res->m_Command == "RPC-Response", "Wrong Commandtype");
+                       $this->assertTrue($res->m_CalledFunction == $func, "Wrong CalledFunction");
+               }
+               function validateRPCResponse($res, $func, $data) {
+                       $this->validateRPCResponseWithoutData($res, $func);
+                       $this->assertTrue($res->m_Data == $data, "'".$res->m_Data."' was unexpected, '".$data."' expected");
+               }
+               function testAuthentication() {
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
+                       $this->validateRPCResponse($res, "IsLoggedIn", "false");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
+                       $this->validateRPCResponse($res, "Register", "{ success: true, msg: 'Benutzer erstellt und erfolgreich angemeldet.' }");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
+                       $this->validateRPCResponse($res, "IsLoggedIn", "true");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Logout", "", "", "", "");
+                       $this->validateRPCResponse($res, "Logout", "true");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
+                       $this->validateRPCResponse($res, "IsLoggedIn", "false");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
+                       $this->validateRPCResponse($res, "Auth", "true");
+               }
+               
+               function testPlantCreation() {
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "", "");
+                       $this->validateRPCResponse($res, "CreatePlant", "{ success: false, msg: 'Sie sind nicht angemeldet' }");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "");
+                       $this->validateRPCResponse($res, "CreatePlant", "{ success: true, msg: 'Es wurde eine neue Pflanze für sie angelegt.' }");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("GetPlantList", "", "", "", "");
+                       $this->validateRPCResponseWithoutData($res, "GetPlantList");
+                       $expectedEnd = "Name: 'newplant', Code: '' }] }";
+                       substr($res->m_Data, -count($expectedEnd), 0);
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("SavePlant", "test", "", "newplant", "a b c");
+                       $this->validateRPCResponse($res, "SavePlant", "{ success: true, msg: '' }");
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("GetPlantList", "", "", "", "");
+                       $this->validateRPCResponseWithoutData($res, "GetPlantList");
+                       $expectedEnd = "Name: 'newplant', Code: 'a b c' }] }";
+                       substr($res->m_Data, -count($expectedEnd), 0);
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("DeletePlant", "test", "", "newplant", "");
+                       $this->validateRPCResponse($res, "DeletePlant", "{ success: true, msg: '' }");
+               }
+               
+               function testValidation() {
+                       
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "RULE GROW BY 1");
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("ValidatePlant", "test", "", "newplant", "");
+                       $this->validateRPCResponse($res, "ValidatePlant", "{ success: true, msg: '' }");
+                       
+               }
+       }
+?>
diff --git a/web/php/tests/DatabaseTest.php b/web/php/tests/DatabaseTest.php
new file mode 100644 (file)
index 0000000..00ac4cd
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+       include("../Database.php");
+
+       class TestOfDataBase extends UnitTestCase
+       {
+               private $m_Database;
+               
+               function __construct() {
+               }
+               
+               function setUp() {
+                       $this->m_Database = new DataBase();
+                       $this->m_Database->Clear();
+               }
+               
+               function tearDown() {
+                       $this->m_Database->Clear();
+                       $this->m_Database->Close();
+               }
+               
+               function testClear() {
+                       $username = "argletam";
+                       $this->m_Database->CreateUser($username, "42aa");
+                       $user = $this->m_Database->GetUser($username);
+                       $userid = $user->ID;
+                       
+                       $res = $this->m_Database->Clear();
+                       $this->assertTrue($res, "Clear failed");
+                       
+                       $user = $this->m_Database->GetUser($username);
+                       $this->assertTrue($user == null, "A User was found even though the database was cleared");
+                       
+                       $plants = $this->m_Database->GetPlantsForUser($userid);
+                       $this->assertTrue(count($user) == 0, "Even though the database was cleared we found plants for a user");
+               }
+               
+               function testUser() {
+                       $username = "testuser";
+                       
+                       $result = $this->m_Database->CreateUser($username, "test");
+                       $this->assertTrue($result);
+                       
+                       $user = $this->m_Database->GetUser($username);
+                       $this->assertTrue($user != null);
+                       
+                       $user = $this->m_Database->GetUser("a");
+                       $this->assertTrue($user == null);
+                       
+                       $user = $this->m_Database->GetUser(null);
+                       $this->assertTrue($user == null);
+               }
+               
+               function testPlant() {
+                       $username = "testuser";
+                       
+                       $result = $this->m_Database->CreateUser($username, "test");
+                       $user = $this->m_Database->GetUser($username);
+                       
+                       $data_name = "myplant";
+                       $data_code = "my fancy plant code!";
+                       
+                       $result = $this->m_Database->InsertNewPlant($user->ID, $data_name, $data_code);
+                       $this->assertTrue($result);
+                       
+                       $plant = $this->m_Database->GetPlant($user->ID, $data_name);
+                       $this->assertTrue($plant != null);
+                       $this->assertTrue($plant->UserID == $user->ID);
+                       $this->assertTrue($plant->Name == $data_name);
+                       $this->assertTrue($plant->Code == $data_code);
+                       
+                       $plants = $this->m_Database->GetPlantsForUser($user->ID);
+                       $this->assertTrue($plants != null);
+                       $this->assertTrue(count($plants) == 1);
+                       $this->assertTrue($plants[0]->ID == $plant->ID);
+                       
+                       $data_code = $plant->Code = "My New and improved Plantcode. Featuring: even fancier code!";
+                       $result = $this->m_Database->UpdatePlant($plant);
+                       $this->assertTrue($result);
+                       
+                       $plant = $this->m_Database->GetPlant($user->ID, $data_name);
+                       $this->assertTrue($plant != null);
+                       $this->assertTrue($plant->UserID == $user->ID);
+                       $this->assertTrue($plant->Name == $data_name);
+                       $this->assertTrue($plant->Code == $data_code);
+                       
+                       $result = $this->m_Database->DropPlant($plant);
+                       $this->assertTrue($result);
+                       
+                       $plant = $this->m_Database->GetPlant($user->ID, $data_name);
+                       $this->assertTrue($plant == null);
+               }
+       }
+
+?>
diff --git a/web/php/tests/all_tests.php b/web/php/tests/all_tests.php
new file mode 100644 (file)
index 0000000..97fd736
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+       require_once('simpletest/unit_tester.php');
+       require_once('simpletest/reporter.php');
+
+       $test = &new GroupTest('All tests');
+       $test->addTestFile('DatabaseTest.php');
+       $test->addTestFile('BuisnessLogicTest.php');
+       $test->run(new HtmlReporter());
+
+?>