Skip to content

Commit 85139d3

Browse files
authored
Merge pull request #482 from mozilla/Hani/Fix_phone_not_matching_DE_FR
Hani/fix phone not matching DE and FR
2 parents 6a87725 + 2176874 commit 85139d3

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

l10n_CM/Unified/test_demo_ad_email_phone_captured_in_doorhanger_and_stored.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ def test_demo_ad_email_phone_captured_in_doorhanger_and_stored(
3838

3939
# containing phone field
4040
expected_phone = address_autofill_data.telephone
41-
with driver.context(driver.CONTEXT_CHROME):
42-
actual_phone = autofill_popup.get_element("address-doorhanger-phone").text
43-
normalize_expected = util.normalize_phone_number(expected_phone)
44-
normalized_actual = util.normalize_phone_number(actual_phone)
45-
assert normalized_actual == normalize_expected
41+
# Skip verification if phone number isn't provided
42+
if expected_phone:
43+
with driver.context(driver.CONTEXT_CHROME):
44+
actual_phone = autofill_popup.get_element("address-doorhanger-phone").text
45+
normalize_expected = util.normalize_regional_phone_numbers(expected_phone, region)
46+
normalized_actual = util.normalize_regional_phone_numbers(actual_phone, region)
47+
assert normalized_actual == normalize_expected, (
48+
f"Phone number mismatch for {region} | Expected: {normalize_expected}, Got: {normalized_actual}"
49+
)
4650

4751
# Click the "Save" button
4852
autofill_popup.click_doorhanger_button("save")

modules/util.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,60 @@ def get_state_province_abbreviation(self, full_name: str) -> str:
469469
"""
470470
return self.state_province_abbr.get(full_name, "Not Found")
471471

472+
def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
473+
"""
474+
Normalizes a phone number by separating the country prefix and verifying the rest of the number as an integer.
475+
This is used for localization (l10n) regional tests.
476+
Parameters:
477+
-----------
478+
phone : str
479+
The phone number to be normalized.
480+
region : str
481+
The region (country) code to determine the correct country prefix.
482+
Returns:
483+
--------
484+
str
485+
The normalized phone number in the format <country-code><number>.
486+
"""
487+
488+
# Country code mapping for different regions
489+
country_codes = {
490+
"US": "1",
491+
"CA": "1",
492+
"FR": "33",
493+
"DE": "49",
494+
}
495+
496+
# Sub out anything that matches this regex statement with an empty string to get rid of extensions in generated phone numbers
497+
phone = re.sub(r"\s*(?:x|ext)\s*\d*$", "", phone, flags=re.IGNORECASE)
498+
# Sub out anything that is not a digit with the empty string to ensure the phone number is formatted with no spaces or special characters
499+
digits = re.sub(r"\D", "", phone)
500+
501+
# Determine country code
502+
country_code = country_codes.get(region, "1") # Default to "1" (US/CA) if region is unknown
503+
local_number = digits
504+
505+
# Check if phone already contains a valid country code
506+
for code in country_codes.values():
507+
if digits.startswith(code):
508+
country_code = code
509+
# Remove country code from local number
510+
local_number = digits[len(code):]
511+
break
512+
513+
# Handle leading zero in local numbers (France & Germany)
514+
if region in ["FR", "DE"] and local_number.startswith("0"):
515+
# Remove the leading zero
516+
local_number = local_number[1:]
517+
518+
# Validate local number length
519+
if len(local_number) < 6: # Too short to be valid
520+
logging.warning(f"Invalid phone number format: {phone}")
521+
return ""
522+
523+
# Return formatted phone number with correct country code
524+
return f"{country_code}{local_number}"
525+
472526

473527
class BrowserActions:
474528
"""

0 commit comments

Comments
 (0)