Contributors: Modularize activity detection for reuse.

This commit is contained in:
Ian Dunn 2022-08-17 10:49:30 -07:00
parent 44e9daccd9
commit 01643a2922
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
2 changed files with 49 additions and 2 deletions

View file

@ -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.
*/

View file

@ -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 ) );
}
}