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

Story Poster: Use dimensions array instead of size name #12418

Merged
merged 17 commits into from
Oct 19, 2022
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
6 changes: 3 additions & 3 deletions includes/Model/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ public function load_from_post( $_post ): bool {
$thumbnail_id = (int) get_post_thumbnail_id( $post );

if ( 0 !== $thumbnail_id ) {
$poster_src = wp_get_attachment_image_src( $thumbnail_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$poster_src = wp_get_attachment_image_src( $thumbnail_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS );

if ( $poster_src ) {
[ $poster_url, $width, $height ] = $poster_src;
$this->poster_portrait = $poster_url;
$this->poster_portrait_size = [ (int) $width, (int) $height ];

$size_array = [ (int) $width, (int) $height ];
$image_meta = wp_get_attachment_metadata( $thumbnail_id );
if ( $image_meta ) {
$size_array = [ $image_meta['width'], $image_meta['height'] ];
$this->poster_sizes = (string) wp_calculate_image_sizes( $size_array, $poster_url, $image_meta, $thumbnail_id );
$this->poster_srcset = (string) wp_calculate_image_srcset( $size_array, $poster_url, $image_meta, $thumbnail_id );
}
Expand All @@ -228,7 +228,7 @@ public function load_from_post( $_post ): bool {
$publisher_logo_id = get_post_meta( $this->id, Story_Post_Type::PUBLISHER_LOGO_META_KEY, true );

if ( ! empty( $publisher_logo_id ) ) {
$img_src = wp_get_attachment_image_src( (int) $publisher_logo_id, Image_Sizes::PUBLISHER_LOGO_IMAGE_SIZE );
$img_src = wp_get_attachment_image_src( (int) $publisher_logo_id, Image_Sizes::PUBLISHER_LOGO_IMAGE_DIMENSIONS );

if ( $img_src ) {
[ $src, $width, $height ] = $img_src;
Expand Down
2 changes: 1 addition & 1 deletion includes/REST_API/Stories_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ private function get_story_poster( WP_Post $post ): ?array {
$thumbnail_id = (int) get_post_thumbnail_id( $post );

if ( 0 !== $thumbnail_id ) {
$poster_src = wp_get_attachment_image_src( $thumbnail_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$poster_src = wp_get_attachment_image_src( $thumbnail_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS );
if ( $poster_src ) {
[$url, $width, $height] = $poster_src;

Expand Down
Binary file added tests/phpunit/integration/data/paint.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions tests/phpunit/integration/tests/Model/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function test_load_from_post_with_product(): void {
'productUrl' => 'http://www.example.com/product/t-shirt-with-logo',
],

]
]
);

$story = new \Google\Web_Stories\Model\Story();
Expand All @@ -123,7 +123,7 @@ public function test_load_from_post_with_poster(): void {

$poster_attachment_id = self::factory()->attachment->create_object(
[
'file' => DIR_TESTDATA . '/images/canola.jpg',
'file' => WEB_STORIES_TEST_DATA_DIR . '/paint.jpeg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_title' => 'Test Image',
Expand All @@ -135,9 +135,11 @@ public function test_load_from_post_with_poster(): void {
$story = new \Google\Web_Stories\Model\Story();
$story->load_from_post( $post );

wp_delete_attachment( $poster_attachment_id, true );

$this->assertEquals( $story->get_title(), 'test title' );
$this->assertEquals( $story->get_url(), get_permalink( $post ) );
$this->assertStringContainsString( 'canola.jpg', $story->get_poster_portrait() );
$this->assertStringContainsString( 'paint-640x853.jpeg', $story->get_poster_portrait() );
$this->assertNotEmpty( $story->get_poster_sizes() );
$this->assertIsString( $story->get_poster_sizes() );
$this->assertNotEmpty( $story->get_poster_srcset() );
Expand Down Expand Up @@ -191,7 +193,7 @@ public function test_load_from_post_with_poster_and_poster_meta(): void {

$poster_attachment_id = self::factory()->attachment->create_object(
[
'file' => DIR_TESTDATA . '/images/canola.jpg',
'file' => WEB_STORIES_TEST_DATA_DIR . '/paint.jpeg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_title' => 'Test Image',
Expand All @@ -214,9 +216,11 @@ public function test_load_from_post_with_poster_and_poster_meta(): void {
$story = new \Google\Web_Stories\Model\Story();
$story->load_from_post( $post );

wp_delete_attachment( $poster_attachment_id, true );

$this->assertEquals( $story->get_title(), 'test title' );
$this->assertEquals( $story->get_url(), get_permalink( $post ) );
$this->assertStringContainsString( 'canola.jpg', $story->get_poster_portrait() );
$this->assertStringContainsString( 'paint-640x853.jpeg', $story->get_poster_portrait() );
$this->assertNotEmpty( $story->get_poster_sizes() );
$this->assertIsString( $story->get_poster_sizes() );
$this->assertNotEmpty( $story->get_poster_srcset() );
Expand Down
78 changes: 70 additions & 8 deletions tests/phpunit/integration/tests/REST_API/Stories_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,22 +533,81 @@ public function test_get_item_with_featured_image(): void {
]
);

$attachment_id = self::factory()->attachment->create_upload_object( WEB_STORIES_TEST_DATA_DIR . '/attachment.jpg', 0 );
$attachment_id = self::factory()->attachment->create_upload_object( WEB_STORIES_TEST_DATA_DIR . '/paint.jpeg', 0 );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
set_post_thumbnail( $story, $attachment_id );

$attachment_src = wp_get_attachment_image_src( $attachment_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS );

$request = new WP_REST_Request( \WP_REST_Server::READABLE, '/web-stories/v1/web-story/' . $story );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

wp_delete_attachment( $attachment_id, true );

$this->assertArrayHasKey( 'story_poster', $data );
$this->assertSame( Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS[0], $attachment_src[1] );
$this->assertSame( Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS[1], $attachment_src[2] );
$this->assertEqualSetsWithIndex(
[
'id' => $attachment_id,
'url' => $attachment_src[0],
'width' => $attachment_src[1],
'height' => $attachment_src[2],
'needsProxy' => false,
],
$data['story_poster']
);
}


/**
* @covers ::get_item
* @covers ::get_story_poster
* @covers \Google\Web_Stories\REST_API\Stories_Base_Controller::prepare_links
*/
public function test_get_item_with_featured_image_in_admin(): void {
global $content_width;

$_content_width = $content_width;
$content_width = 400;
$GLOBALS['current_screen'] = convert_to_screen( \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG );
wp_set_current_user( self::$user_id );

$this->controller->register_routes();

$story = self::factory()->post->create(
[
'post_type' => \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG,
'post_status' => 'future',
'post_date' => ( new DateTime( '+1day' ) )->format( 'Y-m-d H:i:s' ),
'post_author' => self::$user_id,
]
);

$attachment_id = self::factory()->attachment->create_upload_object( WEB_STORIES_TEST_DATA_DIR . '/paint.jpeg', 0 );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
set_post_thumbnail( $story, $attachment_id );

$attachment_src = wp_get_attachment_image_src( $attachment_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$attachment_src = wp_get_attachment_image_src( $attachment_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS );

$request = new WP_REST_Request( \WP_REST_Server::READABLE, '/web-stories/v1/web-story/' . $story );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

wp_delete_attachment( $attachment_id, true );

$content_width = $_content_width;

$this->assertArrayHasKey( 'story_poster', $data );
$this->assertSame( Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS[0], $attachment_src[1] );
$this->assertSame( Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS[1], $attachment_src[2] );
$this->assertEqualSetsWithIndex(
[
'id' => $attachment_id,
'url' => $attachment_src[0],
'height' => $attachment_src[1],
'width' => $attachment_src[2],
'width' => $attachment_src[1],
'height' => $attachment_src[2],
'needsProxy' => false,
],
$data['story_poster']
Expand Down Expand Up @@ -620,10 +679,11 @@ public function test_get_item_with_featured_image_and_hotlinked_poster(): void {
]
);

$attachment_id = self::factory()->attachment->create_upload_object( WEB_STORIES_TEST_DATA_DIR . '/attachment.jpg', 0 );
$attachment_id = self::factory()->attachment->create_upload_object( WEB_STORIES_TEST_DATA_DIR . '/paint.jpeg', 0 );
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
set_post_thumbnail( $story, $attachment_id );

$attachment_src = wp_get_attachment_image_src( $attachment_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
$attachment_src = wp_get_attachment_image_src( $attachment_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_DIMENSIONS );

add_post_meta(
$story,
Expand All @@ -640,13 +700,15 @@ public function test_get_item_with_featured_image_and_hotlinked_poster(): void {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

wp_delete_attachment( $attachment_id, true );

$this->assertArrayHasKey( 'story_poster', $data );
$this->assertEqualSetsWithIndex(
[
'id' => $attachment_id,
'url' => $attachment_src[0],
'height' => $attachment_src[1],
'width' => $attachment_src[2],
'width' => $attachment_src[1],
'height' => $attachment_src[2],
'needsProxy' => false,
],
$data['story_poster']
Expand Down