Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for post_supports_amp() in is_paired_available() #877

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public static function is_paired_available() {
return false;
}

if ( is_singular() && ! post_supports_amp( get_queried_object() ) ) {
return false;
}

$args = array_shift( $support );

if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) {
Expand Down
70 changes: 70 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Tests for Theme Support.
*
* @package AMP
* @since 0.7
*/

/**
* Tests for Theme Support.
*
* @covers AMP_Theme_Support
*/
class Test_AMP_Theme_Support extends WP_UnitTestCase {

/**
* After a test method runs, reset any state in WordPress the test method might have changed.
*/
public function tearDown() {
parent::tearDown();
remove_theme_support( 'amp' );
}

/**
* Test is_paired_available.
*
* @covers AMP_Theme_Support::is_paired_available()
*/
public function test_is_paired_available() {

// Establish initial state.
$post_id = $this->factory()->post->create( array( 'post_title' => 'Test' ) );
remove_theme_support( 'amp' );
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_singular() );

// Paired support is not available if theme support is not present or canonical.
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
add_theme_support( 'amp' );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );

// Paired mode is available once template_dir is supplied.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
) );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );

// Paired mode not available when post does not support AMP.
add_filter( 'amp_skip_post', '__return_true' );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
$this->assertTrue( is_singular() );
query_posts( array( 's' => 'test' ) ); // phpcs:ignore
$this->assertTrue( is_search() );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
remove_filter( 'amp_skip_post', '__return_true' );

// Check that available_callback works.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
'available_callback' => 'is_singular',
) );
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_singular() );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );

query_posts( array( 's' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_search() );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
}
}