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

[REF] change deprecated function to API4 call #18076

Merged

Conversation

MegaphoneJon
Copy link
Contributor

Overview

CRM_Contribute_Form_Contribution_Confirm::onBehalfOrganization() is some pretty messy code. This refactor is a first step toward addressing that.

Before

Deprecated function used.

After

APIv4 used.

Comments

There's a lot more that one could clean up here, but I wanted to keep this change simple for review. Also, I question whether we should be throwing errors on a duplicate relationship at all.

@civibot
Copy link

civibot bot commented Aug 5, 2020

(Standard links)

@eileenmcnaughton
Copy link
Contributor

test this please

@demeritcowboy
Copy link
Contributor

jenkins retest this please

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon test fail relates

CRM_Contribute_BAO_ContributionTest::testProcessOnBehalfOrganization
Exception: CRM_Core_Exception: "Invalid Relationship"
#0 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/Civi/Api4/Generic/Traits/DAOActionTrait.php(140): CRM_Contact_BAO_Relationship::create((Array:5))
#1 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/Civi/Api4/Generic/DAOCreateAction.php(40): Civi\Api4\Generic\DAOCreateAction->writeObjects((Array:1))
#2 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/Civi/Api4/Provider/ActionObjectProvider.php(68): Civi\Api4\Generic\DAOCreateAction->_run(Object(Civi\Api4\Generic\Result))
#3 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/Civi/API/Kernel.php(150): Civi\Api4\Provider\ActionObjectProvider->invoke(Object(Civi\Api4\Generic\DAOCreateAction))
#4 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/Civi/Api4/Generic/AbstractAction.php(238): Civi\API\Kernel->runRequest(Object(Civi\Api4\Generic\DAOCreateAction))
#5 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/CRM/Contribute/Form/Contribution/Confirm.php(1186): Civi\Api4\Generic\AbstractAction->execute()
#6 /home/jenkins/bknix-dfl/build/core-18076-6umoe/web/sites/all/modules/civicrm/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php(1402):

@colemanw
Copy link
Member

I agree it's questionable whether Relationship::create() should throw exceptions on duplicates. But I think Relationship::save() is even more clear-cut, as "save" does not imply it must be new.

$isNotCurrentEmployer = TRUE;
}

// formalities for creating / editing organization.
$behalfOrganization['contact_type'] = 'Organization';
Copy link
Contributor

Choose a reason for hiding this comment

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

If this line is removed then when the array gets passed to hooks via createProfileContact(), the array won't contain contact_type anymore. Unless it's getting set somewhere else in between but I can't see where.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, you're right. I saw that "Organization" was passed as the $ctype argument to createProfileContact(), but that doesn't get passed to the hook. I'll restore this line and put it just above createProfileContact() since it's not needed by the other functions that use $behalfOrganization as an argument.

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Aug 28, 2020

test fail is CRM_Contribute_BAO_ContributionTest.testProcessOnBehalfOrganization

CRM_Contribute_BAO_ContributionTest::testProcessOnBehalfOrganization
Exception: CRM_Core_Exception: "Invalid Relationship"

@eileenmcnaughton
Copy link
Contributor

That test is weird - it's creating an invalid relationship but it's testing 'something' - maybe put the try- catch in your function since a little error seems OK here...

@eileenmcnaughton
Copy link
Contributor

I've added has-test as it's pretty clear CRM_Contribute_BAO_ContributionTest::testProcessOnBehalfOrganization has specific relevant cover

@MegaphoneJon
Copy link
Contributor Author

test this please

@MegaphoneJon
Copy link
Contributor Author

OK, I figured it out. The code is good, the test has a bug - which escaped notice because of a complementary bug in the legacy code.

This test runs processOnBehalfOfOrganization() once, then creates a duplicate organization, then runs it a second time to ensure the dupe is picked up. Of note: the method is supposed to create a relationship between the two contacts.

However, the values are passed to that method by reference - so when you run it a second time, the values are no longer valid. Unfortunately, the legacy method fails silently! So the bug in the test went unnoticed.

My modification to the test passes the original values to the second call, which allows the relationship to be created and the test passes.

@demeritcowboy
Copy link
Contributor

On behalf of the organizations that benefit from this PR I'll say "good sleuthing"!

@eileenmcnaughton
Copy link
Contributor

@demeritcowboy is this good to merge now?

@demeritcowboy
Copy link
Contributor

Did some basic testing and no (related) issues.

Not a blocker but regarding the exception for duplicates - it's probably not that robust to check the string label to determine the exception type. It's not ts'd at the moment (which I think is correct) but somebody might come along and do that, or even just change the string. The api should probably have a numeric error code for it.

@eileenmcnaughton
Copy link
Contributor

The alternative is to use a specific exception type - which we do in a few places eg
CRM_Contribute_Exception_PastContributionPageException

that's probably more consistent with other places

Anyway - merging this - that could be a follow up

@eileenmcnaughton eileenmcnaughton merged commit 2d4074a into civicrm:master Sep 9, 2020
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