88
99namespace Magento \Framework \Encryption \Test \Unit ;
1010
11+ use Magento \Framework \App \DeploymentConfig ;
1112use Magento \Framework \Encryption \Adapter \SodiumChachaIetf ;
12- use Magento \Framework \Encryption \Encryptor ;
1313use Magento \Framework \Encryption \Crypt ;
14+ use Magento \Framework \Encryption \Encryptor ;
15+ use Magento \Framework \Math \Random ;
1416use Magento \Framework \Encryption \KeyValidator ;
1517use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1618
1719class EncryptorTest extends \PHPUnit \Framework \TestCase
1820{
19- const CRYPT_KEY_1 = 'g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ ' ;
20- const CRYPT_KEY_2 = '7wEjmrliuqZQ1NQsndSa8C8WHvddeEbN ' ;
21+ private const CRYPT_KEY_1 = 'g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ ' ;
22+ private const CRYPT_KEY_2 = '7wEjmrliuqZQ1NQsndSa8C8WHvddeEbN ' ;
2123
2224 /**
23- * @var \Magento\Framework\Encryption\ Encryptor
25+ * @var Encryptor
2426 */
2527 private $ encryptor ;
2628
2729 /**
28- * @var \PHPUnit_Framework_MockObject_MockObject
30+ * @var Random | \PHPUnit_Framework_MockObject_MockObject
2931 */
3032 private $ randomGeneratorMock ;
3133
3234 /**
33- * @var KeyValidator| \PHPUnit_Framework_MockObject_MockObject
35+ * @var KeyValidator | \PHPUnit_Framework_MockObject_MockObject
3436 */
3537 private $ keyValidatorMock ;
3638
3739 protected function setUp ()
3840 {
39- $ this ->randomGeneratorMock = $ this ->createMock (\Magento \Framework \Math \Random::class);
40- $ deploymentConfigMock = $ this ->createMock (\Magento \Framework \App \DeploymentConfig::class);
41+ $ this ->randomGeneratorMock = $ this ->createMock (Random::class);
42+ /** @var DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject $deploymentConfigMock */
43+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
4144 $ deploymentConfigMock ->expects ($ this ->any ())
4245 ->method ('get ' )
4346 ->with (Encryptor::PARAM_CRYPT_KEY )
44- ->will ( $ this -> returnValue ( self ::CRYPT_KEY_1 ) );
47+ ->willReturn ( self ::CRYPT_KEY_1 );
4548 $ this ->keyValidatorMock = $ this ->createMock (KeyValidator::class);
4649 $ this ->encryptor = (new ObjectManager ($ this ))->getObject (
47- \ Magento \ Framework \ Encryption \ Encryptor::class,
50+ Encryptor::class,
4851 [
4952 'random ' => $ this ->randomGeneratorMock ,
5053 'deploymentConfig ' => $ deploymentConfigMock ,
@@ -53,42 +56,42 @@ protected function setUp()
5356 );
5457 }
5558
56- public function testGetHashNoSalt ()
59+ public function testGetHashNoSalt (): void
5760 {
5861 $ this ->randomGeneratorMock ->expects ($ this ->never ())->method ('getRandomString ' );
5962 $ expected = '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 ' ;
6063 $ actual = $ this ->encryptor ->getHash ('password ' );
6164 $ this ->assertEquals ($ expected , $ actual );
6265 }
6366
64- public function testGetHashSpecifiedSalt ()
67+ public function testGetHashSpecifiedSalt (): void
6568 {
6669 $ this ->randomGeneratorMock ->expects ($ this ->never ())->method ('getRandomString ' );
6770 $ expected = '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1 ' ;
6871 $ actual = $ this ->encryptor ->getHash ('password ' , 'salt ' );
6972 $ this ->assertEquals ($ expected , $ actual );
7073 }
7174
72- public function testGetHashRandomSaltDefaultLength ()
75+ public function testGetHashRandomSaltDefaultLength (): void
7376 {
7477 $ salt = '-----------random_salt---------- ' ;
7578 $ this ->randomGeneratorMock
7679 ->expects ($ this ->once ())
7780 ->method ('getRandomString ' )
7881 ->with (32 )
79- ->will ( $ this -> returnValue ( $ salt) );
82+ ->willReturn ( $ salt );
8083 $ expected = 'a1c7fc88037b70c9be84d3ad12522c7888f647915db78f42eb572008422ba2fa: ' . $ salt . ':1 ' ;
8184 $ actual = $ this ->encryptor ->getHash ('password ' , true );
8285 $ this ->assertEquals ($ expected , $ actual );
8386 }
8487
85- public function testGetHashRandomSaltSpecifiedLength ()
88+ public function testGetHashRandomSaltSpecifiedLength (): void
8689 {
8790 $ this ->randomGeneratorMock
8891 ->expects ($ this ->once ())
8992 ->method ('getRandomString ' )
9093 ->with (11 )
91- ->will ( $ this -> returnValue ( 'random_salt ' ) );
94+ ->willReturn ( 'random_salt ' );
9295 $ expected = '4c5cab8dd00137d11258f8f87b93fd17bd94c5026fc52d3c5af911dd177a2611:random_salt:1 ' ;
9396 $ actual = $ this ->encryptor ->getHash ('password ' , 11 );
9497 $ this ->assertEquals ($ expected , $ actual );
@@ -101,7 +104,7 @@ public function testGetHashRandomSaltSpecifiedLength()
101104 *
102105 * @dataProvider validateHashDataProvider
103106 */
104- public function testValidateHash ($ password , $ hash , $ expected )
107+ public function testValidateHash ($ password , $ hash , $ expected ): void
105108 {
106109 $ actual = $ this ->encryptor ->validateHash ($ password , $ hash );
107110 $ this ->assertEquals ($ expected , $ actual );
@@ -110,7 +113,7 @@ public function testValidateHash($password, $hash, $expected)
110113 /**
111114 * @return array
112115 */
113- public function validateHashDataProvider ()
116+ public function validateHashDataProvider (): array
114117 {
115118 return [
116119 ['password ' , 'hash:salt:1 ' , false ],
@@ -125,13 +128,13 @@ public function validateHashDataProvider()
125128 * @dataProvider encryptWithEmptyKeyDataProvider
126129 * @expectedException \SodiumException
127130 */
128- public function testEncryptWithEmptyKey ($ key )
131+ public function testEncryptWithEmptyKey ($ key ): void
129132 {
130- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
133+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
131134 $ deploymentConfigMock ->expects ($ this ->any ())
132135 ->method ('get ' )
133136 ->with (Encryptor::PARAM_CRYPT_KEY )
134- ->will ( $ this -> returnValue ( $ key) );
137+ ->willReturn ( $ key );
135138 $ model = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
136139 $ value = 'arbitrary_string ' ;
137140 $ this ->assertEquals ($ value , $ model ->encrypt ($ value ));
@@ -140,7 +143,7 @@ public function testEncryptWithEmptyKey($key)
140143 /**
141144 * @return array
142145 */
143- public function encryptWithEmptyKeyDataProvider ()
146+ public function encryptWithEmptyKeyDataProvider (): array
144147 {
145148 return [[null ], [0 ], ['' ], ['0 ' ]];
146149 }
@@ -150,13 +153,13 @@ public function encryptWithEmptyKeyDataProvider()
150153 *
151154 * @dataProvider decryptWithEmptyKeyDataProvider
152155 */
153- public function testDecryptWithEmptyKey ($ key )
156+ public function testDecryptWithEmptyKey ($ key ): void
154157 {
155- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
158+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
156159 $ deploymentConfigMock ->expects ($ this ->any ())
157160 ->method ('get ' )
158161 ->with (Encryptor::PARAM_CRYPT_KEY )
159- ->will ( $ this -> returnValue ( $ key) );
162+ ->willReturn ( $ key );
160163 $ model = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
161164 $ value = 'arbitrary_string ' ;
162165 $ this ->assertEquals ('' , $ model ->decrypt ($ value ));
@@ -165,36 +168,35 @@ public function testDecryptWithEmptyKey($key)
165168 /**
166169 * @return array
167170 */
168- public function decryptWithEmptyKeyDataProvider ()
171+ public function decryptWithEmptyKeyDataProvider (): array
169172 {
170173 return [[null ], [0 ], ['' ], ['0 ' ]];
171174 }
172175
173- public function testEncrypt ()
176+ public function testEncrypt (): void
174177 {
175178 // sample data to encrypt
176179 $ data = 'Mares eat oats and does eat oats, but little lambs eat ivy. ' ;
177180
178181 $ actual = $ this ->encryptor ->encrypt ($ data );
179182
180183 // Extract the initialization vector and encrypted data
181- $ parts = explode (': ' , $ actual , 3 );
182- list (, , $ encryptedData ) = $ parts ;
184+ [, , $ encryptedData ] = explode (': ' , $ actual , 3 );
183185
184186 $ crypt = new SodiumChachaIetf (self ::CRYPT_KEY_1 );
185187 // Verify decrypted matches original data
186188 $ this ->assertEquals ($ data , $ crypt ->decrypt (base64_decode ((string )$ encryptedData )));
187189 }
188190
189- public function testDecrypt ()
191+ public function testDecrypt (): void
190192 {
191193 $ message = 'Mares eat oats and does eat oats, but little lambs eat ivy. ' ;
192194 $ encrypted = $ this ->encryptor ->encrypt ($ message );
193195
194196 $ this ->assertEquals ($ message , $ this ->encryptor ->decrypt ($ encrypted ));
195197 }
196198
197- public function testLegacyDecrypt ()
199+ public function testLegacyDecrypt (): void
198200 {
199201 // sample data to encrypt
200202 $ data = '0:2:z3a4ACpkU35W6pV692U4ueCVQP0m0v0p: ' .
@@ -203,26 +205,25 @@ public function testLegacyDecrypt()
203205 $ actual = $ this ->encryptor ->decrypt ($ data );
204206
205207 // Extract the initialization vector and encrypted data
206- $ parts = explode (': ' , $ data , 4 );
207- list (, , $ iv , $ encrypted ) = $ parts ;
208+ [, , $ iv , $ encrypted ] = explode (': ' , $ data , 4 );
208209
209210 // Decrypt returned data with RIJNDAEL_256 cipher, cbc mode
210211 $ crypt = new Crypt (self ::CRYPT_KEY_1 , MCRYPT_RIJNDAEL_256 , MCRYPT_MODE_CBC , $ iv );
211212 // Verify decrypted matches original data
212213 $ this ->assertEquals ($ encrypted , base64_encode ($ crypt ->encrypt ($ actual )));
213214 }
214215
215- public function testEncryptDecryptNewKeyAdded ()
216+ public function testEncryptDecryptNewKeyAdded (): void
216217 {
217- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
218+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
218219 $ deploymentConfigMock ->expects ($ this ->at (0 ))
219220 ->method ('get ' )
220221 ->with (Encryptor::PARAM_CRYPT_KEY )
221- ->will ( $ this -> returnValue ( self ::CRYPT_KEY_1 ) );
222+ ->willReturn ( self ::CRYPT_KEY_1 );
222223 $ deploymentConfigMock ->expects ($ this ->at (1 ))
223224 ->method ('get ' )
224225 ->with (Encryptor::PARAM_CRYPT_KEY )
225- ->will ( $ this -> returnValue ( self ::CRYPT_KEY_1 . "\n" . self ::CRYPT_KEY_2 ) );
226+ ->willReturn ( self ::CRYPT_KEY_1 . "\n" . self ::CRYPT_KEY_2 );
226227 $ model1 = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
227228 // simulate an encryption key is being added
228229 $ model2 = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
@@ -236,7 +237,7 @@ public function testEncryptDecryptNewKeyAdded()
236237 $ this ->assertSame ($ data , $ decryptedData , 'Encryptor failed to decrypt data encrypted by old keys. ' );
237238 }
238239
239- public function testValidateKey ()
240+ public function testValidateKey (): void
240241 {
241242 $ this ->keyValidatorMock ->method ('isValid ' )->willReturn (true );
242243 $ this ->encryptor ->validateKey (self ::CRYPT_KEY_1 );
@@ -245,7 +246,7 @@ public function testValidateKey()
245246 /**
246247 * @expectedException \Exception
247248 */
248- public function testValidateKeyInvalid ()
249+ public function testValidateKeyInvalid (): void
249250 {
250251 $ this ->keyValidatorMock ->method ('isValid ' )->willReturn (false );
251252 $ this ->encryptor ->validateKey ('----- ' );
@@ -254,7 +255,7 @@ public function testValidateKeyInvalid()
254255 /**
255256 * @return array
256257 */
257- public function useSpecifiedHashingAlgoDataProvider ()
258+ public function useSpecifiedHashingAlgoDataProvider (): array
258259 {
259260 return [
260261 ['password ' , 'salt ' , Encryptor::HASH_VERSION_MD5 ,
@@ -276,7 +277,7 @@ public function useSpecifiedHashingAlgoDataProvider()
276277 * @param $hashAlgo
277278 * @param $expected
278279 */
279- public function testGetHashMustUseSpecifiedHashingAlgo ($ password , $ salt , $ hashAlgo , $ expected )
280+ public function testGetHashMustUseSpecifiedHashingAlgo ($ password , $ salt , $ hashAlgo , $ expected ): void
280281 {
281282 $ hash = $ this ->encryptor ->getHash ($ password , $ salt , $ hashAlgo );
282283 $ this ->assertEquals ($ expected , $ hash );
0 commit comments