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