Skip to content
This repository
Browse code

Merge branch 'hotfix/ZF2-199' of https://github.com/rhunwicks/zf2 int…

…o hotfix/zf2-199
  • Loading branch information...
commit 60c852d09b171f922f0c8cb5d80e49c7cd837846 2 parents ab7c575 + d532378
Matthew Weier O'Phinney authored April 09, 2012
168  library/Zend/Authentication/Adapter/DbTable.php
@@ -22,17 +22,12 @@
22 22
 namespace Zend\Authentication\Adapter;
23 23
 use Zend\Authentication\Adapter as AuthenticationAdapter,
24 24
     Zend\Authentication\Result as AuthenticationResult,
25  
-    Zend\Db\Db,
26  
-    Zend\Db\Adapter\AbstractAdapter as AbstractDBAdapter,
27  
-    Zend\Db\Expr as DBExpr,
28  
-    Zend\Db\Select as DBSelect,
29  
-    Zend\Db\Table\AbstractTable;
  25
+    Zend\Db\Adapter\Adapter as DbAdapter,
  26
+    Zend\Db\Sql\Select as DbSelect,
  27
+    Zend\Db\Sql\Expression,
  28
+    Zend\Db\ResultSet\ResultSet;
30 29
 
31 30
 /**
32  
- * @uses       Zend\Authentication\Adapter\Exception
33  
- * @uses       Zend\Authentication\Adapter
34  
- * @uses       Zend\Authentication\Result
35  
- * @uses       Zend_Db_Adapter_Abstract
36 31
  * @category   Zend
37 32
  * @package    Zend_Authentication
38 33
  * @subpackage Adapter
@@ -45,12 +40,12 @@ class DbTable implements AuthenticationAdapter
45 40
     /**
46 41
      * Database Connection
47 42
      *
48  
-     * @var Zend\Db\Adapter\AbstractAdapter
  43
+     * @var DbAdapter
49 44
      */
50 45
     protected $_zendDb = null;
51 46
 
52 47
     /**
53  
-     * @var Zend\Db\Select
  48
+     * @var DbSelect
54 49
      */
55 50
     protected $_dbSelect = null;
56 51
 
@@ -109,12 +104,12 @@ class DbTable implements AuthenticationAdapter
109 104
      * @var array
110 105
      */
111 106
     protected $_resultRow = null;
112  
-    
  107
+
113 108
     /**
114  
-     * $_ambiguityIdentity - Flag to indicate same Identity can be used with 
  109
+     * $_ambiguityIdentity - Flag to indicate same Identity can be used with
115 110
      * different credentials. Default is FALSE and need to be set to true to
116 111
      * allow ambiguity usage.
117  
-     * 
  112
+     *
118 113
      * @var boolean
119 114
      */
120 115
     protected $_ambiguityIdentity = false;
@@ -122,17 +117,17 @@ class DbTable implements AuthenticationAdapter
122 117
     /**
123 118
      * __construct() - Sets configuration options
124 119
      *
125  
-     * @param  Zend\Db\Adapter\AbstractAdapter $zendDb
  120
+     * @param  DbAdapter                $zendDb
126 121
      * @param  string                   $tableName
127 122
      * @param  string                   $identityColumn
128 123
      * @param  string                   $credentialColumn
129 124
      * @param  string                   $credentialTreatment
130 125
      * @return void
131 126
      */
