Skip to content

Commit 680df92

Browse files
hongj-srcmeta-codesync[bot]
authored andcommitted
Support updated metrics for PIIUtils
Summary: Update the metrics from 2 chars language token to 8 chars string. Update PIIUtils to fetch the appendix info from AppendixProvider. Mock unit test Differential Revision: D84633955 Privacy Context Container: L1399313 fbshipit-source-id: 7bc6a8efeec532f29c2f7beba335fd101633bc21
1 parent 243159f commit 680df92

2 files changed

Lines changed: 41 additions & 28 deletions

File tree

php/capi-param-builder/src/piiUtil/PIIUtils.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_once __DIR__ . '/StringUtils.php';
1717
require_once __DIR__ . '/ZipCodeUtils.php';
1818
require_once __DIR__ . '/SharedUtils.php';
19+
require_once __DIR__ . '/../util/AppendixProvider.php';
1920

2021
class PIIUtils
2122
{
@@ -72,13 +73,15 @@ public static function getNormalizedAndHashedPII($piiValue, $dataType)
7273
}
7374

7475
if (SharedUtils::looksLikeHashed($piiValue)) {
75-
return mb_strtolower($piiValue) . '.' . LANGUAGE_TOKEN;
76+
return mb_strtolower($piiValue) . '.' .
77+
AppendixProvider::getAppendix(false);
7678
} else {
7779
$normalizedPII = PIIUtils::getNormalizedPII($piiValue, $dataType);
7880
if ($normalizedPII === null || $normalizedPII === '') {
7981
return null;
8082
}
81-
return hash('sha256', $normalizedPII) . '.' . LANGUAGE_TOKEN;
83+
return hash('sha256', $normalizedPII) . '.' .
84+
AppendixProvider::getAppendix(true);
8285
}
8386
}
8487
}

php/capi-param-builder/tests/PIIUtilsTest.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@
99
use PHPUnit\Framework\TestCase;
1010
use FacebookAds\PIIUtils;
1111
use FacebookAds\PII_DATA_TYPE;
12+
use FacebookAds\AppendixProvider;
1213

1314
require_once __DIR__ . '/../src/piiUtil/PIIUtils.php';
1415

