@@ -16,7 +16,7 @@ private function getContext(): LDContext
1616 ->set ('firstName ' , 'Sue ' )
1717 ->build ();
1818 }
19-
19+
2020 private function getContextSpecifyingOwnPrivateAttr ()
2121 {
2222 return LDContext::builder ('abc ' )
@@ -26,7 +26,7 @@ private function getContextSpecifyingOwnPrivateAttr()
2626 ->private ('dizzle ' )
2727 ->build ();
2828 }
29-
29+
3030 private function getFullContextResult ()
3131 {
3232 return [
@@ -37,7 +37,7 @@ private function getFullContextResult()
3737 'dizzle ' => 'ghi '
3838 ];
3939 }
40-
40+
4141 private function getContextResultWithAllAttrsHidden ()
4242 {
4343 return [
@@ -48,7 +48,7 @@ private function getContextResultWithAllAttrsHidden()
4848 ]
4949 ];
5050 }
51-
51+
5252 private function getContextResultWithSomeAttrsHidden ()
5353 {
5454 return [
@@ -60,7 +60,7 @@ private function getContextResultWithSomeAttrsHidden()
6060 ]
6161 ];
6262 }
63-
63+
6464 private function getContextResultWithOwnSpecifiedAttrHidden ()
6565 {
6666 return [
@@ -73,7 +73,7 @@ private function getContextResultWithOwnSpecifiedAttrHidden()
7373 ]
7474 ];
7575 }
76-
76+
7777 private function makeEvent ($ context )
7878 {
7979 return [
@@ -83,14 +83,14 @@ private function makeEvent($context)
8383 'context ' => $ context
8484 ];
8585 }
86-
86+
8787 private function getJsonForContextBySerializingEvent ($ user )
8888 {
8989 $ es = new EventSerializer ([]);
9090 $ event = $ this ->makeEvent ($ user );
9191 return json_decode ($ es ->serializeEvents ([$ event ]), true )[0 ]['context ' ];
9292 }
93-
93+
9494 public function testAllContextAttrsSerialized ()
9595 {
9696 $ es = new EventSerializer ([]);
@@ -108,7 +108,92 @@ public function testAllContextAttrsPrivate()
108108 $ expected = $ this ->makeEvent ($ this ->getContextResultWithAllAttrsHidden ());
109109 $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
110110 }
111-
111+
112+ public function testRedactsAllAttributesFromAnonymousContextWithFeatureEvent ()
113+ {
114+ $ anonymousContext = LDContext::builder ('abc ' )
115+ ->anonymous (true )
116+ ->set ('bizzle ' , 'def ' )
117+ ->set ('dizzle ' , 'ghi ' )
118+ ->set ('firstName ' , 'Sue ' )
119+ ->build ();
120+
121+ $ es = new EventSerializer ([]);
122+ $ event = $ this ->makeEvent ($ anonymousContext );
123+ $ event ['kind ' ] = 'feature ' ;
124+ $ json = $ es ->serializeEvents ([$ event ]);
125+
126+ // But we redact all attributes when the context is anonymous
127+ $ expectedContextOutput = $ this ->getContextResultWithAllAttrsHidden ();
128+ $ expectedContextOutput ['anonymous ' ] = true ;
129+
130+ $ expected = $ this ->makeEvent ($ expectedContextOutput );
131+ $ expected ['kind ' ] = 'feature ' ;
132+
133+ $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
134+ }
135+
136+ public function testDoesNotRedactAttributesFromAnonymousContextWithNonFeatureEvent ()
137+ {
138+ $ anonymousContext = LDContext::builder ('abc ' )
139+ ->anonymous (true )
140+ ->set ('bizzle ' , 'def ' )
141+ ->set ('dizzle ' , 'ghi ' )
142+ ->set ('firstName ' , 'Sue ' )
143+ ->build ();
144+
145+ $ es = new EventSerializer ([]);
146+ $ event = $ this ->makeEvent ($ anonymousContext );
147+ $ json = $ es ->serializeEvents ([$ event ]);
148+
149+ // But we redact all attributes when the context is anonymous
150+ $ expectedContextOutput = $ this ->getFullContextResult ();
151+ $ expectedContextOutput ['anonymous ' ] = true ;
152+
153+ $ expected = $ this ->makeEvent ($ expectedContextOutput );
154+
155+ $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
156+ }
157+
158+ public function testRedactsAllAttributesOnlyIfContextIsAnonymous ()
159+ {
160+ $ userContext = LDContext::builder ('user-key ' )
161+ ->kind ('user ' )
162+ ->anonymous (true )
163+ ->name ('Example user ' )
164+ ->build ();
165+
166+ $ orgContext = LDContext::builder ('org-key ' )
167+ ->kind ('org ' )
168+ ->anonymous (false )
169+ ->name ('Example org ' )
170+ ->build ();
171+
172+ $ multiContext = LDContext::createMulti ($ userContext , $ orgContext );
173+
174+ $ es = new EventSerializer ([]);
175+ $ event = $ this ->makeEvent ($ multiContext );
176+ $ event ['kind ' ] = 'feature ' ;
177+ $ json = $ es ->serializeEvents ([$ event ]);
178+
179+ $ expectedContextOutput = [
180+ 'kind ' => 'multi ' ,
181+ 'user ' => [
182+ 'key ' => 'user-key ' ,
183+ 'anonymous ' => true ,
184+ '_meta ' => ['redactedAttributes ' => ['name ' ]]
185+ ],
186+ 'org ' => [
187+ 'key ' => 'org-key ' ,
188+ 'name ' => 'Example org ' ,
189+ ],
190+ ];
191+ $ expected = $ this ->makeEvent ($ expectedContextOutput );
192+ $ expected ['kind ' ] = 'feature ' ;
193+
194+ $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
195+ }
196+
112197 public function testSomeContextAttrsPrivate ()
113198 {
114199 $ es = new EventSerializer (['private_attribute_names ' => ['firstName ' , 'bizzle ' ]]);
@@ -117,7 +202,7 @@ public function testSomeContextAttrsPrivate()
117202 $ expected = $ this ->makeEvent ($ this ->getContextResultWithSomeAttrsHidden ());
118203 $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
119204 }
120-
205+
121206 public function testPerContextPrivateAttr ()
122207 {
123208 $ es = new EventSerializer ([]);
@@ -135,7 +220,7 @@ public function testPerContextPrivateAttrPlusGlobalPrivateAttrs()
135220 $ expected = $ this ->makeEvent ($ this ->getContextResultWithAllAttrsHidden ());
136221 $ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
137222 }
138-
223+
139224 public function testObjectPropertyRedaction ()
140225 {
141226 $ es = new EventSerializer (['private_attribute_names ' => ['/b/prop1 ' , '/c/prop2/sub1 ' ]]);
0 commit comments