Trim whitespace from address fields before database save#215
Merged
rhoerr merged 5 commits intomage-os:release/3.xfrom May 7, 2026
Merged
Conversation
Contributor
|
Thanks for the PR |
Contributor
|
Can you change the PR base to Thanks |
rhoerr
reviewed
Apr 24, 2026
Add trimAddressFields() to AbstractAddress::beforeSave() to trim leading/trailing whitespace (including Unicode) from all string address fields before persistence. Fixes: mage-os#214 Fields trimmed: firstname, lastname, middlename, prefix, suffix, company, city, region, telephone, fax, postcode, vat_id, email, and each line of the street field. Covers both checkout (Quote\Address) and customer address (Customer\Address) save paths since both extend AbstractAddress. empty
The beforeSave() tests call parent::beforeSave() which dispatches events via _eventManager. The test setUp was missing an event manager mock on the Context object, causing "Call to a member function dispatch() on null" errors. empty
- Replace getMockForAbstractClass() with createMock() for PHPUnit 12 compatibility - Expand unicodeTrim() regex to also strip zero-width space (U+200B), zero-width non-joiner (U+200C), zero-width joiner (U+200D), and byte order mark (U+FEFF) which are not matched by \s in PHP PCRE empty
- Break unicodeTrim regex into variable to stay under 120 char line limit - Use createMock() instead of getMockForAbstractClass() (removed in PHPUnit 12) empty
So before-save observers see the trimmed values that will actually be persisted. Addresses review feedback on PR mage-os#215.
bb4c779 to
df843cc
Compare
rhoerr
approved these changes
May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
trimAddressFields()toAbstractAddress::beforeSave()to trim leading/trailing whitespace (including Unicode) from all string address fields before persistenceQuote\Address) and customer address (Customer\Address) save paths since both extendAbstractAddresspreg_replacewith/uflag to also strip Unicode whitespace (non-breaking spaces, zero-width spaces)Fixes #214
Fields trimmed
firstname,lastname,middlename,prefix,suffix,company,city,region,telephone,fax,postcode,vat_id,email, and each line of thestreetfield.Test plan
sales_order_addressvendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php