Add unit test for core#19628
authorMonish Deb <monish.deb@jmaconsulting.biz>
Tue, 6 Apr 2021 05:33:37 +0000 (11:03 +0530)
committerMonish Deb <monish.deb@jmaconsulting.biz>
Fri, 9 Apr 2021 06:30:21 +0000 (12:00 +0530)
tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php

index 21d96074e50ac734c53364487a3ac09962320c97..8e6686d5c0cf08df677c5414d5c203d738796fba 100644 (file)
@@ -755,6 +755,14 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
       'html_type' => 'Text',
       'custom_group_id' => $customGroup['id'],
     ]);
+    $customDateField = $this->callAPISuccess('CustomField', 'create', [
+      'label' => 'Test Date Field',
+      'data_type' => 'Date',
+      'html_type' => 'Select Date',
+      'date_format' => 'mm/dd/yy',
+      'custom_group_id' => $customGroup['id'],
+    ]);
+
     $this->fixtures['contact_custom_token'] = [
       'id' => $customField['id'],
       'token' => sprintf('{contact.custom_%s}', $customField['id']),
@@ -762,6 +770,32 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
       'value' => 'text ' . substr(sha1(mt_rand()), 0, 7),
     ];
 
+    $this->fixtures['sched_on_custom_date'] = [
+      'name' => 'sched_on_custom_date',
+      'title' => 'sched_on_custom_date',
+      'body_html' => '<p>Send reminder before 1 hour of custom date field</p>',
+      'body_text' => 'Send reminder on custom date field',
+      'subject' => 'Send reminder on custom date field',
+      'mapping_id' => 6,
+      'entity_value' => 'custom_' . $customDateField['id'],
+      'entity_status' => 2,
+      'entity' => [
+        6,
+        ['custom_' . $customDateField['id']],
+        [1],
+      ],
+      'start_action_offset' => 1,
+      'start_action_unit' => 'hour',
+      'start_action_condition' => 'before',
+      'start_action_date' => 'date_field',
+      'record_activity' => 1,
+      'repetition_frequency_unit' => 'hour',
+      'end_frequency_unit' => 'hour',
+      'end_action' => 'before',
+      'end_date' => 'date_field',
+      'custom_field_name' => 'custom_' . $customDateField['id'],
+    ];
+
     $this->_setUp();
   }
 
@@ -951,6 +985,33 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
     $this->mut->clearMessages();
   }
 
+  /**
+   * Send reminder 1 hour before custom date field
+   */
+  public function testReminderWithCustomDateField(): void {
+    $this->createScheduleFromFixtures('sched_on_custom_date');
+    $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], [$this->fixtures['sched_on_custom_date']['custom_field_name'] => '04/06/2021']));
+    $this->assertCronRuns([
+      [
+        // Before the 24-hour mark, no email
+        'time' => '2021-04-02 04:00:00',
+        'recipients' => [],
+        'subjects' => [],
+      ],
+      [
+        // After the 24-hour mark, an email
+        'time' => '2021-04-05 23:00:00',
+        'recipients' => [['test-member@example.com']],
+        'subjects' => ['Send reminder on custom date field'],
+      ],
+      [
+        // Run cron again; message already sent
+        'time' => '',
+        'recipients' => [],
+      ],
+    ]);
+  }
+
   /**
    * Test calculated activity schedule.
    *