diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index b8f5d42..20aeced 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -760,7 +760,7 @@ function prune_unnotifiable_users( array $contributors ) : array { $inactivity_threshold = strtotime( INACTIVITY_THRESHOLD_MONTHS . ' months ago' ); foreach ( $contributors as $index => $contributor ) { - if ( $contributor['last_logged_in'] > $inactivity_threshold ) { + if ( is_active( $contributor['last_logged_in'] ) ) { unset( $contributors[ $index ] ); } @@ -772,6 +772,18 @@ function prune_unnotifiable_users( array $contributors ) : array { return $contributors; } +/** + * Determine if a contributor is active or not. + * + * Currently this only tracks the last login, but in the future it will be expanded to be more granular. + * @link https://github.com/WordPress/five-for-the-future/issues/210 + */ +function is_active( int $last_login ) : bool { + $inactivity_threshold = strtotime( INACTIVITY_THRESHOLD_MONTHS . ' months ago' ); + + return $last_login > $inactivity_threshold; +} + /** * Notify an inactive contributor. */ diff --git a/plugins/wporg-5ftf/tests/test-contributor.php b/plugins/wporg-5ftf/tests/test-contributor.php index 8e15574..24b37d7 100644 --- a/plugins/wporg-5ftf/tests/test-contributor.php +++ b/plugins/wporg-5ftf/tests/test-contributor.php @@ -5,10 +5,12 @@ use WordPressDotOrg\FiveForTheFuture\{ Contributor, Pledge, XProfile }; defined( 'WPINC' ) || die(); /** - * These are integration tests rather than unit tests. They target the the highest functions in the call stack + * Some of these are integration tests rather than unit tests. They target the the highest functions in the call stack * in order to test everything beneath them, to the extent that that's practical. `INPUT_POST` can't be mocked, * so functions that reference it can't be used. * + * Mocking that can become unwieldy, though, so sometimes unit tests are more practical. + * * @group contributor */ class Test_Contributor extends WP_UnitTestCase { @@ -265,4 +267,37 @@ class Test_Contributor extends WP_UnitTestCase { $this->assertContains( $jane->user_email, $mailer->mock_sent[0]['to'][0] ); $this->assertSame( "Removed from $tenup->post_title Five for the Future pledge", $mailer->mock_sent[0]['subject'] ); } + + /** + * @covers ::prune_unnotifiable_users + */ + public function test_prune_unnotifiable_users() { + $contributors = array( + 'active + due for email' => array( + 'last_logged_in' => strtotime( '1 week ago' ), + '5ftf_last_inactivity_email' => 0, + ), + + 'active + not due for email' => array( + 'last_logged_in' => strtotime( '1 week ago' ), + '5ftf_last_inactivity_email' => strtotime( '1 month ago' ), + ), + + 'inactive + due for email' => array( + 'last_logged_in' => strtotime( '4 months ago' ), + '5ftf_last_inactivity_email' => strtotime( '4 months ago' ), + ), + + 'inactive + not due for email' => array( + 'last_logged_in' => strtotime( '4 months ago' ), + '5ftf_last_inactivity_email' => strtotime( '2 months ago' ), + ), + ); + + $expected = array( 'inactive + due for email' ); + + $actual = Contributor\prune_unnotifiable_users( $contributors ); + + $this->assertSame( $expected, array_keys( $actual ) ); + } }