8
8
9
9
namespace Magento \Framework \Encryption \Test \Unit ;
10
10
11
+ use Magento \Framework \App \DeploymentConfig ;
11
12
use Magento \Framework \Encryption \Adapter \SodiumChachaIetf ;
12
- use Magento \Framework \Encryption \Encryptor ;
13
13
use Magento \Framework \Encryption \Crypt ;
14
+ use Magento \Framework \Encryption \Encryptor ;
15
+ use Magento \Framework \Math \Random ;
14
16
use Magento \Framework \Encryption \KeyValidator ;
15
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
18
17
19
class EncryptorTest extends \PHPUnit \Framework \TestCase
18
20
{
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 ' ;
21
23
22
24
/**
23
- * @var \Magento\Framework\Encryption\ Encryptor
25
+ * @var Encryptor
24
26
*/
25
27
private $ encryptor ;
26
28
27
29
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject
30
+ * @var Random | \PHPUnit_Framework_MockObject_MockObject
29
31
*/
30
32
private $ randomGeneratorMock ;
31
33
32
34
/**
33
- * @var KeyValidator| \PHPUnit_Framework_MockObject_MockObject
35
+ * @var KeyValidator | \PHPUnit_Framework_MockObject_MockObject
34
36
*/
35
37
private $ keyValidatorMock ;
36
38
37
39
protected function setUp ()
38
40
{
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);
41
44
$ deploymentConfigMock ->expects ($ this ->any ())
42
45
->method ('get ' )
43
46
->with (Encryptor::PARAM_CRYPT_KEY )
44
- ->will ( $ this -> returnValue ( self ::CRYPT_KEY_1 ) );
47
+ ->willReturn ( self ::CRYPT_KEY_1 );
45
48
$ this ->keyValidatorMock = $ this ->createMock (KeyValidator::class);
46
49
$ this ->encryptor = (new ObjectManager ($ this ))->getObject (
47
- \ Magento \ Framework \ Encryption \ Encryptor::class,
50
+ Encryptor::class,
48
51
[
49
52
'random ' => $ this ->randomGeneratorMock ,
50
53
'deploymentConfig ' => $ deploymentConfigMock ,
@@ -53,42 +56,42 @@ protected function setUp()
53
56
);
54
57
}
55
58
56
- public function testGetHashNoSalt ()
59
+ public function testGetHashNoSalt (): void
57
60
{
58
61
$ this ->randomGeneratorMock ->expects ($ this ->never ())->method ('getRandomString ' );
59
62
$ expected = '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 ' ;
60
63
$ actual = $ this ->encryptor ->getHash ('password ' );
61
64
$ this ->assertEquals ($ expected , $ actual );
62
65
}
63
66
64
- public function testGetHashSpecifiedSalt ()
67
+ public function testGetHashSpecifiedSalt (): void
65
68
{
66
69
$ this ->randomGeneratorMock ->expects ($ this ->never ())->method ('getRandomString ' );
67
70
$ expected = '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1 ' ;
68
71
$ actual = $ this ->encryptor ->getHash ('password ' , 'salt ' );
69
72
$ this ->assertEquals ($ expected , $ actual );
70
73
}
71
74
72
- public function testGetHashRandomSaltDefaultLength ()
75
+ public function testGetHashRandomSaltDefaultLength (): void
73
76
{
74
77
$ salt = '-----------random_salt---------- ' ;
75
78
$ this ->randomGeneratorMock
76
79
->expects ($ this ->once ())
77
80
->method ('getRandomString ' )
78
81
->with (32 )
79
- ->will ( $ this -> returnValue ( $ salt) );
82
+ ->willReturn ( $ salt );
80
83
$ expected = 'a1c7fc88037b70c9be84d3ad12522c7888f647915db78f42eb572008422ba2fa: ' . $ salt . ':1 ' ;
81
84
$ actual = $ this ->encryptor ->getHash ('password ' , true );
82
85
$ this ->assertEquals ($ expected , $ actual );
83
86
}
84
87
85
- public function testGetHashRandomSaltSpecifiedLength ()
88
+ public function testGetHashRandomSaltSpecifiedLength (): void
86
89
{
87
90
$ this ->randomGeneratorMock
88
91
->expects ($ this ->once ())
89
92
->method ('getRandomString ' )
90
93
->with (11 )
91
- ->will ( $ this -> returnValue ( 'random_salt ' ) );
94
+ ->willReturn ( 'random_salt ' );
92
95
$ expected = '4c5cab8dd00137d11258f8f87b93fd17bd94c5026fc52d3c5af911dd177a2611:random_salt:1 ' ;
93
96
$ actual = $ this ->encryptor ->getHash ('password ' , 11 );
94
97
$ this ->assertEquals ($ expected , $ actual );
@@ -101,7 +104,7 @@ public function testGetHashRandomSaltSpecifiedLength()
101
104
*
102
105
* @dataProvider validateHashDataProvider
103
106
*/
104
- public function testValidateHash ($ password , $ hash , $ expected )
107
+ public function testValidateHash ($ password , $ hash , $ expected ): void
105
108
{
106
109
$ actual = $ this ->encryptor ->validateHash ($ password , $ hash );
107
110
$ this ->assertEquals ($ expected , $ actual );
@@ -110,7 +113,7 @@ public function testValidateHash($password, $hash, $expected)
110
113
/**
111
114
* @return array
112
115
*/
113
- public function validateHashDataProvider ()
116
+ public function validateHashDataProvider (): array
114
117
{
115
118
return [
116
119
['password ' , 'hash:salt:1 ' , false ],
@@ -125,13 +128,13 @@ public function validateHashDataProvider()
125
128
* @dataProvider encryptWithEmptyKeyDataProvider
126
129
* @expectedException \SodiumException
127
130
*/
128
- public function testEncryptWithEmptyKey ($ key )
131
+ public function testEncryptWithEmptyKey ($ key ): void
129
132
{
130
- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
133
+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
131
134
$ deploymentConfigMock ->expects ($ this ->any ())
132
135
->method ('get ' )
133
136
->with (Encryptor::PARAM_CRYPT_KEY )
134
- ->will ( $ this -> returnValue ( $ key) );
137
+ ->willReturn ( $ key );
135
138
$ model = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
136
139
$ value = 'arbitrary_string ' ;
137
140
$ this ->assertEquals ($ value , $ model ->encrypt ($ value ));
@@ -140,7 +143,7 @@ public function testEncryptWithEmptyKey($key)
140
143
/**
141
144
* @return array
142
145
*/
143
- public function encryptWithEmptyKeyDataProvider ()
146
+ public function encryptWithEmptyKeyDataProvider (): array
144
147
{
145
148
return [[null ], [0 ], ['' ], ['0 ' ]];
146
149
}
@@ -150,13 +153,13 @@ public function encryptWithEmptyKeyDataProvider()
150
153
*
151
154
* @dataProvider decryptWithEmptyKeyDataProvider
152
155
*/
153
- public function testDecryptWithEmptyKey ($ key )
156
+ public function testDecryptWithEmptyKey ($ key ): void
154
157
{
155
- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
158
+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
156
159
$ deploymentConfigMock ->expects ($ this ->any ())
157
160
->method ('get ' )
158
161
->with (Encryptor::PARAM_CRYPT_KEY )
159
- ->will ( $ this -> returnValue ( $ key) );
162
+ ->willReturn ( $ key );
160
163
$ model = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
161
164
$ value = 'arbitrary_string ' ;
162
165
$ this ->assertEquals ('' , $ model ->decrypt ($ value ));
@@ -165,36 +168,35 @@ public function testDecryptWithEmptyKey($key)
165
168
/**
166
169
* @return array
167
170
*/
168
- public function decryptWithEmptyKeyDataProvider ()
171
+ public function decryptWithEmptyKeyDataProvider (): array
169
172
{
170
173
return [[null ], [0 ], ['' ], ['0 ' ]];
171
174
}
172
175
173
- public function testEncrypt ()
176
+ public function testEncrypt (): void
174
177
{
175
178
// sample data to encrypt
176
179
$ data = 'Mares eat oats and does eat oats, but little lambs eat ivy. ' ;
177
180
178
181
$ actual = $ this ->encryptor ->encrypt ($ data );
179
182
180
183
// Extract the initialization vector and encrypted data
181
- $ parts = explode (': ' , $ actual , 3 );
182
- list (, , $ encryptedData ) = $ parts ;
184
+ [, , $ encryptedData ] = explode (': ' , $ actual , 3 );
183
185
184
186
$ crypt = new SodiumChachaIetf (self ::CRYPT_KEY_1 );
185
187
// Verify decrypted matches original data
186
188
$ this ->assertEquals ($ data , $ crypt ->decrypt (base64_decode ((string )$ encryptedData )));
187
189
}
188
190
189
- public function testDecrypt ()
191
+ public function testDecrypt (): void
190
192
{
191
193
$ message = 'Mares eat oats and does eat oats, but little lambs eat ivy. ' ;
192
194
$ encrypted = $ this ->encryptor ->encrypt ($ message );
193
195
194
196
$ this ->assertEquals ($ message , $ this ->encryptor ->decrypt ($ encrypted ));
195
197
}
196
198
197
- public function testLegacyDecrypt ()
199
+ public function testLegacyDecrypt (): void
198
200
{
199
201
// sample data to encrypt
200
202
$ data = '0:2:z3a4ACpkU35W6pV692U4ueCVQP0m0v0p: ' .
@@ -203,26 +205,25 @@ public function testLegacyDecrypt()
203
205
$ actual = $ this ->encryptor ->decrypt ($ data );
204
206
205
207
// Extract the initialization vector and encrypted data
206
- $ parts = explode (': ' , $ data , 4 );
207
- list (, , $ iv , $ encrypted ) = $ parts ;
208
+ [, , $ iv , $ encrypted ] = explode (': ' , $ data , 4 );
208
209
209
210
// Decrypt returned data with RIJNDAEL_256 cipher, cbc mode
210
211
$ crypt = new Crypt (self ::CRYPT_KEY_1 , MCRYPT_RIJNDAEL_256 , MCRYPT_MODE_CBC , $ iv );
211
212
// Verify decrypted matches original data
212
213
$ this ->assertEquals ($ encrypted , base64_encode ($ crypt ->encrypt ($ actual )));
213
214
}
214
215
215
- public function testEncryptDecryptNewKeyAdded ()
216
+ public function testEncryptDecryptNewKeyAdded (): void
216
217
{
217
- $ deploymentConfigMock = $ this ->createMock (\ Magento \ Framework \ App \ DeploymentConfig::class);
218
+ $ deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
218
219
$ deploymentConfigMock ->expects ($ this ->at (0 ))
219
220
->method ('get ' )
220
221
->with (Encryptor::PARAM_CRYPT_KEY )
221
- ->will ( $ this -> returnValue ( self ::CRYPT_KEY_1 ) );
222
+ ->willReturn ( self ::CRYPT_KEY_1 );
222
223
$ deploymentConfigMock ->expects ($ this ->at (1 ))
223
224
->method ('get ' )
224
225
->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 );
226
227
$ model1 = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
227
228
// simulate an encryption key is being added
228
229
$ model2 = new Encryptor ($ this ->randomGeneratorMock , $ deploymentConfigMock );
@@ -236,7 +237,7 @@ public function testEncryptDecryptNewKeyAdded()
236
237
$ this ->assertSame ($ data , $ decryptedData , 'Encryptor failed to decrypt data encrypted by old keys. ' );
237
238
}
238
239
239
- public function testValidateKey ()
240
+ public function testValidateKey (): void
240
241
{
241
242
$ this ->keyValidatorMock ->method ('isValid ' )->willReturn (true );
242
243
$ this ->encryptor ->validateKey (self ::CRYPT_KEY_1 );
@@ -245,7 +246,7 @@ public function testValidateKey()
245
246
/**
246
247
* @expectedException \Exception
247
248
*/
248
- public function testValidateKeyInvalid ()
249
+ public function testValidateKeyInvalid (): void
249
250
{
250
251
$ this ->keyValidatorMock ->method ('isValid ' )->willReturn (false );
251
252
$ this ->encryptor ->validateKey ('----- ' );
@@ -254,7 +255,7 @@ public function testValidateKeyInvalid()
254
255
/**
255
256
* @return array
256
257
*/
257
- public function useSpecifiedHashingAlgoDataProvider ()
258
+ public function useSpecifiedHashingAlgoDataProvider (): array
258
259
{
259
260
return [
260
261
['password ' , 'salt ' , Encryptor::HASH_VERSION_MD5 ,
@@ -276,7 +277,7 @@ public function useSpecifiedHashingAlgoDataProvider()
276
277
* @param $hashAlgo
277
278
* @param $expected
278
279
*/
279
- public function testGetHashMustUseSpecifiedHashingAlgo ($ password , $ salt , $ hashAlgo , $ expected )
280
+ public function testGetHashMustUseSpecifiedHashingAlgo ($ password , $ salt , $ hashAlgo , $ expected ): void
280
281
{
281
282
$ hash = $ this ->encryptor ->getHash ($ password , $ salt , $ hashAlgo );
282
283
$ this ->assertEquals ($ expected , $ hash );
0 commit comments