wordpress-develop/tests/phpunit/tests/oembed/getResponseData.php
Sergey Biryukov 8be943d06e Tests: Introduce assertSameSets() and assertSameSetsWithIndex(), and use them where appropriate.
This ensures that not only the array values being compared are equal, but also that their type is the same.

These new methods replace most of the existing instances of `assertEqualSets()` and `assertEqualSetsWithIndex()`.

Going forward, stricter type checking by using `assertSameSets()` or `assertSameSetsWithIndex()` should generally be preferred, to make the tests more reliable.

Follow-up to [48937].

See #38266.

git-svn-id: https://develop.svn.wordpress.org/trunk@48939 602fd350-edb4-49c9-b593-d223f7449a82
2020-09-04 07:01:00 +00:00

202 lines
5.2 KiB
PHP

<?php
/**
* @group oembed
*/
class Tests_oEmbed_Response_Data extends WP_UnitTestCase {
function test_get_oembed_response_data_non_existent_post() {
$this->assertFalse( get_oembed_response_data( 0, 100 ) );
}
function test_get_oembed_response_data() {
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Some Post',
)
);
$data = get_oembed_response_data( $post, 400 );
$this->assertSameSets(
array(
'version' => '1.0',
'provider_name' => get_bloginfo( 'name' ),
'provider_url' => home_url(),
'author_name' => get_bloginfo( 'name' ),
'author_url' => home_url(),
'title' => 'Some Post',
'type' => 'rich',
'width' => 400,
'height' => 225,
'html' => get_post_embed_html( 400, 225, $post ),
),
$data
);
}
/**
* Test get_oembed_response_data with an author.
*/
function test_get_oembed_response_data_author() {
$user_id = self::factory()->user->create(
array(
'display_name' => 'John Doe',
)
);
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Some Post',
'post_author' => $user_id,
)
);
$data = get_oembed_response_data( $post, 400 );
$this->assertSameSets(
array(
'version' => '1.0',
'provider_name' => get_bloginfo( 'name' ),
'provider_url' => home_url(),
'author_name' => 'John Doe',
'author_url' => get_author_posts_url( $user_id ),
'title' => 'Some Post',
'type' => 'rich',
'width' => 400,
'height' => 225,
'html' => get_post_embed_html( 400, 225, $post ),
),
$data
);
}
function test_get_oembed_response_link() {
remove_filter( 'oembed_response_data', 'get_oembed_response_data_rich' );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Some Post',
)
);
$data = get_oembed_response_data( $post, 600 );
$this->assertSameSets(
array(
'version' => '1.0',
'provider_name' => get_bloginfo( 'name' ),
'provider_url' => home_url(),
'author_name' => get_bloginfo( 'name' ),
'author_url' => home_url(),
'title' => 'Some Post',
'type' => 'link',
),
$data
);
add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );
}
function test_get_oembed_response_data_with_draft_post() {
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'draft',
)
);
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
}
function test_get_oembed_response_data_with_scheduled_post() {
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'future',
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
)
);
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
}
function test_get_oembed_response_data_with_private_post() {
$post = self::factory()->post->create_and_get(
array(
'post_status' => 'private',
)
);
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
}
function test_get_oembed_response_data_maxwidth_too_high() {
$post = self::factory()->post->create_and_get();
$data = get_oembed_response_data( $post, 1000 );
$this->assertSame( 600, $data['width'] );
$this->assertSame( 338, $data['height'] );
}
function test_get_oembed_response_data_maxwidth_too_low() {
$post = self::factory()->post->create_and_get();
$data = get_oembed_response_data( $post, 100 );
$this->assertSame( 200, $data['width'] );
$this->assertSame( 200, $data['height'] );
}
function test_get_oembed_response_data_maxwidth_invalid() {
$post = self::factory()->post->create_and_get();
$data = get_oembed_response_data( $post, '400;" DROP TABLES' );
$this->assertSame( 400, $data['width'] );
$this->assertSame( 225, $data['height'] );
$data = get_oembed_response_data( $post, "lol this isn't even a number?!?!?" );
$this->assertSame( 200, $data['width'] );
$this->assertSame( 200, $data['height'] );
}
function test_get_oembed_response_data_with_thumbnail() {
$post = self::factory()->post->create_and_get();
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = self::factory()->attachment->create_object(
$file,
$post->ID,
array(
'post_mime_type' => 'image/jpeg',
)
);
set_post_thumbnail( $post, $attachment_id );
$data = get_oembed_response_data( $post, 400 );
$this->assertArrayHasKey( 'thumbnail_url', $data );
$this->assertArrayHasKey( 'thumbnail_width', $data );
$this->assertArrayHasKey( 'thumbnail_height', $data );
$this->assertTrue( 400 >= $data['thumbnail_width'] );
}
function test_get_oembed_response_data_for_attachment() {
$parent = self::factory()->post->create();
$file = DIR_TESTDATA . '/images/canola.jpg';
$post = self::factory()->attachment->create_object(
$file,
$parent,
array(
'post_mime_type' => 'image/jpeg',
)
);
$data = get_oembed_response_data( $post, 400 );
$this->assertArrayHasKey( 'thumbnail_url', $data );
$this->assertArrayHasKey( 'thumbnail_width', $data );
$this->assertArrayHasKey( 'thumbnail_height', $data );
$this->assertTrue( 400 >= $data['thumbnail_width'] );
}
}