Inherited memberships not created correctly when contact count changes.
(Migrating this issue from https://issues.civicrm.org/jira/browse/CRM-21463, since it's been idle there for over a year.)
Suppose you have an organization membership. Suppose the organization has 4 related contacts, each of whom inherits the membership.
Suppose you then add a related contact.
Suppose you then renew the membership.
The newly related contact does not receive the inherited membership. In fact, each time you save / update the membership, an apparently random set of 4 related contacts receives the inherited membership.
============
This turns out to be a bug in Member/BAO/Membership.php. At line 1445, we see
if ($relMembership->find(TRUE)) { $params['id'] = $relMemIds['membership'] = $relMembership->id; }
However, params['id'] is never initialized, so if some related memberships are not found, they are assigned the ID from the last iteration through the loop – meaning the previous contact loses their membership.
The solution is to add $params['id'] = NULL right before. (It would probably be better to set $params = NULL at the beginning of the loop, but I haven't tested that.)
I haven't submitted a patch, but it should be straightforward.