132  
-    public function __construct(AbstractDBAdapter $zendDb = null, $tableName = null, $identityColumn = null,
  127
+    public function __construct(DbAdapter $zendDb, $tableName = null, $identityColumn = null,
133 128
                                 $credentialColumn = null, $credentialTreatment = null)
134 129
     {
135  
-        $this->_setDbAdapter($zendDb);
  130
+        $this->_zendDb = $zendDb;
136 131
 
137 132
         if (null !== $tableName) {
138 133
             $this->setTableName($tableName);
@@ -152,37 +147,10 @@ public function __construct(AbstractDBAdapter $zendDb = null, $tableName = null,
152 147
     }
153 148
 
154 149
     /**
155  
-     * _setDbAdapter() - set the database adapter to be used for quering
156  
-     *
157  
-     * @param Zend_Db_Adapter_Abstract 
158  
-     * @throws Zend_Auth_Adapter_Exception
159  
-     * @return Zend_Auth_Adapter_DbTable
160  
-     */
161  
-    protected function _setDbAdapter(AbstractDBAdapter $zendDb = null)
162  
-    {
163  
-        $this->_zendDb = $zendDb;
164  
-
165  
-        /**
166  
-         * If no adapter is specified, fetch default database adapter.
167  
-         */
168  
-        if(null === $this->_zendDb) {
169  
-            $this->_zendDb = AbstractTable::getDefaultAdapter();
170  
-            if (null === $this->_zendDb) {
171  
-                throw new Exception\RuntimeException(
172  
-                    'Null was provided for the adapter but there is no default'
173  
-                    . ' adatper registered with Zend\Db\Table to utilize.'
174  
-                    );
175  
-            }
176  
-        }
177  
-        
178  
-        return $this;
179  
-    }
180  
-
181  
-    /**
182 150
      * setTableName() - set the table name to be used in the select query
183 151
      *
184 152
      * @param  string $tableName
185  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  153
+     * @return DbTable Provides a fluent interface
186 154
      */
187 155
     public function setTableName($tableName)
188 156
     {
@@ -194,7 +162,7 @@ public function setTableName($tableName)
194 162
      * setIdentityColumn() - set the column name to be used as the identity column
195 163
      *
196 164
      * @param  string $identityColumn
197  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  165
+     * @return DbTable Provides a fluent interface
198 166
      */
199 167
     public function setIdentityColumn($identityColumn)
200 168
     {
@@ -206,7 +174,7 @@ public function setIdentityColumn($identityColumn)
206 174
      * setCredentialColumn() - set the column name to be used as the credential column
207 175
      *
208 176
      * @param  string $credentialColumn
209  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  177
+     * @return DbTable Provides a fluent interface
210 178
      */
211 179
     public function setCredentialColumn($credentialColumn)
212 180
     {
@@ -229,7 +197,7 @@ public function setCredentialColumn($credentialColumn)
229 197
      *  'MD5(?)'
230 198
      *
231 199
      * @param  string $treatment
232  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  200
+     * @return DbTable Provides a fluent interface
233 201
      */
234 202
     public function setCredentialTreatment($treatment)
235 203
     {
@@ -241,7 +209,7 @@ public function setCredentialTreatment($treatment)
241 209
      * setIdentity() - set the value to be used as the identity
242 210
      *
243 211
      * @param  string $value
244  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  212
+     * @return DbTable Provides a fluent interface
245 213
      */
246 214
     public function setIdentity($value)
247 215
     {
@@ -254,21 +222,21 @@ public function setIdentity($value)
254 222
      * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
255 223
      *
256 224
      * @param  string $credential
257  
-     * @return Zend\Authentication\Adapter\DbTable Provides a fluent interface
  225
+     * @return DbTable Provides a fluent interface
258 226
      */
259 227
     public function setCredential($credential)
260 228
     {
261 229
         $this->_credential = $credential;
262 230
         return $this;
263 231
     }
264  
-    
  232
+
265 233
     /**
266 234
      * setAmbiguityIdentity() - sets a flag for usage of identical identities
267 235
      * with unique credentials. It accepts integers (0, 1) or boolean (true,
268 236
      * false) parameters. Default is false.
269  
-     * 
  237
+     *
270 238
      * @param  int|bool $flag
271  
-     * @return Zend_Auth_Adapter_DbTable
  239
+     * @return DbTable Provides a fluent interface
272 240
      */
273 241
     public function setAmbiguityIdentity($flag)
274 242
     {
@@ -279,10 +247,11 @@ public function setAmbiguityIdentity($flag)
279 247
         }
280 248
         return $this;
281 249
     }
  250
+
282 251
     /**
283  
-     * getAmbiguityIdentity() - returns TRUE for usage of multiple identical 
  252
+     * getAmbiguityIdentity() - returns TRUE for usage of multiple identical
284 253
      * identies with different credentials, FALSE if not used.
285  
-     * 
  254
+     *
286 255
      * @return bool
287 256
      */
288 257
     public function getAmbiguityIdentity()
@@ -293,14 +262,13 @@ public function getAmbiguityIdentity()
293 262
     /**
294 263
      * getDbSelect() - Return the preauthentication Db Select object for userland select query modification
295 264
      *
296  
-     * @return Zend\Db\Select
  265
+     * @return DbSelect
297 266
      */
298 267
     public function getDbSelect()
299 268
     {
300 269
         if ($this->_dbSelect == null) {
301  
-            $this->_dbSelect = $this->_zendDb->select();
  270
+            $this->_dbSelect = new DbSelect();
302 271
         }
303  
-
304 272
         return $this->_dbSelect;
305 273
     }
306 274
 
@@ -355,8 +323,8 @@ public function getResultRowObject($returnColumns = null, $omitColumns = null)
355 323
      * been configured with all necessary information to successfully connect to a database
356 324
      * table and attempt to find a record matching the provided identity.
357 325
      *
358  
-     * @throws Zend\Authentication\Adapter\Exception if answering the authentication query is impossible
359  
-     * @return Zend\Authentication\Result
  326
+     * @throws Exception if answering the authentication query is impossible
  327
+     * @return AuthenticationResult
360 328
      */
361 329
     public function authenticate()
362 330
     {
@@ -368,7 +336,7 @@ public function authenticate()
368 336
             return $authResult;
369 337
         }
370 338
 
371  
-        // At this point, ambiguity is allready done. Loop, check and break on success.
  339
+        // At this point, ambiguity is already done. Loop, check and break on success.
372 340
         foreach ($resultIdentities as $identity) {
373 341
             $authResult = $this->_authenticateValidateResult($identity);
374 342
             if ($authResult->isValid()) {
@@ -384,7 +352,7 @@ public function authenticate()
384 352
      * making sure that this adapter was indeed setup properly with all
385 353
      * required pieces of information.
386 354
      *
387  
-     * @throws Zend\Authentication\Adapter\Exception - in the event that setup was not done properly
  355
+     * @throws Exception - in the event that setup was not done properly
388 356
      * @return true
389 357
      */
390 358
     protected function _authenticateSetup()
@@ -392,15 +360,15 @@ protected function _authenticateSetup()
392 360
         $exception = null;
393 361
 
394 362
         if ($this->_tableName == '') {
395  
-            $exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  363
+            $exception = 'A table must be supplied for the DbTable authentication adapter.';
396 364
         } elseif ($this->_identityColumn == '') {
397  
-            $exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  365
+            $exception = 'An identity column must be supplied for the DbTable authentication adapter.';
398 366
         } elseif ($this->_credentialColumn == '') {
399  
-            $exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  367
+            $exception = 'A credential column must be supplied for the DbTable authentication adapter.';
400 368
         } elseif ($this->_identity == '') {
401  
-            $exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  369
+            $exception = 'A value for the identity was not provided prior to authentication with DbTable.';
402 370
         } elseif ($this->_credential === null) {
403  
-            $exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  371
+            $exception = 'A credential value was not provided prior to authentication with DbTable.';
404 372
         }
405 373
 
406 374
         if (null !== $exception) {
@@ -417,10 +385,10 @@ protected function _authenticateSetup()
417 385
     }
418 386
 
419 387
     /**
420  
-     * _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
  388
+     * _authenticateCreateSelect() - This method creates a Zend\Db\Sql\Select object that
421 389
      * is completely configured to be queried against the database.
422 390
      *
423  
-     * @return Zend_Db_Select
  391
+     * @return DbSelect
424 392
      */
425 393
     protected function _authenticateCreateSelect()
426 394
     {
@@ -429,51 +397,45 @@ protected function _authenticateCreateSelect()
429 397
             $this->_credentialTreatment = '?';
430 398
         }
431 399
 
432  
-        $credentialExpression = new DBExpr(
433  
-            '(CASE WHEN ' .
434  
-            $this->_zendDb->quoteInto(
435  
-                $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
436  
-                . ' = ' . $this->_credentialTreatment, $this->_credential
437  
-                )
  400
+        $credentialExpression = new Expression(
  401
+            '(CASE WHEN '
  402
+            . $this->_zendDb->getPlatform()->quoteIdentifier($this->_credentialColumn)
  403
+            . ' = ' . $this->_credentialTreatment
438 404
             . ' THEN 1 ELSE 0 END) AS '
439  
-            . $this->_zendDb->quoteIdentifier(
440  
-                $this->_zendDb->foldCase('zend_auth_credential_match')
441  
-                )
442  
-            );
  405
+            . $this->_zendDb->getPlatform()->quoteIdentifier('zend_auth_credential_match')
  406
+        );
443 407
 
444 408
         // get select
445 409
         $dbSelect = clone $this->getDbSelect();
446  
-        $dbSelect->from($this->_tableName, array('*', $credentialExpression))
447  
-                 ->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
  410
+        $dbSelect->from($this->_tableName)
  411
+                 ->columns(array('*', $credentialExpression))
  412
+                 ->where($this->_zendDb->getPlatform()->quoteIdentifier($this->_identityColumn) . ' = ?');
448 413
 
449 414
         return $dbSelect;
450 415
     }
451 416
 
452 417
     /**
453  
-     * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
  418
+     * _authenticateQuerySelect() - This method accepts a Zend\Db\Sql\Select object and
454 419
      * performs a query against the database with that object.
455 420
      *
456  
-     * @param  Zend_Db_Select $dbSelect
457  
-     * @throws \Zend\Authentication\Adapter\Exception - when an invalid select
458  
-     *                                       object is encountered
  421
+     * @param  DbSelect $dbSelect
  422
+     * @throws Exception - when an invalid select object is encountered
459 423
      * @return array
460 424
      */
461 425
     protected function _authenticateQuerySelect(DBSelect $dbSelect)
462 426
     {
  427
+        $statement = $this->_zendDb->createStatement();
  428
+        $dbSelect->prepareStatement($this->_zendDb, $statement);
  429
+        $resultSet = new ResultSet();
463 430
         try {
464  
-            if ($this->_zendDb->getFetchMode() != Db::FETCH_ASSOC) {
465  
-                $origDbFetchMode = $this->_zendDb->getFetchMode();
466  
-                $this->_zendDb->setFetchMode(Db::FETCH_ASSOC);
467  
-            }
468  
-            $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
469  
-            if (isset($origDbFetchMode)) {
470  
-                $this->_zendDb->setFetchMode($origDbFetchMode);
471  
-                unset($origDbFetchMode);
472  
-            }
  431
+            $resultSet->setDataSource($statement->execute(array($this->_credential, $this->_identity)));
  432
+            $resultIdentities = $resultSet->toArray();
473 433
         } catch (\Exception $e) {
474  
-            throw new Exception\RuntimeException('The supplied parameters to Zend\Authentication\Adapter\DbTable failed to '
475  
-                                                . 'produce a valid sql statement, please check table and column names '
476  
-                                                . 'for validity.', 0, $e);
  434
+            throw new Exception\RuntimeException(
  435
+                'The supplied parameters to DbTable failed to '
  436
+                . 'produce a valid sql statement, please check table and column names '
  437
+                . 'for validity.', 0, $e
  438
+            );
477 439
         }
478 440
         return $resultIdentities;
479 441
     }
@@ -507,19 +469,17 @@ protected function _authenticateValidateResultSet(array $resultIdentities)
507 469
      * identity provided to this adapter.
508 470
      *
509 471
      * @param  array $resultIdentity
510  
-     * @return Zend\Authentication\Result
  472
+     * @return AuthenticationResult
511 473
      */
512 474
     protected function _authenticateValidateResult($resultIdentity)
513 475
     {
514  
-        $zendAuthCredentialMatchColumn = $this->_zendDb->foldCase('zend_auth_credential_match');
515  
-
516  
-        if ($resultIdentity[$zendAuthCredentialMatchColumn] != '1') {
  476
+        if ($resultIdentity['zend_auth_credential_match'] != '1') {
517 477
             $this->_authenticateResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
518 478
             $this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
519 479
             return $this->_authenticateCreateAuthResult();
520 480
         }
521 481
 
522  
-        unset($resultIdentity[$zendAuthCredentialMatchColumn]);
  482
+        unset($resultIdentity['zend_auth_credential_match']);
523 483
         $this->_resultRow = $resultIdentity;
524 484
 
525 485
         $this->_authenticateResultInfo['code'] = AuthenticationResult::SUCCESS;
@@ -531,7 +491,7 @@ protected function _authenticateValidateResult($resultIdentity)
531 491
      * _authenticateCreateAuthResult() - Creates a Zend_Auth_Result object from
532 492
      * the information that has been collected during the authenticate() attempt.
533 493
      *
534  
-     * @return \Zend\Authentication\Result
  494
+     * @return AuthenticationResult
535 495
      */
536 496
     protected function _authenticateCreateAuthResult()
537 497
     {
@@ -539,7 +499,7 @@ protected function _authenticateCreateAuthResult()
539 499
             $this->_authenticateResultInfo['code'],
540 500
             $this->_authenticateResultInfo['identity'],
541 501
             $this->_authenticateResultInfo['messages']
542  
-            );
  502
+        );
543 503
     }
544 504
 
545 505
 }
140  tests/Zend/Authentication/Adapter/DbTableTest.php
@@ -23,9 +23,8 @@
23 23
 
24 24
 use Zend\Authentication\Adapter,
25 25
     Zend\Authentication,
26  
-    Zend\Db\Db,
27  
-    Zend\Db\Adapter\Pdo\Sqlite as SQLiteAdapter,
28  
-    Zend\Db\Select as DBSelect;
  26
+    Zend\Db\Adapter\Adapter as DbAdapter,
  27
+    Zend\Db\Sql\Select as DBSelect;
29 28
 
30 29
 /**
31 30
  * @category   Zend
@@ -41,14 +40,14 @@ class DbTableTest extends \PHPUnit_Framework_TestCase
41 40
     /**
42 41
      * Sqlite database connection
43 42
      *
44  
-     * @var Zend_Db_Adapter_Pdo_Sqlite
  43
+     * @var Zend\Db\Adapter
45 44
      */
46 45
     protected $_db = null;
47 46
 
48 47
     /**
49 48
      * Database table authentication adapter
50 49
      *
51  
-     * @var Zend_Auth_Adapter_DbTable
  50
+     * @var Zend\Authentication\Adapter\DbTable
52 51
      */
53 52
     protected $_adapter = null;
54 53
 
@@ -137,8 +136,8 @@ public function testAuthenticateFailureIdentityNotFound()
137 136
      */
138 137
     public function testAuthenticateFailureIdentityAmbigious()
139 138
     {
140  
-        $sql_insert = 'INSERT INTO users (username, password, real_name) VALUES ("my_username", "my_password", "My Real Name")';
141  
-        $this->_db->query($sql_insert);
  139
+        $sqlInsert = 'INSERT INTO users (username, password, real_name) VALUES ("my_username", "my_password", "My Real Name")';
  140
+        $this->_db->query($sqlInsert, DbAdapter::QUERY_MODE_EXECUTE);
142 141
 
143 142
         $this->_adapter->setIdentity('my_username');
144 143
         $this->_adapter->setCredential('my_password');
@@ -217,17 +216,16 @@ public function testAdapterCanReturnDbSelectObject()
217 216
      */
218 217
     public function testAdapterCanUseModifiedDbSelectObject()
219 218
     {
220  
-        $this->_db->getProfiler()->setEnabled(true);
221 219
         $select = $this->_adapter->getDbSelect();
222  
-        $select->where('1 = 1');
  220
+        $select->where('1 = 0');
223 221
         $this->_adapter->setIdentity('my_username');
224 222
         $this->_adapter->setCredential('my_password');
225  
-        $this->_adapter->authenticate();
226  
-        $profiler = $this->_db->getProfiler();
227  
-        $this->assertEquals(
228  
-            '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\')',
229  
-            $profiler->getLastQueryProfile()->getQuery()
230  
-            );
  223
+        try {
  224
+            $result = $this->_adapter->authenticate();
  225
+            $this->assertEquals(Authentication\Result::FAILURE_IDENTITY_NOT_FOUND, $result->getCode());
  226
+        } catch (Adapter\Exception\RuntimeException $e) {
  227
+            $this->fail('Exception should have been thrown');
  228
+        }
231 229
     }
232 230
 
233 231
     /**
@@ -241,9 +239,11 @@ public function testAdapterReturnsASelectObjectWithoutAuthTimeModificationsAfter
241 239
         $this->_adapter->setCredential('my_password');
242 240
         $this->_adapter->authenticate();
243 241
         $selectAfterAuth = $this->_adapter->getDbSelect();
244  
-        $whereParts = $selectAfterAuth->getPart(DBSelect::WHERE);
  242
+        $whereParts = $selectAfterAuth->where->getPredicates();
245 243
         $this->assertEquals(1, count($whereParts));
246  
-        $this->assertEquals('(1 = 1)', array_pop($whereParts));
  244
+        $lastWherePart = array_pop($whereParts);
  245
+        $expressionData = $lastWherePart[1]->getExpressionData();
  246
+        $this->assertEquals('1 = 1', $expressionData[0][0]);
247 247
     }
248 248
 
249 249
     /**
@@ -308,79 +308,18 @@ public function testCatchExceptionBadSql()
308 308
     }
309 309
 
310 310
     /**
311  
-     *
312  
-     * @group ZF-3068
313  
-     */
314  
-    public function testDbTableAdapterUsesCaseFolding()
315  
-    {
316  
-        $this->tearDown();
317  
-        $this->_setupDbAdapter(array(Db::CASE_FOLDING => Db::CASE_UPPER));
318  
-        $this->_setupAuthAdapter();
319  
-
320  
-        $this->_adapter->setIdentity('my_username');
321  
-        $this->_adapter->setCredential('my_password');
322  
-        $this->_db->foldCase(Db::CASE_UPPER);
323  
-        $this->_adapter->authenticate();
324  
-    }
325  
-
326  
-
327  
-    /**
328  
-     * Test fallback to default database adapter, when no such adapter set
329  
-     *
330  
-     * @group ZF-7510
331  
-     */
332  
-    public function testAuthenticateWithDefaultDbAdapterNoAdapterException()
333  
-    {
334  
-        $this->setExpectedException('Zend\Authentication\Adapter\Exception\RuntimeException', 'Null was provided');
335  
-
336  
-        // make sure that no default adapter exists
337  
-        \Zend\Db\Table\AbstractTable::setDefaultAdapter(null);
338  
-        $this->_adapter = new Adapter\DbTable();
339  
-    }
340  
-
341  
-    /**
342  
-     * Test fallback to default database adapter
343  
-     *
344  
-     * @group ZF-7510
345  
-     */
346  
-    public function testAuthenticateWithDefaultDbAdapter()
347  
-    {
348  
-        // preserve default adapter between cases
349  
-        $tmp = \Zend\Db\Table\AbstractTable::getDefaultAdapter();
350  
-
351  
-        // make sure that default db adapter exists
352  
-        \Zend\Db\Table\AbstractTable::setDefaultAdapter($this->_db);
353  
-
354  
-        // check w/o passing adapter
355  
-        $this->_adapter = new Adapter\DbTable();
356  
-        $this->_adapter
357  
-             ->setTableName('users')
358  
-             ->setIdentityColumn('username')
359  
-             ->setCredentialColumn('password')
360  
-             ->setTableName('users')
361  
-             ->setIdentity('my_username')
362  
-             ->setCredential('my_password');
363  
-        $result = $this->_adapter->authenticate();
364  
-        $this->assertTrue($result->isValid());
365  
-
366  
-        // restore adapter
367  
-        \Zend\Db\Table\AbstractTable::setDefaultAdapter($tmp);
368  
-    }
369  
-    /**
370 311
      * Test to see same usernames with different passwords can not authenticate
371  
-     * when flag is not set. This is the current state of 
  312
+     * when flag is not set. This is the current state of
372 313
      * Zend_Auth_Adapter_DbTable (up to ZF 1.10.6)
373  
-     * 
  314
+     *
374 315
      * @group   ZF-7289
375 316
      */
376 317
     public function testEqualUsernamesDifferentPasswordShouldNotAuthenticateWhenFlagIsNotSet()
377 318
     {
378  
-        $this->_db->insert('users', array (
379  
-            'username' => 'my_username',
380  
-            'password' => 'my_otherpass',
381  
-            'real_name' => 'Test user 2',
382  
-        ));
383  
-        
  319
+        $sqlInsert = 'INSERT INTO users (username, password, real_name) '
  320
+        . 'VALUES ("my_username", "my_otherpass", "Test user 2")';
  321
+        $this->_db->query($sqlInsert, DbAdapter::QUERY_MODE_EXECUTE);
  322
+
384 323
         // test if user 1 can authenticate
385 324
         $this->_adapter->setIdentity('my_username')
386 325
                        ->setCredential('my_password');
@@ -389,20 +328,19 @@ public function testEqualUsernamesDifferentPasswordShouldNotAuthenticateWhenFlag
389 328
             $result->getMessages()));
390 329
         $this->assertFalse($result->isValid());
391 330
     }
  331
+
392 332
     /**
393 333
      * Test to see same usernames with different passwords can authenticate when
394 334
      * a flag is set
395  
-     * 
  335
+     *
396 336
      * @group   ZF-7289
397 337
      */
398 338
     public function testEqualUsernamesDifferentPasswordShouldAuthenticateWhenFlagIsSet()
399 339
     {
400  
-        $this->_db->insert('users', array (
401  
-            'username' => 'my_username',
402  
-            'password' => 'my_otherpass',
403  
-            'real_name' => 'Test user 2',
404  
-        ));
405  
-        
  340
+        $sqlInsert = 'INSERT INTO users (username, password, real_name) '
  341
+        . 'VALUES ("my_username", "my_otherpass", "Test user 2")';
  342
+        $this->_db->query($sqlInsert, DbAdapter::QUERY_MODE_EXECUTE);
  343
+
406 344
         // test if user 1 can authenticate
407 345
         $this->_adapter->setIdentity('my_username')
408 346
                        ->setCredential('my_password')
@@ -412,10 +350,10 @@ public function testEqualUsernamesDifferentPasswordShouldAuthenticateWhenFlagIsS
412 350
             $result->getMessages()));
413 351
         $this->assertTrue($result->isValid());
414 352
         $this->assertEquals('my_username', $result->getIdentity());
415  
-        
  353
+
416 354
         $this->_adapter = null;
417 355
         $this->_setupAuthAdapter();
418  
-        
  356
+
419 357
         // test if user 2 can authenticate
420 358
         $this->_adapter->setIdentity('my_username')
421 359
                        ->setCredential('my_otherpass')
@@ -430,28 +368,34 @@ public function testEqualUsernamesDifferentPasswordShouldAuthenticateWhenFlagIsS
430 368
 
431 369
     protected function _setupDbAdapter($optionalParams = array())
432 370
     {
433  
-        $params = array('dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE);
  371
+        $params = array('driver' => 'pdo_sqlite',
  372
+                        'dbname' => TESTS_ZEND_AUTH_ADAPTER_DBTABLE_PDO_SQLITE_DATABASE);
434 373
 
435 374
         if (!empty($optionalParams)) {
436 375
             $params['options'] = $optionalParams;
437 376
         }
438 377
 
439  
-        $this->_db = new SQLiteAdapter($params);
  378
+        $this->_db = new DbAdapter($params);
440 379
 
441  
-        $sqlCreate = 'CREATE TABLE [users] ( '
  380
+        $sqlCreate = 'CREATE TABLE IF NOT EXISTS [users] ( '
442 381
                    . '[id] INTEGER  NOT NULL PRIMARY KEY, '
443 382
                    . '[username] VARCHAR(50) NOT NULL, '
444 383
                    . '[password] VARCHAR(32) NULL, '
445 384
                    . '[real_name] VARCHAR(150) NULL)';
446  
-        $this->_db->query($sqlCreate);
  385
+        $this->_db->query($sqlCreate, DbAdapter::QUERY_MODE_EXECUTE);
  386
+
  387
+        $sqlDelete = 'DELETE FROM users';
  388
+        $this->_db->query($sqlDelete, DbAdapter::QUERY_MODE_EXECUTE);
447 389
 
448 390
         $sqlInsert = 'INSERT INTO users (username, password, real_name) '
449 391
                    . 'VALUES ("my_username", "my_password", "My Real Name")';
450  
-        $this->_db->query($sqlInsert);
  392
+        $this->_db->query($sqlInsert, DbAdapter::QUERY_MODE_EXECUTE);
451 393
     }
452 394
 
453 395
     protected function _setupAuthAdapter()
454 396
     {
455 397
         $this->_adapter = new Adapter\DbTable($this->_db, 'users', 'username', 'password');
456 398
     }
  399
+
457 400
 }
  401
+

0 notes on commit 60c852d

Please sign in to comment.
Something went wrong with that request. Please try again.