diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 1c036e2..36fd652 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -92,7 +92,7 @@ install_wp() { tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR fi - download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php + download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php } install_test_suite() { @@ -176,6 +176,11 @@ install_db() { fi } +install_gp() { + git clone --branch develop --single-branch -q https://github.com/GlotPress/GlotPress.git "$WP_CORE_DIR/wp-content/plugins/glotpress" +} + install_wp install_test_suite install_db +install_gp diff --git a/inc/class-anniversary.php b/inc/class-anniversary.php index d031bc0..1ec2dcb 100644 --- a/inc/class-anniversary.php +++ b/inc/class-anniversary.php @@ -13,6 +13,9 @@ * Sends an email to translators in their translation anniversary. */ class Anniversary { + /** + * Constructor. + */ public function __construct() { 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 ); @@ -103,8 +106,8 @@ private function get_translators_in_anniversary( ?array $users ): array { */ private function get_number_of_translations( int $user_id ): int { global $wpdb; - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return intval( + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) @@ -120,9 +123,9 @@ private function get_number_of_translations( int $user_id ): int { /** * Send an email to the translators. * - * @param int $user_id The user_id of the anniversary user. + * @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. + * @param int $number_of_translations The number of translations made so far. * * @return void */ @@ -139,7 +142,7 @@ public function send_email_to_translator( int $user_id, string $date, int $numbe $message = sprintf( // translators: Email body. %1$s: Display name. %2$d: number of years since the first translation. %3$d: number of translations. _n( - ' + ' Dear %1$s,

do you remember? On this day, %2$d year ago, you contributed your first translation to translate.wordpress.org. @@ -150,7 +153,7 @@ public function send_email_to_translator( int $user_id, string $date, int $numbe

The Global Polyglots Team ', - ' + ' Dear %1$s,

do you remember? On this day, %2$d years ago, you contributed your first translation to translate.wordpress.org. diff --git a/inc/class-consistency.php b/inc/class-consistency.php index f435ca2..4ecd711 100644 --- a/inc/class-consistency.php +++ b/inc/class-consistency.php @@ -19,6 +19,9 @@ class Consistency { */ private array $months_to_notify = array( 48, 24, 12, 6 ); + /** + * Constructor. + */ public function __construct() { 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' ) ); @@ -111,11 +114,12 @@ private function get_users_with_consistency_last_months( int $months = 6 ): arra /** * Send an email to the translators. * - * @param array $users_to_notify The users to notify. + * @param int $months The number of months. + * @param int $user_id The user ID. * * @return void */ - public function send_email_to_translators( $months, $user_id ): void { + public function send_email_to_translators( int $months, int $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 ); @@ -158,12 +162,12 @@ public function send_email_to_translators( $months, $user_id ): void { } /** - * Send a Slack notification to the users + * Send a Slack notification about the notified users * - * @param array $users_to_notify The users to notify. + * @param array $users_notified The users that were notify. */ - public function send_slack_notifications( array $users_to_notify ) { - foreach ( $users_to_notify as $months => $user_ids ) { + public function send_slack_notifications( array $users_notified ) { + foreach ( $users_notified as $months => $user_ids ) { $years = intdiv( $months, 12 ); // Translators: Number of years or months of translation consistency, to be used in the Slack message. $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 ); diff --git a/inc/class-first-translation.php b/inc/class-first-translation.php index f284806..bb65725 100644 --- a/inc/class-first-translation.php +++ b/inc/class-first-translation.php @@ -15,6 +15,9 @@ * Sends an email to translators who for the first time had a translation approved. */ class First_Translation { + /** + * Constructor. + */ public function __construct() { add_action( 'wporg_translate_notification_first_translation', array( $this, 'send_email_to_translator' ) ); add_action( 'wporg_translate_notification_summary_first_translation', array( $this, 'send_slack_notification' ) ); @@ -41,7 +44,6 @@ public function __invoke( ?GP_Translation $translation ) { do_action( 'wporg_translate_notification_first_translation', $translation ); $this->update_user_option( $translation->user_id ); do_action( 'wporg_translate_notification_summary_first_translation', $translation ); - } /** diff --git a/inc/class-inactive.php b/inc/class-inactive.php index 40b1761..2339a02 100644 --- a/inc/class-inactive.php +++ b/inc/class-inactive.php @@ -14,22 +14,25 @@ * Sends an email to translators in their translation anniversary. */ class Inactive { + /** + * Constructor. + */ public function __construct() { add_action( 'wporg_translate_notification_inactive', array( $this, 'send_email_to_translator' ) ); add_action( 'wporg_translate_notification_summary_inactive', array( $this, 'send_slack_notification' ) ); } /** - * Send an email to translators who have been inactive in the last years. - * - * @return void - */ + * Send an email to translators who have been inactive in the last years. + * + * @return void + */ public function __invoke() { $one_year_ago = ( new DateTime() )->modify( '-1 year' )->format( 'Y-m-d' ); $all_users = $this->get_users_with_translation_on_date( $one_year_ago ); $inactive_users = $this->get_inactive_users( $all_users, $one_year_ago ); - foreach ( $inactive_users as $user_id ) { - do_action( 'wporg_translate_notification_inactive', $user_id ); - } + foreach ( $inactive_users as $user_id ) { + do_action( 'wporg_translate_notification_inactive', $user_id ); + } do_action( 'wporg_translate_notification_summary_inactive', $inactive_users ); } diff --git a/inc/class-plugin.php b/inc/class-plugin.php index fcfb728..4e7c860 100644 --- a/inc/class-plugin.php +++ b/inc/class-plugin.php @@ -112,9 +112,4 @@ public function wp_schedule_crons() { wp_schedule_event( $timestamp, 'monthly', 'gp_engagement_consistency' ); } } - - public function send_notification_email( $user, $subject, $message ) { - $email = new Notification(); - $email->send_email( $user, $subject, $message ); - } } diff --git a/inc/class-translation-milestone.php b/inc/class-translation-milestone.php index 10793f7..dc7374f 100644 --- a/inc/class-translation-milestone.php +++ b/inc/class-translation-milestone.php @@ -34,6 +34,9 @@ class Translation_Milestone { 1000000, ); + /** + * Constructor. + */ public function __construct() { 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 ); diff --git a/phpcs.xml b/phpcs.xml index 79d4c42..0ddacba 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,6 +7,10 @@ . inc + + tests/* + + */vendor/* @@ -17,4 +21,4 @@ - \ No newline at end of file + diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9ee8f4e..3407df6 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1,21 @@ array( time(), 0 ), - '1 years ago' => array( strtotime( '-1 year' ), 1 ), + 'today' => array( time(), 0 ), + '1 years ago' => array( strtotime( '-1 year' ), 1 ), '1.5 years ago' => array( strtotime( '-1.5 year' ), 0 ), - '2 years ago' => array( strtotime( '-2 year' ), 1 ), + '2 years ago' => array( strtotime( '-2 year' ), 1 ), ); } /** + * Test the anniversary notification + * * @dataProvider anniversary_data_provider + * + * @param int $date The date to test. + * @param int $expected The expected number of calls to the mock action. */ public function test_anniversary( $date, $expected ) { $user = $this->factory->user->create(); - $translation = $this->factory->translation->create( array( - 'status' => 'current', - 'locale' => 'en', - 'translation_set_id' => 1, - 'user_id' => $user, - 'user_id_last_modified' => $user, - ) ); + $translation = $this->factory->translation->create( + array( + 'status' => 'current', + 'locale' => 'en', + 'translation_set_id' => 1, + 'user_id' => $user, + 'user_id_last_modified' => $user, + ) + ); - $translation->update( array( 'date_added' => date( 'Y-m-d H:i:s', $date ) ) ); + $translation->update( array( 'date_added' => gmdate( 'Y-m-d H:i:s', $date ) ) ); remove_all_actions( 'wporg_translate_notification_milestone' ); remove_all_actions( 'wporg_translate_notification_summary_milestone' ); diff --git a/tests/test-consistency.php b/tests/test-consistency.php index 923697d..9915bf4 100644 --- a/tests/test-consistency.php +++ b/tests/test-consistency.php @@ -1,5 +1,14 @@