PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tests/kernel/datatypes/ezuser/ezuser_test.php

http://github.com/ezsystems/ezpublish
PHP | 102 lines | 59 code | 17 blank | 26 comment | 4 complexity | 701aa7977b0f2dfc31d31dbfcb121c62 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZUserTest class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package tests
  9. */
  10. class eZUserTest extends ezpDatabaseTestCase
  11. {
  12. public $username = 'admin';
  13. public $password = 'publish';
  14. public $email = 'nospam@ez.no';
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->setName( "eZUser Unit Tests" );
  19. }
  20. public function setUp()
  21. {
  22. parent::setUp();
  23. // Set HashType to md5_password (to update the password_hash in the ezuser table)
  24. ezpINIHelper::setINISetting( 'site.ini', 'UserSettings', 'HashType', 'md5_password' );
  25. ezpINIHelper::setINISetting( 'site.ini', 'UserSettings', 'UpdateHash', 'true' );
  26. ezpINIHelper::setINISetting( 'site.ini', 'UserSettings', 'AuthenticateMatch', 'login;email' );
  27. // Login the user
  28. $userClass = eZUserLoginHandler::instance( 'standard' );
  29. $user = $userClass->loginUser( $this->username, $this->password );
  30. // Verify that the username and password were accepted
  31. if ( !( $user instanceof eZUser ) )
  32. {
  33. self::markTestSkipped( "User {$this->username} is not in database.");
  34. }
  35. }
  36. public function tearDown()
  37. {
  38. ezpINIHelper::restoreINISettings();
  39. parent::tearDown();
  40. }
  41. /**
  42. * Test for issue #16328: Wrong hash stored in database on hash update in ezUser.php
  43. */
  44. public function testPasswordHashSamePasswordToUser()
  45. {
  46. // Get the password_hash
  47. $db = eZDB::instance();
  48. $rows = $db->arrayQuery( "SELECT * FROM ezuser where login = '{$this->username}'" );
  49. if ( count( $rows ) !== 1 )
  50. {
  51. $this->fail( "User {$this->username} is not in database.");
  52. }
  53. // Not used in this test
  54. $passwordHashMD5Password = $rows[0]['password_hash'];
  55. // Above it was only the setup for the test, the real test begins now
  56. // Set HashType to md5_user (password_hash in the ezuser table is updated again)
  57. ezpINIHelper::setINISetting( 'site.ini', 'UserSettings', 'HashType', 'md5_user' );
  58. // Login the user with email instead of username
  59. $userClass = eZUserLoginHandler::instance( 'standard' );
  60. $user = $userClass->loginUser( $this->email, $this->password );
  61. // Verify that the email and password were accepted
  62. if ( !( $user instanceof eZUser ) )
  63. {
  64. $this->fail( "User {$this->email} is not in database.");
  65. }
  66. // Get the password_hash
  67. $db = eZDB::instance();
  68. $rows = $db->arrayQuery( "SELECT * FROM ezuser where login = '{$this->username}'" );
  69. $passwordHashMD5User = $rows[0]['password_hash'];
  70. // The value that is expected to be saved in the ezuser table after updating the HashType to md5_user
  71. // (using the username and not the email address, which caused issue #16328)
  72. $hashMD5Expected = md5( "{$this->username}\n{$this->password}" );
  73. // Verify that the 2 password hashes saved above are the same
  74. $this->assertEquals( $hashMD5Expected, $passwordHashMD5User );
  75. // Verify that the user can still login with username
  76. $userClass = eZUserLoginHandler::instance( 'standard' );
  77. $user = $userClass->loginUser( $this->username, $this->password );
  78. // Verify that the username and password were accepted
  79. if ( !( $user instanceof eZUser ) )
  80. {
  81. $this->fail( "User {$this->username} is not in database.");
  82. }
  83. }
  84. }
  85. ?>