Skip to content

Commit

Permalink
Account for non-set image in schema.org meta; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Aug 19, 2019
1 parent 205d436 commit abba62b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 3 additions & 1 deletion includes/class-amp-story-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public static function filter_schemaorg_metadata_images( $data ) {
return $data;
}

if ( is_string( $data['image'] ) ) {
if ( empty( $data['image'] ) ) {
$data['image'] = [];
} elseif ( is_string( $data['image'] ) ) {
$data['image'] = [ $data['image'] ];
} elseif ( is_array( $data['image'] ) && isset( $data['image']['@type'] ) ) {
$data['image'] = [ $data['image'] ];
Expand Down
57 changes: 56 additions & 1 deletion tests/php/test-class-amp-story-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp() {
$wp_styles = null;
AMP_Options_Manager::update_option( 'experiences', [ AMP_Options_Manager::STORIES_EXPERIENCE ] );

AMP_Story_Media::init();
AMP_Story_Post_Type::register(); // This calls AMP_Story_Media::init().
}

/**
Expand All @@ -52,6 +52,61 @@ public function tearDown() {
parent::tearDown();
}

/**
* Test filter_schemaorg_metadata_images()
*
* @covers AMP_Story_Media::filter_schemaorg_metadata_images
*/
public function test_filter_schemaorg_metadata_images() {
$this->assertSame( [], AMP_Story_Media::filter_schemaorg_metadata_images( [] ) );

$post_id = $this->factory()->post->create( [ 'post_type' => AMP_Story_Post_Type::POST_TYPE_SLUG ] );
$this->go_to( get_permalink( $post_id ) );
$this->assertTrue( is_singular( AMP_Story_Post_Type::POST_TYPE_SLUG ) );

$custom_image_url = 'https://example.com/foo.jpg';
$custom_image_obj = [
'url' => $custom_image_url,
'@type' => 'ImageObject',
];
$fallback_image_url = amp_get_asset_url( 'images/stories-editor/story-fallback-poster.jpg' );

$this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [] ) );
$this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => null ] ) );
$this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => false ] ) );
$this->assertSame( [ 'image' => [ $fallback_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => '' ] ) );

$this->assertSame( [ 'image' => [ $fallback_image_url, $custom_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => $custom_image_url ] ) );
$this->assertSame(
[
'image' => [
$fallback_image_url,
$custom_image_obj,
],
],
AMP_Story_Media::filter_schemaorg_metadata_images(
[
'image' => $custom_image_obj,
]
)
);
$this->assertSame( [ 'image' => [ $fallback_image_url, $custom_image_url ] ], AMP_Story_Media::filter_schemaorg_metadata_images( [ 'image' => $custom_image_url ] ) );
$this->assertSame(
[
'image' => [
$fallback_image_url,
$custom_image_obj,
$custom_image_obj,
],
],
AMP_Story_Media::filter_schemaorg_metadata_images(
[
'image' => [ $custom_image_obj, $custom_image_obj ],
]
)
);
}

/**
* Test rest_api_init()
*
Expand Down

0 comments on commit abba62b

Please sign in to comment.