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

dev/report#53: search on relationship and case (2) #20002

Merged
merged 5 commits into from
Apr 19, 2021

Conversation

jaapjansma
Copy link
Contributor

Overview

When doing an advanced search with case parameters set and displaying related contacts. Gives all clients of all the found cases which have this relationship. What we would expect is that related contact is linked to the found cases.

Steps to reproduce

Preparation:

  1. Create a contact Contact A
  2. Create another contact Contact B
  3. Create a third contact Contact C
  4. Create a case of type Housing Support for Contact A
  5. On the case assign the role Benefit specialist is to Contact B
  6. Create a second case of type Adult day care referral
  7. On the case assign the role Benefit specialist is to Contact C

Searching:

  1. Go to advanced search
  2. Click on View contact as related contact
  3. Select Benefit Specialist as relationship type
  4. Go to tab cases and select Housing Support as case type

Expected results

I expected to see only Contact B as that one has a relationship on the case Housing Support.

Actual results

I see both Contact B and Contact C.

Comments

See also the discussion of a similar related topic: https://lab.civicrm.org/dev/report/-/issues/53

@civibot
Copy link

civibot bot commented Apr 8, 2021

(Standard links)

@civibot civibot bot added the master label Apr 8, 2021
@eileenmcnaughton
Copy link
Contributor

Can we get a unit test for this? We've been pretty strict for a long time about not having changes to advanced search without adding tests. I know we let your last one through without but we really should stick to it since this has been some of the hardest code to maintain.

The current failure should give a good starting point

CRM_Contact_SelectorTest::testSelectorQuery with data set #10 (array('Test display relationships', 'CRM_Contact_Selector', array(), array('1_b_a'), null, array(), 'advanced', 0, null, false, array('SELECT contact_a.id as contac...egion`', 'WHERE displayRelType.relation...d = 0)')))
different sql
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'WHERE displayRelType.relationship_type_id = 1 AND displayRelType.is_active = 1 AND ( 1 ) AND (contact_a.is_deleted = 0)'
+'AND displayRelType.relationship_type_id = 1 AND displayRelType.is_active = 1 AND ( 1 ) AND (contact_a.is_deleted = 0)'

/home/jenkins/bknix-dfl/build/core-20002-a4wl/web/sites/all/modules/civicrm/tests/phpunit/CiviTest/CiviUnitTestCase.php:2667
/home/jenkins/bknix-dfl/build/core-20002-a4wl/web/sites/all/modules/civicrm/tests/phpunit/CRM/Contact/SelectorTest.php:62
/home/jenkins/bknix-dfl/build/core-20002-a4wl/web/sites/all/modules/civicrm/tests/phpunit/CiviTest/CiviUnitTestCase.php:226
/home/jenkins/bknix-dfl/extern/phpunit7/phpunit7.phar:615

@jaapjansma
Copy link
Contributor Author

I have fixed the failing test.
And I have added a test for this specific use case.

$clientContactID = $this->individualCreate(['first_name' => 'John', 'last_name' => 'Smith']);
$benefitSpecialist1 = $this->individualCreate(['Individual', 'first_name' => 'Alexa', 'last_name' => 'Clarke']);
$benefitSpecialist2 = $this->individualCreate(['Individual', 'first_name' => 'Sandra', 'last_name' => 'Johnson']);
$housingSupportCase = $this->createCase($clientContactID, NULL, ['case_type_id' => 'housing_support', 'case_type_id' => 1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a typo: case_type_id used twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes indeed is a typo

@@ -5837,10 +5837,11 @@ public function filterRelatedContacts(&$from, &$where, &$having) {
else {
$from .= $qcache['from'];
}
$where = $qcache['where'];
$where .= $qcache['where'];
Copy link
Contributor

Choose a reason for hiding this comment

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

I kind of remember this code area from the earlier PR but I don't remember if there was ever a time when $where was passed in to the function and is blank or didn't have "WHERE" in it already. I wonder if it's worth adding a guard against that, e.g. if $where does not contain "WHERE", then $where = "WHERE (1) " . $qcache['where']. Or something like that - just an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a check whether $where is empty.

@demeritcowboy
Copy link
Contributor

In case the jenkins log expires:

CRM_Contact_SelectorTest::testSelectorQuery with data set #11 (array('Test display relationships', 'CRM_Contact_Selector', array(), array('1_b_a'), null, array(), 'advanced', 0, null, false, array('SELECT contact_a.id as contac...egion`', 'AND displayRelType.relationsh...d = 0)')))
different sql
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'AND displayRelType.relationship_type_id = 1 AND displayRelType.is_active = 1 AND ( 1 ) AND (contact_a.is_deleted = 0)'
+'WHERE 1 AND displayRelType.relationship_type_id = 1 AND displayRelType.is_active = 1 AND ( 1 ) AND (contact_a.is_deleted = 0)'

/home/jenkins/bknix-dfl/build/core-20002-h980/web/sites/all/modules/civicrm/tests/phpunit/CRM/Contact/SelectorTest.php:66

@jaapjansma
Copy link
Contributor Author

Thanks for storing the jenkins log. I have fixed the failing test.

@demeritcowboy
Copy link
Contributor

Thanks @jaapjansma I think this is ok to merge.

  • General standards
    • [r-explain] PASS
    • [r-user] PASS
    • [r-doc] PASS
    • [r-run] PASS
      • Can reproduce and this fixes it.
  • Developer standards
    • [r-tech] PASS
      • I'm going to rely on my earlier look at this code area where I said:

        There's already code that does str_replace and if strpos, so it isn't doing something new. ... This code is only ever called when _displayRelationshipType is set to something, and I'm pretty sure, at least in core, that this only happens from the advanced search form (via CRM_Contact_Selector). It's technically possible to call CRM_Contact_BAO_Query::__construct in a way you could end up here, but just browsing a few I didn't see any. I didn't see a way to trigger this via api.

    • [r-code] PASS
    • [r-maint] PASS
    • [r-test] PASS

@eileenmcnaughton
Copy link
Contributor

merging based on @demeritcowboy review

@eileenmcnaughton eileenmcnaughton merged commit 8d7a356 into civicrm:master Apr 19, 2021
@eileenmcnaughton
Copy link
Contributor

Just noting this appears to have caused a regression - #20997

@eileenmcnaughton
Copy link
Contributor

@jaapjansma we made the call to revert this for now in order to fix the regression & get the releases out. @jmcclelland added a test for the regression so we can be confident it won't regress again. Note your test was not reverted - just marked incomplete for now.

I will re-open in gitlab

@eileenmcnaughton
Copy link
Contributor

Actually the original issue had a whole lot of stuff in it not just this so I opened https://lab.civicrm.org/dev/report/-/issues/72 to track

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.

3 participants