From c3110f1b089f41f3e107baab8c2a559ec7ea14cd Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 26 Oct 2024 20:43:43 -0500 Subject: [PATCH 1/5] Set any pending users to pending --- upgrade.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/upgrade.php b/upgrade.php index 956d468..fe9c38a 100644 --- a/upgrade.php +++ b/upgrade.php @@ -41,4 +41,40 @@ function wpau_upgrade_to_12() { ) ); // phpcs:enable WordPress.DB + + wpau_set_users_pending(); +} + +/** + * Set 100 users to pending per cron run. + * + * @param int $processed Number of users processed. + */ +function wpau_set_users_pending( $processed = 0 ) { + $users = get_users( + array( + 'fields' => 'ID', + 'number' => 100, + 'offset' => $processed, + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + array( + 'key' => 'wp-approve-user', + 'compare' => 'NOT EXISTS', + ), + ), + ) + ); + $users = array_diff( $users, array( get_user_by( 'email', get_bloginfo( 'admin_email' ) )->ID ) ); + + foreach ( $users as $user_id ) { + update_user_meta( $user_id, 'wp-approve-user', 'pending' ); + } + + $processed += count( $users ); + $count = count_users(); + + if ( $processed < $count['total_users'] ) { + wp_schedule_single_event( time() + 5, 'wpau_pending_users_cron', array( $processed ) ); + } } +add_action( 'wpau_pending_users_cron', 'wpau_set_users_pending' ); From e863f2f9bace241809321c96efabb362972769ff Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 26 Oct 2024 20:52:26 -0500 Subject: [PATCH 2/5] include users with empty meta --- upgrade.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/upgrade.php b/upgrade.php index fe9c38a..33aae77 100644 --- a/upgrade.php +++ b/upgrade.php @@ -57,10 +57,16 @@ function wpau_set_users_pending( $processed = 0 ) { 'number' => 100, 'offset' => $processed, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + 'relation' => 'OR', array( 'key' => 'wp-approve-user', 'compare' => 'NOT EXISTS', ), + array( + 'key' => 'wp-approve-user', + 'value' => '', + 'compare' => '=', + ), ), ) ); @@ -71,9 +77,8 @@ function wpau_set_users_pending( $processed = 0 ) { } $processed += count( $users ); - $count = count_users(); - if ( $processed < $count['total_users'] ) { + if ( count( $users ) >= 99 ) { wp_schedule_single_event( time() + 5, 'wpau_pending_users_cron', array( $processed ) ); } } From d36dcaac7b3cdec79cbd6e44c5f63882d36f1706 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 26 Oct 2024 21:21:01 -0500 Subject: [PATCH 3/5] Use database update --- upgrade.php | 44 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/upgrade.php b/upgrade.php index 33aae77..a1ecfbb 100644 --- a/upgrade.php +++ b/upgrade.php @@ -40,46 +40,14 @@ function wpau_upgrade_to_12() { 'meta_value' => true, ) ); - // phpcs:enable WordPress.DB - - wpau_set_users_pending(); -} -/** - * Set 100 users to pending per cron run. - * - * @param int $processed Number of users processed. - */ -function wpau_set_users_pending( $processed = 0 ) { - $users = get_users( + $wpdb->update( + $wpdb->usermeta, + array( 'meta_value' => 'pending' ), array( - 'fields' => 'ID', - 'number' => 100, - 'offset' => $processed, - 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - 'relation' => 'OR', - array( - 'key' => 'wp-approve-user', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'wp-approve-user', - 'value' => '', - 'compare' => '=', - ), - ), + 'meta_key' => 'wp-approve-user', + 'meta_value' => false, ) ); - $users = array_diff( $users, array( get_user_by( 'email', get_bloginfo( 'admin_email' ) )->ID ) ); - - foreach ( $users as $user_id ) { - update_user_meta( $user_id, 'wp-approve-user', 'pending' ); - } - - $processed += count( $users ); - - if ( count( $users ) >= 99 ) { - wp_schedule_single_event( time() + 5, 'wpau_pending_users_cron', array( $processed ) ); - } + // phpcs:enable WordPress.DB } -add_action( 'wpau_pending_users_cron', 'wpau_set_users_pending' ); From 55f63b75e8b883374fea2a7dd589aa0860e7629e Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 2 Nov 2024 15:54:08 -0500 Subject: [PATCH 4/5] add tests --- .gitignore | 2 + tests/test-upgrade-routine.php | 175 +++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 tests/test-upgrade-routine.php diff --git a/.gitignore b/.gitignore index 2ccbe46..b0520ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /node_modules/ +/vendor/ +.phpunit.result.cache diff --git a/tests/test-upgrade-routine.php b/tests/test-upgrade-routine.php new file mode 100644 index 0000000..1c1df96 --- /dev/null +++ b/tests/test-upgrade-routine.php @@ -0,0 +1,175 @@ +test_users[] = $this->create_test_user_with_meta( true ); // Approved user. + $this->test_users[] = $this->create_test_user_with_meta( true ); // Another approved user. + $this->test_users[] = $this->create_test_user_with_meta( false ); // Pending user. + $this->test_users[] = $this->create_test_user_with_meta( false ); // Another pending user. + } + + /** + * Clean up after each test. + */ + public function tearDown() { + // Delete test users and their meta. + foreach ( $this->test_users as $user_id ) { + delete_user_meta( $user_id, 'wp-approve-user' ); + wp_delete_user( $user_id ); + } + + $this->test_users = array(); + + delete_site_option( 'wpau_db_version' ); + unset( $GLOBALS['wpau_db_version'] ); + + parent::tearDown(); + } + + /** + * Helper function to create a test user with approval meta + * + * @param bool $approved Whether the user should be approved. + * @return int User ID + */ + private function create_test_user_with_meta( $approved ) { + $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + + update_user_meta( $user_id, 'wp-approve-user', $approved ); + + return $user_id; + } + + /** + * Test upgrade is skipped when versions match. + * + * @covers ::wpau_upgrade_all + */ + public function test_upgrade_all_no_upgrade_needed() { + // Store original meta values. + $original_values = array(); + foreach ( $this->test_users as $user_id ) { + $original_values[ $user_id ] = get_user_meta( $user_id, 'wp-approve-user', true ); + } + + // Set current version equal to required version. + update_site_option( 'wpau_db_version', 12 ); + + // Run upgrade. + wpau_upgrade_all(); + + // Verify no changes were made by comparing with original values. + foreach ( $this->test_users as $user_id ) { + $current_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertSame( + $original_values[ $user_id ], + $current_value, + "Meta value for user $user_id should remain unchanged" + ); + } + } + + /** + * Test upgrade process when needed. + * + * @covers ::wpau_upgrade_all + */ + public function test_upgrade_all_performs_upgrade() { + $GLOBALS['wpau_db_version'] = 12; + + // Run upgrade. + wpau_upgrade_all(); + + // Verify approved users were updated. + $approved_users = array_slice( $this->test_users, 0, 2 ); + foreach ( $approved_users as $user_id ) { + $meta_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertEquals( 'approved', $meta_value, 'User should be marked as approved' ); + } + + // Verify pending users were updated. + $pending_users = array_slice( $this->test_users, 2, 2 ); + foreach ( $pending_users as $user_id ) { + $meta_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertEquals( 'pending', $meta_value, 'User should be marked as pending' ); + } + + // Verify database version was updated. + $this->assertEquals( 12, get_site_option( 'wpau_db_version' ), 'Database version should be updated' ); + } + + /** + * Test direct upgrade to version 12. + * + * @covers ::wpau_upgrade_to_12 + */ + public function test_upgrade_to_12() { + // Run the upgrade function directly. + wpau_upgrade_to_12(); + + // Check approved users. + $approved_users = array_slice( $this->test_users, 0, 2 ); + foreach ( $approved_users as $user_id ) { + $meta_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertEquals( 'approved', $meta_value, 'Boolean true should be converted to "approved"' ); + } + + // Check pending users. + $pending_users = array_slice( $this->test_users, 2, 2 ); + foreach ( $pending_users as $user_id ) { + $meta_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertEquals( 'pending', $meta_value, 'Boolean false should be converted to "pending"' ); + } + } + + /** + * Test upgrade with empty meta values. + * + * @covers ::wpau_upgrade_to_12 + */ + public function test_upgrade_with_empty_meta() { + // Create user with empty meta value. + $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $this->test_users[] = $user_id; + update_user_meta( $user_id, 'wp-approve-user', '' ); + + // Run upgrade. + wpau_upgrade_to_12(); + + // Empty values should become 'pending'. + $meta_value = get_user_meta( $user_id, 'wp-approve-user', true ); + $this->assertEquals( 'pending', $meta_value, 'Empty meta value should be converted to pending' ); + } +} From 79647f5026922e4afc530908fe33aa753189f5c3 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Sat, 2 Nov 2024 15:56:57 -0500 Subject: [PATCH 5/5] fix tests --- tests/test-upgrade-routine.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test-upgrade-routine.php b/tests/test-upgrade-routine.php index 1c1df96..dce5435 100644 --- a/tests/test-upgrade-routine.php +++ b/tests/test-upgrade-routine.php @@ -30,8 +30,8 @@ class Test_Upgrade_Routine extends WP_UnitTestCase { /** * Set up test environment before each test. */ - public function setUp() { - parent::setUp(); + public function set_up() { + parent::set_up(); // Create test users with different approval states. $this->test_users[] = $this->create_test_user_with_meta( true ); // Approved user. @@ -43,7 +43,7 @@ public function setUp() { /** * Clean up after each test. */ - public function tearDown() { + public function tear_down() { // Delete test users and their meta. foreach ( $this->test_users as $user_id ) { delete_user_meta( $user_id, 'wp-approve-user' ); @@ -55,7 +55,7 @@ public function tearDown() { delete_site_option( 'wpau_db_version' ); unset( $GLOBALS['wpau_db_version'] ); - parent::tearDown(); + parent::tear_down(); } /**