From 1c759e5c7fab98f6ec8a6faa8567382d489e4d78 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 1 Jul 2018 15:46:12 -0700 Subject: [PATCH] Add tests for read_theme_support, is_support_added_via_option, get_theme_support_args Store version with options to keep track of format --- includes/class-amp-theme-support.php | 10 ++-- .../options/class-amp-options-manager.php | 13 ++-- includes/options/class-amp-options-menu.php | 1 + tests/test-class-amp-options-manager.php | 2 +- tests/test-class-amp-theme-support.php | 60 +++++++++++++++++-- 5 files changed, 69 insertions(+), 17 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 09c62518338..35cd52d4a95 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -157,8 +157,10 @@ public static function is_support_added_via_option() { * Read theme support and apply options from admin for whether theme support is enabled and via what template is enabled. * * @see AMP_Post_Type_Support::add_post_type_support() For where post type support is added, since it is irrespective of theme support. + * + * @param bool $check_args Whether the theme support args should be checked. */ - public static function read_theme_support() { + public static function read_theme_support( $check_args = WP_DEBUG ) { self::$support_added_via_option = false; self::$initial_theme_support_args = false; @@ -170,7 +172,7 @@ public static function read_theme_support() { self::$initial_theme_support_args = array_shift( $support ); // Validate theme support usage. - if ( WP_DEBUG ) { + if ( $check_args ) { $keys = array( 'template_dir', 'comments_live_list', 'mode', 'optional', 'templates_supported' ); if ( ! is_array( self::$initial_theme_support_args ) ) { trigger_error( esc_html__( 'Expected AMP theme support arg to be array.', 'amp' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error @@ -199,8 +201,8 @@ public static function read_theme_support() { $theme_support_args = array(); } - // If theme support is not present, then allow it to be added via the admin. - if ( ! current_theme_supports( 'amp' ) ) { + // If theme support is not present (or it is optional), then allow it to be added via the admin. + if ( ! current_theme_supports( 'amp' ) || ! empty( $theme_support_args['optional'] ) ) { if ( 'disabled' === $theme_support_option ) { return; } diff --git a/includes/options/class-amp-options-manager.php b/includes/options/class-amp-options-manager.php index fd052b5f93d..71f3a0f7caa 100644 --- a/includes/options/class-amp-options-manager.php +++ b/includes/options/class-amp-options-manager.php @@ -24,16 +24,14 @@ class AMP_Options_Manager { */ protected static $defaults = array( 'theme_support' => 'disabled', - 'supported_post_types' => array( 'post' ), // @todo Try updating from 0.7 to 1.0. + 'supported_post_types' => array( 'post' ), 'analytics' => array(), 'force_sanitization' => false, 'accept_tree_shaking' => false, 'disable_admin_bar' => false, 'all_templates_supported' => true, - 'unrecognized_templates_supported' => true, - 'supported_templates' => array( - 'is_singular', // @todo Why? - ), + 'unrecognized_templates_supported' => false, + 'supported_templates' => array( 'is_singular' ), ); /** @@ -132,7 +130,6 @@ public static function validate_options( $new_options ) { $options['unrecognized_templates_supported'] = ! empty( $new_options['unrecognized_templates_supported'] ); } - // @todo Store associative array instead? // Validate post type support. $options['supported_post_types'] = array(); if ( isset( $new_options['supported_post_types'] ) ) { @@ -145,7 +142,6 @@ public static function validate_options( $new_options ) { } } - // @todo Store associative array instead. // Validate supported templates. $options['supported_templates'] = array(); if ( isset( $new_options['supported_templates'] ) ) { @@ -201,6 +197,9 @@ public static function validate_options( $new_options ) { } } + // Store the current version with the options so we know the format. + $options['version'] = AMP__VERSION; + return $options; } diff --git a/includes/options/class-amp-options-menu.php b/includes/options/class-amp-options-menu.php index 5c5b70557f5..f2b516aee1b 100644 --- a/includes/options/class-amp-options-menu.php +++ b/includes/options/class-amp-options-menu.php @@ -258,6 +258,7 @@ public function render_validation_handling() {