test/snac/server/database/DBUserTest.php
/**
     * Test fundamentals of Roles and Privileges
     *
     */
Test fundamentals of Roles and Privileges
Test fundamentals of Roles and Privileges
public function testRolePrivilege()
    {
        $priv = new \snac\data\Privilege(sprintf("demo1 priv %s", time()), 'This is a demo test privilege');
        $this->dbu->writePrivilege($priv);
        $privList = $this->dbu->listPrivileges();
        $foundPriv = false;
        foreach($privList as $tPriv)
        {
            if ($tPriv->getID() == $priv->getID())
            {
                $foundPriv = true;
            }
        }
        $this->assertTrue($foundPriv);
        $role = new \snac\data\Role();
        $role->setLabel(sprintf("demo2 role %s", time()));
        $role->setDescription('This is a demo test role');
        $role->addPrivilege($priv);
        $this->dbu->writeRole($role);
        $allRole = $this->dbu->listRoles();
        $roleHasPriv = false;
        foreach($allRole as $tRole)
        {
            if ($tRole->getID() == $role->getID())
            {
                foreach($tRole->getPrivilegeList() as $tPriv)
                {
                    if ($tPriv->getID() == $priv->getID())
                    {
                        $roleHasPriv = true;
                    }
                }
            }
        }
        $this->assertTrue($roleHasPriv);
        $preDeleteRoleCount = count($allRole);
        $this->dbu->eraseRole($role);
        $postDeleteRoleCount = count($allRole = $this->dbu->listRoles());
        $this->assertEquals($preDeleteRoleCount, ($postDeleteRoleCount+1));
        $preDeletePrivilegeCount = count($privList);
        $this->dbu->erasePrivilege($priv);
        $postDeletePrivilegeCount = count($this->dbu->listPrivileges());
        $this->assertEquals($preDeletePrivilegeCount, ($postDeletePrivilegeCount+1));
    }
public function testRolePrivilege ( ) { $ priv = new \ snac \ data \ Privilege ( sprintf ( " demo1 priv %s " , time ( ) ) , 'This is a demo test privilege' ) ; $ this -> dbu -> writePrivilege ( $ priv ) ; $ privList = $ this -> dbu -> listPrivileges ( ) ; $ foundPriv = false ; foreach ( $ privList as $ tPriv ) { if ( $ tPriv -> getID ( ) == $ priv -> getID ( ) ) { $ foundPriv = true ; } } $ this -> assertTrue ( $ foundPriv ) ; $ role = new \ snac \ data \ Role ( ) ; $ role -> setLabel ( sprintf ( " demo2 role %s " , time ( ) ) ) ; $ role -> setDescription ( 'This is a demo test role' ) ; $ role -> addPrivilege ( $ priv ) ; $ this -> dbu -> writeRole ( $ role ) ; $ allRole = $ this -> dbu -> listRoles ( ) ; $ roleHasPriv = false ; foreach ( $ allRole as $ tRole ) { if ( $ tRole -> getID ( ) == $ role -> getID ( ) ) { foreach ( $ tRole -> getPrivilegeList ( ) as $ tPriv ) { if ( $ tPriv -> getID ( ) == $ priv -> getID ( ) ) { $ roleHasPriv = true ; } } } } $ this -> assertTrue ( $ roleHasPriv ) ; $ preDeleteRoleCount = count ( $ allRole ) ; $ this -> dbu -> eraseRole ( $ role ) ; $ postDeleteRoleCount = count ( $ allRole = $ this -> dbu -> listRoles ( ) ) ; $ this -> assertEquals ( $ preDeleteRoleCount , ( $ postDeleteRoleCount + 1 ) ) ; $ preDeletePrivilegeCount = count ( $ privList ) ; $ this -> dbu -> erasePrivilege ( $ priv ) ; $ postDeletePrivilegeCount = count ( $ this -> dbu -> listPrivileges ( ) ) ; $ this -> assertEquals ( $ preDeletePrivilegeCount , ( $ postDeletePrivilegeCount + 1 ) ) ; }
Test fundamentals of Roles and Privileges
Test fundamentals of Roles and Privileges
/*
         * $priv has the id added, in place.
         */ /*
         * Check that the priv we just wrote really made it to the database.
         */ /*
         * Create a new role, add a priv to it before writing to the db, then write to the db.
         * An alternate way of adding a priv to a role would is to call addPrivilegeToRole()
         */ /*
         * Check that the priv we added to the role is still there. Read all the roles from the db, and check
         * that our role exists then check that is has the expected priv.
         */ /*
         * Save the role count, erase the demo role, check the post erase count.  Must delete the role before
         * deleting any privileges used by it. The low level SQL code checks that a privilege is not used by
         * any role before deleting the privilege.
         */ /*
         * Save the priv count, erase the demo priv, check the count afterwards.
         */