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;
}
/**
* Check if the user identified by the given credentials is available
*
@ -154,8 +153,6 @@ class DbUserBackend implements UserBackend
*/
public function authenticate(Credential $credential)
{
$this->assertDbConnection();
try {
$salt = $this->getUserSalt($credential->getUsername());
} catch (Exception $e) {
@ -196,8 +193,6 @@ class DbUserBackend implements UserBackend
*/
private function getUserSalt($username)
{
$this->assertDbConnection();
$res = $this->db->select()
->from($this->userTable, self::SALT_COLUMN)
->where(self::USER_NAME_COLUMN.' = ?', $username)
@ -218,9 +213,6 @@ class DbUserBackend implements UserBackend
*/
private function getUserByName($username)
{
$this->assertDbConnection();
$this->db->getConnection();
$res = $this->db->
select()->from($this->userTable)
@ -249,32 +241,16 @@ class DbUserBackend implements UserBackend
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
*
* 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
* @see UserBackend::getUserCount
* @return int The number of users set in this backend
* @see UserBackend::getUserCount
*/
public function getUserCount()
{
$this->db->getConnection();
$query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query();
return $query->fetch()->count;
}

View File

@ -153,6 +153,12 @@ class LdapUserBackend implements UserBackend
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()
{
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.php';
require_once 'Zend/Log.php';
require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php';
@ -172,7 +173,7 @@ class DbUserBackendTest extends BaseTestCase
$usr[self::SALT_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]
);
$resource->insert($this->testTable, $data);
@ -284,4 +285,30 @@ class DbUserBackendTest extends BaseTestCase
$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;
}
/**
* 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');
}
}