Login   Register  
PHP Classes
elePHPant
Icontem

File: test/mainTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Abius X  >  PHP RBAC Class  >  test/mainTest.php  >  Download  
File: test/mainTest.php
Role: Unit test script
Content type: text/plain
Description: main test file
Class: PHP RBAC Class
Role Based Access Control based on NIST Level 2
Author: By
Last change:
Date: 1 year ago
Size: 4,962 bytes
 

Contents

Class file image Download
<?php
require_once __DIR__."/base.php";
class 
PHPRBACMainTest extends PHPRBAC_Test
{
    function 
setUp()
    {
        
parent::setUp ();
    }
    function 
testAssign()
    {
        
$RID jf::$RBAC->Roles->AddPath "/CEO/CIO/Admin" );
        
jf::$RBAC->Permissions->AddPath "/Users/add" );
        
jf::$RBAC->Permissions->AddPath "/Users/edit" );
        
jf::$RBAC->Permissions->AddPath "/Users/remove" );
        
$PID jf::$RBAC->Permissions->AddPath "/Users/changepass" );
        
        
$this->assertTrue jf::$RBAC->Assign $RID$PID ) );
        
$this->assertTrue jf::$RBAC->Assign $RID"/Users/edit" ) );
        
$this->assertTrue jf::$RBAC->Assign $RID"add" ) );
        
$this->assertTrue jf::$RBAC->Assign "/CEO/CIO""/Users/remove" ) );
        
$this->assertTrue jf::$RBAC->Assign "CEO""Users" ) );
        
$this->assertTrue jf::$RBAC->Assign "CEO"$PID ) );
        
$this->assertTrue jf::$RBAC->Assign "/CEO/CIO"$PID ) );
        
$this->assertTrue jf::$RBAC->Assign "/CEO""/Users/add" ) );
        
$this->assertTrue jf::$RBAC->Assign "/CEO/CIO/Admin""remove" ) );
    }
    function 
testCheck()
    {
        
        
// adding roles
        
jf::$RBAC->Roles->AddPath "/CEO/CIO/Admin" );
        
jf::$RBAC->Roles->AddPath "/CEO/CIO/Networking" );
        
jf::$RBAC->Roles->AddPath "/CEO/CIO/CISO" );
        
jf::$RBAC->Roles->AddPath "/CEO/Financial" );
        
jf::$RBAC->Roles->AddPath "/CEO/Secretary" );
        
        
// assingning roles to users
        
$res jf::$RBAC->Users->Assign "/CEO");
        
$res $res and jf::$RBAC->Users->Assign "/CEO/Financial");
        
        
$res $res and jf::$RBAC->Users->Assign "/CEO/CIO/Admin");
        
$res $res and jf::$RBAC->Users->Assign "/CEO/CIO/Networking");
        
$res $res and jf::$RBAC->Users->Assign "/CEO/CIO/CISO");
        
        
$res $res and jf::$RBAC->Users->Assign "/CEO/Secretary");
        
$this->assertTrue $res );
        
        
// adding permissions
        
jf::$RBAC->Permissions->AddPath "/Users/add" );
        
jf::$RBAC->Permissions->AddPath "/Users/edit" );
        
jf::$RBAC->Permissions->AddPath "/Users/remove" );
        
jf::$RBAC->Permissions->AddPath "/Users/changepass" );
        
jf::$RBAC->Permissions->AddPath "/Signature/financial" );
        
jf::$RBAC->Permissions->AddPath "/Signature/office" );
        
jf::$RBAC->Permissions->AddPath "/Signature/order" );
        
jf::$RBAC->Permissions->AddPath "/Signature/network" );
        
jf::$RBAC->Permissions->AddPath "/reports/IT/network" );
        
jf::$RBAC->Permissions->AddPath "/reports/IT/security" );
        
jf::$RBAC->Permissions->AddPath "/reports/financial" );
        
jf::$RBAC->Permissions->AddPath "/reports/general" );
        
        
// assigning permissions to roles
        
$res jf::$RBAC->Assign "CEO""Users" );
        
$res $res and jf::$RBAC->Assign "CEO""Signature" );
        
$res $res and jf::$RBAC->Assign "CEO""/reports" );
        
$this->assertTrue $res );
        
        
$res $res and jf::$RBAC->Assign "CIO""/reports/IT" );
        
$res $res and jf::$RBAC->Assign "CIO""/Users" );
        
        
$res $res and jf::$RBAC->Assign "Admin""/Users" );
        
$res $res and jf::$RBAC->Assign "Admin""/reports/IT" );
        
        
$res $res and jf::$RBAC->Assign "Networking""/reports/network" );
        
$res $res and jf::$RBAC->Assign "Networking""/Signature/network" );
        
        
$res $res and jf::$RBAC->Assign "CISO""/reports/security" );
        
$res $res and jf::$RBAC->Assign "CISO""/Users/changepass" );
        
$this->assertTrue $res );
        
        
$res $res and jf::$RBAC->Assign "Financial""/Signature/order" );
        
$res $res and jf::$RBAC->Assign "Financial""/Signature/financial" );
        
$res $res and jf::$RBAC->Assign "Financial""/reports/financial" );
        
        
$res $res and jf::$RBAC->Assign "Secretary""/reports/financial" );
        
$res $res and jf::$RBAC->Assign "Secretary""/Signature/office" );
        
$this->assertTrue $res );
        

        
// now checking
        
        
$this->assertTrue jf::$RBAC->Users->HasRole "/CEO/Financial") );
        
$this->assertTrue jf::$RBAC->Check "/Signature/financial") );
        
$this->assertTrue jf::$RBAC->Check "/reports/general") );
        
$this->assertTrue jf::$RBAC->Check "/reports/IT/security") );
        
        
$this->assertTrue jf::$RBAC->Check "/reports/IT/security") );
        
$this->assertTrue jf::$RBAC->Check "/reports/IT/network") );
        
$this->assertTrue jf::$RBAC->Check "/Users") );
        
        
$this->assertTrue jf::$RBAC->Check "/Signature/office") );
        
$this->assertFalse jf::$RBAC->Check "/Signature/order") );
        
$this->assertTrue jf::$RBAC->Check "/reports/financial") );
        
$this->assertFalse jf::$RBAC->Check "/reports/general") );
        
        
        try
        {
            
$this->assertFalse jf::$RBAC->Check "/reports/generalz") );
            
$this->fail "No error on unknown permission" );
        } catch ( 
RBACPermissionNotFoundException $e )
        {
        }
    }
    function 
testEnforce()
    {
        
        try
        {
            
$this->assertFalse jf::$RBAC->Check "/reports/generalz""root" ) );
            
$this->fail "No error on unknown permission" );
        } catch ( 
RBACPermissionNotFoundException $e )
        {
        }
    }
}