1+ <?php 
2+ 
3+ namespace  EasySwoole \Permission \Tests ;
4+ 
5+ use  PHPUnit \Framework \TestCase ;
6+ use  EasySwoole \ORM \DbManager ;
7+ use  EasySwoole \Permission \Model \RulesModel ;
8+ use  EasySwoole \Permission \Casbin ;
9+ use  EasySwoole \Permission \Config ;
10+ use  EasySwoole \ORM \Db \Connection ;
11+ use  EasySwoole \EasySwoole \Config  as  ESConfig ;
12+ use  EasySwoole \DDL \Blueprint \Create \Table  as  CreateTable ;
13+ use  EasySwoole \DDL \DDLBuilder ;
14+ use  EasySwoole \DDL \Enum \Character ;
15+ use  EasySwoole \DDL \Enum \Engine ;
16+ 
17+ class  DatabaseAdapterTest extends  TestCase
18+ {
19+     protected  function  initDb ()
20+     {
21+         RulesModel::create ()->destroy (null , true );
22+         RulesModel::create (['ptype '  => 'p ' , 'v0 '   => 'alice ' , 'v1 '  => 'data1 ' , 'v2 '  => 'read ' ])->save ();
23+         RulesModel::create (['ptype '  => 'p ' , 'v0 '   => 'bob ' , 'v1 '  => 'data2 ' , 'v2 '  => 'write ' ])->save ();
24+         RulesModel::create (['ptype '  => 'p ' , 'v0 '   => 'data2_admin ' , 'v1 '  => 'data2 ' , 'v2 '  => 'read ' ])->save ();
25+         RulesModel::create (['ptype '  => 'p ' , 'v0 '   => 'data2_admin ' , 'v1 '  => 'data2 ' , 'v2 '  => 'write ' ])->save ();
26+         RulesModel::create (['ptype '  => 'g ' , 'v0 '   => 'alice ' , 'v1 '  => 'data2_admin ' ])->save ();
27+     }
28+ 
29+     protected  function  getEnforcer ()
30+     {
31+         $ this  ->initConfig ();
32+         $ config  = new  Config ();
33+         $ casbin  = new  Casbin ($ config );
34+         $ this  ->initTable ();
35+         $ this  ->initDb ();
36+         return  $ casbin ->enforcer ();
37+     }
38+ 
39+     protected  function  initConfig ()
40+     {
41+         $ instance  = \EasySwoole \EasySwoole \Config::getInstance ();
42+         $ conf  = $ instance ->getConf ();
43+         $ conf ['MYSQL ' ] = [
44+             'host '           => '127.0.0.1 ' ,
45+             'port '           => 3306 ,
46+             'user '           => 'root ' ,
47+             'password '       => '' ,
48+             'database '       => 'easyswoole ' ,
49+             'timeout '        => 5 ,
50+             'charset '        => 'utf8mb4 ' ,
51+         ];
52+         $ instance ->load ($ conf );
53+         $ config  = new  \EasySwoole \ORM \Db \Config (ESConfig::getInstance ()->getConf ('MYSQL ' ));
54+         DbManager::getInstance ()->addConnection (new  Connection ($ config ));
55+     }
56+ 
57+     public  function  initTable ()
58+     {
59+         DDLBuilder::create ('casbin_rules ' , function  (CreateTable   $ table ) {
60+             $ table ->setIfNotExists ()->setTableComment ('rule table of casbin ' );
61+             $ table ->setTableCharset (Character::UTF8MB4_GENERAL_CI );
62+             $ table ->setTableEngine (Engine::MYISAM );
63+             $ table ->int ('id ' )->setIsUnsigned ()->setIsAutoIncrement ()->setIsPrimaryKey ();
64+             $ table ->varchar ('ptype ' , 255 );
65+             $ table ->varchar ('v0 ' , 255 );
66+             $ table ->varchar ('v1 ' , 255 );
67+             $ table ->varchar ('v2 ' , 255 );
68+             $ table ->varchar ('v3 ' , 255 );
69+             $ table ->varchar ('v4 ' , 255 );
70+             $ table ->varchar ('v5 ' , 255 );
71+         });
72+     }
73+ 
74+     public  function  testRemovePolicy ()
75+     {
76+         $ e  = $ this  ->getEnforcer ();
77+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data5 ' , 'read ' ));
78+         $ e ->addPermissionForUser ('alice ' , 'data5 ' , 'read ' );
79+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data5 ' , 'read ' ));
80+         $ e ->deletePermissionForUser ('alice ' , 'data5 ' , 'read ' );
81+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data5 ' , 'read ' ));
82+     }
83+ 
84+     public  function  testLoadPolicy ()
85+     {
86+         $ e  = $ this  ->getEnforcer ();
87+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data1 ' , 'read ' ));
88+         $ this  ->assertFalse ($ e ->enforce ('bob ' , 'data1 ' , 'read ' ));
89+         $ this  ->assertTrue ($ e ->enforce ('bob ' , 'data2 ' , 'write ' ));
90+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data2 ' , 'read ' ));
91+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data2 ' , 'write ' ));
92+     }
93+ 
94+     public  function  testAddPolicy ()
95+     {
96+         $ e  = $ this  ->getEnforcer ();
97+         $ this  ->assertFalse ($ e ->enforce ('eve ' , 'data3 ' , 'read ' ));
98+         $ e ->addPermissionForUser ('eve ' , 'data3 ' , 'read ' );
99+         $ this  ->assertTrue ($ e ->enforce ('eve ' , 'data3 ' , 'read ' ));
100+     }
101+ 
102+     public  function  testRemoveFilteredPolicy ()
103+     {
104+         $ e  = $ this  ->getEnforcer ();
105+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data1 ' , 'read ' ));
106+         $ e ->removeFilteredPolicy (1 , 'data1 ' );
107+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data1 ' , 'read ' ));
108+         $ this  ->assertTrue ($ e ->enforce ('bob ' , 'data2 ' , 'write ' ));
109+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data2 ' , 'read ' ));
110+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data2 ' , 'write ' ));
111+         $ e ->removeFilteredPolicy (1 , 'data2 ' , 'read ' );
112+         $ this  ->assertTrue ($ e ->enforce ('bob ' , 'data2 ' , 'write ' ));
113+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data2 ' , 'read ' ));
114+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data2 ' , 'write ' ));
115+         $ e ->removeFilteredPolicy (2 , 'write ' );
116+         $ this  ->assertFalse ($ e ->enforce ('bob ' , 'data2 ' , 'write ' ));
117+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data2 ' , 'write ' ));
118+     }
119+ 
120+     public  function  testSavePolicy ()
121+     {
122+         $ e  = $ this  ->getEnforcer ();
123+         $ this  ->assertFalse ($ e ->enforce ('alice ' , 'data4 ' , 'read ' ));
124+ 
125+         $ model  = $ e ->getModel ();
126+         $ model ->clearPolicy ();
127+         $ model ->addPolicy ('p ' , 'p ' , ['alice ' , 'data4 ' , 'read ' ]);
128+ 
129+         $ adapter  = $ e ->getAdapter ();
130+         $ adapter ->savePolicy ($ model );
131+         $ this  ->assertTrue ($ e ->enforce ('alice ' , 'data4 ' , 'read ' ));
132+     }
133+ }
0 commit comments