+ public function createMembershipAndContact($contactFixture, $membershipTypeId) {
+ $result = $this->callAPISuccess('contact', 'create', $contactFixture);
+ $contact = $result['values'][$result['id']];
+ $params = [
+ 'status_id' => 2,
+ 'contact_id' => $contact['id'],
+ 'membership_type_id' => $membershipTypeId,
+ 'owner_membership_id' => 'NULL',
+ ];
+ $params = array_merge($this->fixtures['rolling_membership'], $params);
+ $membership = $this->createTestObject('CRM_Member_DAO_Membership', $params);
+ $this->assertTrue(is_numeric($membership->id));
+ return $membership;
+ }
+
+ /**
+ * This test is very similar to testMembershipEndDateMatch, but it adds
+ * another contact because there was a bug in
+ * RecipientBuilder::buildRelFirstPass where it was only sending the
+ * reminder for the first contact returned in a query for renewed
+ * memberships. Other contacts wouldn't get the mail.
+ */
+ public function testMultipleMembershipEndDateMatch() {
+ $membershipTypeId = $this->membershipTypeCreate($this->fixtures['rolling_membership']['membership_type_id']);
+ $membershipOne = $this->createMembershipAndContact($this->fixtures['contact'], $membershipTypeId);
+ $membershipTwo = $this->createMembershipAndContact($this->fixtures['contact_2'], $membershipTypeId);
+ $actionSchedule = $this->fixtures['sched_membership_end_2week'];
+ $actionSchedule['entity_value'] = $membershipTypeId;
+ $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
+ $this->assertTrue(is_numeric($actionScheduleDao->id));
+
+ // end_date=2012-06-15 ; schedule is 2 weeks before end_date
+ $this->assertCronRuns([
+ [
+ // Before the 2-week mark, no email.
+ 'time' => '2012-05-31 01:00:00',
+ 'recipients' => [],
+ ],
+ [
+ // After the 2-week mark, send emails.
+ 'time' => '2012-06-01 01:00:00',
+ 'recipients' => [
+ ['test-member@example.com'],
+ ['test-contact-2@example.com'],
+ ],
+ ],
+ [
+ // After the email is sent, another one is not sent
+ 'time' => '2012-06-01 02:00:00',
+ 'recipients' => [],
+ ],
+ ]);
+
+ // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
+ // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
+ $membershipOne->end_date = '2012-09-15';
+ $membershipOne->save();
+ $membershipTwo->end_date = '2012-09-15';
+ $membershipTwo->save();
+
+ // end_date=2012-09-15 ; schedule is 2 weeks before end_date
+ $this->assertCronRuns([
+ [
+ // Before the 2-week mark, no email
+ 'time' => '2012-08-31 01:00:00',
+ 'recipients' => [],
+ ],
+ [
+ // After the 2-week mark, send an email
+ 'time' => '2012-09-01 01:00:00',
+ 'recipients' => [
+ ['test-member@example.com'],
+ ['test-contact-2@example.com'],
+ ],
+ ],
+ [
+ // After the email is sent, another one is not sent
+ 'time' => '2012-06-01 02:00:00',
+ 'recipients' => [],
+ ],
+ ]);
+ }