@@ -16,7 +16,7 @@ private function getContext(): LDContext
16
16
->set ('firstName ' , 'Sue ' )
17
17
->build ();
18
18
}
19
-
19
+
20
20
private function getContextSpecifyingOwnPrivateAttr ()
21
21
{
22
22
return LDContext::builder ('abc ' )
@@ -26,7 +26,7 @@ private function getContextSpecifyingOwnPrivateAttr()
26
26
->private ('dizzle ' )
27
27
->build ();
28
28
}
29
-
29
+
30
30
private function getFullContextResult ()
31
31
{
32
32
return [
@@ -37,7 +37,7 @@ private function getFullContextResult()
37
37
'dizzle ' => 'ghi '
38
38
];
39
39
}
40
-
40
+
41
41
private function getContextResultWithAllAttrsHidden ()
42
42
{
43
43
return [
@@ -48,7 +48,7 @@ private function getContextResultWithAllAttrsHidden()
48
48
]
49
49
];
50
50
}
51
-
51
+
52
52
private function getContextResultWithSomeAttrsHidden ()
53
53
{
54
54
return [
@@ -60,7 +60,7 @@ private function getContextResultWithSomeAttrsHidden()
60
60
]
61
61
];
62
62
}
63
-
63
+
64
64
private function getContextResultWithOwnSpecifiedAttrHidden ()
65
65
{
66
66
return [
@@ -73,7 +73,7 @@ private function getContextResultWithOwnSpecifiedAttrHidden()
73
73
]
74
74
];
75
75
}
76
-
76
+
77
77
private function makeEvent ($ context )
78
78
{
79
79
return [
@@ -83,14 +83,14 @@ private function makeEvent($context)
83
83
'context ' => $ context
84
84
];
85
85
}
86
-
86
+
87
87
private function getJsonForContextBySerializingEvent ($ user )
88
88
{
89
89
$ es = new EventSerializer ([]);
90
90
$ event = $ this ->makeEvent ($ user );
91
91
return json_decode ($ es ->serializeEvents ([$ event ]), true )[0 ]['context ' ];
92
92
}
93
-
93
+
94
94
public function testAllContextAttrsSerialized ()
95
95
{
96
96
$ es = new EventSerializer ([]);
@@ -108,7 +108,92 @@ public function testAllContextAttrsPrivate()
108
108
$ expected = $ this ->makeEvent ($ this ->getContextResultWithAllAttrsHidden ());
109
109
$ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
110
110
}
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
+
112
197
public function testSomeContextAttrsPrivate ()
113
198
{
114
199
$ es = new EventSerializer (['private_attribute_names ' => ['firstName ' , 'bizzle ' ]]);
@@ -117,7 +202,7 @@ public function testSomeContextAttrsPrivate()
117
202
$ expected = $ this ->makeEvent ($ this ->getContextResultWithSomeAttrsHidden ());
118
203
$ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
119
204
}
120
-
205
+
121
206
public function testPerContextPrivateAttr ()
122
207
{
123
208
$ es = new EventSerializer ([]);
@@ -135,7 +220,7 @@ public function testPerContextPrivateAttrPlusGlobalPrivateAttrs()
135
220
$ expected = $ this ->makeEvent ($ this ->getContextResultWithAllAttrsHidden ());
136
221
$ this ->assertEquals ([$ expected ], json_decode ($ json , true ));
137
222
}
138
-
223
+
139
224
public function testObjectPropertyRedaction ()
140
225
{
141
226
$ es = new EventSerializer (['private_attribute_names ' => ['/b/prop1 ' , '/c/prop2/sub1 ' ]]);
0 commit comments