Skip to content

Commit

Permalink
Let template_availability return supported when no templates match bu…
Browse files Browse the repository at this point in the history
…t all supported

* Fix remaining unit tests
* Mock external HTTP requests

[ci skip]
  • Loading branch information
westonruter committed Jul 1, 2018
1 parent fafb94e commit 95fd958
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 10 deletions.
17 changes: 13 additions & 4 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,19 @@ public static function get_template_availability( $query = null ) {

// If there aren't any matching templates left that are supported, then we consider it to not be available.
if ( ! $matching_template ) {
return array_merge(
$default_response,
array( 'errors' => array( 'no_matching_template' ) )
);
if ( AMP_Options_Manager::get_option( 'all_templates_supported' ) ) {
return array_merge(
$default_response,
array(
'supported' => true,
)
);
} else {
return array_merge(
$default_response,
array( 'errors' => array( 'no_matching_template' ) )
);
}
}
$matching_template = array_merge( $default_response, $matching_template );

Expand Down
15 changes: 15 additions & 0 deletions tests/test-class-amp-hulu-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public function setUp() {
global $post;
parent::setUp();

// Mock the HTTP request.
add_filter( 'pre_http_request', function( $pre, $r, $url ) {
unset( $r );
if ( false === strpos( $url, '771496' ) ) {
return $pre;
}
return array(
'body' => '{"title":"Out of the Box / Run Down Race Car (Doc McStuffins)","author_name":"Disney Junior","type":"video","provider_name":"Hulu","air_date":"Fri Mar 23 00:00:00 UTC 2012","embed_url":"//www.hulu.com/embed.html?eid=_hHzwnAcj3RrXMJFDDvkuw","thumbnail_url":"http://ib.huluim.com/video/60528019?size=240x180&caller=h1o&img=i","width":500,"thumbnail_width":500,"provider_url":"//www.hulu.com/","thumbnail_height":375,"cache_age":3600,"version":"1.0","large_thumbnail_url":"http://ib.huluim.com/video/60528019?size=512x288&caller=h1o&img=i","height":289,"large_thumbnail_width":512,"html":"<iframe width=\\"500\\" height=\\"289\\" src=\\"//www.hulu.com/embed.html?eid=_hHzwnAcj3RrXMJFDDvkuw\\" frameborder=\\"0\\" scrolling=\\"no\\" webkitAllowFullScreen mozallowfullscreen allowfullscreen> </iframe>","duration":1446.25,"large_thumbnail_height":288}',
'response' => array(
'code' => 200,
'message' => 'OK',
),
);
}, 10, 3 );

/*
* As #34115 in 4.9 a post is not needed for context to run oEmbeds. Prior ot 4.9, the WP_Embed::shortcode()
* method would short-circuit when this is the case:
Expand Down
15 changes: 15 additions & 0 deletions tests/test-class-amp-imgur-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public function setUp() {
global $post;
parent::setUp();

// Mock the HTTP request.
add_filter( 'pre_http_request', function( $pre, $r, $url ) {
unset( $r );
if ( false === strpos( $url, 'f462IUj' ) ) {
return $pre;
}
return array(
'body' => '{"version":"1.0","type":"rich","provider_name":"Imgur","provider_url":"https:\\/\\/imgur.com","width":500,"height":750,"html":"<blockquote class=\\"imgur-embed-pub\\" lang=\\"en\\" data-id=\\"f462IUj\\"><a href=\\"https:\\/\\/imgur.com\\/f462IUj\\">Getting that beach body ready<\\/a><\\/blockquote><script async src=\\"\\/\\/s.imgur.com\\/min\\/embed.js\\" charset=\\"utf-8\\"><\\/script>"}', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'response' => array(
'code' => 200,
'message' => 'OK',
),
);
}, 10, 3 );

/*
* As #34115 in 4.9 a post is not needed for context to run oEmbeds. Prior ot 4.9, the WP_Embed::shortcode()
* method would short-circuit when this is the case:
Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function test_render_status() {

// No template is available to render the post.
add_filter( 'amp_supportable_templates', '__return_empty_array' );
AMP_Options_Manager::update_option( 'all_templates_supported', false );
ob_start();
$this->instance->render_status( $post );
$output = ob_get_clean();
Expand Down
9 changes: 8 additions & 1 deletion tests/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public function test_get_and_set_options() {
$this->assertEquals(
array(
'theme_support' => 'disabled',
'supported_post_types' => array(),
'supported_post_types' => array(
'post',
),
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
Expand Down Expand Up @@ -210,6 +212,11 @@ public function test_get_and_set_options() {
*/
public function test_check_supported_post_type_update_errors() {
global $wp_settings_errors;
add_theme_support( 'amp' );
AMP_Options_Manager::update_option( 'all_templates_supported', false );
foreach ( get_post_types() as $post_type ) {
remove_post_type_support( $post_type, 'amp' );
}

register_post_type( 'foo', array(
'public' => true,
Expand Down
2 changes: 1 addition & 1 deletion tests/test-class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function test_add_menu_items() {
// Test add_setting_field().
$this->assertArrayHasKey( 'amp-options', $wp_settings_fields );
$this->assertArrayHasKey( 'general', $wp_settings_fields['amp-options'] );
$this->assertArrayHasKey( 'supported_post_types', $wp_settings_fields['amp-options']['general'] );
$this->assertArrayHasKey( 'supported_templates', $wp_settings_fields['amp-options']['general'] );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/test-class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function test_get_eligible_post_types() {
* @covers AMP_Post_Type_Support::add_post_type_support()
*/
public function test_add_post_type_support() {
remove_theme_support( 'amp' );
register_post_type( 'book', array(
'label' => 'Book',
'public' => true,
Expand All @@ -62,7 +63,7 @@ public function test_add_post_type_support() {
'label' => 'Poem',
'public' => true,
) );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'poem' ) );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post', 'poem' ) );

AMP_Post_Type_Support::add_post_type_support();
$this->assertTrue( post_type_supports( 'post', amp_get_slug() ) );
Expand All @@ -76,6 +77,7 @@ public function test_add_post_type_support() {
* @covers AMP_Post_Type_Support::get_support_errors()
*/
public function test_get_support_error() {
remove_theme_support( 'amp' );
register_post_type( 'book', array(
'label' => 'Book',
'public' => true,
Expand Down
12 changes: 9 additions & 3 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,15 @@ public function test_is_paired_available() {
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
remove_filter( 'amp_skip_post', '__return_true' );

// Check that available_callback works.
// Check that mode=paired works.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
'available_callback' => 'is_singular',
'mode' => 'paired',
) );
add_filter( 'amp_supportable_templates', function( $supportable_templates ) {
$supportable_templates['is_singular']['supported'] = true;
$supportable_templates['is_search']['supported'] = false;
return $supportable_templates;
} );
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_singular() );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
Expand Down Expand Up @@ -1068,6 +1072,8 @@ public function test_prepare_response() {
$wp_widget_factory = new WP_Widget_Factory();
wp_widgets_init();

$this->assertTrue( is_amp_endpoint() );

add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script( 'amp-list' );
} );
Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function test_construct() {
* @see AMP_Widget_Archives::widget().
*/
public function test_widget() {
$this->assertTrue( is_amp_endpoint() );
$arguments = array(
'before_widget' => '<div>',
'after_widget' => '</div>',
Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function test_construct() {
* @covers AMP_Widget_Categories::widget()
*/
public function test_widget() {
$this->assertTrue( is_amp_endpoint() );
$arguments = array(
'before_widget' => '<div>',
'after_widget' => '</div>',
Expand Down

0 comments on commit 95fd958

Please sign in to comment.