Skip to content

Commit

Permalink
Unit Tests: fix tests according to changes in Core (#16562)
Browse files Browse the repository at this point in the history
* Recipes: handle new markup now that lazy loading is available

For more info: https://core.trac.wordpress.org/changeset/48572

* Sync: update option names according to latest core changes

See:
- https://make.wordpress.org/core/2020/07/23/codebase-language-improvements-in-5-5/
- https://core.trac.wordpress.org/changeset/48575

We still continue to use the old option names for folks on WP 5.4.

* Make the function more general so it can be used w/ other versions

* Add deprecated options to tests if testing on older version of WP.

* Comment for future devs.

Co-authored-by: Matthew Denton <matt@mdbitz.com>
  • Loading branch information
jeherve and mdbitz authored Jul 24, 2020
1 parent 9e26f38 commit 623e234
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Defaults {
'social_notifications_like',
'social_notifications_reblog',
'social_notifications_subscribe',
'comment_whitelist',
'comment_previously_approved',
'comment_max_links',
'moderation_keys',
'jetpack_wga',
Expand Down Expand Up @@ -130,7 +130,7 @@ class Defaults {
'advanced_seo_title_formats', // Jetpack_SEO_Titles::TITLE_FORMATS_OPTION.
'jetpack_api_cache_enabled',
'start_of_week',
'blacklist_keys',
'disallowed_keys',
'posts_per_page',
'posts_per_rss',
'show_on_front',
Expand Down
29 changes: 29 additions & 0 deletions packages/sync/src/modules/class-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function init_listeners( $callable ) {
add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );

// Handle deprecated options.
add_filter( 'jetpack_options_whitelist', array( $this, 'add_deprecated_options' ) );

$whitelist_option_handler = array( $this, 'whitelist_options' );
add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
Expand Down Expand Up @@ -111,6 +114,32 @@ public function set_late_default() {
}
}

/**
* Add old deprecated options to the list of options to keep in sync.
*
* @since 8.8.0
*
* @access public
*
* @param array $options The default list of site options.
*/
public function add_deprecated_options( $options ) {
global $wp_version;

$deprecated_options = array(
'blacklist_keys' => '5.5-alpha', // Replaced by disallowed_keys.
'comment_whitelist' => '5.5-alpha', // Replaced by comment_previously_approved.
);

foreach ( $deprecated_options as $option => $version ) {
if ( version_compare( $wp_version, $version, '<=' ) ) {
$options[] = $option;
}
}

return $options;
}

/**
* Enqueue the options actions for full sync.
*
Expand Down
34 changes: 32 additions & 2 deletions tests/php/modules/shortcodes/test-class.recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,22 @@ public function test_shortcodes_recipe_image_valid_attachment() {
$content = '[recipe image="' . $attachment_id . '"]';

$shortcode_content = do_shortcode( $content );
$this->assertContains( '<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo" alt="" itemprop="image" />', $shortcode_content );

// We expect a different image markup in WP 5.5 when Lazy Load is enabled.
if (
function_exists( 'wp_lazy_loading_enabled' )
&& wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' )
) {
$this->assertContains(
'<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo" alt="" loading="lazy" itemprop="image" />',
$shortcode_content
);
} else {
$this->assertContains(
'<img src="http://example.org/wp-content/uploads/example.jpg" class="jetpack-recipe-image u-photo photo" alt="" itemprop="image" />',
$shortcode_content
);
}
}

/**
Expand All @@ -190,7 +205,22 @@ public function test_shortcodes_recipe_image_src() {
$content = '[recipe image="https://example.com"]';

$shortcode_content = do_shortcode( $content );
$this->assertContains( '<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />', $shortcode_content );

// We expect a different image markup in WP 5.5 when Lazy Load is enabled.
if (
function_exists( 'wp_lazy_loading_enabled' )
&& wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' )
) {
$this->assertContains(
'<img class="jetpack-recipe-image u-photo photo" itemprop="image" loading="lazy" src="https://example.com" />',
$shortcode_content
);
} else {
$this->assertContains(
'<img class="jetpack-recipe-image u-photo photo" itemprop="image" src="https://example.com" />',
$shortcode_content
);
}
}

/**
Expand Down
12 changes: 10 additions & 2 deletions tests/php/sync/test_class.jetpack-sync-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function test_sync_options_that_use_filter() {
}

public function test_sync_default_options() {
global $wp_version;

$this->setSyncClientDefaults();
// check that these values exists in the whitelist options
$options = array(
Expand Down Expand Up @@ -118,7 +120,7 @@ public function test_sync_default_options() {
'moderation_notify' => 'pineapple',
'social_notifications_reblog' => 'pineapple',
'social_notifications_subscribe' => 'pineapple',
'comment_whitelist' => 'pineapple',
'comment_previously_approved' => 'pineapple',
'comment_max_links' => 99,
'moderation_keys' => 'pineapple',
'jetpack_wga' => 'pineapple',
Expand Down Expand Up @@ -174,7 +176,7 @@ public function test_sync_default_options() {
'jetpack_api_cache_enabled' => '1',
'sidebars_widgets' => array( 'array_version' => 3 ),
'start_of_week' => '0',
'blacklist_keys' => '',
'disallowed_keys' => '',
'posts_per_page' => '1',
'posts_per_rss' => '1',
'show_on_front' => '0',
Expand Down Expand Up @@ -212,6 +214,12 @@ public function test_sync_default_options() {
'jetpack_sync_non_blocking' => false,
);

// Options Deprecated in WP 5.5.
if ( version_compare( $wp_version, '5.5-alpha', '<=' ) ) {
$options['blacklist_keys'] = '';
$options['comment_whitelist'] = 'pineapple';
}

$theme_mod_key = 'theme_mods_' . get_option( 'stylesheet' );
$options[ $theme_mod_key ] = 'pineapple';

Expand Down

0 comments on commit 623e234

Please sign in to comment.