Skip to content

Commit

Permalink
Add test for AMP_Theme_Support::get_template_availability()
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 2, 2018
1 parent 4bde27d commit c172763
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
7 changes: 5 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public static function get_template_availability( $query = null ) {
);

if ( ! ( $query instanceof WP_Query ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'No WP_Query available.', 'amp' ), '1.0' );
_doing_it_wrong( __METHOD__, esc_html__( 'No WP_Query available.', 'amp' ), '1.0' );
return array_merge(
$default_response,
array( 'errors' => array( 'no_query_available' ) )
Expand Down Expand Up @@ -503,6 +503,9 @@ public static function get_template_availability( $query = null ) {

// Make sure children override their parents.
$matching_template_ids = array_keys( $matching_templates );
foreach ( array_diff( array_keys( $supportable_templates ), $matching_template_ids ) as $template_id ) {
unset( $supportable_templates[ $template_id ] );
}
foreach ( $matching_template_ids as $id ) {
$has_children = false;
foreach ( $supportable_templates as $other_id => $supportable_template ) {
Expand Down Expand Up @@ -538,7 +541,7 @@ public static function get_template_availability( $query = null ) {
* Template conditions need to be set up properly to prevent this from happening.
*/
if ( count( $matching_templates ) > 1 ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Did not expect there to be more than one matching template. Did you filter amp_supportable_templates to not honor the template hierarchy?', 'amp' ), '1.0' );
_doing_it_wrong( __METHOD__, esc_html__( 'Did not expect there to be more than one matching template. Did you filter amp_supportable_templates to not honor the template hierarchy?', 'amp' ), '1.0' );
}

$matching_template = array_shift( $matching_templates );
Expand Down
103 changes: 97 additions & 6 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,109 @@ public function test_validate_non_amp_theme() {
$this->assertContains( '<script type="text/javascript" src="https://cdn.ampproject.org/v0/amp-mathml-latest.js" async custom-element="amp-mathml"></script>', $sanitized_html ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}

/**
* Test incorrect usage for get_template_availability.
*
* @expectedIncorrectUsage AMP_Theme_Support::get_template_availability
* @covers AMP_Theme_Support::get_template_availability()
*/
public function test_incorrect_usage_get_template_availability() {
global $wp_query;

// Test no query available.
$wp_query = null; // WPCS: override ok.
$availability = AMP_Theme_Support::get_template_availability();
$this->assertInternalType( 'array', $availability );
$this->assertEquals( array( 'no_query_available' ), $availability['errors'] );
$this->assertFalse( $availability['supported'] );
$this->assertNull( $availability['immutable'] );
$this->assertNull( $availability['template'] );

// Test no theme support.
remove_theme_support( 'amp' );
$this->go_to( get_permalink( $this->factory()->post->create() ) );
$availability = AMP_Theme_Support::get_template_availability();
$this->assertEquals( array( 'no_theme_support' ), $availability['errors'] );
$this->assertFalse( $availability['supported'] );
$this->assertNull( $availability['immutable'] );
$this->assertNull( $availability['template'] );
}

/**
* Test get_template_availability.
*
* @covers AMP_Theme_Support::get_template_availability()
*/
public function test_get_template_availability() {
$this->markTestIncomplete();
// @todo Test nested.
// @todo Test callback vs ID.
// @todo Test with query, post, or page.
// @todo Test without availability of WP_Query (no_query_available).
// @todo Test without theme support (no_theme_support).
global $wp_query;
$post_id = $this->factory()->post->create();
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore

// Test successful match of singular template.
$this->assertTrue( is_singular() );
AMP_Options_Manager::update_option( 'all_templates_supported', false );
add_theme_support( 'amp' );
$availability = AMP_Theme_Support::get_template_availability();
$this->assertEmpty( $availability['errors'] );
$this->assertTrue( $availability['supported'] );
$this->assertFalse( $availability['immutable'] );
$this->assertEquals( 'is_singular', $availability['template'] );

// Test successful match when passing WP_Query and WP_Post into method.
$query = $wp_query;
$wp_query = null; // WPCS: override ok.
$availability = AMP_Theme_Support::get_template_availability( $query );
$this->assertTrue( $availability['supported'] );
$this->assertEquals( 'is_singular', $availability['template'] );
$availability = AMP_Theme_Support::get_template_availability( get_post( $post_id ) );
$this->assertTrue( $availability['supported'] );
$this->assertEquals( 'is_singular', $availability['template'] );
$this->assertNull( $wp_query ); // Make sure it is reset.

// Test nested hierarchy.
AMP_Options_Manager::update_option( 'supported_templates', array( 'is_special' ) );
add_filter( 'amp_supportable_templates', function( $templates ) {
$templates['is_single'] = array(
'label' => 'Single post',
'supported' => false,
'parent' => 'is_singular',
);
$templates['is_special'] = array(
'label' => 'Special post',
'parent' => 'is_single',
'callback' => function( WP_Query $query ) {
return $query->is_singular() && 'special' === get_post( $query->get_queried_object_id() )->post_name;
},
);
$templates['is_page'] = array(
'label' => 'Page',
'supported' => true,
'parent' => 'is_singular',
);
return $templates;
} );

$availability = AMP_Theme_Support::get_template_availability( get_post( $post_id ) );
$this->assertFalse( $availability['supported'] );
$this->assertTrue( $availability['immutable'] );
$this->assertEquals( 'is_single', $availability['template'] );

$special_id = $this->factory()->post->create( array(
'post_type' => 'post',
'post_name' => 'special',
) );
$availability = AMP_Theme_Support::get_template_availability( get_post( $special_id ) );
$this->assertTrue( $availability['supported'] );
$this->assertEquals( 'is_special', $availability['template'] );
$this->assertFalse( $availability['immutable'] );

$availability = AMP_Theme_Support::get_template_availability( $this->factory()->post->create_and_get( array( 'post_type' => 'page' ) ) );
$this->assertFalse( $availability['supported'] );
$this->assertEquals( array( 'post-type-support' ), $availability['errors'] );
$this->assertEquals( 'is_page', $availability['template'] );
add_post_type_support( 'page', 'amp' );
$availability = AMP_Theme_Support::get_template_availability( $this->factory()->post->create_and_get( array( 'post_type' => 'page' ) ) );
$this->assertTrue( $availability['supported'] );
}

/**
Expand Down

0 comments on commit c172763

Please sign in to comment.