[Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[174] trunk/0.3
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2010-06-21 17:56:54
|
Revision: 174 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=174&view=rev Author: crazedsanity Date: 2010-06-21 17:56:47 +0000 (Mon, 21 Jun 2010) Log Message: ----------- Lots of tests, and minor changes so all the tests work. Modified Paths: -------------- trunk/0.3/abstract/cs_genericGroup.abstract.class.php trunk/0.3/abstract/cs_genericUserGroup.abstract.class.php trunk/0.3/cs_genericPermission.class.php trunk/0.3/tests/testOfCSGenericPermissions.php trunk/0.3/tests/testOfCSWebAppLibs.php Modified: trunk/0.3/abstract/cs_genericGroup.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_genericGroup.abstract.class.php 2010-06-21 15:59:57 UTC (rev 173) +++ trunk/0.3/abstract/cs_genericGroup.abstract.class.php 2010-06-21 17:56:47 UTC (rev 174) @@ -23,7 +23,7 @@ const groupTable = "cswal_group_table"; /** Sequence for groups table. */ - const groupSeq = "cswal_group_table_group_id"; + const groupSeq = "cswal_group_table_group_id_seq"; //============================================================================ public function __construct(cs_phpDB $db) { @@ -42,6 +42,7 @@ else { throw new exception(__METHOD__ .":: invalid string (". $name .")"); } + return($name); }//end clean_group_name() //============================================================================ @@ -52,7 +53,7 @@ try{ $name = $this->clean_group_name($name); $sql = "INSERT INTO ". self::groupTable ." (group_name) VALUES ('". $name ."')"; - $newId = $this->db->run_insert($sql, self::gropuSeq); + $newId = $this->db->run_insert($sql, self::groupSeq); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to create new record, DETAILS::: ". $e->getMessage()); @@ -82,6 +83,22 @@ //============================================================================ + public function get_all_groups() { + try { + $sql = "SELECT * FROM ". self::groupTable ." ORDER BY group_name"; + $retval = $this->db->run_query($sql); + } + catch(Exception $e) { + throw new exception(__METHOD__ .":: failed to retrieve groups, DETAILS::: ". $e->getMessage()); + } + + return($retval); + }//end get_all_groups() + //============================================================================ + + + + //============================================================================ public function get_group_by_id($groupId) { try { if(!is_null($groupId) && is_numeric($groupId)) { Modified: trunk/0.3/abstract/cs_genericUserGroup.abstract.class.php =================================================================== --- trunk/0.3/abstract/cs_genericUserGroup.abstract.class.php 2010-06-21 15:59:57 UTC (rev 173) +++ trunk/0.3/abstract/cs_genericUserGroup.abstract.class.php 2010-06-21 17:56:47 UTC (rev 174) @@ -53,8 +53,8 @@ if(is_numeric($userId) && $userId >= 0) { try { $sql = "SELECT ug.*, g.group_name FROM ". self::ugTable ." AS ug INNER " - ."JOIN ". parent::groupTable ." as g WHERE user_id=". $userId; - $retval = $this->db->run_query($sql); + ."JOIN ". parent::groupTable ." as g USING (group_id) WHERE user_id=". $userId; + $retval = $this->db->run_query($sql, 'group_id'); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to locate group (". $groupName ."), DETAILS::: ". $e->getMessage()); Modified: trunk/0.3/cs_genericPermission.class.php =================================================================== --- trunk/0.3/cs_genericPermission.class.php 2010-06-21 15:59:57 UTC (rev 173) +++ trunk/0.3/cs_genericPermission.class.php 2010-06-21 17:56:47 UTC (rev 174) @@ -23,7 +23,7 @@ const objTable = "cswal_object_table"; /** Sequence for permissions table. */ - const objSeq = "cswal_object_table_object_id"; + const objSeq = "cswal_object_table_object_id_seq"; /** List of valid keys... */ protected $keys = array(); @@ -89,13 +89,14 @@ //============================================================================ protected function build_permission_string(array $perms) { $this->_sanityCheck(); - if(is_array($perms) && count($perms) == count($this->keys)) { + if(is_array($perms) && count($perms) >= count($this->keys)) { $retval = ""; foreach($this->keys as $dbColName) { if(isset($perms[$dbColName])) { //get the last character of the column name. - $permChar = substring($dbColName, -1); - if($perms[$dbColName] === false) { + $permChar = substr($dbColName, -1); + if($perms[$dbColName] === false || !strlen($perms[$dbColName]) || $perms[$dbColName] === '-' || $perms[$dbColName] === 'f') { + $permChar = '-'; } $retval .= $permChar; @@ -125,13 +126,27 @@ //============================================================================ public function create_permission($name, $userId, $groupId, $permString) { if(is_string($name) && strlen($name) && is_numeric($userId) && $userId >= 0 && is_numeric($groupId) && $groupId >= 0) { + $cleanStringArr = array( + 'object_name' => 'sql', + 'user_id' => 'numeric', + 'group_id' => 'numeric', + 'u_r' => 'bool', + 'u_w' => 'bool', + 'u_x' => 'bool', + 'g_r' => 'bool', + 'g_w' => 'bool', + 'g_x' => 'bool', + 'o_r' => 'bool', + 'o_w' => 'bool', + 'o_x' => 'bool' + ); try{ $insertArr = $this->parse_permission_string($permString); $insertArr['object_name'] = $this->gfObj->cleanString($name, 'sql', 0); $insertArr['user_id'] = $userId; $insertArr['group_id'] = $groupId; - $insertSql = $this->gfObj->string_from_array($insertArr, 'insert'); + $insertSql = $this->gfObj->string_from_array($insertArr, 'insert', null, $cleanStringArr); $sql = "INSERT INTO ". self::objTable ." ". $insertSql; $newId = $this->db->run_insert($sql, self::objSeq); } @@ -160,8 +175,8 @@ //============================================================================ public function get_permission($name) { try { - $name = $this->clean_permission_name($name); - $sql = "SELECT * FROM ". self::objTable ." WHERE permission_name='". $name ."'"; + $name = $this->gfObj->cleanString($name, 'sql', 0); + $sql = "SELECT * FROM ". self::objTable ." WHERE object_name='". $name ."'"; $retval = $this->db->run_query($sql); } catch(Exception $e) { Modified: trunk/0.3/tests/testOfCSGenericPermissions.php =================================================================== --- trunk/0.3/tests/testOfCSGenericPermissions.php 2010-06-21 15:59:57 UTC (rev 173) +++ trunk/0.3/tests/testOfCSGenericPermissions.php 2010-06-21 17:56:47 UTC (rev 174) @@ -14,19 +14,29 @@ class testOfCSGenericPermissions extends UnitTestCase { //-------------------------------------------------------------------------- - function __construct() { + function setUp() { $this->gfObj = new cs_globalFunctions; $this->gfObj->debugPrintOpt=1; if(!defined('CS_UNITTEST')) { throw new exception(__METHOD__ .": FATAL: constant 'CS_UNITTEST' not set, can't do testing safely"); } $this->get_valid_users(); - }//end __construct() + $perm = new _gpTester($this->create_dbconn()); + $perm->do_schema(); + }//end setUp() //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- + public function tearDown() { + $this->remove_tables(); + } + //-------------------------------------------------------------------------- + + + + //-------------------------------------------------------------------------- private function create_dbconn() { $dbParams = array( 'host' => constant('cs_webapplibs-DB_CONNECT_HOST'), @@ -81,15 +91,96 @@ //-------------------------------------------------------------------------- public function test_userGroups() { $perm = new _gpTester($this->create_dbconn()); - $perm->do_schema(); - //create a group with an invalid group_id. - $perm->create_user_group($this->validUsers[0]['uid'],1); + //make sure there are groups available. + { + $groupList = $perm->get_all_groups(); + $keys = array_keys($groupList); + $myKey = $keys[0]; + + $this->assertTrue(is_array($groupList)); + $this->assertTrue(count($groupList) > 0); + + $this->assertTrue(isset($groupList[$myKey])); + $this->assertTrue(isset($groupList[$myKey]['group_name'])); + $this->assertTrue(isset($groupList[$myKey]['group_id'])); + } + + //create some groups. + { + $newGroupId = $perm->create_group(__METHOD__); + $this->assertTrue(is_numeric($newGroupId)); + + $groupList = $perm->get_all_groups(); + + foreach($groupList as $groupData) { + $this->assertEqual($perm->get_group_by_id($groupData['group_id']), $groupData); + $this->assertEqual($perm->get_group($groupData['group_name']), $groupData); + } + } + + //create & test user_group relationships. + { + $newId = $perm->create_user_group($this->validUsers[$myKey]['uid'],1); + $this->assertTrue(is_numeric($newId)); + + $ugList = $perm->get_user_groups($this->validUsers[$myKey]['uid']); + $this->assertTrue(is_array($ugList)); + $this->assertTrue(count($ugList) > 0); + $this->assertFalse(isset($ugList['group_name'])); + } }//end test_userGroups //-------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + public function test_permissions() { + + $perm = new _gpTester($this->create_dbconn()); + + //Test permission string parsing. + { + $myPermArr = array( + 'u_r' => 'x', + 'u_w' => 'x', + 'u_x' => 'r', + 'g_r' => '-', + 'g_w' => '-', + 'g_x' => '', + 'o_r' => '', + 'o_w' => '-', + 'o_x' => 'x' + ); + $permString = 'rwx-----x'; + + $this->assertEqual(strlen($perm->make_perm_string($myPermArr)),9); + $this->assertEqual(count($perm->parse_perm_string($permString)), 9); + $this->assertEqual($perm->make_perm_string($myPermArr), $permString); + $this->assertEqual(array_keys($perm->parse_perm_string($permString)), array_keys($myPermArr)); + $this->assertEqual($perm->make_perm_string($perm->parse_perm_string($permString)), $permString); + $this->assertEqual(array_keys($perm->parse_perm_string($perm->make_perm_string($myPermArr))), array_keys($myPermArr)); + } + + //create some permissions. + { + $userKeys = array_keys($this->validUsers); + $myUser = $this->validUsers[$userKeys[0]]; + + $usePermString = 'rwxrw-rw-'; + $usePermName = __METHOD__ .'/test1'; + $permId = $perm->create_permission($usePermName, $myUser['uid'], 1, $usePermString); + $this->assertTrue(is_numeric($permId)); + + //the method 'build_permissions_string()' should disregard extra indices in the array & build the string. + $this->assertEqual($perm->make_perm_string($perm->get_permission_by_id($permId)), $usePermString); + $this->assertEqual($perm->make_perm_string($perm->get_object_by_id($permId)), $usePermString); + $this->assertEqual($perm->make_perm_string($perm->get_permission($usePermName)), $usePermString); + $this->assertEqual($perm->make_perm_string($perm->get_object($usePermName)), $usePermString); + } + }//end test_permissions + //-------------------------------------------------------------------------- + } class _gpTester extends cs_genericPermission { @@ -100,5 +191,13 @@ public function do_schema() { $this->build_schema(); } + + public function make_perm_string(array $perms) { + return($this->build_permission_string($perms)); + } + + public function parse_perm_string($string) { + return($this->parse_permission_string($string)); + } } ?> Modified: trunk/0.3/tests/testOfCSWebAppLibs.php =================================================================== --- trunk/0.3/tests/testOfCSWebAppLibs.php 2010-06-21 15:59:57 UTC (rev 173) +++ trunk/0.3/tests/testOfCSWebAppLibs.php 2010-06-21 17:56:47 UTC (rev 174) @@ -291,7 +291,7 @@ // now REALLY test paths. { - #$gdl = new gdlTester($this->create_dbconn()); + $gdl = new gdlTester($this->create_dbconn()); $myBasePath = '/character/sheet/Xander Cage'; $x->set_base_path($myBasePath); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |