From c3a050254de0c56b42c77bd26cc27ebb5ee80da4 Mon Sep 17 00:00:00 2001 From: Moon Date: Wed, 24 Nov 2021 17:44:30 -0800 Subject: [PATCH] Add a new update callback for the 3.0 version to update is_read column (https://github.com/woocommerce/woocommerce-admin/pull/7945) * Added a new update callback for 3.0 version to migrate is_read col from last_read user pref value * Remove unnecessary comments * Remove add column logic -- not needed as tables are updates during the bootstrap * Remove column existence check * Use the latest woocommerce_admin_activity_panel_inbox_last_read value * Clean up woocommerce_admin_activity_panel_inbox_last_read after updating is_read * Remove unnecessary var * Update includes/wc-admin-update-functions.php Co-authored-by: Joshua T Flowers * Update test to use ms * Use get_users() to get the latest meta value Co-authored-by: Joshua T Flowers --- .../includes/wc-admin-update-functions.php | 32 ++++++ plugins/woocommerce-admin/src/Install.php | 4 + ...wc-tests-update-is-read-from-last-read.php | 102 ++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 plugins/woocommerce-admin/tests/db-updates/class-wc-tests-update-is-read-from-last-read.php diff --git a/plugins/woocommerce-admin/includes/wc-admin-update-functions.php b/plugins/woocommerce-admin/includes/wc-admin-update-functions.php index 2946997fb6a..e1ca6d9264e 100644 --- a/plugins/woocommerce-admin/includes/wc-admin-update-functions.php +++ b/plugins/woocommerce-admin/includes/wc-admin-update-functions.php @@ -316,3 +316,35 @@ function wc_admin_update_290_update_apperance_task_option() { function wc_admin_update_290_db_version() { Installer::update_db_version( '2.9.0' ); } + +/** + * Use woocommerce_admin_activity_panel_inbox_last_read from the user meta to set wc_admin_notes.is_read col. + */ +function wc_admin_update_300_update_is_read_from_last_read() { + global $wpdb; + $meta_key = 'woocommerce_admin_activity_panel_inbox_last_read'; + // phpcs:ignore + $users = get_users( "meta_key={$meta_key}&orderby={$meta_key}&fields=all_with_meta&number=1" ); + + if ( count( $users ) ) { + $last_read = current( $users )->{$meta_key}; + $date_in_utc = gmdate( 'Y-m-d H:i:s', intval( $last_read ) / 1000 ); + $wpdb->query( + $wpdb->prepare( + " + update {$wpdb->prefix}wc_admin_notes set is_read = 1 + where + date_created <= %s", + $date_in_utc + ) + ); + $wpdb->query( $wpdb->prepare( "delete from {$wpdb->usermeta} where meta_key=%s", $meta_key ) ); + } +} + +/** + * Update DB Version. + */ +function wc_admin_update_300_db_version() { + Installer::update_db_version( '3.0.0' ); +} diff --git a/plugins/woocommerce-admin/src/Install.php b/plugins/woocommerce-admin/src/Install.php index 9691201523a..05a6c811497 100644 --- a/plugins/woocommerce-admin/src/Install.php +++ b/plugins/woocommerce-admin/src/Install.php @@ -73,6 +73,10 @@ class Install { 'wc_admin_update_290_update_apperance_task_option', 'wc_admin_update_290_db_version', ), + '3.0.0' => array( + 'wc_admin_update_300_update_is_read_from_last_read', + 'wc_admin_update_300_db_version', + ), ); /** diff --git a/plugins/woocommerce-admin/tests/db-updates/class-wc-tests-update-is-read-from-last-read.php b/plugins/woocommerce-admin/tests/db-updates/class-wc-tests-update-is-read-from-last-read.php new file mode 100644 index 00000000000..6d7f0b1b05f --- /dev/null +++ b/plugins/woocommerce-admin/tests/db-updates/class-wc-tests-update-is-read-from-last-read.php @@ -0,0 +1,102 @@ +user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + /** + * Given woocommerce_admin_activity_panel_inbox_last_read does not exist + * When the update runs + * Then it should not update is_read col + */ + public function test_update_does_not_run_when_usermeta_does_not_exist() { + global $wpdb; + + $wpdb->query( + " + delete from {$wpdb->prefix}usermeta where meta_key = 'woocommerce_admin_activity_panel_inbox_last_read' + " + ); + + wc_admin_update_300_update_is_read_from_last_read(); + + $notes_with_is_read = $wpdb->get_var( + "select count(*) from {$wpdb->prefix}wc_admin_notes where is_read = 1 + " + ); + + $this->assertTrue( '0' === $notes_with_is_read ); + } + + /** + * Give woocommerce_admin_activity_panel_inbox_last_read + * When the update runs + * Then it should update notes where date_created value is less than woocommerce_admin_activity_panel_inbox_last_read + */ + public function test_it_updates_is_read_when_date_created_value_is_less_than_last_read() { + global $wpdb; + $time = time(); + + $meta_key = 'woocommerce_admin_activity_panel_inbox_last_read'; + + wp_set_current_user( $this->user ); + $wpdb->query( "delete from {$wpdb->prefix}wc_admin_notes" ); + + // Note with date_created less than woocommerce_admin_activity_panel_inbox_last_read. + $note = new Note(); + $note->set_title( 'test1' ); + $note->set_content( 'test1' ); + $note->set_name( 'test1' ); + $note->save(); + $date_created_1 = gmdate( 'Y-m-d H:i:s', $time - 3600 ); + + // Note with date_created greater than woocommerce_admin_activity_panel_inbox_last_read. + $note = new Note(); + $note->set_title( 'test2' ); + $note->set_content( 'test2' ); + $note->set_name( 'test2' ); + $note->save(); + $date_created_2 = gmdate( 'Y-m-d H:i:s', $time + 3600 ); + + // phpcs:ignore + $wpdb->query( "update {$wpdb->prefix}wc_admin_notes set date_created = '{$date_created_1}' where name='test1'" ); + // phpcs:ignore + $wpdb->query( "update {$wpdb->prefix}wc_admin_notes set date_created = '{$date_created_2}' where name='test2'" ); + + update_user_meta( $this->user, $meta_key, $time * 1000 ); + + wc_admin_update_300_update_is_read_from_last_read(); + + $notes_with_is_read = $wpdb->get_var( + "select count(*) from {$wpdb->prefix}wc_admin_notes where is_read = 1 + " + ); + + $this->assertTrue( '1' === $notes_with_is_read ); + + // phpcs:ignore + $last_read_count = $wpdb->get_var("select count(*) from {$wpdb->usermeta} where meta_key='{$meta_key}'"); + $this->assertTrue( '0' === $last_read_count ); + } +}