2
0

BasicSqliteTest.php 11 KB

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