From e5436bd8f016d722649f0877e1422192b328ea36 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 28 Mar 2026 20:35:52 +0000 Subject: [PATCH] Use dynamic previous month length for monthly email reports. Replace the hardcoded 30-day period with the actual number of days in the month of the send date using DateTimeImmutable::format('t'). This ensures monthly reports always cover the correct calendar month regardless of its length (28, 29, 30, or 31 days). Update the frontend label from "Last 28 days" to "Last month" and add parameterised test cases covering all month-length variants including leap year February. --- .../components/email-reporting/FrequencySelector.js | 2 +- .../__snapshots__/FrequencySelector.test.js.snap | 12 ++++++------ includes/Core/Email_Reporting/Initiator_Task.php | 2 +- .../Core/Email_Reporting/Initiator_TaskTest.php | 13 ++++++++----- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/assets/js/components/email-reporting/FrequencySelector.js b/assets/js/components/email-reporting/FrequencySelector.js index f78c398c8bf..a6753cfa831 100644 --- a/assets/js/components/email-reporting/FrequencySelector.js +++ b/assets/js/components/email-reporting/FrequencySelector.js @@ -99,7 +99,7 @@ export default function FrequencySelector( { isUserSubscribed, isLoading } ) { }, monthly: { label: __( 'Monthly', 'google-site-kit' ), - period: __( 'Last 28 days', 'google-site-kit' ), + period: __( 'Last month', 'google-site-kit' ), description: __( 'Sent on the 1st of each month', 'google-site-kit' diff --git a/assets/js/components/email-reporting/__snapshots__/FrequencySelector.test.js.snap b/assets/js/components/email-reporting/__snapshots__/FrequencySelector.test.js.snap index c428e727987..4a86f5883ac 100644 --- a/assets/js/components/email-reporting/__snapshots__/FrequencySelector.test.js.snap +++ b/assets/js/components/email-reporting/__snapshots__/FrequencySelector.test.js.snap @@ -75,7 +75,7 @@ exports[`FrequencySelector Story states (visual + DOM) Monthly selected renders - Last 28 days + Last month
- Last 28 days + Last month
- Last 28 days + Last month
- Last 28 days + Last month
- Last 28 days + Last month
- Last 28 days + Last month
7, - Email_Reporting_Settings::FREQUENCY_MONTHLY => 30, + Email_Reporting_Settings::FREQUENCY_MONTHLY => (int) $send_date->format( 't' ), Email_Reporting_Settings::FREQUENCY_QUARTERLY => 90, ); diff --git a/tests/phpunit/integration/Core/Email_Reporting/Initiator_TaskTest.php b/tests/phpunit/integration/Core/Email_Reporting/Initiator_TaskTest.php index 541ffc43c76..cf3fbbe476f 100644 --- a/tests/phpunit/integration/Core/Email_Reporting/Initiator_TaskTest.php +++ b/tests/phpunit/integration/Core/Email_Reporting/Initiator_TaskTest.php @@ -201,7 +201,7 @@ public function test_handle_callback_action_without_subscribers_still_schedules_ /** * @dataProvider data_build_reference_dates_uses_expected_period_length */ - public function test_build_reference_dates_uses_expected_period_length( $frequency, $expected_days ) { + public function test_build_reference_dates_uses_expected_period_length( $frequency, $expected_days, $timestamp_string ) { $original_timezone_string = get_option( 'timezone_string' ); $original_gmt_offset = get_option( 'gmt_offset' ); @@ -209,7 +209,7 @@ public function test_build_reference_dates_uses_expected_period_length( $frequen update_option( 'gmt_offset', 0 ); try { - $timestamp = strtotime( '2026-03-16 00:00:00 UTC' ); + $timestamp = strtotime( $timestamp_string ); $reference_dates = Initiator_Task::build_reference_dates( $frequency, @@ -256,9 +256,12 @@ public function test_build_reference_dates_uses_previous_day_as_send_date() { public function data_build_reference_dates_uses_expected_period_length() { return array( - 'weekly' => array( Email_Reporting_Settings::FREQUENCY_WEEKLY, 7 ), - 'monthly' => array( Email_Reporting_Settings::FREQUENCY_MONTHLY, 30 ), - 'quarterly' => array( Email_Reporting_Settings::FREQUENCY_QUARTERLY, 90 ), + 'weekly' => array( Email_Reporting_Settings::FREQUENCY_WEEKLY, 7, '2026-03-16 00:00:00 UTC' ), + 'monthly_31_day_month' => array( Email_Reporting_Settings::FREQUENCY_MONTHLY, 31, '2026-04-01 00:00:00 UTC' ), + 'monthly_30_day_month' => array( Email_Reporting_Settings::FREQUENCY_MONTHLY, 30, '2026-07-01 00:00:00 UTC' ), + 'monthly_28_day_month' => array( Email_Reporting_Settings::FREQUENCY_MONTHLY, 28, '2026-03-01 00:00:00 UTC' ), + 'monthly_29_day_leap' => array( Email_Reporting_Settings::FREQUENCY_MONTHLY, 29, '2028-03-01 00:00:00 UTC' ), + 'quarterly' => array( Email_Reporting_Settings::FREQUENCY_QUARTERLY, 90, '2026-03-16 00:00:00 UTC' ), ); } }