ModelTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Encryption;
  8. class ModelTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Encryption\Encryptor
  12. */
  13. protected $_model;
  14. protected function setUp()
  15. {
  16. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  17. \Magento\Framework\Encryption\Encryptor::class
  18. );
  19. }
  20. public function testEncryptDecrypt()
  21. {
  22. $encryptor = $this->_model;
  23. $this->assertEquals('', $encryptor->decrypt($encryptor->encrypt('')));
  24. $this->assertEquals('test', $encryptor->decrypt($encryptor->encrypt('test')));
  25. }
  26. public function testEncryptDecrypt2()
  27. {
  28. $encryptor = $this->_model;
  29. $initial = md5(uniqid());
  30. $encrypted = $encryptor->encrypt($initial);
  31. $this->assertNotEquals($initial, $encrypted);
  32. $this->assertEquals($initial, $encryptor->decrypt($encrypted));
  33. }
  34. public function testValidateKey()
  35. {
  36. $validKey = md5(uniqid());
  37. $this->_model->validateKey($validKey);
  38. }
  39. /**
  40. * @expectedException \Exception
  41. */
  42. public function testValidateKeyInvalid()
  43. {
  44. $invalidKey = '---- ';
  45. $this->_model->validateKey($invalidKey);
  46. }
  47. public function testGetValidateHash()
  48. {
  49. $password = uniqid();
  50. $hash = $this->_model->getHash($password, true);
  51. $this->assertTrue(is_string($hash));
  52. $this->assertTrue($this->_model->validateHash($password, $hash));
  53. }
  54. }