BasicSqliteTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  23. /**
  24. * PHPUnit_Framework_TestCase
  25. */
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Sqlite
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
  30. /**
  31. * This is required because Zend_Db_Adapter_Pdo_Sqlite uses Zend_Db constants
  32. * but does not load the file containing the Zend_Db class.
  33. *
  34. * @see Zend_Db
  35. */
  36. require_once 'Zend/Db.php';
  37. /**
  38. * @see Zend_Auth_Adapter_DbTable
  39. */
  40. require_once 'Zend/Auth/Adapter/DbTable.php';
  41. /**
  42. * @category Zend
  43. * @package Zend_Auth
  44. * @subpackage UnitTests
  45. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. * @group Zend_Auth
  48. * @group Zend_Db_Table
  49. */
  50. class Zend_Auth_Adapter_DbTable_BasicSqliteTest extends PHPUnit_Framework_TestCase
  51. {
  52. /**
  53. * Sqlite database connection
  54. *
  55. * @var Zend_Db_Adapter_Pdo_Sqlite
  56. */
  57. protected $_db = null;
  58. /**
  59. * Database table authentication adapter
  60. *
  61. * @var Zend_Auth_Adapter_DbTable
  62. */
  63. protected $_adapter = null;
  64. /**
  65. * Set up test configuration
  66. *
  67. * @return void
  68. */
  69. public function setUp()
  70. {
  71. $this->_setupDbAdapter();
  72. $this->_setupAuthAdapter();
  73. }
  74. public function tearDown()
  75. {
  76. $this->_adapter = null;
  77. $this->_db->query('DROP TABLE [users]');
  78. $this->_db = null;
  79. }
  80. /**
  81. * Ensures expected behavior for authentication success
  82. *
  83. * @return void
  84. */
  85. public function testAuthenticateSuccess()
  86. {
  87. $this->_adapter->setIdentity('my_username');
  88. $this->_adapter->setCredential('my_password');
  89. $result = $this->_adapter->authenticate();
  90. $this->assertTrue($result->isValid());
  91. }
  92. /**
  93. * Ensures expected behavior for authentication success
  94. *
  95. * @return void
  96. */
  97. public function testAuthenticateSuccessWithTreatment()
  98. {
  99. $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password', '?');
  100. $this->_adapter->setIdentity('my_username');
  101. $this->_adapter->setCredential('my_password');
  102. $result = $this->_adapter->authenticate();
  103. $this->assertTrue($result->isValid());
  104. }
  105. /**
  106. * Ensures expected behavior for for authentication failure
  107. * reason: Identity not found.
  108. *
  109. */
  110. public function testAuthenticateFailureIdentityNotFound()
  111. {
  112. $this->_adapter->setIdentity('non_existent_username');
  113. $this->_adapter->setCredential('my_password');
  114. try {
  115. $result = $this->_adapter->authenticate();
  116. $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $result->getCode());
  117. } catch (Zend_Auth_Exception $e) {
  118. $this->fail('Exception should have been thrown');
  119. }
  120. }
  121. /**
  122. * Ensures expected behavior for for authentication failure
  123. * reason: Identity not found.
  124. *
  125. */
  126. public function testAuthenticateFailureIdentityAmbigious()
  127. {
  128. $sql_insert = 'INSERT INTO users (username, password, real_name) VALUES ("my_username", "my_password", "My Real Name")';
  129. $this->_db->query($sql_insert);
  130. $this->_adapter->setIdentity('my_username');
  131. $this->_adapter->setCredential('my_password');
  132. try {
  133. $result = $this->_adapter->authenticate();
  134. $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $result->getCode());
  135. } catch (Zend_Auth_Exception $e) {
  136. $this->fail('Exception should have been thrown');
  137. }
  138. }
  139. /**
  140. * Ensures expected behavior for authentication failure because of a bad password
  141. *
  142. * @return void
  143. */
  144. public function testAuthenticateFailureInvalidCredential()
  145. {
  146. $this->_adapter->setIdentity('my_username');
  147. $this->_adapter->setCredential('my_password_bad');
  148. $result = $this->_adapter->authenticate();
  149. $this->assertFalse($result->isValid());
  150. }
  151. /**
  152. * Ensures that getResultRowObject() works for successful authentication
  153. *
  154. * @return void
  155. */
  156. public function testGetResultRow()
  157. {
  158. $this->_adapter->setIdentity('my_username');
  159. $this->_adapter->setCredential('my_password');
  160. $result = $this->_adapter->authenticate();
  161. $resultRow = $this->_adapter->getResultRowObject();
  162. $this->assertEquals($resultRow->username, 'my_username');
  163. }
  164. /**
  165. * Ensure that ResultRowObject returns only what told to be included
  166. *
  167. */
  168. public function testGetSpecificResultRow()
  169. {
  170. $this->_adapter->setIdentity('my_username');
  171. $this->_adapter->setCredential('my_password');
  172. $result = $this->_adapter->authenticate();
  173. $resultRow = $this->_adapter->getResultRowObject(array('username', 'real_name'));
  174. $this->assertEquals('O:8:"stdClass":2:{s:8:"username";s:11:"my_username";s:9:"real_name";s:12:"My Real Name";}', serialize($resultRow));
  175. }
  176. /**
  177. * Ensure that ResultRowObject returns an object has specific omissions
  178. *
  179. */
  180. public function testGetOmittedResultRow()
  181. {
  182. $this->_adapter->setIdentity('my_username');
  183. $this->_adapter->setCredential('my_password');
  184. $result = $this->_adapter->authenticate();
  185. $resultRow = $this->_adapter->getResultRowObject(null, 'password');
  186. $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));
  187. }
  188. /**
  189. * @group ZF-5957
  190. */
  191. public function testAdapterCanReturnDbSelectObject()
  192. {
  193. $this->assertTrue($this->_adapter->getDbSelect() instanceof Zend_Db_Select);
  194. }
  195. /**
  196. * @group ZF-5957
  197. */
  198. public function testAdapterCanUseModifiedDbSelectObject()
  199. {
  200. $this->_db->getProfiler()->setEnabled(true);
  201. $select = $this->_adapter->getDbSelect();
  202. $select->where('1 = 1');
  203. $this->_adapter->setIdentity('my_username');
  204. $this->_adapter->setCredential('my_password');
  205. $this->_adapter->authenticate();
  206. $profiler = $this->_db->getProfiler();
  207. $this->assertEquals(
  208. '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\')',
  209. $profiler->getLastQueryProfile()->getQuery()
  210. );
  211. }
  212. /**
  213. * @group ZF-5957
  214. */
  215. public function testAdapterReturnsASelectObjectWithoutAuthTimeModificationsAfterAuth()
  216. {
  217. $select = $this->_adapter->getDbSelect();
  218. $select->where('1 = 1');
  219. $this->_adapter->setIdentity('my_username');
  220. $this->_adapter->setCredential('my_password');
  221. $this->_adapter->authenticate();
  222. $selectAfterAuth = $this->_adapter->getDbSelect();
  223. $whereParts = $selectAfterAuth->getPart(Zend_Db_Select::WHERE);
  224. $this->assertEquals(1, count($whereParts));
  225. $this->assertEquals('(1 = 1)', array_pop($whereParts));
  226. }
  227. /**
  228. * Ensure that exceptions are caught
  229. *
  230. * @expectedException Zend_Auth_Exception
  231. */
  232. public function testCatchExceptionNoTable()
  233. {
  234. $adapter = new Zend_Auth_Adapter_DbTable($this->_db);
  235. $result = $adapter->authenticate();
  236. // $this->assertEquals($e->getMessage(), 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  237. }
  238. /**
  239. * Ensure that exceptions are caught
  240. *
  241. * @expectedException Zend_Auth_Exception
  242. */
  243. public function testCatchExceptionNoIdentityColumn()
  244. {
  245. $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users');
  246. $result = $adapter->authenticate();
  247. // $this->assertEquals($e->getMessage(), 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  248. }
  249. /**
  250. * Ensure that exceptions are caught
  251. *
  252. * @expectedException Zend_Auth_Exception
  253. */
  254. public function testCatchExceptionNoCredentialColumn()
  255. {
  256. $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username');
  257. $result = $adapter->authenticate();
  258. // $this->assertEquals($e->getMessage(), 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  259. }
  260. /**
  261. * Ensure that exceptions are caught
  262. *
  263. * @expectedException Zend_Auth_Exception
  264. */
  265. public function testCatchExceptionNoIdentity()
  266. {
  267. $result = $this->_adapter->authenticate();
  268. // $this->assertEquals($e->getMessage(), 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
  269. }
  270. /**
  271. * Ensure that exceptions are caught
  272. *
  273. * @expectedException Zend_Auth_Exception
  274. */
  275. public function testCatchExceptionNoCredential()
  276. {
  277. $this->_adapter->setIdentity('my_username');
  278. $result = $this->_adapter->authenticate();
  279. // $this->assertEquals($e->getMessage(), 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
  280. }
  281. /**
  282. * Ensure that exceptions are caught
  283. *
  284. * @expectedException Zend_Auth_Exception
  285. */
  286. public function testCatchExceptionBadSql()
  287. {
  288. $this->_adapter->setTableName('bad_table_name');
  289. $this->_adapter->setIdentity('value');
  290. $this->_adapter->setCredential('value');
  291. $result = $this->_adapter->authenticate();
  292. // $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.');
  293. }
  294. /**
  295. *
  296. * @group ZF-3068
  297. */
  298. public function testDbTableAdapterUsesCaseFolding()
  299. {
  300. $this->tearDown();
  301. $this->_setupDbAdapter(array(Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER));
  302. $this->_setupAuthAdapter();
  303. $this->_adapter->setIdentity('my_username');
  304. $this->_adapter->setCredential('my_password');
  305. $this->_db->foldCase(Zend_Db::CASE_UPPER);
  306. $this->_adapter->authenticate();
  307. }
  308. /**
  309. * Test fallback to default database adapter, when no such adapter set
  310. *
  311. * @expectedException Zend_Auth_Adapter_Exception
  312. * @group ZF-7510
  313. */
  314. public function testAuthenticateWithDefaultDbAdapterNoAdapterException()
  315. {
  316. require_once('Zend/Db/Table/Abstract.php');
  317. // preserve default db adapter between cases
  318. $tmp = Zend_Db_Table_Abstract::getDefaultAdapter();
  319. // make sure that no default adapter exists
  320. Zend_Db_Table_Abstract::setDefaultAdapter(null);
  321. try {
  322. $this->_adapter = new Zend_Auth_Adapter_DbTable();
  323. } catch (Exception $e) {
  324. $this->assertContains('No database adapter present', $e->getMessage());
  325. throw $e;
  326. }
  327. // restore adapter
  328. Zend_Db_Table_Abstract::setDefaultAdapter($tmp);
  329. }
  330. /**
  331. * Test fallback to default database adapter
  332. *
  333. * @group ZF-7510
  334. */
  335. public function testAuthenticateWithDefaultDbAdapter()
  336. {
  337. require_once('Zend/Db/Table/Abstract.php');
  338. // preserve default adapter between cases
  339. $tmp = Zend_Db_Table_Abstract::getDefaultAdapter();
  340. // make sure that default db adapter exists
  341. Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
  342. // check w/o passing adapter
  343. $this->_adapter = new Zend_Auth_Adapter_DbTable();
  344. $this->_adapter
  345. ->setTableName('users')
  346. ->setIdentityColumn('username')
  347. ->setCredentialColumn('password')
  348. ->setTableName('users')
  349. ->setIdentity('my_username')
  350. ->setCredential('my_password');
  351. $result = $this->_adapter->authenticate();
  352. $this->assertTrue($result->isValid());
  353. // restore adapter
  354. Zend_Db_Table_Abstract::setDefaultAdapter($tmp);
  355. }
  356. /**
  357. * Test to see same usernames with different passwords can not authenticate
  358. * when flag is not set. This is the current state of
  359. * Zend_Auth_Adapter_DbTable (up to ZF 1.10.6)
  360. *
  361. * @group ZF-7289
  362. */
  363. public function testEqualUsernamesDifferentPasswordShouldNotAuthenticateWhenFlagIsNotSet()
  364. {
  365. $this->_db->insert('users', array (
  366. 'username' => 'my_username',
  367. 'password' => 'my_otherpass',
  368. 'real_name' => 'Test user 2',
  369. ));
  370. // test if user 1 can authenticate
  371. $this->_adapter->setIdentity('my_username')
  372. ->setCredential('my_password');
  373. $result = $this->_adapter->authenticate();
  374. $this->assertTrue(in_array('More than one record matches the supplied identity.',
  375. $result->getMessages()));
  376. $this->assertFalse($result->isValid());
  377. }
  378. /**
  379. * Test to see same usernames with different passwords can authenticate when
  380. * a flag is set
  381. *
  382. * @group ZF-7289
  383. */
  384. public function testEqualUsernamesDifferentPasswordShouldAuthenticateWhenFlagIsSet()
  385. {
  386. $this->_db->insert('users', array (
  387. 'username' => 'my_username',
  388. 'password' => 'my_otherpass',
  389. 'real_name' => 'Test user 2',
  390. ));
  391. // test if user 1 can authenticate
  392. $this->_adapter->setIdentity('my_username')
  393. ->setCredential('my_password')
  394. ->setAmbiguityIdentity(true);
  395. $result = $this->_adapter->authenticate();
  396. $this->assertFalse(in_array('More than one record matches the supplied identity.',
  397. $result->getMessages()));
  398. $this->assertTrue($result->isValid());
  399. $this->assertEquals('my_username', $result->getIdentity());
  400. $this->_adapter = null;
  401. $this->_setupAuthAdapter();
  402. // test if user 2 can authenticate
  403. $this->_adapter->setIdentity('my_username')
  404. ->setCredential('my_otherpass')
  405. ->setAmbiguityIdentity(true);
  406. $result2 = $this->_adapter->authenticate();
  407. $this->assertFalse(in_array('More than one record matches the supplied identity.',
  408. $result->getMessages()));
  409. $this->assertTrue($result->isValid());
  410. $this->assertEquals('my_username', $result->getIdentity());
  411. }
  412. protected function _setupDbAdapter($optionalParams = array())
  413. {
  414. $params = array('dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE);
  415. if (!empty($optionalParams)) {
  416. $params['options'] = $optionalParams;
  417. }
  418. $this->_db = new Zend_Db_Adapter_Pdo_Sqlite($params);
  419. $sqlCreate = 'CREATE TABLE [users] ( '
  420. . '[id] INTEGER NOT NULL PRIMARY KEY, '
  421. . '[username] VARCHAR(50) NOT NULL, '
  422. . '[password] VARCHAR(32) NULL, '
  423. . '[real_name] VARCHAR(150) NULL)';
  424. $this->_db->query($sqlCreate);
  425. $sqlInsert = 'INSERT INTO users (username, password, real_name) '
  426. . 'VALUES ("my_username", "my_password", "My Real Name")';
  427. $this->_db->query($sqlInsert);
  428. }
  429. protected function _setupAuthAdapter()
  430. {
  431. $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password');
  432. }
  433. }
  434. class Zend_Auth_Adapter_DbTable_BasicSqliteTest_Skip extends Zend_Auth_Adapter_DbTable_BasicSqliteTest
  435. {
  436. public function setUp()
  437. {
  438. $this->markTestSkipped('Zend_Auth_Adapter_DbTable Sqlite tests are not enabled in TestConfiguration.php');
  439. }
  440. }