started some input validation code and unit tests for it
[gwvp.git] / unittests / sqlitedb.php
1 <?php
2 $lib_base = "../gwvplib/";
3
4 // initial http file
5 $WEB_ROOT_FS = realpath(dirname(__FILE__));
6 $BASE_URL = dirname($_SERVER["PHP_SELF"]);
7
8 global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $db_username, $db_password;
9
10 // add gwvplib as if it were a path in ../gwvplib
11 if(file_exists($lib_base)) {
12         $path = realpath($lib_base);
13         set_include_path(get_include_path().PATH_SEPARATOR.$path);
14 }
15
16 require_once("gwvplib.php");
17 global $db_type, $db_url;
18
19 $db_type = "sqlite";
20 $db_name = "/tmp/unittestdb.db";
21
22 gwvp_dbCreateSQLiteStructure("$db_name");
23
24 gwvp_ConnectDB();
25
26 // function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
27 // test users creation
28 gwvp_createUser("test@test.com", "test user", "password", "username", "desc", "0");
29 gwvp_createUser("test2@test.com", "test2 user", "password", "username2", "desc2", "0");
30 gwvp_createUser("test3@test.com", "test2 user", "password", "usernme3", "desc2", "0");
31 $users = gwvp_getUsers();
32 echo "Users\n";
33 echo "\tCreate test@test.com\n";
34 if($users[0]["email"] != "test@test.com") {
35         echo "\t\tUser 0 isnt correct: ".$users[0]["email"]."\n";
36 } else {
37         echo "\t\tUser 0 correct\n";
38 }
39 if($users[1]["email"] != "test2@test.com") {
40         echo "\t\tUser 1 isnt correct: ".$users[0]["email"]."\n";
41 } else {
42         echo "\t\tUser 1 correct\n";
43 }
44 echo "\tCheck User It\n";
45 $uid1 = gwvp_getUserId("test@test.com");
46 $uid2 = gwvp_getUserId("test2@test.com");
47 if($uid1 != 1) {
48         echo "\t\tUser 1 id not correct\n";
49 } else {
50         echo "\t\tUser 1 id correct\n";
51 }
52 if($uid2 != 2) {
53         echo "\t\tUser 2 id not correct\n";
54 } else {
55         echo "\t\tUser 2 id correct\n";
56 }
57
58 echo "\tDelete test@test.com\n";
59 gwvp_deleteUser("test@test.com");
60 $users = gwvp_getUsers();
61 if($users[0]["email"] != "test2@test.com") {
62         echo "\t\tUser 0 isnt correct: ".$users[0]["email"]."\n";
63 } else {
64         echo "\t\tUser 0 is correct\n";
65 }
66
67 // test group creation
68 // function gwvp_createGroup($group_name, $is_admin, $owner_id)
69 echo "\n\nGroups\n";
70 echo "\tCreate testgroups\n";
71 gwvp_createGroup("testgroup", true, "3");
72 gwvp_createGroup("testgroup2", false, "3");
73 gwvp_createGroup("testgroup3", true, "2");
74 $groups = gwvp_getGroups();
75 if($groups[0]["name"] != "testgroup") {
76         echo "\t\tGroup 0 isnt correct: ".$groups[0]["name"]."\n";
77 } else {
78         echo "\t\tGroup 0 correct\n";
79 }
80 if($groups[1]["name"] != "testgroup2") {
81         echo "\t\tGroup 1 isnt correct: ".$groups[1]["name"]."\n";
82 } else {
83         echo "\t\tGroup 1 correct\n";
84 }
85 echo "\tCheck group id\n";
86 $gid1 = gwvp_getGroupId("testgroup");
87 $gid2 = gwvp_getGroupId("testgroup2");
88 if($gid1 != 1) {
89         echo "\t\tGroup 1 ID not correct\n";
90 } else {
91         echo "\t\tGroup 1 ID correct\n";
92 }
93 if($gid2 != 2) {
94         echo "\t\tGroup 2 ID not correct\n";
95 } else {
96         echo "\t\tGroup 2 ID correct\n";
97 }
98
99 echo "\tNow delete group\n";
100 gwvp_deleteGroup("testgroup");
101 $groups = gwvp_getGroups();
102 if($groups[0]["name"] != "testgroup2") {
103         echo "\t\tGroup 0 isnt correct: ".$groups[0]["name"]."\n";
104 } else {
105         echo "\t\tGroup 0 correct\n";
106 }
107
108
109 // setup some user permissions to groups
110 echo "\n\nGroup Memberships\n";
111 echo "\tAdd to groups\n";
112 if(!gwvp_addGroupMember("test2@test.com", "testgroup2")) {
113         echo "\t\tadd test2@test.com to testgroup2 failed\n";
114 } else {
115         echo "\t\tadd test2@test.com to testgroup2 ok\n";
116 }
117 if(!gwvp_addGroupMember("test3@test.com", "testgroup3")) {
118         echo "\t\tadd test3@test.com to testgroup3 failed\n";
119 } else {
120         echo "\t\tadd test2@test.com to testgroup2 ok\n";
121 }
122
123 echo "\tTest Group Membership\n";
124 // function gwvp_IsGroupMember($email, $groupname)
125 if(!gwvp_isGroupMember("test2@test.com", "testgroup2")) {
126         echo "\t\ttest2@test.com in testgorup2 FAILED\n";
127 } else {
128         echo "\t\ttest2@test.com in testgroup2 ok\n";
129 }
130 if(!gwvp_isGroupMember("test3@test.com", "testgroup3")) {
131         echo "\t\ttest2@test.com in testgorup2 FAILED\n";
132 } else {
133         echo "\t\ttest3@test.com in testgroup3 ok\n";
134 }
135 if(gwvp_isGroupMember("test2@test.com", "testgroup3")) {
136         echo "\t\ttest2@test.com in testgorup3 FAILED\n";
137 } else {
138         echo "\t\ttest2@test.com in testgroup3 ok\n";
139 }
140 if(gwvp_isGroupMember("test3@test.com", "testgroup2")) {
141         echo "\t\ttest3@test.com in testgorup2 FAILED\n";
142 } else {
143         echo "\t\ttest3@test.com in testgroup2 ok\n";
144 }
145
146 echo "\tTest Admin Role\n";
147 if(gwvp_IsUserAdmin("test2@test.com")) {
148         echo "\t\tUser test2@test.com reported admin FAILED\n";
149 } else {
150         echo "\t\tUset test2@test.com reported as not admin, ok\n";
151 }
152 if(!gwvp_IsUserAdmin("test3@test.com")) {
153         echo "\t\tUser test3@test.com reported not admin FAILED\n";
154 } else {
155         echo "\t\tUset test3@test.com reported as admin, ok\n";
156 }
157
158 echo "\ttest group memberships\n";
159 gwvp_createGroup("testgroup4", false, "1");
160 gwvp_createGroup("testgroup5", true, "1");
161 gwvp_addGroupMember("test3@test.com", "testgroup4");
162 gwvp_addGroupMember("test3@test.com", "testgroup5");
163 gwvp_addGroupMember("test2@test.com", "testgroup5");
164 $groupstest2 = gwvp_getGroupsForUser("test2@test.com");
165 $groupstest3 = gwvp_getGroupsForUser("test3@test.com");
166 echo "\t\tGroups for test2 should be testgroup2, testgroup5 - ";
167 if($groupstest2[0] != "testgroup2"||$groupstest2[1] != "testgroup5") {
168         echo "FAILED!\n";
169 } else {
170         echo "ok\n";
171 }
172 echo "\t\tGroups for test3 should be testgroup3, testgroup4, testgroup5 - ";
173 if($groupstest3[0] != "testgroup3"||$groupstest3[1] != "testgroup4"||$groupstest3[2] != "testgroup5") {
174         echo "FAILED!\n";
175 } else {
176         echo "ok\n";
177 }
178
179 echo "\tTest Group Ownership\n";
180 if(gwvp_groupOwner("testgroup2") != "test3@test.com") {
181         echo "\t\tTest owner of testgroup2 = test3@test.com - FAILED!\n";
182 } else {
183         echo "\t\tTest owner of testgroup2 = test3@test.com - ok!\n";
184 }
185 if(gwvp_groupOwner("testgroup3") != "test2@test.com") {
186         echo "\t\tTest owner of testgroup3 = test2@test.com - FAILED!\n";
187 } else {
188         echo "\t\tTest owner of testgroup3 = test2@test.com - ok!\n";
189 }
190
191
192 unlink($db_name);
193 ?>