302687664627761f0164d35513173d9a84284971
[L-seed.git] / web / php-tests / BuisnessLogicTest.php
1 <?php
2         include("../php/User.php");
3         include("../php/Plant.php");
4         include("../php/Controller.php");
5         include("../php/Season.php");
6         include("../php/SeasonScore.php");
7
8         class TestOfBuisnessLogic extends UnitTestCase
9         {
10                 private $m_Controller;
11                 
12                 function __construct() {
13                 }
14                 
15                 function setUp() {
16                         $this->m_Controller = new Controller();
17                         $db = new DataBase();
18                         $db->Clear();
19                         $db->Close();
20                 }
21                 
22                 function tearDown() {
23                         $db = new DataBase();
24                         $db->Clear();
25                         $db->Close();
26                         unset($_SESSION['user']);
27                         unset($_SESSION['pw']);
28                 }
29                 
30                 function validateRPCResponseWithoutData($res, $func) {
31                         $this->assertTrue($res != null, "No Result given");
32                         $this->assertTrue($res->m_Command == "RPC-Response", "Wrong Commandtype");
33                         $this->assertTrue($res->m_CalledFunction == $func, "Wrong CalledFunction");
34                 }
35                 function validateRPCResponse($res, $func, $data) {
36                         $this->validateRPCResponseWithoutData($res, $func);
37                         $this->assertTrue($res->m_Data == $data, "'".$res->m_Data."' was unexpected, '".$data."' expected");
38                 }
39                 function testAuthentication() {
40                         $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
41                         $this->validateRPCResponse($res, "IsLoggedIn", "false");
42                         
43                         $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
44                         $this->validateRPCResponse($res, "Register", "{ success: true, msg: 'Benutzer erstellt und erfolgreich angemeldet.' }");
45                         
46                         $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
47                         $this->validateRPCResponse($res, "IsLoggedIn", "true");
48                         
49                         $res = $this->m_Controller->HandleRemoteProcedureCall("Logout", "", "", "", "");
50                         $this->validateRPCResponse($res, "Logout", "true");
51                         
52                         $res = $this->m_Controller->HandleRemoteProcedureCall("IsLoggedIn", "", "", "", "");
53                         $this->validateRPCResponse($res, "IsLoggedIn", "false");
54                         
55                         $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
56                         $this->validateRPCResponse($res, "Auth", "true");
57                 }
58                 
59                 function testPlantCreation() {
60                         $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "", "");
61                         $this->validateRPCResponse($res, "CreatePlant", "{ success: false, msg: 'Sie sind nicht angemeldet' }");
62                         
63                         $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
64                         $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
65                         
66                         $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "");
67                         $this->validateRPCResponse($res, "CreatePlant", "{ success: true, msg: 'Es wurde eine neue Pflanze für sie angelegt.' }");
68                         
69                         $res = $this->m_Controller->HandleRemoteProcedureCall("GetPlantList", "", "", "", "");
70                         $this->validateRPCResponseWithoutData($res, "GetPlantList");
71                         $expectedEnd = "Name: 'newplant', Code: '' }] }";
72                         substr($res->m_Data, -count($expectedEnd), 0);
73                         
74                         $res = $this->m_Controller->HandleRemoteProcedureCall("SavePlant", "test", "", "newplant", "a b c");
75                         $this->validateRPCResponse($res, "SavePlant", "{ success: true, msg: '' }");
76                         
77                         $res = $this->m_Controller->HandleRemoteProcedureCall("GetPlantList", "", "", "", "");
78                         $this->validateRPCResponseWithoutData($res, "GetPlantList");
79                         $expectedEnd = "Name: 'newplant', Code: 'a b c' }] }";
80                         substr($res->m_Data, -count($expectedEnd), 0);
81                         
82                         $res = $this->m_Controller->HandleRemoteProcedureCall("DeletePlant", "test", "", "newplant", "");
83                         $this->validateRPCResponse($res, "DeletePlant", "{ success: true, msg: '' }");
84                 }
85                 
86                 function testValidation() {
87                         
88                         $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
89                         $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
90                         $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "RULE a GROW BY 1");
91                         
92                         $GLOBALS['ValidatorFile'] = '../../cgi/validate';
93                         $res = $this->m_Controller->HandleRemoteProcedureCall("ValidatePlant", "test", "", "newplant", "");
94                         $this->validateRPCResponse($res, "ValidatePlant", "{\"valid\":true}");
95                         
96                 }
97         }
98 ?>