Update inbox notes to display localized strings when locale changed (#34038)

* Add a filter woocommerce_get_note to modify note before it is returned

* Make ChoosingTheme.get_note public so we can call it outside

* Implement the get_note filter to return localized strings

* Create get_actions method to reuse it in RemoteInboxNotificationsEngine

* Implement get_note filter in RemoteInboxNotificationsEngine to return localized note

* Add changelog

* Make GivingFeedbackNotes.get_note public so we can call it outside

* Check if note class has NOTE_NAME constant

* Update WooSubscriptionsNotes to show localized content

* Rename filter hook name

* Remove todo to pass php lint

* Update filter description

* Move locale check into individual filter functions

* Fix get_note_from_db logic

* Fix php lint errors

* Update the filter doc

* Add test for spec-runner.php

* Add tests for RemoteInboxNotificationsEngine

* Remove unneeded check

* Check if $note_from_class is instance of Note class

* Fix unit test
This commit is contained in:
Chi-Hsuan Huang 2022-08-18 09:53:40 +08:00 committed by GitHub
parent 71f722942d
commit 26a3d0d3c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 376 additions and 127 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Update inbox notes to display localized note when the locale is changed

View File

@ -40,6 +40,13 @@ class Notes {
foreach ( (array) $raw_notes as $raw_note ) {
try {
$note = new Note( $raw_note );
/**
* Filter the note from db. This is used to modify the note before it is returned.
*
* @since 6.9.0
* @param Note $note The note object from the database.
*/
$note = apply_filters( 'woocommerce_get_note_from_db', $note );
$note_id = $note->get_id();
$notes[ $note_id ] = $note->get_data();
$notes[ $note_id ]['name'] = $note->get_name( $context );

View File

@ -9,6 +9,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
use \Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use \Automattic\WooCommerce\Admin\Notes\Note;
/**
* Remote Inbox Notifications engine.
@ -47,7 +48,7 @@ class RemoteInboxNotificationsEngine {
array( __CLASS__, 'run_on_woocommerce_admin_updated' ),
'woocommerce-remote-inbox-engine'
);
if ( null === $next_hook ) {
if ( $next_hook === null ) {
WC()->queue()->schedule_single(
time(),
'woocommerce_run_on_woocommerce_admin_updated',
@ -57,6 +58,8 @@ class RemoteInboxNotificationsEngine {
}
}
);
add_filter( 'woocommerce_get_note_from_db', array( __CLASS__, 'get_note_from_db' ), 10, 1 );
}
/**
@ -108,7 +111,7 @@ class RemoteInboxNotificationsEngine {
public static function run() {
$specs = DataSourcePoller::get_instance()->get_specs_from_data_sources();
if ( false === $specs || 0 === count( $specs ) ) {
if ( $specs === false || count( $specs ) === 0 ) {
return;
}
@ -141,7 +144,7 @@ class RemoteInboxNotificationsEngine {
public static function get_stored_state() {
$stored_state = get_option( self::STORED_STATE_OPTION_NAME );
if ( false === $stored_state ) {
if ( $stored_state === false ) {
$stored_state = new \stdClass();
$stored_state = StoredStateSetupForProducts::init_stored_state(
@ -180,4 +183,33 @@ class RemoteInboxNotificationsEngine {
public static function update_stored_state( $stored_state ) {
update_option( self::STORED_STATE_OPTION_NAME, $stored_state, false );
}
/**
* Get the note. This is used to display localized note.
*
* @param Note $note_from_db The note object created from db.
* @return Note The note.
*/
public static function get_note_from_db( $note_from_db ) {
if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) {
return $note_from_db;
}
$specs = DataSourcePoller::get_instance()->get_specs_from_data_sources();
foreach ( $specs as $spec ) {
if ( $spec->slug !== $note_from_db->get_name() ) {
continue;
}
$locale = SpecRunner::get_locale( $spec->locales, true );
if ( $locale === null ) {
// No locale found, so don't update the note.
break;
}
$note_from_db->set_title( $locale->title );
$note_from_db->set_content( $locale->content );
$note_from_db->set_actions( SpecRunner::get_actions( $spec ) );
}
return $note_from_db;
}
}

View File

@ -25,12 +25,12 @@ class SpecRunner {
// Create or update the note.
$existing_note_ids = $data_store->get_notes_with_name( $spec->slug );
if ( 0 === count( $existing_note_ids ) ) {
if ( count( $existing_note_ids ) === 0 ) {
$note = new Note();
$note->set_status( Note::E_WC_ADMIN_NOTE_PENDING );
} else {
$note = Notes::get_note( $existing_note_ids[0] );
if ( false === $note ) {
if ( $note === false ) {
return;
}
}
@ -52,7 +52,7 @@ class SpecRunner {
// Get the matching locale or fall back to en-US.
$locale = self::get_locale( $spec->locales );
if ( null === $locale ) {
if ( $locale === null ) {
return;
}
@ -67,23 +67,8 @@ class SpecRunner {
$note->set_source( $spec->source );
}
// Clear then create actions.
$note->clear_actions();
$actions = isset( $spec->actions ) ? $spec->actions : array();
foreach ( $actions as $action ) {
$action_locale = self::get_action_locale( $action->locales );
$url = self::get_url( $action );
$note->add_action(
$action->name,
( null === $action_locale || ! isset( $action_locale->label ) )
? ''
: $action_locale->label,
$url,
$action->status
);
}
// Recreate actions.
$note->set_actions( self::get_actions( $spec ) );
$note->save();
}
@ -119,7 +104,7 @@ class SpecRunner {
* @returns object The locale that was found, or null if no matching locale was found.
*/
public static function get_locale( $locales ) {
$wp_locale = get_locale();
$wp_locale = get_user_locale();
$matching_wp_locales = array_values(
array_filter(
$locales,
@ -129,7 +114,7 @@ class SpecRunner {
)
);
if ( 0 !== count( $matching_wp_locales ) ) {
if ( count( $matching_wp_locales ) !== 0 ) {
return $matching_wp_locales[0];
}
@ -138,12 +123,12 @@ class SpecRunner {
array_filter(
$locales,
function( $l ) {
return 'en_US' === $l->locale;
return $l->locale === 'en_US';
}
)
);
if ( 0 !== count( $en_us_locales ) ) {
if ( count( $en_us_locales ) !== 0 ) {
return $en_us_locales[0];
}
@ -159,7 +144,7 @@ class SpecRunner {
* @return object The matching locale, or the en_US fallback locale, or null if neither was found.
*/
public static function get_action_locale( $action_locales ) {
$wp_locale = get_locale();
$wp_locale = get_user_locale();
$matching_wp_locales = array_values(
array_filter(
$action_locales,
@ -169,7 +154,7 @@ class SpecRunner {
)
);
if ( 0 !== count( $matching_wp_locales ) ) {
if ( count( $matching_wp_locales ) !== 0 ) {
return $matching_wp_locales[0];
}
@ -178,15 +163,42 @@ class SpecRunner {
array_filter(
$action_locales,
function( $l ) {
return 'en_US' === $l->locale;
return $l->locale === 'en_US';
}
)
);
if ( 0 !== count( $en_us_locales ) ) {
if ( count( $en_us_locales ) !== 0 ) {
return $en_us_locales[0];
}
return null;
}
/**
* Get the actions for a note.
*
* @param object $spec The spec.
*
* @return array The actions.
*/
public static function get_actions( $spec ) {
$note = new Note();
$actions = isset( $spec->actions ) ? $spec->actions : array();
foreach ( $actions as $action ) {
$action_locale = self::get_action_locale( $action->locales );
$url = self::get_url( $action );
$note->add_action(
$action->name,
( $action_locale === null || ! isset( $action_locale->label ) )
? ''
: $action_locale->label,
$url,
$action->status
);
}
return $note->get_actions();
}
}

View File

@ -8,40 +8,49 @@ namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Features\Features;
use \Automattic\WooCommerce\Internal\Admin\Notes\AddingAndManangingProducts;
use \Automattic\WooCommerce\Internal\Admin\Notes\ChoosingTheme;
use \Automattic\WooCommerce\Internal\Admin\Notes\CustomizingProductCatalog;
use \Automattic\WooCommerce\Internal\Admin\Notes\FirstDownlaodableProduct;
use \Automattic\WooCommerce\Internal\Admin\Notes\InsightFirstProductAndPayment;
use \Automattic\WooCommerce\Internal\Admin\Notes\MobileApp;
use \Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord;
use \Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn;
use \Automattic\WooCommerce\Internal\Admin\Notes\OnboardingPayments;
use \Automattic\WooCommerce\Internal\Admin\Notes\PersonalizeStore;
use \Automattic\WooCommerce\Internal\Admin\Notes\EUVATNumber;
use \Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments;
use \Automattic\WooCommerce\Internal\Admin\Notes\MarketingJetpack;
use \Automattic\WooCommerce\Internal\Admin\Notes\WooCommerceSubscriptions;
use \Automattic\WooCommerce\Internal\Admin\Notes\MigrateFromShopify;
use \Automattic\WooCommerce\Internal\Admin\Notes\LaunchChecklist;
use \Automattic\WooCommerce\Internal\Admin\Notes\RealTimeOrderAlerts;
use \Automattic\WooCommerce\Admin\RemoteInboxNotifications\DataSourcePoller;
use \Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine;
use \Automattic\WooCommerce\Internal\Admin\Notes\MerchantEmailNotifications;
use \Automattic\WooCommerce\Internal\Admin\Notes\InsightFirstSale;
use \Automattic\WooCommerce\Internal\Admin\Notes\OnlineClothingStore;
use \Automattic\WooCommerce\Internal\Admin\Notes\FirstProduct;
use \Automattic\WooCommerce\Internal\Admin\Notes\CustomizeStoreWithBlocks;
use \Automattic\WooCommerce\Internal\Admin\Notes\TestCheckout;
use \Automattic\WooCommerce\Internal\Admin\Notes\EditProductsOnTheMove;
use \Automattic\WooCommerce\Internal\Admin\Notes\PerformanceOnMobile;
use \Automattic\WooCommerce\Internal\Admin\Notes\ManageOrdersOnTheGo;
use \Automattic\WooCommerce\Internal\Admin\Notes\AddFirstProduct;
use \Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler;
use \Automattic\WooCommerce\Internal\Admin\Notes\AddingAndManangingProducts;
use \Automattic\WooCommerce\Internal\Admin\Notes\ChoosingTheme;
use \Automattic\WooCommerce\Internal\Admin\Notes\CompleteStoreDetails;
use \Automattic\WooCommerce\Internal\Admin\Notes\UpdateStoreDetails;
use \Automattic\WooCommerce\Internal\Admin\Notes\PaymentsRemindMeLater;
use \Automattic\WooCommerce\Internal\Admin\Notes\CouponPageMoved;
use \Automattic\WooCommerce\Internal\Admin\Notes\CustomizeStoreWithBlocks;
use \Automattic\WooCommerce\Internal\Admin\Notes\CustomizingProductCatalog;
use \Automattic\WooCommerce\Internal\Admin\Notes\EditProductsOnTheMove;
use \Automattic\WooCommerce\Internal\Admin\Notes\EUVATNumber;
use \Automattic\WooCommerce\Internal\Admin\Notes\FirstDownlaodableProduct;
use \Automattic\WooCommerce\Internal\Admin\Notes\FirstProduct;
use \Automattic\WooCommerce\Internal\Admin\Notes\InsightFirstProductAndPayment;
use \Automattic\WooCommerce\Internal\Admin\Notes\InsightFirstSale;
use \Automattic\WooCommerce\Internal\Admin\Notes\InstallJPAndWCSPlugins;
use \Automattic\WooCommerce\Internal\Admin\Notes\LaunchChecklist;
use \Automattic\WooCommerce\Internal\Admin\Notes\MagentoMigration;
use \Automattic\WooCommerce\Internal\Admin\Notes\ManageOrdersOnTheGo;
use \Automattic\WooCommerce\Internal\Admin\Notes\ManageStoreActivityFromHomeScreen;
use \Automattic\WooCommerce\Internal\Admin\Notes\MarketingJetpack;
use \Automattic\WooCommerce\Internal\Admin\Notes\MerchantEmailNotifications;
use \Automattic\WooCommerce\Internal\Admin\Notes\MigrateFromShopify;
use \Automattic\WooCommerce\Internal\Admin\Notes\MobileApp;
use \Automattic\WooCommerce\Internal\Admin\Notes\NewSalesRecord;
use \Automattic\WooCommerce\Internal\Admin\Notes\OnboardingPayments;
use \Automattic\WooCommerce\Internal\Admin\Notes\OnlineClothingStore;
use \Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones;
use \Automattic\WooCommerce\Internal\Admin\Notes\PaymentsRemindMeLater;
use \Automattic\WooCommerce\Internal\Admin\Notes\PerformanceOnMobile;
use \Automattic\WooCommerce\Internal\Admin\Notes\PersonalizeStore;
use \Automattic\WooCommerce\Internal\Admin\Notes\RealTimeOrderAlerts;
use \Automattic\WooCommerce\Internal\Admin\Notes\SellingOnlineCourses;
use \Automattic\WooCommerce\Internal\Admin\Notes\TestCheckout;
use \Automattic\WooCommerce\Internal\Admin\Notes\TrackingOptIn;
use \Automattic\WooCommerce\Internal\Admin\Notes\UnsecuredReportFiles;
use \Automattic\WooCommerce\Internal\Admin\Notes\UpdateStoreDetails;
use \Automattic\WooCommerce\Internal\Admin\Notes\WelcomeToWooCommerceForStoreUsers;
use \Automattic\WooCommerce\Internal\Admin\Notes\WooCommercePayments;
use \Automattic\WooCommerce\Internal\Admin\Notes\WooCommerceSubscriptions;
use \Automattic\WooCommerce\Internal\Admin\Notes\WooSubscriptionsNotes;
use \Automattic\WooCommerce\Internal\Admin\Schedulers\MailchimpScheduler;
use \Automattic\WooCommerce\Admin\Notes\Note;
/**
* Events Class.
@ -61,13 +70,68 @@ class Events {
*/
protected function __construct() {}
/**
* Array of note class to be added or updated.
*
* @var array
*/
private static $note_classes_to_added_or_updated = array(
AddFirstProduct::class,
AddingAndManangingProducts::class,
ChoosingTheme::class,
CompleteStoreDetails::class,
CustomizeStoreWithBlocks::class,
CustomizingProductCatalog::class,
EditProductsOnTheMove::class,
EUVATNumber::class,
FirstDownlaodableProduct::class,
FirstProduct::class,
InsightFirstProductAndPayment::class,
InsightFirstSale::class,
LaunchChecklist::class,
MagentoMigration::class,
ManageOrdersOnTheGo::class,
MarketingJetpack::class,
MigrateFromShopify::class,
MobileApp::class,
NewSalesRecord::class,
OnboardingPayments::class,
OnlineClothingStore::class,
PaymentsRemindMeLater::class,
PerformanceOnMobile::class,
PersonalizeStore::class,
RealTimeOrderAlerts::class,
TestCheckout::class,
TrackingOptIn::class,
UpdateStoreDetails::class,
WooCommercePayments::class,
WooCommerceSubscriptions::class,
);
/**
* The other note classes that are added in other places.
*
* @var array
*/
private static $other_note_classes = array(
CouponPageMoved::class,
InstallJPAndWCSPlugins::class,
ManageStoreActivityFromHomeScreen::class,
OrderMilestones::class,
SellingOnlineCourses::class,
UnsecuredReportFiles::class,
WelcomeToWooCommerceForStoreUsers::class,
WooSubscriptionsNotes::class,
);
/**
* Get class instance.
*
* @return object Instance.
*/
final public static function instance() {
if ( null === static::$instance ) {
if ( static::$instance === null ) {
static::$instance = new static();
}
return static::$instance;
@ -78,6 +142,7 @@ class Events {
*/
public function init() {
add_action( 'wc_admin_daily', array( $this, 'do_wc_admin_daily' ) );
add_filter( 'woocommerce_get_note_from_db', array( $this, 'get_note_from_db' ), 10, 1 );
}
/**
@ -104,40 +169,43 @@ class Events {
}
}
/**
* Get note.
*
* @param Note $note_from_db The note object from the database.
*/
public function get_note_from_db( $note_from_db ) {
if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) {
return $note_from_db;
}
$note_classes = array_merge( self::$note_classes_to_added_or_updated, self::$other_note_classes );
foreach ( $note_classes as $note_class ) {
if ( defined( "$note_class::NOTE_NAME" ) && $note_class::NOTE_NAME === $note_from_db->get_name() ) {
$note_from_class = method_exists( $note_class, 'get_note' ) ? $note_class::get_note() : null;
if ( $note_from_class instanceof Note ) {
$note = clone $note_from_db;
$note->set_title( $note_from_class->get_title() );
$note->set_content( $note_from_class->get_content() );
$note->set_actions( $note_from_class->get_actions() );
return $note;
}
break;
}
}
return $note_from_db;
}
/**
* Adds notes that should be added.
*/
protected function possibly_add_notes() {
NewSalesRecord::possibly_add_note();
MobileApp::possibly_add_note();
TrackingOptIn::possibly_add_note();
OnboardingPayments::possibly_add_note();
PersonalizeStore::possibly_add_note();
WooCommercePayments::possibly_add_note();
EUVATNumber::possibly_add_note();
MarketingJetpack::possibly_add_note();
WooCommerceSubscriptions::possibly_add_note();
MigrateFromShopify::possibly_add_note();
InsightFirstSale::possibly_add_note();
LaunchChecklist::possibly_add_note();
OnlineClothingStore::possibly_add_note();
FirstProduct::possibly_add_note();
RealTimeOrderAlerts::possibly_add_note();
CustomizeStoreWithBlocks::possibly_add_note();
TestCheckout::possibly_add_note();
EditProductsOnTheMove::possibly_add_note();
PerformanceOnMobile::possibly_add_note();
ManageOrdersOnTheGo::possibly_add_note();
ChoosingTheme::possibly_add_note();
InsightFirstProductAndPayment::possibly_add_note();
AddFirstProduct::possibly_add_note();
AddingAndManangingProducts::possibly_add_note();
CustomizingProductCatalog::possibly_add_note();
FirstDownlaodableProduct::possibly_add_note();
CompleteStoreDetails::possibly_add_note();
UpdateStoreDetails::possibly_add_note();
PaymentsRemindMeLater::possibly_add_note();
MagentoMigration::possibly_add_note();
foreach ( self::$note_classes_to_added_or_updated as $note_class ) {
if ( method_exists( $note_class, 'possibly_add_note' ) ) {
$note_class::possibly_add_note();
}
}
}
/**
@ -151,36 +219,11 @@ class Events {
* Updates notes that should be updated.
*/
protected function possibly_update_notes() {
NewSalesRecord::possibly_update_note();
MobileApp::possibly_update_note();
TrackingOptIn::possibly_update_note();
OnboardingPayments::possibly_update_note();
PersonalizeStore::possibly_update_note();
WooCommercePayments::possibly_update_note();
EUVATNumber::possibly_update_note();
MarketingJetpack::possibly_update_note();
WooCommerceSubscriptions::possibly_update_note();
MigrateFromShopify::possibly_update_note();
InsightFirstSale::possibly_update_note();
LaunchChecklist::possibly_update_note();
OnlineClothingStore::possibly_update_note();
FirstProduct::possibly_update_note();
RealTimeOrderAlerts::possibly_update_note();
CustomizeStoreWithBlocks::possibly_update_note();
TestCheckout::possibly_update_note();
EditProductsOnTheMove::possibly_update_note();
PerformanceOnMobile::possibly_update_note();
ManageOrdersOnTheGo::possibly_update_note();
ChoosingTheme::possibly_update_note();
InsightFirstProductAndPayment::possibly_update_note();
AddFirstProduct::possibly_update_note();
AddingAndManangingProducts::possibly_update_note();
CustomizingProductCatalog::possibly_update_note();
FirstDownlaodableProduct::possibly_update_note();
CompleteStoreDetails::possibly_update_note();
UpdateStoreDetails::possibly_update_note();
PaymentsRemindMeLater::possibly_update_note();
MagentoMigration::possibly_update_note();
foreach ( self::$note_classes_to_added_or_updated as $note_class ) {
if ( method_exists( $note_class, 'possibly_update_note' ) ) {
$note_class::possibly_update_note();
}
}
}
/**
@ -195,7 +238,7 @@ class Events {
}
// Check if the site has opted out of marketplace suggestions.
if ( 'yes' !== get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) {
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) !== 'yes' ) {
return false;
}
@ -210,7 +253,7 @@ class Events {
*/
protected function is_merchant_email_notifications_enabled() {
// Check if the feature flag is disabled.
if ( 'yes' !== get_option( 'woocommerce_merchant_email_notifications', 'no' ) ) {
if ( get_option( 'woocommerce_merchant_email_notifications', 'no' ) !== 'yes' ) {
return false;
}

View File

@ -31,7 +31,7 @@ class ChoosingTheme {
*
* @return Note
*/
protected static function get_note() {
public static function get_note() {
// We need to show choosing a theme notification after 1 day of install.
if ( ! self::is_wc_admin_active_in_date_range( 'week-1', DAY_IN_SECONDS ) ) {
return;

View File

@ -32,7 +32,7 @@ class GivingFeedbackNotes {
*
* @return Note
*/
protected static function get_note() {
public static function get_note() {
if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) ) {
return;
}

View File

@ -17,6 +17,7 @@ use \Automattic\WooCommerce\Admin\Notes\Notes;
*/
class WooSubscriptionsNotes {
const LAST_REFRESH_OPTION_KEY = 'woocommerce_admin-wc-helper-last-refresh';
const NOTE_NAME = 'wc-admin-wc-helper-connection';
const CONNECTION_NOTE_NAME = 'wc-admin-wc-helper-connection';
const SUBSCRIPTION_NOTE_NAME = 'wc-admin-wc-helper-subscription';
const NOTIFY_WHEN_DAYS_LEFT = 60;
@ -181,6 +182,14 @@ class WooSubscriptionsNotes {
* Adds a note prompting to connect to WooCommerce.com.
*/
public function add_no_connection_note() {
$note = self::get_note();
$note->save();
}
/**
* Get the WooCommerce.com connection note
*/
public static function get_note() {
$note = new Note();
$note->set_title( __( 'Connect to WooCommerce.com', 'woocommerce' ) );
$note->set_content( __( 'Connect to get important product notifications and updates.', 'woocommerce' ) );
@ -194,7 +203,7 @@ class WooSubscriptionsNotes {
'?page=wc-addons&section=helper',
Note::E_WC_ADMIN_NOTE_UNACTIONED
);
$note->save();
return $note;
}
/**

View File

@ -0,0 +1,113 @@
<?php
/**
* RemoteInboxNotificationsEngine tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\DataSourcePoller;
use \Automattic\WooCommerce\Admin\Notes\Note;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_SpecRunner
*/
class WC_Admin_Tests_RemoteInboxNotifications_RemoteInboxNotificationsEngine extends WC_Unit_Test_Case {
/**
* Set up.
*/
public function setUp(): void {
parent::setUp();
add_filter(
'transient_woocommerce_admin_' . DataSourcePoller::ID . '_specs',
function( $value ) {
if ( $value ) {
return $value;
}
$specs = array(
'zh_TW' => json_decode(
'[{
"slug": "test",
"status": "unactioned",
"type": "info",
"locales": [{
"locale": "zh_TW",
"title": "名稱",
"content": "內容"
}],
"rules": [],
"actions": [ {
"name": "test-action",
"locales": [
{
"locale": "zh_TW",
"label": "標籤"
}],
"url": "test",
"url_is_admin_query": false,
"status": "unactioned"
}]
}]'
),
);
return $specs;
}
);
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
delete_transient( 'woocommerce_admin_' . DataSourcePoller::ID . '_specs' );
remove_all_filters( 'transient_woocommerce_admin_' . DataSourcePoller::ID . '_specs' );
}
/**
* Tests get_note_from_db function with a invalid note.
*
*/
public function test_get_note_from_db_with_invalid_note() {
$invalid_note = array(
'note_name' => 'invalid',
);
$this->assertEquals( RemoteInboxNotificationsEngine::get_note_from_db( $invalid_note ), $invalid_note );
}
/**
* Tests get_note_from_db function when locale is the same
*
*/
public function test_get_note_from_db_when_the_locale_is_the_same() {
$note = new Note();
$note->set_locale( get_user_locale() );
$this->assertEquals( RemoteInboxNotificationsEngine::get_note_from_db( $note ), $note );
}
/**
* Tests get_note_from_db function when locales are different
*
*/
public function test_get_note_from_db_when_locales_are_different() {
$note_from_db = new Note();
$note_from_db->set_locale( 'en_US' );
$note_from_db->set_name( 'test' );
add_filter(
'locale',
function( $locale ) {
return 'zh_TW';
}
);
$note = RemoteInboxNotificationsEngine::get_note_from_db( $note_from_db );
$this->assertEquals( $note->get_title(), '名稱' );
$this->assertEquals( $note->get_content(), '內容' );
$this->assertEquals( $note->get_actions()[0]->label, '標籤' );
}
}

View File

@ -87,4 +87,33 @@ class WC_Admin_Tests_RemoteInboxNotifications_SpecRunner extends WC_Unit_Test_Ca
$action = $note->get_action( 'test-action' );
$this->assertEquals( 'http://test.com', $action->query );
}
/**
* Tests get actions function.
*
* @group fast
*/
public function test_get_actions() {
$spec = $this->get_spec( 'http://test.com', false );
$actions = SpecRunner::get_actions( $spec );
$this->assertCount( 1, $actions );
$this->assertEquals( $actions[0]->name, 'test-action' );
}
/**
* Tests get actions function with no actions in specs.
*
* @group fast
*/
public function test_get_actions_with_empty_specs() {
$actions = SpecRunner::get_actions( array() );
$this->assertCount( 0, $actions );
$actions = SpecRunner::get_actions(
array(
'actions' => array(),
)
);
$this->assertCount( 0, $actions );
}
}