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 <joshuatf@gmail.com>

* Update test to use ms

* Use get_users() to get the latest meta value

Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
This commit is contained in:
Moon 2021-11-24 17:44:30 -08:00 committed by GitHub
parent c634ed5eb7
commit c3a050254d
3 changed files with 138 additions and 0 deletions

View File

@ -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' );
}

View File

@ -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',
),
);
/**

View File

@ -0,0 +1,102 @@
<?php
use Automattic\WooCommerce\Admin\Notes\Note;
/**
* DB Update test for wc_admin_update_300_update_is_read_from_last_read()
*
* @package WooCommerce\Admin\Tests\DBUpdates
*/
class WC_Tests_Update_Is_Read_From_Last_Read extends WC_Unit_Test_Case {
/**
* @var object current user
*/
private $user;
/**
* setUp
*/
public function setUp() {
parent::setUp();
$this->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 );
}
}