Skip to content

Commit

Permalink
Make first test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Dec 3, 2024
1 parent f5d5dcc commit bc37a5c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 34 deletions.
35 changes: 19 additions & 16 deletions inc/class-anniversary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
class Anniversary {
public function __construct() {
add_action( 'wporg_translate_notification_anniversary', array( $this, 'send_email_to_translator' ) );
add_action( 'wporg_translate_notification_summary_anniversary', array( $this, 'send_slack_notification' ) );
add_action( 'wporg_translate_notification_anniversary', array( $this, 'send_email_to_translator' ), 10, 3 );
add_action( 'wporg_translate_notification_summary_anniversary', array( $this, 'send_slack_notification' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -45,10 +45,10 @@ private function get_users_and_first_translation_date(): array {
$users = array();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$max_user_id = $wpdb->get_var( 'SELECT MAX(user_id) FROM translate_translations' );
$max_user_id = $wpdb->get_var( "SELECT MAX(user_id) FROM {$wpdb->gp_translations}" );

// Todo: change to 1.
$first_id = 21_000_000;
$first_id = 1;
$batch_size = 50_000;

do {
Expand Down Expand Up @@ -101,29 +101,32 @@ private function get_translators_in_anniversary( ?array $users ): array {
*
* @return array An array with the user_id as key and the number of translations as value.
*/
private function get_number_of_translations( int $user_id ): array {
private function get_number_of_translations( int $user_id ): int {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(id)
FROM translate_translations
WHERE user_id = %d
AND status = 'current'",
$user_id
return intval(
$wpdb->get_var(

Check warning on line 108 in inc/class-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Use of a direct database call is discouraged.

Check warning on line 108 in inc/class-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().
$wpdb->prepare(
"SELECT COUNT(id)
FROM {$wpdb->gp_translations}
WHERE user_id = %d
AND status = 'current'",
$user_id
)
)
);
}

/**
* Send an email to the translators.
*
* @param int $anniversary_users The user_id (key) that have an anniversary and their start date (value. Y-m-d format).
* @param int $number_of_translations The number of translations for each user as value. The user_id is the key.
* @param int $user_id The user_id of the anniversary user.
* @param string $date The user's first translation date (Y-m-d format).
* @param int $number_of_translations The number of translations made so far.
*
* @return void
*/
private function send_email_to_translator( int $user_id, string $date, int $number_of_translations ) {
public function send_email_to_translator( int $user_id, string $date, int $number_of_translations ) {
$user = get_userdata( $user_id );
$start_date = new \DateTime( $date );
$today = new \DateTime();
Expand Down Expand Up @@ -183,7 +186,7 @@ private function send_email_to_translator( int $user_id, string $date, int $numb
*
* @return void
*/
private function send_slack_notification( ?array $anniversary_users, ?array $number_of_translations ) {
public function send_slack_notification( ?array $anniversary_users, ?array $number_of_translations ) {
if ( ! $anniversary_users ) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions inc/class-consistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Consistency {
private array $months_to_notify = array( 48, 24, 12, 6 );

public function __construct() {

Check failure on line 22 in inc/class-consistency.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing doc comment for function __construct()
add_action( 'wporg_translate_notification_consistency', array( $this, 'send_email_to_translator' ) );
add_action( 'wporg_translate_notification_consistency', array( $this, 'send_email_to_translator' ), 10, 2 );
add_action( 'wporg_translate_notification_summary_consistency', array( $this, 'send_slack_notification' ) );
}

Expand Down Expand Up @@ -115,7 +115,7 @@ private function get_users_with_consistency_last_months( int $months = 6 ): arra
*
* @return void
*/
private function send_email_to_translators( $months, $user_id ): void {
public function send_email_to_translators( $months, $user_id ): void {
$years = intdiv( $months, 12 );
// Translators: Number of years or months of translation consistency, to be used in the email subject.
$time_period = $years > 0 ? sprintf( _n( '%d year', '%d years', $years, 'wporg-gp-engagement' ), $years ) : sprintf( _n( '%d month', '%d months', $months, 'wporg-gp-engagement' ), $months );
Expand Down Expand Up @@ -162,7 +162,7 @@ private function send_email_to_translators( $months, $user_id ): void {
*
* @param array $users_to_notify The users to notify.
*/
private function send_slack_notifications( array $users_to_notify ) {
public function send_slack_notifications( array $users_to_notify ) {
foreach ( $users_to_notify as $months => $user_ids ) {
$years = intdiv( $months, 12 );
// Translators: Number of years or months of translation consistency, to be used in the Slack message.
Expand Down
4 changes: 2 additions & 2 deletions inc/class-first-translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function date_of_first_approved_translation( int $user_id ): string {
*
* @return void
*/
private function send_email_to_translator( GP_Translation $translation ) {
public function send_email_to_translator( GP_Translation $translation ) {
$notification_elements = $this->get_notification_elements( $translation );
if ( false === $notification_elements ) {
return;
Expand Down Expand Up @@ -186,7 +186,7 @@ private function send_email_to_translator( GP_Translation $translation ) {
*
* @return void
*/
private function send_slack_notification( GP_Translation $translation ) {
public function send_slack_notification( GP_Translation $translation ) {
$notification_elements = $this->get_notification_elements( $translation );
if ( false === $notification_elements ) {
return;
Expand Down
4 changes: 2 additions & 2 deletions inc/class-inactive.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function get_inactive_users( ?array $users, string $date ): array {
*
* @return void
*/
private function send_email_to_translators( int $user_id ) {
public function send_email_to_translators( int $user_id ) {
$subject = esc_html__( 'We did not see you in the last year and we miss you! ⏳', 'wporg-gp-engagement' );
$user = get_user_by( 'id', $user_id );
$message = sprintf(
Expand Down Expand Up @@ -151,7 +151,7 @@ private function send_email_to_translators( int $user_id ) {
*
* @return void
*/
private function send_slack_notification( array $inactive_users ) {
public function send_slack_notification( array $inactive_users ) {
$users = array();
foreach ( $inactive_users as $user_id ) {
$user = get_userdata( $user_id );
Expand Down
2 changes: 1 addition & 1 deletion inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct() {
add_action( 'gp_engagement_consistency', array( new Consistency(), '__invoke' ) );

$notification = new Notification();
add_action( 'wporg_translate_notification_email', array( $notification, 'send_email' ) );
add_action( 'wporg_translate_notification_email', array( $notification, 'send_email' ), 10, 3 );
add_action( 'wporg_translate_notification_slack', array( $notification, 'send_slack_notification' ) );
}

Expand Down
8 changes: 4 additions & 4 deletions inc/class-translation-milestone.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Translation_Milestone {
);

public function __construct() {

Check failure on line 37 in inc/class-translation-milestone.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing doc comment for function __construct()
add_action( 'wporg_translate_notification_milestone', array( $this, 'send_email_to_translator' ) );
add_action( 'wporg_translate_notification_summary_milestone', array( $this, 'send_slack_notification' ) );
add_action( 'wporg_translate_notification_milestone', array( $this, 'send_email_to_translator' ), 10, 2 );
add_action( 'wporg_translate_notification_summary_milestone', array( $this, 'send_slack_notification' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ private function is_milestone( ?GP_Translation $translation ) {
*
* @return void
*/
private function send_email_to_translator( GP_Translation $translation, int $milestone ) {
public function send_email_to_translator( GP_Translation $translation, int $milestone ) {
$user = get_userdata( $translation->user_id );
$subject = sprintf(
// translators: Email subject.
Expand Down Expand Up @@ -156,7 +156,7 @@ private function send_email_to_translator( GP_Translation $translation, int $mil
*
* @return void
*/
private function send_slack_notification( GP_Translation $translation, int $milestone ) {
public function send_slack_notification( GP_Translation $translation, int $milestone ) {
$user = get_userdata( $translation->user_id );

// translators: Slack message. %s: Display name. %d: Milestone.
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
testdox="true"
>
<testsuites>
<testsuite name="tests">
Expand Down
28 changes: 22 additions & 6 deletions tests/test-anniversary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
namespace WordPressdotorg\GlotPress\Engagement;

class Anniversary_Test extends \GP_UnitTestCase {
public function test_anniversary() {

public function anniversary_data_provider() {
return array(
'today' => array( time(), 0 ),

Check warning on line 8 in tests/test-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Array double arrow not aligned correctly; expected 9 space(s) between "'today'" and double arrow, but found 1.
'1 years ago' => array( strtotime( '-1 year' ), 1 ),

Check warning on line 9 in tests/test-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Array double arrow not aligned correctly; expected 3 space(s) between "'1 years ago'" and double arrow, but found 1.
'1.5 years ago' => array( strtotime( '-1.5 year' ), 0 ),
'2 years ago' => array( strtotime( '-2 year' ), 1 ),

Check warning on line 11 in tests/test-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Array double arrow not aligned correctly; expected 3 space(s) between "'2 years ago'" and double arrow, but found 1.
);
}

/**
* @dataProvider anniversary_data_provider
*/
public function test_anniversary( $date, $expected ) {

$user = $this->factory->user->create();

$translation = $this->factory->translation->create( array(
'status' => 'current',

Check warning on line 23 in tests/test-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Array double arrow not aligned correctly; expected 16 space(s) between "'status'" and double arrow, but found 1.
'locale' => 'en',

Check warning on line 24 in tests/test-anniversary.php

View workflow job for this annotation

GitHub Actions / phpcs

Array double arrow not aligned correctly; expected 16 space(s) between "'locale'" and double arrow, but found 1.
Expand All @@ -12,16 +27,17 @@ public function test_anniversary() {
'user_id_last_modified' => $user,
) );

$translation->update( array( 'date_added' => date( 'Y-m-d H:i:s', $date ) ) );

remove_all_actions( 'wporg_translate_notification_milestone' );
remove_all_actions( 'wporg_translate_notification_summary_milestone' );

$mock = new \MockAction();
add_action( 'wporg_translate_notification_milestone', array( $mock, 'action' ), 10, 2 );
add_action( 'wporg_translate_notification_anniversary', array( $mock, 'action' ), 10, 2 );

$translation_milestone = new Translation_Milestone();
$translation_milestone( $translation );
$anniversary = new Anniversary();
$anniversary();

// Ensure the email was sent.
$this->assertEquals( 1, $mock->get_call_count() );
$this->assertEquals( $expected, $mock->get_call_count() );
}
}

0 comments on commit bc37a5c

Please sign in to comment.