UserBackend: Fix interface in test

refs #4593
This commit is contained in:
Marius Hein 2013-08-30 15:31:21 +02:00
parent e2e744a424
commit 446da85a88
4 changed files with 49 additions and 27 deletions

View File

@ -131,7 +131,6 @@ class DbUserBackend implements UserBackend
return $this->name; return $this->name;
} }
/** /**
* Check if the user identified by the given credentials is available * Check if the user identified by the given credentials is available
* *
@ -154,8 +153,6 @@ class DbUserBackend implements UserBackend
*/ */
public function authenticate(Credential $credential) public function authenticate(Credential $credential)
{ {
$this->assertDbConnection();
try { try {
$salt = $this->getUserSalt($credential->getUsername()); $salt = $this->getUserSalt($credential->getUsername());
} catch (Exception $e) { } catch (Exception $e) {
@ -196,8 +193,6 @@ class DbUserBackend implements UserBackend
*/ */
private function getUserSalt($username) private function getUserSalt($username)
{ {
$this->assertDbConnection();
$res = $this->db->select() $res = $this->db->select()
->from($this->userTable, self::SALT_COLUMN) ->from($this->userTable, self::SALT_COLUMN)
->where(self::USER_NAME_COLUMN.' = ?', $username) ->where(self::USER_NAME_COLUMN.' = ?', $username)
@ -218,9 +213,6 @@ class DbUserBackend implements UserBackend
*/ */
private function getUserByName($username) private function getUserByName($username)
{ {
$this->assertDbConnection();
$this->db->getConnection(); $this->db->getConnection();
$res = $this->db-> $res = $this->db->
select()->from($this->userTable) select()->from($this->userTable)
@ -249,32 +241,16 @@ class DbUserBackend implements UserBackend
return $usr; return $usr;
} }
/**
* Assert a valid database connection
*
* @throws ConfigurationError
*/
private function assertDbConnection()
{
if ($this->db === null) {
$msg = 'DbUserBackend ' . $this->getName() . ' has no valid database connection.';
Logger::fatal($msg);
throw new ConfigurationError($msg);
}
}
/** /**
* Return the number of users in this database connection * Return the number of users in this database connection
* *
* This class is mainly used for determining whether the authentication backend is valid or not * This class is mainly used for determining whether the authentication backend is valid or not
* *
* @return int The number of users set in this backend * @return int The number of users set in this backend
* @see UserBackend::getUserCount * @see UserBackend::getUserCount
*/ */
public function getUserCount() public function getUserCount()
{ {
$this->db->getConnection();
$query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query(); $query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query();
return $query->fetch()->count; return $query->fetch()->count;
} }

View File

@ -153,6 +153,12 @@ class LdapUserBackend implements UserBackend
return $user; return $user;
} }
/**
* Return number of users in this backend
*
* @return int The number of users set in this backend
* @see UserBackend::getUserCount
*/
public function getUserCount() public function getUserCount()
{ {
return $this->connection->count( return $this->connection->count(

View File

@ -40,6 +40,7 @@ require_once 'Zend/Config/Ini.php';
require_once 'Zend/Db/Adapter/Abstract.php'; require_once 'Zend/Db/Adapter/Abstract.php';
require_once 'Zend/Db.php'; require_once 'Zend/Db.php';
require_once 'Zend/Log.php'; require_once 'Zend/Log.php';
require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php'; require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php'; require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php';
@ -172,7 +173,7 @@ class DbUserBackendTest extends BaseTestCase
$usr[self::SALT_COLUMN], $usr[self::SALT_COLUMN],
$usr[self::PASSWORD_COLUMN] $usr[self::PASSWORD_COLUMN]
), ),
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN], self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
self::SALT_COLUMN => $usr[self::SALT_COLUMN] self::SALT_COLUMN => $usr[self::SALT_COLUMN]
); );
$resource->insert($this->testTable, $data); $resource->insert($this->testTable, $data);
@ -284,4 +285,30 @@ class DbUserBackendTest extends BaseTestCase
$this->assertSame($testName, $backend->getName()); $this->assertSame($testName, $backend->getName());
} }
/**
* @dataProvider mysqlDb
*/
public function testCountUsersMySql($db)
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$this->assertGreaterThan(0, $backend->getUserCount());
}
/**
* @dataProvider pgsqlDb
*/
public function testCountUsersPgSql($db)
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$this->assertGreaterThan(0, $backend->getUserCount());
}
} }

View File

@ -88,4 +88,17 @@ class ErrorProneBackendMock implements UserBackend
{ {
return $this->name; return $this->name;
} }
/**
* Get the number of users available through this backend
*
* @return int
* @throws Exception
*/
public function getUserCount()
{
throw new Exception('getUserCount error: No users in this error prone backend');
}
} }