@@ -77,7 +77,7 @@ public function testEncryptWithMissingKey(): void
7777
7878 $ result = $ cryptographer ->encrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ]);
7979
80- self ::assertEquals (['id ' => 'foo ' , '! email ' => 'encrypted ' ], $ result );
80+ self ::assertEquals (['id ' => 'foo ' , 'email ' => 'encrypted ' ], $ result );
8181 }
8282
8383 public function testEncryptWithExistingKey (): void
@@ -109,6 +109,39 @@ public function testEncryptWithExistingKey(): void
109109
110110 $ result = $ cryptographer ->encrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ]);
111111
112+ self ::assertEquals (['id ' => 'foo ' , 'email ' => 'encrypted ' ], $ result );
113+ }
114+
115+ public function testEncryptWithExistingKeyEncryptedFieldName (): void
116+ {
117+ $ cipherKey = new CipherKey (
118+ 'foo ' ,
119+ 'bar ' ,
120+ 'baz ' ,
121+ );
122+
123+ $ cipherKeyStore = $ this ->prophesize (CipherKeyStore::class);
124+ $ cipherKeyStore ->get ('foo ' )->willReturn ($ cipherKey );
125+ $ cipherKeyStore ->store ('foo ' , Argument::type (CipherKey::class))->shouldNotBeCalled ();
126+
127+ $ cipherKeyFactory = $ this ->prophesize (CipherKeyFactory::class);
128+ $ cipherKeyFactory ->__invoke ()->shouldNotBeCalled ();
129+
130+ $ cipher = $ this ->prophesize (Cipher::class);
131+ $ cipher
132+ ->encrypt ($ cipherKey , 'info@patchlevel.de ' )
133+ ->willReturn ('encrypted ' )
134+ ->shouldBeCalledOnce ();
135+
136+ $ cryptographer = new PersonalDataPayloadCryptographer (
137+ $ cipherKeyStore ->reveal (),
138+ $ cipherKeyFactory ->reveal (),
139+ $ cipher ->reveal (),
140+ true ,
141+ );
142+
143+ $ result = $ cryptographer ->encrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ]);
144+
112145 self ::assertEquals (['id ' => 'foo ' , '!email ' => 'encrypted ' ], $ result );
113146 }
114147
@@ -148,10 +181,9 @@ public function testDecryptWithMissingKey(): void
148181 $ cipherKeyStore ->reveal (),
149182 $ cipherKeyFactory ->reveal (),
150183 $ cipher ->reveal (),
151- false ,
152184 );
153185
154- $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , '! email ' => 'encrypted ' ]);
186+ $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'encrypted ' ]);
155187
156188 self ::assertEquals (['id ' => 'foo ' , 'email ' => new Email ('unknown ' )], $ result );
157189 }
@@ -181,15 +213,14 @@ public function testDecryptWithInvalidKey(): void
181213 $ cipherKeyStore ->reveal (),
182214 $ cipherKeyFactory ->reveal (),
183215 $ cipher ->reveal (),
184- false ,
185216 );
186217
187- $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , '! email ' => 'encrypted ' ]);
218+ $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'encrypted ' ]);
188219
189220 self ::assertEquals (['id ' => 'foo ' , 'email ' => new Email ('unknown ' )], $ result );
190221 }
191222
192- public function testDecryptWithExistingKey (): void
223+ public function testDecryptWithValidKey (): void
193224 {
194225 $ cipherKey = new CipherKey (
195226 'foo ' ,
@@ -217,12 +248,45 @@ public function testDecryptWithExistingKey(): void
217248 false ,
218249 );
219250
251+ $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'encrypted ' ]);
252+
253+ self ::assertEquals (['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ], $ result );
254+ }
255+
256+ public function testDecryptWithValidKeyAndEncryptedFieldName (): void
257+ {
258+ $ cipherKey = new CipherKey (
259+ 'foo ' ,
260+ 'bar ' ,
261+ 'baz ' ,
262+ );
263+
264+ $ cipherKeyStore = $ this ->prophesize (CipherKeyStore::class);
265+ $ cipherKeyStore ->get ('foo ' )->willReturn ($ cipherKey );
266+ $ cipherKeyStore ->store ('foo ' , Argument::type (CipherKey::class))->shouldNotBeCalled ();
267+
268+ $ cipherKeyFactory = $ this ->prophesize (CipherKeyFactory::class);
269+ $ cipherKeyFactory ->__invoke ()->shouldNotBeCalled ();
270+
271+ $ cipher = $ this ->prophesize (Cipher::class);
272+ $ cipher
273+ ->decrypt ($ cipherKey , 'encrypted ' )
274+ ->willReturn ('info@patchlevel.de ' )
275+ ->shouldBeCalledOnce ();
276+
277+ $ cryptographer = new PersonalDataPayloadCryptographer (
278+ $ cipherKeyStore ->reveal (),
279+ $ cipherKeyFactory ->reveal (),
280+ $ cipher ->reveal (),
281+ true ,
282+ );
283+
220284 $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , '!email ' => 'encrypted ' ]);
221285
222286 self ::assertEquals (['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ], $ result );
223287 }
224288
225- public function testDecryptWithoutPrefixField (): void
289+ public function testDecryptWithValidKeyAndEncryptedFieldNameWithoutEncryptedData (): void
226290 {
227291 $ cipherKey = new CipherKey (
228292 'foo ' ,
@@ -243,15 +307,15 @@ public function testDecryptWithoutPrefixField(): void
243307 $ cipherKeyStore ->reveal (),
244308 $ cipherKeyFactory ->reveal (),
245309 $ cipher ->reveal (),
246- false ,
310+ true ,
247311 );
248312
249313 $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ]);
250314
251315 self ::assertEquals (['id ' => 'foo ' , 'email ' => 'info@patchlevel.de ' ], $ result );
252316 }
253317
254- public function testDecryptWithFallbackWithoutPrefix (): void
318+ public function testDecryptWithValidKeyAndEncryptedFieldNameAndFallbackFieldName (): void
255319 {
256320 $ cipherKey = new CipherKey (
257321 'foo ' ,
@@ -276,6 +340,8 @@ public function testDecryptWithFallbackWithoutPrefix(): void
276340 $ cipherKeyStore ->reveal (),
277341 $ cipherKeyFactory ->reveal (),
278342 $ cipher ->reveal (),
343+ true ,
344+ true ,
279345 );
280346
281347 $ result = $ cryptographer ->decrypt ($ this ->metadata (PersonalDataProfileCreated::class), ['id ' => 'foo ' , 'email ' => 'encrypted ' ]);
0 commit comments