Repository navigation
Fix date custom fields being dropped by the create/update API - #25
Conversation
Contact and company date custom fields sent to the API were silently discarded. The endpoint returned success because it echoes the request payload rather than the saved record. api/create_customer.php normalises the payload with zeroBS_buildObjArr(), turning "2026-07-23" into a timestamp, then passes the result to zeroBS_addUpdateCustomer(), which normalises it a second time with $removeEmpties on. The second pass can't read the timestamp the first pass produced, jpcrm_date_str_to_uts() returns false, and the field is stripped before it reaches the DAL. api/create_company.php does the same thing. The date and datetime branches of zeroBS_buildObjArr() now pass an already-converted timestamp through untouched, so normalising twice matches normalising once. Adds jpcrm_value_is_uts() for the check. The check runs before conversion, not after. zeroBSCRM_locale_dateToUTS() falls back to strtotime(), which reads a negative timestamp as a relative offset and returns roughly the current time, so a pre-1970 datetime would never reach a guard placed after the conversion. Reported by Oleksandr Zaichenko. Fixes #21
PHPUnit 12 ignores the @dataProvider docblock, so the two data-driven tests ran with no arguments and errored. The rest of the repo uses attributes already. Also assert the key exists before reading it in the two tests that indexed straight into the result, so a regression fails with the message rather than an undefined array key warning.
|
While this works, it's a taping over a symptom that the create_customer.php shouldn't be calling the build meta. @donnchawp did you explore reworking the /api/create_customer.php (and other relevant API files). Here's where that got to I think if we point an agent at the API layer, and ask it to rework it, it would be beneficial and any API docs (so the correct date format is passed by the user) could be auto-generated on the back of it. |
|
Thanks @mikestottuk. Will take a look at that! |
|
I've written the end-to-end test this needed and it settles the reported bug either way.
With this branch both pass, and the full suite is green at 52 tests. The Where it goes. The second normalisation is inside On your point about the endpoint. You're right, and I'd put it more strongly than "it causes this bug". Where I'd still push back is that #25 isn't only about the API. The internal representation of a date here is already a UTS. That matters because the builder is shared. Three callers hand it an array that's already been through it: The merge one is the interesting case, because reworking the API layer doesn't reach it. I wrote a test and it confirms the bug. On trunk, merging a contact whose main record has an empty date custom field and whose secondary record has a value silently drops the value, and this branch fixes it: I've kept that test out of this PR and opened #28 for it. It trips a pre-existing deprecation at Two notes on #27 as it stands. It only does the contact endpoint, so I'd also say moving the endpoint from full-row replace to PATCH semantics is a public behaviour change worth its own issue and release note, rather than arriving as part of a date fix. A correction to my own description. The The weakest part of this PR is the nine-digit check, and it's worth saying where it comes from. The end-to-end check I had outstanding is now covered by tests rather than by clicking through a site, so this can come out of draft whenever someone's happy with it. You're away until the 13th, so I'll leave it unless somebody else wants to pick it up. On pointing an agent at the API layer and generating docs from it, agreed. The format the endpoint expects is undocumented and that's most of why this went unnoticed. |
The tests in Object_Array_Builder_Date_Test.php stop at zeroBS_buildObjArr(). These go the whole way: they register a real date custom field, make the same calls api/create_customer.php and api/create_company.php make, then read the record back out of the database. Both fail against trunk, where the read side returns '' for a custom field that was never written, and pass with the idempotency fix. register_custom_field() goes on the base integration test case because custom fields are stored twice, once per object type for DAL3 and once in the legacy customfields setting. zeroBSCRM_unpackCustomFields() gates the DAL3 overload on the object key existing in the legacy setting, so both are needed before the field reaches the field globals.
|
Ran the outstanding end-to-end check in wp-env (PHP 8.3, MySQL 8, WP 7.1.3), over real HTTP against On this branch:
On trunk (control): the identical HTTP create returns success and echoes the date back, but no That closes out the "Still to do" list, so per the PR description I'll take this off draft. Two merge notes:
|
# Conflicts: # tests/php/class-jpcrm-base-integration-testcase.php
The helper rewrote the customfields settings and the field globals, which outlive the test: a later company custom field test then saw stale settings and treated its field as a column. It now records what it changed and tear_down() puts it back. Also file doc comments and comment punctuation in the new tests, and the naming ignore on the second sanitised line, for the PHP lint added since this PR was opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Brought this up to date with trunk for 6.8.6 (merged trunk in rather than rebasing, so no force-push; it'll be squash-merged).
Suite (216 tests) passes in default, reverse and random order; |
…n the field length check Merging contacts copies a date custom field from the secondary record since #25. This adds the test for it. The merge passes unset social fields to the length check as null, so that check now casts to a string first. Fixes #28. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…n the field length check (#110) Merging contacts copies a date custom field from the secondary record since #25. This adds the test for it. The merge passes unset social fields to the length check as null, so that check now casts to a string first. Fixes #28. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Fixes #21
Please don't merge this yet. The tests pass, but there's an end-to-end check still outstanding, see the note at the end.
What was broken
Send a date custom field to the contact or company create/update API and the value is silently thrown away. The contact is created, the response says success, and the custom field is simply missing. The response is misleading because the create endpoints echo the request payload back rather than the record that was saved. #22 covers that separately.
Why
The payload gets normalised twice:
api/create_customer.php:51callszeroBS_buildContactMeta(), which turns"2026-07-23"into1784764800.zeroBS_addUpdateCustomer(), which calls the builder again with$removeEmptieson.datebranch, this time with a timestamp.DateTime::createFromFormat( '!Y-m-d', '1784764800' )fails,jpcrm_date_str_to_uts()returnsfalse, and the empty-stripping atZeroBSCRM.DAL3.Helpers.php:3029drops the key before it reaches the DAL.api/create_company.phpdoes the same thing. The admin UI is fine, it normalises once and goes straight to the DAL.The fix
The
dateanddatetimebranches ofzeroBS_buildObjArr()now recognise an already-converted timestamp and pass it through, so normalising twice matches normalising once. New helperjpcrm_value_is_uts()does the check.I left the double normalisation alone.
zeroBS_addUpdateCustomer()is called by a lot of integrations that pass raw fields, so changing its contract is a much bigger job than making the conversion idempotent.Two decisions worth a look:
The rule for "this is already a timestamp" is an int, or a string of nine or more digits with an optional leading minus. The digit floor is there because a bare
is_numeric()would swallow short numeric strings: a date field submitted as"2026"would become a timestamp in 1970 instead of failing to parse the way it does today. Nine digits covers 1973 onwards, and the optional minus covers pre-1970 dates, which matters for birthday fields.The check runs before the conversion, not after. I had it as a fallback on
falsefirst, which is the obvious shape, but it doesn't work for thedatetimebranch.zeroBSCRM_locale_dateToUTS()falls back tostrtotime(), andstrtotime( '-631152000' )doesn't fail, it reads the minus as a relative offset and returns roughly now:So a pre-1970 datetime would have quietly become today rather than being dropped. Checking first sidesteps that.
One behaviour change to be aware of: a date custom field submitted directly as
"1784764800"used to be dropped and will now be stored as that timestamp. That seems right to me, it's the same value the read side hands back, but shout if you disagree.Tests
tests/php/entities/Object_Array_Builder_Date_Test.phpcovers the two-pass path for contacts and companies, date and datetime, the pre-1970 case, and that"2026", empty and unparseable values still behave as they did.All 50 tests across the four suites pass locally in wp-env on PHP 8.3:
I checked the new tests actually catch the bug rather than just passing alongside it. Reverting
includes/to trunk and running them again fails 6 of the 8:The two that still pass with the fix reverted are the "behaviour unchanged" ones, which is what you'd want.
There's no PHPUnit workflow in
.github/workflows/, so nothing runs these on the PR.phpcsisn't installed locally so the lint didn't run either.Still to do
wp_zbs_customfieldsand the value in the admin UI. The tests here stop at the builder, so that's the gap.Leaving this as a draft until that end-to-end check is done.
zeroBS_buildObjArr()is the shared normaliser for contacts, companies, transactions, invoices, quotes and tasks, across the admin UI, API, forms and imports, so it's worth confirming on a real site before it goes anywhere near a release. Happy for someone else to do that check and take it off draft if it's urgent.Reported by @alllexandrz.