1516
final class PIIUtilsTest extends TestCase
1617
{
17-
// Tests for getNormalizedPII function
18+
private $appendix_is_new;
19+
private $appendix_is_normal;
20+
21+
protected function setUp(): void
22+
{
23+
// Get the actual appendix values from AppendixProvider
24+
$this->appendix_is_new = AppendixProvider::getAppendix(true);
25+
$this->appendix_is_normal = AppendixProvider::getAppendix(false);
26+
}
27+
1828

1929
public function testGetNormalizedPIIWithValidEmail()
2030
{
@@ -195,59 +205,59 @@ public function testGetNormalizedPIIWithInvalidGender()
195205
public function testGetNormalizedAndHashedPIIWithValidEmail()
196206
{
197207
$email = ' Test.User@Example.COM ';
198-
$expectedHash = hash('sha256', 'test.user@example.com') . '.AQ';
208+
$expectedHash = hash('sha256', 'test.user@example.com') . '.' . $this->appendix_is_new;
199209
$result = PIIUtils::getNormalizedAndHashedPII($email, PII_DATA_TYPE::EMAIL);
200210
$this->assertEquals($expectedHash, $result);
201211
}
202212

203213
public function testGetNormalizedAndHashedPIIWithValidPhone()
204214
{
205215
$phone = '+1 (555) 123-4567';
206-
$expectedHash = hash('sha256', '15551234567') . '.AQ'; // No + prefix in PHP implementation
216+
$expectedHash = hash('sha256', '15551234567') . '.' . $this->appendix_is_new; // No + prefix in PHP implementation
207217
$result = PIIUtils::getNormalizedAndHashedPII($phone, PII_DATA_TYPE::PHONE);
208218
$this->assertEquals($expectedHash, $result);
209219
}
210220

211221
public function testGetNormalizedAndHashedPIIWithValidFirstName()
212222
{
213223
$firstName = ' John Smith ';
214-
$expectedHash = hash('sha256', 'johnsmith') . '.AQ';
224+
$expectedHash = hash('sha256', 'johnsmith') . '.' . $this->appendix_is_new;
215225
$result = PIIUtils::getNormalizedAndHashedPII($firstName, PII_DATA_TYPE::FIRST_NAME);
216226
$this->assertEquals($expectedHash, $result);
217227
}
218228

219229
public function testGetNormalizedAndHashedPIIWithSha256Hash()
220230
{
221-
// SHA256 hash (64 hex characters) - implementation appends language token
231+
// SHA256 hash (64 hex characters) - implementation appends appendix
222232
$hashedValue = 'A665A45920422F9D417E4867EFDC4FB8A04A1F3FFF1FA07E998E86F7F7A27AE3';
223-
$expected = mb_strtolower($hashedValue) . '.AQ';
233+
$expected = mb_strtolower($hashedValue) . '.' . $this->appendix_is_normal;
224234
$result = PIIUtils::getNormalizedAndHashedPII($hashedValue, PII_DATA_TYPE::EMAIL);
225235
$this->assertEquals($expected, $result);
226236
}
227237

228238
public function testGetNormalizedAndHashedPIIWithMd5Hash()
229239
{
230-
// MD5 hash (32 hex characters) - implementation appends language token
240+
// MD5 hash (32 hex characters) - implementation appends appendix
231241
$hashedValue = 'D41D8CD98F00B204E9800998ECF8427E';
232-
$expected = mb_strtolower($hashedValue) . '.AQ';
242+
$expected = mb_strtolower($hashedValue) . '.' . $this->appendix_is_normal;
233243
$result = PIIUtils::getNormalizedAndHashedPII($hashedValue, PII_DATA_TYPE::EMAIL);
234244
$this->assertEquals($expected, $result);
235245
}
236246

237247
public function testGetNormalizedAndHashedPIIWithLowercaseSha256Hash()
238248
{
239-
// Already lowercase SHA256 hash - implementation appends language token
249+
// Already lowercase SHA256 hash - implementation appends normal appendix
240250
$hashedValue = 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3';
241-
$expected = $hashedValue . '.AQ';
251+
$expected = $hashedValue . '.' . $this->appendix_is_normal;
242252
$result = PIIUtils::getNormalizedAndHashedPII($hashedValue, PII_DATA_TYPE::EMAIL);
243253
$this->assertEquals($expected, $result);
244254
}
245255

246256
public function testGetNormalizedAndHashedPIIWithMixedCaseSha256Hash()
247257
{
248-
// Mixed case SHA256 hash - implementation appends language token
258+
// Mixed case SHA256 hash - implementation appends normal appendix
249259
$hashedValue = 'A665a45920422f9D417e4867efdc4FB8a04a1f3fff1fa07E998e86f7f7a27ae3';
250-
$expected = mb_strtolower($hashedValue) . '.AQ';
260+
$expected = mb_strtolower($hashedValue) . '.' . $this->appendix_is_normal;
251261
$result = PIIUtils::getNormalizedAndHashedPII($hashedValue, PII_DATA_TYPE::EMAIL);
252262
$this->assertEquals($expected, $result);
253263
}
@@ -288,7 +298,7 @@ public function testGetNormalizedAndHashedPIIWithInvalidPhone()
288298
{
289299
// Invalid phone gets normalized to digits only, then hashed
290300
$invalidPhone = 'abc123';
291-
$expectedHash = hash('sha256', '123') . '.AQ'; // Only digits remain
301+
$expectedHash = hash('sha256', '123') . '.' . $this->appendix_is_new; // Only digits remain
292302
$result = PIIUtils::getNormalizedAndHashedPII($invalidPhone, PII_DATA_TYPE::PHONE);
293303
$this->assertEquals($expectedHash, $result);
294304
}
@@ -304,55 +314,55 @@ public function testGetNormalizedAndHashedPIIWithInvalidDOB()
304314
public function testGetNormalizedAndHashedPIIWithValidDOB()
305315
{
306316
$dob = '01/15/1990';
307-
$expectedHash = hash('sha256', '19900115') . '.AQ';
317+
$expectedHash = hash('sha256', '19900115') . '.' . $this->appendix_is_new;
308318
$result = PIIUtils::getNormalizedAndHashedPII($dob, PII_DATA_TYPE::DATE_OF_BIRTH);
309319
$this->assertEquals($expectedHash, $result);
310320
}
311321

312322
public function testGetNormalizedAndHashedPIIWithValidGender()
313323
{
314324
$gender = ' Female ';
315-
$expectedHash = hash('sha256', 'f') . '.AQ';
325+
$expectedHash = hash('sha256', 'f') . '.' . $this->appendix_is_new;
316326
$result = PIIUtils::getNormalizedAndHashedPII($gender, PII_DATA_TYPE::GENDER);
317327
$this->assertEquals($expectedHash, $result);
318328
}
319329

320330
public function testGetNormalizedAndHashedPIIWithValidCity()
321331
{
322332
$city = ' San Francisco ';
323-
$expectedHash = hash('sha256', 'sanfrancisco') . '.AQ';
333+
$expectedHash = hash('sha256', 'sanfrancisco') . '.' . $this->appendix_is_new;
324334
$result = PIIUtils::getNormalizedAndHashedPII($city, PII_DATA_TYPE::CITY);
325335
$this->assertEquals($expectedHash, $result);
326336
}
327337

328338
public function testGetNormalizedAndHashedPIIWithValidState()
329339
{
330340
$state = ' New York ';
331-
$expectedHash = hash('sha256', 'ny') . '.AQ';
341+
$expectedHash = hash('sha256', 'ny') . '.' . $this->appendix_is_new;
332342
$result = PIIUtils::getNormalizedAndHashedPII($state, PII_DATA_TYPE::STATE);
333343
$this->assertEquals($expectedHash, $result);
334344
}
335345

336346
public function testGetNormalizedAndHashedPIIWithValidCountry()
337347
{
338348
$country = ' Germany ';
339-
$expectedHash = hash('sha256', 'de') . '.AQ';
349+
$expectedHash = hash('sha256', 'de') . '.' . $this->appendix_is_new;
340350
$result = PIIUtils::getNormalizedAndHashedPII($country, PII_DATA_TYPE::COUNTRY);
341351
$this->assertEquals($expectedHash, $result);
342352
}
343353

344354
public function testGetNormalizedAndHashedPIIWithValidExternalID()
345355
{
346356
$externalId = ' Customer123 ';
347-
$expectedHash = hash('sha256', 'customer123') . '.AQ';
357+
$expectedHash = hash('sha256', 'customer123') . '.' . $this->appendix_is_new;
348358
$result = PIIUtils::getNormalizedAndHashedPII($externalId, PII_DATA_TYPE::EXTERNAL_ID);
349359
$this->assertEquals($expectedHash, $result);
350360
}
351361

352362
public function testGetNormalizedAndHashedPIIWithValidZipCode()
353363
{
354364
$zipCode = ' 90210-1234 ';
355-
$expectedHash = hash('sha256', '90210') . '.AQ';
365+
$expectedHash = hash('sha256', '90210') . '.' . $this->appendix_is_new;
356366
$result = PIIUtils::getNormalizedAndHashedPII($zipCode, PII_DATA_TYPE::ZIP_CODE);
357367
$this->assertEquals($expectedHash, $result);
358368
}
@@ -361,7 +371,7 @@ public function testGetNormalizedAndHashedPIIWithHashThatLooksLikeHex()
361371
{
362372
// String that looks like hex but isn't a valid hash length - should be treated as regular input
363373
$notAHash = 'abcdef123456';
364-
$expectedHash = hash('sha256', $notAHash) . '.AQ'; // Should be treated as regular input with language token
374+
$expectedHash = hash('sha256', $notAHash) . '.' . $this->appendix_is_new; // Should be treated as regular input with new appendix
365375
$result = PIIUtils::getNormalizedAndHashedPII($notAHash, PII_DATA_TYPE::EXTERNAL_ID);
366376
$this->assertEquals($expectedHash, $result);
367377
}
@@ -370,18 +380,18 @@ public function testGetNormalizedAndHashedPIIWithSha1Hash()
370380
{
371381
// SHA1 hash (40 hex characters) - not recognized as hash, should be treated as regular input
372382
$sha1Hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
373-
$expectedHash = hash('sha256', $sha1Hash) . '.AQ'; // Should be treated as regular input with language token
383+
$expectedHash = hash('sha256', $sha1Hash) . '.' . $this->appendix_is_new; // Should be treated as regular input with new appendix
374384
$result = PIIUtils::getNormalizedAndHashedPII($sha1Hash, PII_DATA_TYPE::EXTERNAL_ID);
375385
$this->assertEquals($expectedHash, $result);
376386
}
377387

378388
public function testGetNormalizedAndHashedPIIHashOutputFormat()
379389
{
380-
// Verify that the hash output is always 64 characters + '.AQ' = 67 characters (SHA256 + language token)
390+
// Verify that the hash output is always 64 characters + '.$appendix' = 73 characters (SHA256 + language token)
381391
$input = 'test@example.com';
382392
$result = PIIUtils::getNormalizedAndHashedPII($input, PII_DATA_TYPE::EMAIL);
383-
$this->assertEquals(67, strlen($result));
384-
$this->assertRegExp('/^[a-f0-9]{64}\.AQ$/', $result);
393+
$this->assertEquals(73, strlen($result));
394+
$this->assertRegExp('/^[a-f0-9]{64}\.' . preg_quote($this->appendix_is_new, '/') . '$/', $result);
385395
}
386396

387397
public function testGetNormalizedAndHashedPIIConsistentHashing()
@@ -408,7 +418,7 @@ public function testGetNormalizedAndHashedPIIWithComplexInput()
408418
// Test with complex input that requires normalization
409419
$email = ' Test.User+Tag@EXAMPLE.COM ';
410420
$normalizedEmail = 'test.user+tag@example.com';
411-
$expectedHash = hash('sha256', $normalizedEmail) . '.AQ';
421+
$expectedHash = hash('sha256', $normalizedEmail) . '.' . $this->appendix_is_new;
412422
$result = PIIUtils::getNormalizedAndHashedPII($email, PII_DATA_TYPE::EMAIL);
413423
$this->assertEquals($expectedHash, $result);
414424
}

0 commit comments

Comments
 (0)