BasicSqliteTest.php 16 KB

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