| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Auth
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_Adapter_Pdo_Sqlite
- */
- require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
- /**
- * This is required because Zend_Db_Adapter_Pdo_Sqlite uses Zend_Db constants
- * but does not load the file containing the Zend_Db class.
- *
- * @see Zend_Db
- */
- require_once 'Zend/Db.php';
- /**
- * @see Zend_Auth_Adapter_DbTable
- */
- require_once 'Zend/Auth/Adapter/DbTable.php';
- /**
- * @category Zend
- * @package Zend_Auth
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Auth
- * @group Zend_Db_Table
- */
- class Zend_Auth_Adapter_DbTable_BasicSqliteTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Sqlite database connection
- *
- * @var Zend_Db_Adapter_Pdo_Sqlite
- */
- protected $_db = null;
- /**
- * Database table authentication adapter
- *
- * @var Zend_Auth_Adapter_DbTable
- */
- protected $_adapter = null;
- /**
- * Set up test configuration
- *
- * @return void
- */
- public function setUp()
- {
- $this->_setupDbAdapter();
- $this->_setupAuthAdapter();
- }
- public function tearDown()
- {
- $this->_adapter = null;
- $this->_db->query('DROP TABLE [users]');
- $this->_db = null;
- }
- /**
- * Ensures expected behavior for authentication success
- *
- * @return void
- */
- public function testAuthenticateSuccess()
- {
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $this->assertTrue($result->isValid());
- }
- /**
- * Ensures expected behavior for authentication success
- *
- * @return void
- */
- public function testAuthenticateSuccessWithTreatment()
- {
- $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password', '?');
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $this->assertTrue($result->isValid());
- }
- /**
- * Ensures expected behavior for for authentication failure
- * reason: Identity not found.
- *
- */
- public function testAuthenticateFailureIdentityNotFound()
- {
- $this->_adapter->setIdentity('non_existent_username');
- $this->_adapter->setCredential('my_password');
- try {
- $result = $this->_adapter->authenticate();
- $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $result->getCode());
- } catch (Zend_Auth_Exception $e) {
- $this->fail('Exception should have been thrown');
- }
- }
- /**
- * Ensures expected behavior for for authentication failure
- * reason: Identity not found.
- *
- */
- public function testAuthenticateFailureIdentityAmbigious()
- {
- $sql_insert = 'INSERT INTO users (username, password, real_name) VALUES ("my_username", "my_password", "My Real Name")';
- $this->_db->query($sql_insert);
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- try {
- $result = $this->_adapter->authenticate();
- $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $result->getCode());
- } catch (Zend_Auth_Exception $e) {
- $this->fail('Exception should have been thrown');
- }
- }
- /**
- * Ensures expected behavior for authentication failure because of a bad password
- *
- * @return void
- */
- public function testAuthenticateFailureInvalidCredential()
- {
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password_bad');
- $result = $this->_adapter->authenticate();
- $this->assertFalse($result->isValid());
- }
- /**
- * Ensures that getResultRowObject() works for successful authentication
- *
- * @return void
- */
- public function testGetResultRow()
- {
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $resultRow = $this->_adapter->getResultRowObject();
- $this->assertEquals($resultRow->username, 'my_username');
- }
- /**
- * Ensure that ResultRowObject returns only what told to be included
- *
- */
- public function testGetSpecificResultRow()
- {
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $resultRow = $this->_adapter->getResultRowObject(array('username', 'real_name'));
- $this->assertEquals('O:8:"stdClass":2:{s:8:"username";s:11:"my_username";s:9:"real_name";s:12:"My Real Name";}', serialize($resultRow));
- }
- /**
- * Ensure that ResultRowObject returns an object has specific omissions
- *
- */
- public function testGetOmittedResultRow()
- {
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $resultRow = $this->_adapter->getResultRowObject(null, 'password');
- $this->assertEquals('O:8:"stdClass":3:{s:2:"id";s:1:"1";s:8:"username";s:11:"my_username";s:9:"real_name";s:12:"My Real Name";}', serialize($resultRow));
- }
- /**
- * @group ZF-5957
- */
- public function testAdapterCanReturnDbSelectObject()
- {
- $this->assertTrue($this->_adapter->getDbSelect() instanceof Zend_Db_Select);
- }
- /**
- * @group ZF-5957
- */
- public function testAdapterCanUseModifiedDbSelectObject()
- {
- $this->_db->getProfiler()->setEnabled(true);
- $select = $this->_adapter->getDbSelect();
- $select->where('1 = 1');
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $this->_adapter->authenticate();
- $profiler = $this->_db->getProfiler();
- $this->assertEquals(
- 'SELECT "users".*, (CASE WHEN "password" = \'my_password\' THEN 1 ELSE 0 END) AS "zend_auth_credential_match" FROM "users" WHERE (1 = 1) AND ("username" = \'my_username\')',
- $profiler->getLastQueryProfile()->getQuery()
- );
- }
- /**
- * @group ZF-5957
- */
- public function testAdapterReturnsASelectObjectWithoutAuthTimeModificationsAfterAuth()
- {
- $select = $this->_adapter->getDbSelect();
- $select->where('1 = 1');
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $this->_adapter->authenticate();
- $selectAfterAuth = $this->_adapter->getDbSelect();
- $whereParts = $selectAfterAuth->getPart(Zend_Db_Select::WHERE);
- $this->assertEquals(1, count($whereParts));
- $this->assertEquals('(1 = 1)', array_pop($whereParts));
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionNoTable()
- {
- $adapter = new Zend_Auth_Adapter_DbTable($this->_db);
- $result = $adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionNoIdentityColumn()
- {
- $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users');
- $result = $adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionNoCredentialColumn()
- {
- $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username');
- $result = $adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionNoIdentity()
- {
- $result = $this->_adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionNoCredential()
- {
- $this->_adapter->setIdentity('my_username');
- $result = $this->_adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
- }
- /**
- * Ensure that exceptions are caught
- *
- * @expectedException Zend_Auth_Exception
- */
- public function testCatchExceptionBadSql()
- {
- $this->_adapter->setTableName('bad_table_name');
- $this->_adapter->setIdentity('value');
- $this->_adapter->setCredential('value');
- $result = $this->_adapter->authenticate();
- // $this->assertEquals($e->getMessage(), 'The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.');
- }
- /**
- *
- * @group ZF-3068
- */
- public function testDbTableAdapterUsesCaseFolding()
- {
- $this->tearDown();
- $this->_setupDbAdapter(array(Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER));
- $this->_setupAuthAdapter();
- $this->_adapter->setIdentity('my_username');
- $this->_adapter->setCredential('my_password');
- $this->_db->foldCase(Zend_Db::CASE_UPPER);
- $this->_adapter->authenticate();
- }
- /**
- * Test fallback to default database adapter, when no such adapter set
- *
- * @expectedException Zend_Auth_Adapter_Exception
- * @group ZF-7510
- */
- public function testAuthenticateWithDefaultDbAdapterNoAdapterException()
- {
- require_once('Zend/Db/Table/Abstract.php');
- // preserve default db adapter between cases
- $tmp = Zend_Db_Table_Abstract::getDefaultAdapter();
- // make sure that no default adapter exists
- Zend_Db_Table_Abstract::setDefaultAdapter(null);
- try {
- $this->_adapter = new Zend_Auth_Adapter_DbTable();
- } catch (Exception $e) {
- $this->assertContains('No database adapter present', $e->getMessage());
- throw $e;
- }
- // restore adapter
- Zend_Db_Table_Abstract::setDefaultAdapter($tmp);
- }
- /**
- * Test fallback to default database adapter
- *
- * @group ZF-7510
- */
- public function testAuthenticateWithDefaultDbAdapter()
- {
- require_once('Zend/Db/Table/Abstract.php');
- // preserve default adapter between cases
- $tmp = Zend_Db_Table_Abstract::getDefaultAdapter();
- // make sure that default db adapter exists
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
- // check w/o passing adapter
- $this->_adapter = new Zend_Auth_Adapter_DbTable();
- $this->_adapter
- ->setTableName('users')
- ->setIdentityColumn('username')
- ->setCredentialColumn('password')
- ->setTableName('users')
- ->setIdentity('my_username')
- ->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $this->assertTrue($result->isValid());
- // restore adapter
- Zend_Db_Table_Abstract::setDefaultAdapter($tmp);
- }
- /**
- * Test to see same usernames with different passwords can not authenticate
- * when flag is not set. This is the current state of
- * Zend_Auth_Adapter_DbTable (up to ZF 1.10.6)
- *
- * @group ZF-7289
- */
- public function testEqualUsernamesDifferentPasswordShouldNotAuthenticateWhenFlagIsNotSet()
- {
- $this->_db->insert('users', array (
- 'username' => 'my_username',
- 'password' => 'my_otherpass',
- 'real_name' => 'Test user 2',
- ));
- // test if user 1 can authenticate
- $this->_adapter->setIdentity('my_username')
- ->setCredential('my_password');
- $result = $this->_adapter->authenticate();
- $this->assertTrue(in_array('More than one record matches the supplied identity.',
- $result->getMessages()));
- $this->assertFalse($result->isValid());
- }
- /**
- * Test to see same usernames with different passwords can authenticate when
- * a flag is set
- *
- * @group ZF-7289
- */
- public function testEqualUsernamesDifferentPasswordShouldAuthenticateWhenFlagIsSet()
- {
- $this->_db->insert('users', array (
- 'username' => 'my_username',
- 'password' => 'my_otherpass',
- 'real_name' => 'Test user 2',
- ));
- // test if user 1 can authenticate
- $this->_adapter->setIdentity('my_username')
- ->setCredential('my_password')
- ->setAmbiguityIdentity(true);
- $result = $this->_adapter->authenticate();
- $this->assertFalse(in_array('More than one record matches the supplied identity.',
- $result->getMessages()));
- $this->assertTrue($result->isValid());
- $this->assertEquals('my_username', $result->getIdentity());
- $this->_adapter = null;
- $this->_setupAuthAdapter();
- // test if user 2 can authenticate
- $this->_adapter->setIdentity('my_username')
- ->setCredential('my_otherpass')
- ->setAmbiguityIdentity(true);
- $result2 = $this->_adapter->authenticate();
- $this->assertFalse(in_array('More than one record matches the supplied identity.',
- $result->getMessages()));
- $this->assertTrue($result->isValid());
- $this->assertEquals('my_username', $result->getIdentity());
- }
- protected function _setupDbAdapter($optionalParams = array())
- {
- $params = array('dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE);
- if (!empty($optionalParams)) {
- $params['options'] = $optionalParams;
- }
- $this->_db = new Zend_Db_Adapter_Pdo_Sqlite($params);
- $sqlCreate = 'CREATE TABLE [users] ( '
- . '[id] INTEGER NOT NULL PRIMARY KEY, '
- . '[username] VARCHAR(50) NOT NULL, '
- . '[password] VARCHAR(32) NULL, '
- . '[real_name] VARCHAR(150) NULL)';
- $this->_db->query($sqlCreate);
- $sqlInsert = 'INSERT INTO users (username, password, real_name) '
- . 'VALUES ("my_username", "my_password", "My Real Name")';
- $this->_db->query($sqlInsert);
- }
- protected function _setupAuthAdapter()
- {
- $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password');
- }
- }
- class Zend_Auth_Adapter_DbTable_BasicSqliteTest_Skip extends Zend_Auth_Adapter_DbTable_BasicSqliteTest
- {
- public function setUp()
- {
- $this->markTestSkipped('Zend_Auth_Adapter_DbTable Sqlite tests are not enabled in TestConfiguration.php');
- }
- }
|