From 3a939bb9ad0f63198437dd295c06e18f21f6afda Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 18 Aug 2022 17:39:45 -0700 Subject: [PATCH] Contributors: Add user data for users that don't have expected meta. Some users don't have any of the expected meta fields. The previous commit caused them to be skipped, resulting in distorted stats. --- plugins/wporg-5ftf/includes/contributor.php | 34 +++++++++++++-------- plugins/wporg-5ftf/tests/helpers.php | 14 ++++++++- plugins/wporg-5ftf/tests/test-stats.php | 13 +++++--- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 88eebab..9a8ccc9 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -711,17 +711,23 @@ function add_user_data_to_xprofile( array $xprofiles ) : array { // phpcs:disable -- `$id_placeholders` is safely created above. $established_users = $wpdb->get_results( $wpdb->prepare( " SELECT - u.ID, u.user_email, u.user_registered, u.user_nicename, - GROUP_CONCAT( um.meta_key ) AS meta_keys, - GROUP_CONCAT( um.meta_value ) AS meta_values + u.ID, u.user_email, u.user_registered, u.user_nicename, + um.meta_keys, um.meta_values FROM `$wpdb->users` u - JOIN `$wpdb->usermeta` um ON u.ID = um.user_id - WHERE - um.user_id IN ( $id_placeholders ) AND - um.meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' ) - GROUP BY um.user_id + LEFT JOIN ( + SELECT + user_id, + GROUP_CONCAT( meta_key ) AS meta_keys, + GROUP_CONCAT( meta_value ) AS meta_values + FROM `$wpdb->usermeta` + WHERE + user_id IN ( $id_placeholders ) AND + meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' ) + GROUP BY user_id + ) um ON u.ID = um.user_id + WHERE u.ID IN ( $id_placeholders ) ORDER BY u.ID", - $user_ids + array_merge( $user_ids, $user_ids ) ) ); // phpcs:enable @@ -734,11 +740,13 @@ function add_user_data_to_xprofile( array $xprofiles ) : array { 'user_nicename' => $user->user_nicename, ); - $keys = explode( ',', $user->meta_keys ); - $values = explode( ',', $user->meta_values ); + if ( ! empty( $user->meta_keys ) ) { + $keys = explode( ',', $user->meta_keys ); + $values = explode( ',', $user->meta_values ); - foreach ( $keys as $index => $key ) { - $full_user[ $key ] = maybe_unserialize( $values[ $index ] ); + foreach ( $keys as $index => $key ) { + $full_user[ $key ] = maybe_unserialize( $values[ $index ] ); + } } $full_user['last_logged_in'] = intval( strtotime( $full_user['last_logged_in'] ?? '' ) ); // Convert `false` to `0`. diff --git a/plugins/wporg-5ftf/tests/helpers.php b/plugins/wporg-5ftf/tests/helpers.php index 82a7a0f..175ec54 100644 --- a/plugins/wporg-5ftf/tests/helpers.php +++ b/plugins/wporg-5ftf/tests/helpers.php @@ -45,6 +45,14 @@ function database_setup_before_class( WP_UnitTest_Factory $factory ) : array { ) ) ); + // Some users don't have any of the expected fields, so make sure they're included in tests. + $fixtures['users']['kimi'] = $factory->user->create_and_get( array( + 'user_login' => 'kimi', + 'user_email' => 'kimi@example.org', + 'meta_input' => array() + ) ); + delete_user_meta( $fixtures['users']['kimi']->ID, 'first_name' ); + $fixtures['users']['andrea'] = $factory->user->create_and_get( array( 'user_login' => 'andrea', 'user_email' => 'andrea@example.org', @@ -107,6 +115,8 @@ function database_set_up( array $user_ids ) : void { (NULL, 30, %d, 'a:1:{i:0;s:9:\"Core Team\";}', '2019-12-03 11:00:00' ), (NULL, 29, %d, '35', '2019-12-02 10:00:00' ), (NULL, 30, %d, 'a:1:{i:0;s:18:\"Documentation Team\";}', '2019-12-03 11:00:00' ), + (NULL, 29, %d, '5', '2019-12-02 10:00:00' ), + (NULL, 30, %d, 'a:2:{i:0;s:9:\"Meta Team\";i:1;s:13:\"Training Team\";}', '2019-12-03 11:00:00' ), (NULL, 29, %d, '7', '2019-12-02 10:00:00' ), (NULL, 30, %d, 'a:1:{i:0;s:14:\"Polyglots Team\";}', '2019-12-03 11:00:00' ), (NULL, 29, %d, '4', '2019-12-02 10:00:00' ), @@ -118,7 +128,9 @@ function database_set_up( array $user_ids ) : void { $user_ids[2], $user_ids[2], $user_ids[3], - $user_ids[3] + $user_ids[3], + $user_ids[4], + $user_ids[4] ) ); } diff --git a/plugins/wporg-5ftf/tests/test-stats.php b/plugins/wporg-5ftf/tests/test-stats.php index 57b660f..fc4286c 100644 --- a/plugins/wporg-5ftf/tests/test-stats.php +++ b/plugins/wporg-5ftf/tests/test-stats.php @@ -65,7 +65,7 @@ class Test_Stats extends WP_UnitTestCase { $expected = array( 'company_sponsored_hours' => 75, - 'self_sponsored_hours' => 11, + 'self_sponsored_hours' => 16, 'team_company_sponsored_contributors' => array( 'Core Team' => 1, @@ -73,17 +73,20 @@ class Test_Stats extends WP_UnitTestCase { ), 'team_self_sponsored_contributors' => array( - 'Meta Team' => 1, + 'Meta Team' => 2, 'Polyglots Team' => 1, + 'Training Team' => 1, ), 'companies' => 2, 'company_sponsored_contributors' => 2, - 'self_sponsored_contributors' => 2, - 'self_sponsored_contributor_activity' => 50.0, + 'self_sponsored_contributors' => 3, + 'self_sponsored_contributor_activity' => 33.33, 'company_sponsored_contributor_activity' => 50.0, ); - $this->assertSame( $expected, get_snapshot_data() ); + $actual = get_snapshot_data(); + + $this->assertSame( $expected, $actual ); } }