Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API4: Allow save() to match on null values #24971

Merged

Conversation

highfalutin
Copy link
Contributor

Before

Code was trying to allow save() to match on null values, but was shooting itself in the foot.

After

Matching on fields works even when their value is null.

@civibot
Copy link

civibot bot commented Nov 13, 2022

(Standard links)

@civibot civibot bot added the master label Nov 13, 2022
@colemanw
Copy link
Member

Code was trying to allow save() to match on null values

I'm not entirely sure that it was. But I'm not against updating it to do so, with a unit test, and I think we ought to add an additional safety-guard that a positive match must include at least one non-null value.

@highfalutin
Copy link
Contributor Author

@colemanw, the already-merged PR #22882 (@mattwire) intended to allow matching on null -- see the comment. I'm keen on having this work properly.

I added a test. I also added in the "safety guard" (this commit) but after thinking about it, I took it back out. I don't know why we'd assume people won't want to match on a set of empty values. The function already aborts if the number of matches > 1, and that seems like enough of a safety guard.

@colemanw
Copy link
Member

Wow, that was fast!
Ok, to clarify (the test is a little hard to grok), is NULL being treated like a wildcard that matches any value? Or does NULL only match NULL?

@highfalutin
Copy link
Contributor Author

NULL or empty-string only matches NULL or empty-string (using the IS EMPTY operator). And I agree, the test was impressively clever, but almost impossible to read, so I've rewritten it from scratch.

@highfalutin
Copy link
Contributor Author

I could break the test up into a few smaller tests to make it even more grokkable, @colemanw,
but does this work well enough?

Contact::delete(FALSE)
->setUseTrash(FALSE)
->addWhere('id', 'IN', $allContactIds)
->execute();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was thoughtful of you to add this cleanup at the end, but that's not how we do test cleanup :)
For one thing, it's a pain for test authors to remember to do this every time, and, more importantly, if the test fails then it will never get to this step!

Best practice these days is to use $this->createTestRecord() instead of the api. As the name implies, the function creates test records... but it also automatically cleans them up afterwards, even if the test crashes halfway through!

@colemanw
Copy link
Member

colemanw commented Nov 27, 2022

@highfalutin test is fine now can you just remove the bit at the end and use $this->createTestRecord() instead of the api.

@colemanw
Copy link
Member

colemanw commented Dec 8, 2022

@highfalutin are you able to update this? It would be great to get it merged.

@highfalutin
Copy link
Contributor Author

@colemanw a busy few weeks here but I'll get to it. IIRC, we can't take out the bit at the end and still have a passing test, but I'll take another look

@highfalutin highfalutin force-pushed the abstractsaveaction-match-on-null branch from 8abe3a3 to fe4bd48 Compare January 10, 2023 02:45
@highfalutin
Copy link
Contributor Author

@colemanw I got rid of the "delete" action, and switched to relying on TransactionalInterface to handle cleanup. Back to using a data provider. But hopefully the test is still not too hard to grok.

@highfalutin
Copy link
Contributor Author

I believe the test failure is unrelated.

@civicrm-builder
Copy link

Can one of the admins verify this patch?

@highfalutin
Copy link
Contributor Author

civibot, test this please

@highfalutin highfalutin force-pushed the abstractsaveaction-match-on-null branch from fe4bd48 to 4849b6e Compare January 18, 2023 23:39
@highfalutin
Copy link
Contributor Author

@colemanw can we get this into the new rc?

@colemanw
Copy link
Member

colemanw commented Feb 2, 2023

@civicrm-builder retest this please

@colemanw
Copy link
Member

colemanw commented Feb 2, 2023

Yes, thanks for your work on this @highfalutin

@eileenmcnaughton eileenmcnaughton merged commit f3ab963 into civicrm:master Feb 6, 2023
@highfalutin highfalutin deleted the abstractsaveaction-match-on-null branch February 6, 2023 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants