Skip to content

Commit

Permalink
Merge pull request #877 from Automattic/update/paired-mode-available
Browse files Browse the repository at this point in the history
Account for post_supports_amp() in is_paired_available()
  • Loading branch information
Thierry Muller authored Jan 22, 2018
2 parents 89e4227 + 31e6797 commit 0428ecc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
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() );
}
}

0 comments on commit 0428ecc

Please sign in to comment.