Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Dec 3, 2024
1 parent bc37a5c commit f968c86
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 39 deletions.
7 changes: 6 additions & 1 deletion bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
13 changes: 8 additions & 5 deletions inc/class-anniversary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -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,
<br><br>
do you remember? On this day, %2$d year ago, you contributed your first translation to translate.wordpress.org.
Expand All @@ -150,7 +153,7 @@ public function send_email_to_translator( int $user_id, string $date, int $numbe
<br><br>
The Global Polyglots Team
',
'
'
Dear %1$s,
<br><br>
do you remember? On this day, %2$d years ago, you contributed your first translation to translate.wordpress.org.
Expand Down
16 changes: 10 additions & 6 deletions inc/class-consistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
4 changes: 3 additions & 1 deletion inc/class-first-translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand All @@ -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 );

}

/**
Expand Down
17 changes: 10 additions & 7 deletions inc/class-inactive.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
5 changes: 0 additions & 5 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
3 changes: 3 additions & 0 deletions inc/class-translation-milestone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 5 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<file>.</file>
<file>inc</file>

<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Exclude the vendor directory -->
<exclude-pattern>*/vendor/*</exclude-pattern>

Expand All @@ -17,4 +21,4 @@

<!-- Additional arguments -->
<arg name="basepath" value="."/>
</ruleset>
</ruleset>
12 changes: 11 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<?php
/**
* GlotPress Engagement Tests Bootstrap.
*
* @package wporg-gp-engagement
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

/**
* Get the path to the GlotPress repository.
*
* @param string $path The path to append to the GlotPress repository path.
* @return string The full path to the GlotPress repository.
*/
function _glotpress_path( string $path ): string {
$glotpress_path = dirname( __DIR__, 2 ) . '/glotpress/';
if ( getenv( 'GITHUB_ACTIONS' ) ) {
Expand Down Expand Up @@ -47,4 +58,3 @@ function _manually_load_plugin() {
require_once _glotpress_path( '/tests/phpunit/lib/testcase.php' );
require_once _glotpress_path( '/tests/phpunit/lib/testcase-route.php' );
require_once _glotpress_path( '/tests/phpunit/lib/testcase-request.php' );

44 changes: 32 additions & 12 deletions tests/test-anniversary.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
<?php
/**
* Tests for the Anniversary class.
*
* @package wporg-gp-engagement
*/

namespace WordPressdotorg\GlotPress\Engagement;

/**
* Test the Anniversary class.
*/
class Anniversary_Test extends \GP_UnitTestCase {

/**
* Data provider for the anniversary tests.
*
* @return array
*/
public function anniversary_data_provider() {
return array(
'today' => 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' );
Expand Down
9 changes: 9 additions & 0 deletions tests/test-consistency.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php
/**
* Tests for the Consistency class.
*
* @package wporg-gp-engagement
*/

namespace WordPressdotorg\GlotPress\Engagement;

/**
* Test the Consistency class.
*/
class Consistency_Test {
}
10 changes: 10 additions & 0 deletions tests/test-first-translation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php
/**
* Tests for the First_Translation class.
*
* @package wporg-gp-engagement
*/

namespace WordPressdotorg\GlotPress\Engagement;

/**
* Test the First_Translation class.
*/
class First_Translation_Test {
}
9 changes: 9 additions & 0 deletions tests/test-inactive.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php
/**
* Tests for the Inactive class.
*
* @package wporg-gp-engagement
*/

namespace WordPressdotorg\GlotPress\Engagement;

/**
* Test the Inactive class.
*/
class Inactive_Test {
}
9 changes: 9 additions & 0 deletions tests/test-translation-milestone.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php
/**
* Tests for the Translation_Milestone class.
*
* @package wporg-gp-engagement
*/

namespace WordPressdotorg\GlotPress\Engagement;

/**
* Test the Translation_Milestone class.
*/
class Translation_Milestone_Test {
}

0 comments on commit f968c86

Please sign in to comment.