Browse Source

permission test

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
78d3dc39a6
2 changed files with 175 additions and 0 deletions
  1. 74
    0
      test/TestPermission.php
  2. 101
    0
      test/test_permissions.sql

+ 74
- 0
test/TestPermission.php View File

@@ -0,0 +1,74 @@
1
+<?php
2
+
3
+/**
4
+ * Created by PhpStorm.
5
+ * User: robin
6
+ * Date: 9/26/15
7
+ * Time: 11:56 PM
8
+ */
9
+
10
+
11
+require_once('../vendor/autoload.php');
12
+
13
+Dotenv::load("..");
14
+
15
+class TestPermission
16
+{
17
+    private $_pdo;
18
+
19
+    public function __construct()
20
+    {
21
+        $dsn = getenv("DB_CONNECTION") . ":dbname=" . getenv("DB_DATABASE") . ";host="
22
+            . getenv("DB_HOST") . ";port=" . getenv("DB_PORT");
23
+        $this->_pdo = new PDO($dsn, getenv("DB_USERNAME"), getenv("DB_PASSWORD"));
24
+    }
25
+
26
+    protected function printError($query, $message)
27
+    {
28
+        echo $message . "\n";
29
+        var_dump($query->errorInfo());
30
+        var_dump($this->_pdo->errorInfo());
31
+        return null;
32
+    }
33
+
34
+    public function getPermission($user_id, $name)
35
+    {
36
+        $req = $this->_pdo->prepare("SELECT * FROM sp_lu_get_user_permission(:user_id, :name)");
37
+        if ($req->execute(array(":user_id" => $user_id, ":name" => $name)))
38
+        {
39
+            return $req->fetchAll()[0][0];
40
+        }
41
+        else
42
+        {
43
+            $this->printError($req, "Failed to get permission");
44
+            return null;
45
+        }
46
+    }
47
+}
48
+
49
+$test = new TestPermission();
50
+
51
+$user_id = 1;
52
+$permissions = array(
53
+    "perm_1" => true,
54
+    "perm_2" => false,
55
+    "perm_3" => true,
56
+    "perm_4" => false,
57
+    "perm_5" => false,
58
+    "perm_6" => true,
59
+    "perm_7" => true,
60
+    "perm_8" => false
61
+);
62
+
63
+foreach ($permissions as $permission => $value)
64
+{
65
+    $p = $test->getPermission($user_id, $permission);
66
+    if ($p == $value)
67
+    {
68
+        echo "[ OK ] $permission\n";
69
+    }
70
+    else if ($p != null)
71
+    {
72
+        echo "[ERR ] $permission\n";
73
+    }
74
+}

+ 101
- 0
test/test_permissions.sql View File

@@ -0,0 +1,101 @@
1
+--
2
+-- PostgreSQL database dump
3
+--
4
+
5
+SET statement_timeout = 0;
6
+SET lock_timeout = 0;
7
+SET client_encoding = 'UTF8';
8
+SET standard_conforming_strings = on;
9
+SET check_function_bodies = false;
10
+SET client_min_messages = warning;
11
+
12
+SET search_path = public, pg_catalog;
13
+
14
+--
15
+-- Data for Name: luticate_groups; Type: TABLE DATA; Schema: public; Owner: dev
16
+--
17
+
18
+COPY luticate_groups (id, name) FROM stdin;
19
+1	test
20
+2	test2
21
+\.
22
+
23
+
24
+--
25
+-- Name: luticate_groups_id_seq; Type: SEQUENCE SET; Schema: public; Owner: dev
26
+--
27
+
28
+SELECT pg_catalog.setval('luticate_groups_id_seq', 2, true);
29
+
30
+
31
+--
32
+-- Data for Name: luticate_permissions; Type: TABLE DATA; Schema: public; Owner: dev
33
+--
34
+
35
+COPY luticate_permissions (name, value) FROM stdin;
36
+perm_1	t
37
+perm_2	f
38
+perm_3	t
39
+perm_4	f
40
+perm_5	t
41
+perm_6	f
42
+perm_7	f
43
+perm_8	t
44
+\.
45
+
46
+
47
+--
48
+-- Data for Name: luticate_permissions_groups; Type: TABLE DATA; Schema: public; Owner: dev
49
+--
50
+
51
+COPY luticate_permissions_groups (name, value, group_id) FROM stdin;
52
+perm_3	t	1
53
+perm_4	f	1
54
+perm_5	t	1
55
+perm_5	f	2
56
+perm_6	t	1
57
+perm_6	t	2
58
+perm_7	f	1
59
+\.
60
+
61
+
62
+--
63
+-- Data for Name: luticate_users; Type: TABLE DATA; Schema: public; Owner: dev
64
+--
65
+
66
+COPY luticate_users (id, username, email, password, salt) FROM stdin;
67
+1	test	test@test.test	test	test
68
+\.
69
+
70
+
71
+--
72
+-- Data for Name: luticate_permissions_users; Type: TABLE DATA; Schema: public; Owner: dev
73
+--
74
+
75
+COPY luticate_permissions_users (name, value, user_id) FROM stdin;
76
+perm_7	t	1
77
+perm_8	f	1
78
+\.
79
+
80
+
81
+--
82
+-- Data for Name: luticate_users_groups; Type: TABLE DATA; Schema: public; Owner: dev
83
+--
84
+
85
+COPY luticate_users_groups (user_id, group_id) FROM stdin;
86
+1	1
87
+1	2
88
+\.
89
+
90
+
91
+--
92
+-- Name: luticate_users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: dev
93
+--
94
+
95
+SELECT pg_catalog.setval('luticate_users_id_seq', 1, true);
96
+
97
+
98
+--
99
+-- PostgreSQL database dump complete
100
+--
101
+

Loading…
Cancel
Save