Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions assets/js/components/email-reporting/FrequencySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ 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'
),
},
quarterly: {
label: __( 'Quarterly', 'google-site-kit' ),
period: __( 'Last 90 days', 'google-site-kit' ),
period: __( 'Last quarter', 'google-site-kit' ),
description: __(
'Sent on the 1st of each quarter',
'google-site-kit'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ import {
import UserSettingsSelectionPanel from '@/js/components/email-reporting/UserSettingsSelectionPanel';
import SelectionPanelFooter from '@/js/components/email-reporting/UserSettingsSelectionPanel/SelectionPanelFooter';

// This suite tests panel behavior; mock the invite list to avoid async datastore
// updates from child-level fetching that are covered in InviteOthersToSubscribe tests.
jest.mock(
'@/js/components/email-reporting/InviteOthersToSubscribe',
() => () => null
);

describe( 'UserSettingsSelectionPanel', () => {
const features = [ 'proactiveUserEngagement' ];
const emailReportingSettingsEndpoint = new RegExp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ exports[`FrequencySelector Story states (visual + DOM) Monthly selected renders
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -117,7 +117,7 @@ exports[`FrequencySelector Story states (visual + DOM) Monthly selected renders
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down Expand Up @@ -210,7 +210,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -252,7 +252,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down Expand Up @@ -345,7 +345,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -387,7 +387,7 @@ exports[`FrequencySelector Story states (visual + DOM) Previously saved frequenc
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down Expand Up @@ -480,7 +480,7 @@ exports[`FrequencySelector Story states (visual + DOM) Quarterly selected render
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -522,7 +522,7 @@ exports[`FrequencySelector Story states (visual + DOM) Quarterly selected render
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down Expand Up @@ -615,7 +615,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected (default
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -657,7 +657,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected (default
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down Expand Up @@ -750,7 +750,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected with Sund
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 28 days
Last month
</span>
</div>
<div
Expand Down Expand Up @@ -792,7 +792,7 @@ exports[`FrequencySelector Story states (visual + DOM) Weekly selected with Sund
<span
class="googlesitekit-typography googlesitekit-typography--body googlesitekit-typography--small"
>
Last 90 days
Last quarter
</span>
</div>
<div
Expand Down
4 changes: 2 additions & 2 deletions includes/Core/Email_Reporting/Email_Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static function get_date_range_from_log( $email_log ) {
$normalized = array();
$keys = array(
'startDate' => 'startDate',
'sendDate' => 'endDate',
'endDate' => 'endDate',
'compareStartDate' => 'compareStartDate',
'compareEndDate' => 'compareEndDate',
);
Expand Down Expand Up @@ -573,7 +573,7 @@ public static function sanitize_admin_notified( $value ) {
* @return array Normalized timestamps keyed by meta field.
*/
protected static function normalize_reference_dates( array $raw_dates ) {
$keys = array( 'startDate', 'sendDate', 'compareStartDate', 'compareEndDate' );
$keys = array( 'startDate', 'endDate', 'compareStartDate', 'compareEndDate' );
$normalized = array();

foreach ( $keys as $key ) {
Expand Down
39 changes: 30 additions & 9 deletions includes/Core/Email_Reporting/Initiator_Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,49 @@ public static function build_reference_dates( $frequency, $timestamp ) {
->setTime( 0, 0, 0 );
// Initiators are scheduled at period-boundary midnight in site timezone.
// The reporting window should end on "yesterday" (inclusive end date), so subtract one day.
$send_date = $scheduled_date->sub( new DateInterval( 'P1D' ) );
$end_date = $scheduled_date->sub( new DateInterval( 'P1D' ) );

$period_lengths = array(
Email_Reporting_Settings::FREQUENCY_WEEKLY => 7,
Email_Reporting_Settings::FREQUENCY_MONTHLY => 30,
Email_Reporting_Settings::FREQUENCY_QUARTERLY => 90,
);
$period_days = 7;

$period_days = isset( $period_lengths[ $frequency ] ) ? $period_lengths[ $frequency ] : $period_lengths[ Email_Reporting_Settings::FREQUENCY_WEEKLY ];
if ( Email_Reporting_Settings::FREQUENCY_MONTHLY === $frequency ) {
$period_days = (int) $end_date->format( 't' );
} elseif ( Email_Reporting_Settings::FREQUENCY_QUARTERLY === $frequency ) {
$period_days = self::get_days_in_quarter( $end_date );
}

// endDate is inclusive, so startDate must be endDate - (period_days - 1) for an exact period-length window.
$start_date = $send_date->sub( new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) ) );
$start_date = $end_date->sub( new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) ) );
$compare_end_date = $start_date->sub( new DateInterval( 'P1D' ) );
$compare_start_date = $compare_end_date->sub(
new DateInterval( sprintf( 'P%dD', max( $period_days - 1, 0 ) ) )
);

return array(
'startDate' => $start_date->format( 'Y-m-d' ),
'sendDate' => $send_date->format( 'Y-m-d' ),
'endDate' => $end_date->format( 'Y-m-d' ),
'compareStartDate' => $compare_start_date->format( 'Y-m-d' ),
'compareEndDate' => $compare_end_date->format( 'Y-m-d' ),
);
}

/**
* Gets the number of days in the quarter for a date.
*
* @since n.e.x.t
*
* @param DateTimeImmutable $date Date in the target quarter.
* @return int Days in the quarter.
*/
private static function get_days_in_quarter( DateTimeImmutable $date ) {
$year = (int) $date->format( 'Y' );
$month = (int) $date->format( 'n' );
$quarter_start_month = floor( ( $month - 1 ) / 3 ) * 3 + 1;

$quarter_start = $date->setDate( $year, $quarter_start_month, 1 );
$quarter_end = $quarter_start
->add( new DateInterval( 'P3M' ) )
->sub( new DateInterval( 'P1D' ) );

return (int) $quarter_start->diff( $quarter_end )->days + 1;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private function create_log_post( $batch_id, $status, $attempts, array $override
Email_Log::META_REPORT_REFERENCE_DATES,
array(
'startDate' => time() - DAY_IN_SECONDS,
'sendDate' => time(),
'endDate' => time(),
)
);

Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/integration/Core/Email_Reporting/Email_LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function test_meta_sanitization_behaviour() {
$reference_callback,
array(
'startDate' => '1700000000',
'sendDate' => 1800000000,
'endDate' => 1800000000,
'compareStartDate' => 'not-a-number',
)
);
Expand All @@ -200,7 +200,7 @@ public function test_meta_sanitization_behaviour() {
$this->assertSame(
array(
'startDate' => 1700000000,
'sendDate' => 1800000000,
'endDate' => 1800000000,
'compareStartDate' => 0,
'compareEndDate' => 0,
),
Expand All @@ -224,7 +224,7 @@ public function test_get_date_range_from_log_returns_normalized_dates() {
Email_Log::META_REPORT_REFERENCE_DATES,
array(
'startDate' => strtotime( '2024-07-01' ),
'sendDate' => strtotime( '2024-07-31' ),
'endDate' => strtotime( '2024-07-31' ),
'compareStartDate' => strtotime( '2024-06-01' ),
'compareEndDate' => strtotime( '2024-06-30' ),
)
Expand All @@ -239,7 +239,7 @@ public function test_get_date_range_from_log_returns_normalized_dates() {
'compareEndDate' => gmdate( 'Y-m-d', strtotime( '2024-06-30' ) ),
);

$this->assertSame( $expected, $date_range, 'Date range should be normalized to Y-m-d strings with endDate from sendDate.' );
$this->assertSame( $expected, $date_range, 'Date range should be normalized to Y-m-d strings with explicit endDate.' );
}

public function test_get_date_range_from_log_returns_null_when_incomplete() {
Expand All @@ -261,7 +261,7 @@ public function test_get_date_range_from_log_returns_null_when_incomplete() {
update_post_meta(
$post_id,
Email_Log::META_REPORT_REFERENCE_DATES,
wp_json_encode( array( 'sendDate' => strtotime( '2024-07-31' ) ) )
wp_json_encode( array( 'endDate' => strtotime( '2024-07-31' ) ) )
);

$this->assertNull( Email_Log::get_date_range_from_log( get_post( $post_id ) ), 'Missing start date should return null.' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_build_sections_with_report_payloads() {

$date_range_meta = array(
'startDate' => strtotime( '2024-09-01' ),
'sendDate' => strtotime( '2024-10-01' ),
'endDate' => strtotime( '2024-10-01' ),
'compareStartDate' => strtotime( '2024-08-01' ),
'compareEndDate' => strtotime( '2024-08-31' ),
);
Expand All @@ -53,7 +53,7 @@ public function test_build_sections_with_report_payloads() {

$expected_date_range = array(
'startDate' => $format_date( $date_range_meta['startDate'] ),
'endDate' => $format_date( $date_range_meta['sendDate'] ),
'endDate' => $format_date( $date_range_meta['endDate'] ),
'compareStartDate' => $format_date( $date_range_meta['compareStartDate'] ),
'compareEndDate' => $format_date( $date_range_meta['compareEndDate'] ),
);
Expand Down
Loading
Loading