BasicSqliteTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-2009 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-2009 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->_db = new Zend_Db_Adapter_Pdo_Sqlite(
  73. array('dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE)
  74. );
  75. $sqlCreate = 'CREATE TABLE [users] ( '
  76. . '[id] INTEGER NOT NULL PRIMARY KEY, '
  77. . '[username] VARCHAR(50) NOT NULL, '
  78. . '[password] VARCHAR(32) NULL, '
  79. . '[real_name] VARCHAR(150) NULL)';
  80. $this->_db->query($sqlCreate);
  81. $sqlInsert = 'INSERT INTO users (username, password, real_name) '
  82. . 'VALUES ("my_username", "my_password", "My Real Name")';
  83. $this->_db->query($sqlInsert);
  84. $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password');
  85. }
  86. /**
  87. * Ensures expected behavior for authentication success
  88. *
  89. * @return void
  90. */
  91. public function testAuthenticateSuccess()
  92. {
  93. $this->_adapter->setIdentity('my_username');
  94. $this->_adapter->setCredential('my_password');
  95. $result = $this->_adapter->authenticate();
  96. $this->assertTrue($result->isValid());
  97. }
  98. /**
  99. * Ensures expected behavior for authentication success
  100. *
  101. * @return void
  102. */
  103. public function testAuthenticateSuccessWithTreatment()
  104. {
  105. $this->_adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username', 'password', '?');
  106. $this->_adapter->setIdentity('my_username');
  107. $this->_adapter->setCredential('my_password');
  108. $result = $this->_adapter->authenticate();
  109. $this->assertTrue($result->isValid());
  110. }
  111. /**
  112. * Ensures expected behavior for for authentication failure
  113. * reason: Identity not found.
  114. *
  115. */
  116. public function testAuthenticateFailureIdentityNotFound()
  117. {
  118. $this->_adapter->setIdentity('non_existent_username');
  119. $this->_adapter->setCredential('my_password');
  120. try {
  121. $result = $this->_adapter->authenticate();
  122. $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $result->getCode());
  123. } catch (Zend_Auth_Exception $e) {
  124. $this->fail('Exception should have been thrown');
  125. }
  126. }
  127. /**
  128. * Ensures expected behavior for for authentication failure
  129. * reason: Identity not found.
  130. *
  131. */
  132. public function testAuthenticateFailureIdentityAmbigious()
  133. {
  134. $sql_insert = 'INSERT INTO users (username, password, real_name) VALUES ("my_username", "my_password", "My Real Name")';
  135. $this->_db->query($sql_insert);
  136. $this->_adapter->setIdentity('my_username');
  137. $this->_adapter->setCredential('my_password');
  138. try {
  139. $result = $this->_adapter->authenticate();
  140. $this->assertEquals(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $result->getCode());
  141. } catch (Zend_Auth_Exception $e) {
  142. $this->fail('Exception should have been thrown');
  143. }
  144. }
  145. /**
  146. * Ensures expected behavior for authentication failure because of a bad password
  147. *
  148. * @return void
  149. */
  150. public function testAuthenticateFailureInvalidCredential()
  151. {
  152. $this->_adapter->setIdentity('my_username');
  153. $this->_adapter->setCredential('my_password_bad');
  154. $result = $this->_adapter->authenticate();
  155. $this->assertFalse($result->isValid());
  156. }
  157. /**
  158. * Ensures that getResultRowObject() works for successful authentication
  159. *
  160. * @return void
  161. */
  162. public function testGetResultRow()
  163. {
  164. $this->_adapter->setIdentity('my_username');
  165. $this->_adapter->setCredential('my_password');
  166. $result = $this->_adapter->authenticate();
  167. $resultRow = $this->_adapter->getResultRowObject();
  168. $this->assertEquals($resultRow->username, 'my_username');
  169. }
  170. /**
  171. * Ensure that ResultRowObject returns only what told to be included
  172. *
  173. */
  174. public function testGetSpecificResultRow()
  175. {
  176. $this->_adapter->setIdentity('my_username');
  177. $this->_adapter->setCredential('my_password');
  178. $result = $this->_adapter->authenticate();
  179. $resultRow = $this->_adapter->getResultRowObject(array('username', 'real_name'));
  180. $this->assertEquals('O:8:"stdClass":2:{s:8:"username";s:11:"my_username";s:9:"real_name";s:12:"My Real Name";}', serialize($resultRow));
  181. }
  182. /**
  183. * Ensure that ResultRowObject returns an object has specific omissions
  184. *
  185. */
  186. public function testGetOmittedResultRow()
  187. {
  188. $this->_adapter->setIdentity('my_username');
  189. $this->_adapter->setCredential('my_password');
  190. $result = $this->_adapter->authenticate();
  191. $resultRow = $this->_adapter->getResultRowObject(null, 'password');
  192. $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));
  193. }
  194. /**
  195. * @group ZF-5957
  196. */
  197. public function testAdapterCanReturnDbSelectObject()
  198. {
  199. $this->assertTrue($this->_adapter->getDbSelect() instanceof Zend_Db_Select);
  200. }
  201. /**
  202. * @group ZF-5957
  203. */
  204. public function testAdapterCanUseModifiedDbSelectObject()
  205. {
  206. $this->_db->getProfiler()->setEnabled(true);
  207. $select = $this->_adapter->getDbSelect();
  208. $select->where('1 = 1');
  209. $this->_adapter->setIdentity('my_username');
  210. $this->_adapter->setCredential('my_password');
  211. $this->_adapter->authenticate();
  212. $profiler = $this->_db->getProfiler();
  213. $this->assertEquals(
  214. '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\')',
  215. $profiler->getLastQueryProfile()->getQuery()
  216. );
  217. }
  218. /**
  219. * @group ZF-5957
  220. */
  221. public function testAdapterReturnsASelectObjectWithoutAuthTimeModificationsAfterAuth()
  222. {
  223. $select = $this->_adapter->getDbSelect();
  224. $select->where('1 = 1');
  225. $this->_adapter->setIdentity('my_username');
  226. $this->_adapter->setCredential('my_password');
  227. $this->_adapter->authenticate();
  228. $selectAfterAuth = $this->_adapter->getDbSelect();
  229. $whereParts = $selectAfterAuth->getPart(Zend_Db_Select::WHERE);
  230. $this->assertEquals(1, count($whereParts));
  231. $this->assertEquals('(1 = 1)', array_pop($whereParts));
  232. }
  233. /**
  234. * Ensure that exceptions are caught
  235. *
  236. * @expectedException Zend_Auth_Exception
  237. */
  238. public function testCatchExceptionNoTable()
  239. {
  240. $adapter = new Zend_Auth_Adapter_DbTable($this->_db);
  241. $result = $adapter->authenticate();
  242. // $this->assertEquals($e->getMessage(), 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  243. }
  244. /**
  245. * Ensure that exceptions are caught
  246. *
  247. * @expectedException Zend_Auth_Exception
  248. */
  249. public function testCatchExceptionNoIdentityColumn()
  250. {
  251. $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users');
  252. $result = $adapter->authenticate();
  253. // $this->assertEquals($e->getMessage(), 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  254. }
  255. /**
  256. * Ensure that exceptions are caught
  257. *
  258. * @expectedException Zend_Auth_Exception
  259. */
  260. public function testCatchExceptionNoCredentialColumn()
  261. {
  262. $adapter = new Zend_Auth_Adapter_DbTable($this->_db, 'users', 'username');
  263. $result = $adapter->authenticate();
  264. // $this->assertEquals($e->getMessage(), 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.');
  265. }
  266. /**
  267. * Ensure that exceptions are caught
  268. *
  269. * @expectedException Zend_Auth_Exception
  270. */
  271. public function testCatchExceptionNoIdentity()
  272. {
  273. $result = $this->_adapter->authenticate();
  274. // $this->assertEquals($e->getMessage(), 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
  275. }
  276. /**
  277. * Ensure that exceptions are caught
  278. *
  279. * @expectedException Zend_Auth_Exception
  280. */
  281. public function testCatchExceptionNoCredential()
  282. {
  283. $this->_adapter->setIdentity('my_username');
  284. $result = $this->_adapter->authenticate();
  285. // $this->assertEquals($e->getMessage(), 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.');
  286. }
  287. /**
  288. * Ensure that exceptions are caught
  289. *
  290. * @expectedException Zend_Auth_Exception
  291. */
  292. public function testCatchExceptionBadSql()
  293. {
  294. $this->_adapter->setTableName('bad_table_name');
  295. $this->_adapter->setIdentity('value');
  296. $this->_adapter->setCredential('value');
  297. $result = $this->_adapter->authenticate();
  298. // $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.');
  299. }
  300. }
  301. class Zend_Auth_Adapter_DbTable_BasicSqliteTest_Skip extends Zend_Auth_Adapter_DbTable_BasicSqliteTest
  302. {
  303. public function setUp()
  304. {
  305. $this->markTestSkipped('Zend_Auth_Adapter_DbTable Sqlite tests are not enabled in TestConfiguration.php');
  306. }
  307. }