[Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[192] trunk/0.4
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2011-01-20 01:27:15
|
Revision: 192 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=192&view=rev Author: crazedsanity Date: 2011-01-20 01:27:08 +0000 (Thu, 20 Jan 2011) Log Message: ----------- Better testing using created+dropped database to avoid breaking existing db. /abstract/testDb.abstract.class.php [NEW]: * class for creating, accessing, and destroying a test database. * NOTE::: this class lacks proper exception/error handling. /setup/genericPermissions.pgsql.sql: * remove the BEGIN statement from the top of the file. /tests/testOfCSGenericPermissions.php: * MAIN: -- extends testDbAbstract instead of UnitTestCase. * setUp(): -- call parent to create database -- create new _gpTester, use connection from extended (parent) class * tearDown(): -- destroy the database instead of removing tables * create_dbconn() [DELETED]: -- removed, using connection from parent class. * remove_tables() [DELETED]: -- no longer needed; using a temporary database instead. * get_valid_users(): -- remove call to deleted method -- reference internal db var * test_userGroups(): -- use internal permObj var. -- use internal db var. * test_permissions(): -- don't create new _gpTester (unnecessary) /tests/files/test_db.sql: * fixed uid's so they would not collide. Modified Paths: -------------- trunk/0.4/setup/genericPermissions.pgsql.sql trunk/0.4/tests/files/test_db.sql trunk/0.4/tests/testOfCSGenericPermissions.php Added Paths: ----------- trunk/0.4/abstract/testDb.abstract.class.php Added: trunk/0.4/abstract/testDb.abstract.class.php =================================================================== --- trunk/0.4/abstract/testDb.abstract.class.php (rev 0) +++ trunk/0.4/abstract/testDb.abstract.class.php 2011-01-20 01:27:08 UTC (rev 192) @@ -0,0 +1,83 @@ +<?php + + +abstract class testDbAbstract extends UnitTestCase { + + protected $config = array(); + protected $db; + private $templateDb; + private $templateConfig; + + //----------------------------------------------------------------------------- + public function __construct($superUserName, $password, $hostname, $port) { + /* + 'host' => $this->host, + 'port' => $this->port, + 'dbname' => $this->dbname, + 'user' => $this->user, + 'password' => $this->password + */ + $this->config = array( + 'user' => $superUserName, + 'password' => $password, + 'host' => $hostname, + 'port' => $port, + + //make sure the database name is unique and has (almost) no chance of clashing. + 'dbname' => $this->set_dbname(__CLASS__) + ); + $this->templateConfig = $this->config; + $this->templateConfig['dbname'] = 'template1'; + $this->templateDb = new cs_phpdb('pgsql'); + $this->templateDb->connect($this->templateConfig); + + $this->gfObj = new cs_globalFunctions; + + $this->db = new cs_phpdb('pgsql'); + $this->create_db(); + }//end __construct() + //----------------------------------------------------------------------------- + + + + //----------------------------------------------------------------------------- + private function set_dbname($prefix) { + return(strtolower(__CLASS__ .'_'. preg_replace('/\./', '', microtime(true)))); + }//end set_dbname() + //----------------------------------------------------------------------------- + + + + //----------------------------------------------------------------------------- + protected function create_db() { + $sql = "CREATE DATABASE ". $this->config['dbname']; + $this->templateDb->exec($sql); + + //now run the SQL file. + $this->db->connect($this->config); + $this->db->run_sql_file(dirname(__FILE__) .'/../tests/files/test_db.sql'); + }//end create_db() + //----------------------------------------------------------------------------- + + + + //----------------------------------------------------------------------------- + protected function destroy_db() { + $this->db->close(); + $this->templateDb->exec("DROP DATABASE ". $this->config['dbname']); + }//end destroy_db() + //----------------------------------------------------------------------------- + + + + //----------------------------------------------------------------------------- + public function __destruct() { + #$this->destroy_db(); + }//end __destruct() + //----------------------------------------------------------------------------- + + + +}//end testDbAbstract{} + +?> Modified: trunk/0.4/setup/genericPermissions.pgsql.sql =================================================================== --- trunk/0.4/setup/genericPermissions.pgsql.sql 2011-01-15 05:27:39 UTC (rev 191) +++ trunk/0.4/setup/genericPermissions.pgsql.sql 2011-01-20 01:27:08 UTC (rev 192) @@ -1,4 +1,3 @@ -BEGIN; -- -- Group table Modified: trunk/0.4/tests/files/test_db.sql =================================================================== --- trunk/0.4/tests/files/test_db.sql 2011-01-15 05:27:39 UTC (rev 191) +++ trunk/0.4/tests/files/test_db.sql 2011-01-20 01:27:08 UTC (rev 192) @@ -67,11 +67,11 @@ -- INSERT INTO cs_authentication_table VALUES (101, 'slaughter', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'sla...@de...ll', 1); -INSERT INTO cs_authentication_table VALUES (101, 'mary', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ma...@de...ll', 1); -INSERT INTO cs_authentication_table VALUES (101, 'einstein', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ein...@de...ll', 1); -INSERT INTO cs_authentication_table VALUES (101, 'alexander.graham.bell', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ale...@de...ll', 1); -INSERT INTO cs_authentication_table VALUES (101, 'john', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'jo...@de...ll', 1); -INSERT INTO cs_authentication_table VALUES (101, 'xavier', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'xa...@de...ll', 1); +INSERT INTO cs_authentication_table VALUES (102, 'mary', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ma...@de...ll', 1); +INSERT INTO cs_authentication_table VALUES (103, 'einstein', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ein...@de...ll', 1); +INSERT INTO cs_authentication_table VALUES (104, 'alexander.graham.bell', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'ale...@de...ll', 1); +INSERT INTO cs_authentication_table VALUES (105, 'john', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'jo...@de...ll', 1); +INSERT INTO cs_authentication_table VALUES (106, 'xavier', 'x', true, '2008-06-01', '2011-01-10 21:07:07.029629-06', 'xa...@de...ll', 1); -- Modified: trunk/0.4/tests/testOfCSGenericPermissions.php =================================================================== --- trunk/0.4/tests/testOfCSGenericPermissions.php 2011-01-15 05:27:39 UTC (rev 191) +++ trunk/0.4/tests/testOfCSGenericPermissions.php 2011-01-20 01:27:08 UTC (rev 192) @@ -11,18 +11,16 @@ * $LastChangedRevision$ */ -class testOfCSGenericPermissions extends UnitTestCase { +class testOfCSGenericPermissions extends testDbAbstract { //-------------------------------------------------------------------------- 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"); - } + parent::__construct('postgres','', 'localhost', '5432'); $this->get_valid_users(); - $perm = new _gpTester($this->create_dbconn()); - $perm->do_schema(); + $this->permObj = new _gpTester($this->db); + $this->permObj->do_schema(); }//end setUp() //-------------------------------------------------------------------------- @@ -30,60 +28,20 @@ //-------------------------------------------------------------------------- public function tearDown() { - $this->remove_tables(); + $this->destroy_db(); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- - private function create_dbconn() { - $dbParams = array( - 'host' => constant('cs_webapplibs-DB_CONNECT_HOST'), - 'dbname' => constant('cs_webapplibs-DB_CONNECT_DBNAME'), - 'user' => constant('cs_webapplibs-DB_CONNECT_USER'), - 'password' => constant('cs_webapplibs-DB_CONNECT_PASSWORD'), - 'port' => constant('cs_webapplibs-DB_CONNECT_PORT') - ); - $db = new cs_phpDB(constant('DBTYPE')); - $db->connect($dbParams); - return($db); - }//end create_dbconn() - //-------------------------------------------------------------------------- - - - - //-------------------------------------------------------------------------- - private function remove_tables() { - $tableList = array( - 'cswal_gdl_object_table', 'cswal_gdl_attribute_table', 'cswal_gdl_path_table', - 'cswal_object_table', 'cswal_user_group_table', 'cswal_group_table' - ); - - $db = $this->create_dbconn(); - foreach($tableList as $name) { - try { - $db->run_update("DROP TABLE ". $name ." CASCADE", true); - } - catch(exception $e) { - //force an error. - //$this->assertTrue(false, "Error while dropping (". $name .")::: ". $e->getMessage()); - } - } - }//end remove_tables() - //-------------------------------------------------------------------------- - - - - //-------------------------------------------------------------------------- /** * Just like the schema, this SQL will need to change to match your database in order to work. */ private function get_valid_users() { $sql = "SELECT uid,username FROM cs_authentication_table ORDER BY uid"; try { - $db = $this->create_dbconn(); - $this->validUsers = $db->run_query($sql); + $this->validUsers = $this->db->run_query($sql); } catch(Exception $e) { cs_debug_backtrace(1); @@ -96,11 +54,9 @@ //-------------------------------------------------------------------------- public function test_userGroups() { - $perm = new _gpTester($this->create_dbconn()); - //make sure there are groups available. { - $groupList = $perm->get_all_groups(); + $groupList = $this->permObj->get_all_groups(); $keys = array_keys($groupList); $myKey = $keys[0]; @@ -114,24 +70,24 @@ //create some groups. { - $newGroupId = $perm->create_group(__METHOD__); + $newGroupId = $this->permObj->create_group(__METHOD__); $this->assertTrue(is_numeric($newGroupId)); - $groupList = $perm->get_all_groups(); + $groupList = $this->permObj->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); + $this->assertEqual($this->permObj->get_group_by_id($groupData['group_id']), $groupData); + $this->assertEqual($this->permObj->get_group($groupData['group_name']), $groupData); } } //create & test user_group relationships. { - $newId = $perm->create_user_group($this->validUsers[$myKey]['uid'],1); + $newId = $this->permObj->create_user_group($this->validUsers[$myKey]['uid'],1); $this->assertTrue(is_numeric($newId)); - $this->assertTrue($perm->is_group_member($this->validUsers[$myKey]['uid'],1)); + $this->assertTrue($this->permObj->is_group_member($this->validUsers[$myKey]['uid'],1)); - $ugList = $perm->get_user_groups($this->validUsers[$myKey]['uid']); + $ugList = $this->permObj->get_user_groups($this->validUsers[$myKey]['uid']); $this->assertTrue(is_array($ugList)); $this->assertTrue(count($ugList) > 0); $this->assertFalse(isset($ugList['group_name'])); @@ -144,8 +100,6 @@ //-------------------------------------------------------------------------- public function test_permissions() { - $perm = new _gpTester($this->create_dbconn()); - //Test permission string parsing. { $myPermArr = array( @@ -178,16 +132,16 @@ ); $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)); + $this->assertEqual(strlen($this->permObj->make_perm_string($myPermArr)),9); + $this->assertEqual(count($this->permObj->parse_perm_string($permString)), 9); + $this->assertEqual($this->permObj->make_perm_string($myPermArr), $permString); + $this->assertEqual(array_keys($this->permObj->parse_perm_string($permString)), array_keys($myPermArr)); + $this->assertEqual($this->permObj->make_perm_string($this->permObj->parse_perm_string($permString)), $permString); + $this->assertEqual(array_keys($this->permObj->parse_perm_string($this->permObj->make_perm_string($myPermArr))), array_keys($myPermArr)); - $this->assertEqual($perm->get_perm_list($myPermArr,'u'), $permByType['u']); - $this->assertEqual($perm->get_perm_list($myPermArr,'g'), $permByType['g']); - $this->assertEqual($perm->get_perm_list($myPermArr,'o'), $permByType['o']); + $this->assertEqual($this->permObj->get_perm_list($myPermArr,'u'), $permByType['u']); + $this->assertEqual($this->permObj->get_perm_list($myPermArr,'g'), $permByType['g']); + $this->assertEqual($this->permObj->get_perm_list($myPermArr,'o'), $permByType['o']); } //create some permissions. @@ -198,39 +152,39 @@ $usePermString = 'rwxrw-r--'; $usePermName = __METHOD__ .'/test1'; - $this->assertFalse($perm->permission_exists($usePermName)); - $permId = $perm->create_permission($usePermName, $myUid, 1, $usePermString); - $this->assertTrue($perm->permission_exists($usePermName)); + $this->assertFalse($this->permObj->permission_exists($usePermName)); + $permId = $this->permObj->create_permission($usePermName, $myUid, 1, $usePermString); + $this->assertTrue($this->permObj->permission_exists($usePermName)); $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); + $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_permission_by_id($permId)), $usePermString); + $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_object_by_id($permId)), $usePermString); + $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_permission($usePermName)), $usePermString); + $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_object($usePermName)), $usePermString); //check to make sure individual permission requests work as expected. - $this->assertTrue($perm->has_read_permission($myUid, $usePermName)); - $this->assertTrue($perm->has_write_permission($myUid, $usePermName)); - $this->assertTrue($perm->has_execute_permission($myUid, $usePermName)); + $this->assertTrue($this->permObj->has_read_permission($myUid, $usePermName)); + $this->assertTrue($this->permObj->has_write_permission($myUid, $usePermName)); + $this->assertTrue($this->permObj->has_execute_permission($myUid, $usePermName)); //make sure "anonymous" permissions are correct. - $this->assertTrue($perm->has_read_permission(0,$usePermName)); - $this->assertFalse($perm->has_write_permission(0,$usePermName)); - $this->assertFalse($perm->has_execute_permission(0,$usePermName)); + $this->assertTrue($this->permObj->has_read_permission(0,$usePermName)); + $this->assertFalse($this->permObj->has_write_permission(0,$usePermName)); + $this->assertFalse($this->permObj->has_execute_permission(0,$usePermName)); //put a second user into the proper user_group, then test group permissions. $secondUser = $this->validUsers[$userKeys[1]]['uid']; - $this->assertTrue(is_numeric($perm->create_user_group($secondUser, 1))); - $this->assertTrue($perm->has_read_permission($secondUser, $usePermName)); - $this->assertTrue($perm->has_write_permission($secondUser, $usePermName)); - $this->assertFalse($perm->has_execute_permission($secondUser, $usePermName)); + $this->assertTrue(is_numeric($this->permObj->create_user_group($secondUser, 1))); + $this->assertTrue($this->permObj->has_read_permission($secondUser, $usePermName)); + $this->assertTrue($this->permObj->has_write_permission($secondUser, $usePermName)); + $this->assertFalse($this->permObj->has_execute_permission($secondUser, $usePermName)); //test a THIRD user (non-zero uid), make sure they are subject to the same permissions as anonymous (uid=0) $thirdUser = $this->validUsers[$userKeys[2]]['uid']; - $this->assertEqual($perm->has_read_permission(0,$usePermName), $perm->has_read_permission($thirdUser,$usePermName)); - $this->assertEqual($perm->has_write_permission(0,$usePermName), $perm->has_write_permission($thirdUser,$usePermName)); - $this->assertEqual($perm->has_execute_permission(0,$usePermName), $perm->has_execute_permission($thirdUser,$usePermName)); + $this->assertEqual($this->permObj->has_read_permission(0,$usePermName), $this->permObj->has_read_permission($thirdUser,$usePermName)); + $this->assertEqual($this->permObj->has_write_permission(0,$usePermName), $this->permObj->has_write_permission($thirdUser,$usePermName)); + $this->assertEqual($this->permObj->has_execute_permission(0,$usePermName), $this->permObj->has_execute_permission($thirdUser,$usePermName)); } }//end test_permissions //-------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |