2
0

BasicSqliteTest.php 16 KB

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