Skip to content

Commit

Permalink
Add tests for read_theme_support, is_support_added_via_option, get_th…
Browse files Browse the repository at this point in the history
…eme_support_args

Store version with options to keep track of format
  • Loading branch information
westonruter committed Jul 1, 2018
1 parent 2062816 commit 1c759e5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
10 changes: 6 additions & 4 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 6 additions & 7 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);

/**
Expand Down Expand Up @@ -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'] ) ) {
Expand All @@ -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'] ) ) {
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public function render_validation_handling() {
<script>
jQuery( 'input[type=radio][name="amp-options[theme_support]"]' ).change( function() {
jQuery( '.amp-force-sanitize' ).toggleClass( 'hidden', 'native' === this.value );
jQuery( '.amp-validation-field' ).toggleClass( 'hidden', 'disabled' === this.value );
jQuery( '.amp-force-sanitize-canonical' ).toggleClass( 'hidden', 'native' !== this.value );
jQuery( '#force_sanitization' ).trigger( 'change' );
} ).filter( ':checked' ).trigger( 'change' );
Expand Down
2 changes: 1 addition & 1 deletion tests/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function test_get_and_set_options() {
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'all_templates_supported' => true,
'unrecognized_templates_supported' => true,
'unrecognized_templates_supported' => false,
'supported_templates' => array( 'is_singular' ),
),
AMP_Options_Manager::get_options()
Expand Down
60 changes: 55 additions & 5 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,76 @@ public function test_init() {
$this->assertEquals( 10, has_action( 'widgets_init', array( self::TESTED_CLASS, 'register_widgets' ) ) );
$this->assertEquals( PHP_INT_MAX, has_action( 'wp', array( self::TESTED_CLASS, 'finish_init' ) ) );
$this->assertFalse( isset( $_REQUEST['__amp_source_origin'] ) ); // WPCS: CSRF ok.
}

add_theme_support( 'amp', 'invalid_argumnet_type' );
/**
* Test read_theme_support, get_theme_support_args, and is_support_added_via_option.
*
* @covers \AMP_Theme_Support::read_theme_support()
* @covers \AMP_Theme_Support::is_support_added_via_option()
* @covers \AMP_Theme_Support::get_theme_support_args()
*/
public function test_read_theme_support_and_support_args() {

// Test invalid arg for theme support.
add_theme_support( 'amp', 'invalid_argument_type' );
$e = null;
try {
AMP_Theme_Support::init();
AMP_Theme_Support::read_theme_support( true );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertInstanceOf( 'PHPUnit_Framework_Error_Notice', $e );
$this->assertEquals( 'Expected AMP theme support arg to be array.', $e->getMessage() );
$this->assertTrue( current_theme_supports( 'amp' ) );

add_theme_support( 'amp', array(
// Test invalid args for theme support.
$args = array(
'mode' => 'native',
'invalid_param_key' => array(),
) );
);
add_theme_support( 'amp', $args );
try {
AMP_Theme_Support::init();
AMP_Theme_Support::read_theme_support( true );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertStringStartsWith( 'Expected AMP theme support to keys', $e->getMessage() );
$this->assertEquals( $args, AMP_Theme_Support::get_theme_support_args() );
$this->assertEquals( $args, AMP_Theme_Support::get_theme_support_args( array( 'initial' => true ) ) );
$this->assertTrue( current_theme_supports( 'amp' ) );

// Test behavior of optional flag, that AMP is not enabled if the DB option is not set.
$args = array(
'optional' => true,
'mode' => 'native',
);
AMP_Options_Manager::update_option( 'theme_support', 'disabled' );
add_theme_support( 'amp', $args );
AMP_Theme_Support::read_theme_support();
$this->assertEquals( $args, AMP_Theme_Support::get_theme_support_args( array( 'initial' => true ) ) );
$this->assertFalse( AMP_Theme_Support::get_theme_support_args() );
$this->assertFalse( AMP_Theme_Support::is_support_added_via_option() );
$this->assertFalse( current_theme_supports( 'amp' ) );

// Test again with option set, but some configs supplied via theme support.
AMP_Options_Manager::update_option( 'theme_support', 'native' );
$args = array(
'optional' => true,
'templates_supported' => 'native',
);
add_theme_support( 'amp', $args );
AMP_Theme_Support::read_theme_support();
$this->assertEquals( $args, AMP_Theme_Support::get_theme_support_args( array( 'initial' => true ) ) );
$this->assertEquals(
array_merge(
$args,
array( 'mode' => 'native' )
),
AMP_Theme_Support::get_theme_support_args( array( 'initial' => false ) )
);
$this->assertTrue( AMP_Theme_Support::is_support_added_via_option() );
$this->assertTrue( current_theme_supports( 'amp' ) );
}

/**
Expand Down

0 comments on commit 1c759e5

Please sign in to comment.