123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- /**
- * @group post
- * @group media
- */
- class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
- protected static $post;
- protected static $different_post;
- protected static $attachment_id;
- protected $current_size_filter_data = null;
- protected $current_size_filter_result = null;
- public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$post = $factory->post->create_and_get();
- self::$different_post = $factory->post->create_and_get();
- $file = DIR_TESTDATA . '/images/canola.jpg';
- self::$attachment_id = $factory->attachment->create_upload_object(
- $file,
- self::$post->ID,
- array(
- 'post_mime_type' => 'image/jpeg',
- )
- );
- }
- public static function tearDownAfterClass() {
- wp_delete_post( self::$attachment_id, true );
- parent::tearDownAfterClass();
- }
- function test_has_post_thumbnail() {
- $this->assertFalse( has_post_thumbnail( self::$post ) );
- $this->assertFalse( has_post_thumbnail( self::$post->ID ) );
- $this->assertFalse( has_post_thumbnail() );
- $GLOBALS['post'] = self::$post;
- $this->assertFalse( has_post_thumbnail() );
- unset( $GLOBALS['post'] );
- set_post_thumbnail( self::$post, self::$attachment_id );
- $this->assertTrue( has_post_thumbnail( self::$post ) );
- $this->assertTrue( has_post_thumbnail( self::$post->ID ) );
- $this->assertFalse( has_post_thumbnail() );
- $GLOBALS['post'] = self::$post;
- $this->assertTrue( has_post_thumbnail() );
- }
- function test_get_post_thumbnail_id() {
- $this->assertSame( 0, get_post_thumbnail_id( self::$post ) );
- $this->assertSame( 0, get_post_thumbnail_id( self::$post->ID ) );
- $this->assertFalse( get_post_thumbnail_id() );
- set_post_thumbnail( self::$post, self::$attachment_id );
- $this->assertSame( self::$attachment_id, get_post_thumbnail_id( self::$post ) );
- $this->assertSame( self::$attachment_id, get_post_thumbnail_id( self::$post->ID ) );
- $GLOBALS['post'] = self::$post;
- $this->assertSame( self::$attachment_id, get_post_thumbnail_id() );
- }
- function test_update_post_thumbnail_cache() {
- set_post_thumbnail( self::$post, self::$attachment_id );
- $query = new WP_Query(
- array(
- 'post_type' => 'any',
- 'post__in' => array( self::$post->ID ),
- 'orderby' => 'post__in',
- )
- );
- $this->assertFalse( $query->thumbnails_cached );
- update_post_thumbnail_cache( $query );
- $this->assertTrue( $query->thumbnails_cached );
- }
- /**
- * @ticket 12235
- */
- function test_get_the_post_thumbnail_caption() {
- $this->assertSame( '', get_the_post_thumbnail_caption() );
- $caption = 'This is a caption.';
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- 'image.jpg',
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- 'post_excerpt' => $caption,
- )
- );
- set_post_thumbnail( $post_id, $attachment_id );
- $this->assertSame( $caption, get_the_post_thumbnail_caption( $post_id ) );
- }
- /**
- * @ticket 12235
- */
- function test_get_the_post_thumbnail_caption_empty() {
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- 'image.jpg',
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- 'post_excerpt' => '',
- )
- );
- set_post_thumbnail( $post_id, $attachment_id );
- $this->assertSame( '', get_the_post_thumbnail_caption( $post_id ) );
- }
- /**
- * @ticket 12235
- */
- function test_the_post_thumbnail_caption() {
- $caption = 'This is a caption.';
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- 'image.jpg',
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- 'post_excerpt' => $caption,
- )
- );
- set_post_thumbnail( $post_id, $attachment_id );
- $this->expectOutputString( $caption );
- the_post_thumbnail_caption( $post_id );
- }
- function test_get_the_post_thumbnail() {
- $this->assertSame( '', get_the_post_thumbnail() );
- $this->assertSame( '', get_the_post_thumbnail( self::$post ) );
- set_post_thumbnail( self::$post, self::$attachment_id );
- $expected = wp_get_attachment_image(
- self::$attachment_id,
- 'post-thumbnail',
- false,
- array(
- 'class' => 'attachment-post-thumbnail size-post-thumbnail wp-post-image',
- )
- );
- $this->assertSame( $expected, get_the_post_thumbnail( self::$post ) );
- $GLOBALS['post'] = self::$post;
- $this->assertSame( $expected, get_the_post_thumbnail() );
- }
- function test_the_post_thumbnail() {
- $this->expectOutputString( '' );
- the_post_thumbnail();
- $GLOBALS['post'] = self::$post;
- $this->expectOutputString( '' );
- the_post_thumbnail();
- set_post_thumbnail( self::$post, self::$attachment_id );
- $expected = wp_get_attachment_image(
- self::$attachment_id,
- 'post-thumbnail',
- false,
- array(
- 'class' => 'attachment-post-thumbnail size-post-thumbnail wp-post-image',
- )
- );
- $this->expectOutputString( $expected );
- the_post_thumbnail();
- }
- /**
- * @ticket 33070
- */
- function test_get_the_post_thumbnail_url() {
- $this->assertFalse( has_post_thumbnail( self::$post ) );
- $this->assertFalse( get_the_post_thumbnail_url() );
- $this->assertFalse( get_the_post_thumbnail_url( self::$post ) );
- set_post_thumbnail( self::$post, self::$attachment_id );
- $this->assertFalse( get_the_post_thumbnail_url() );
- $this->assertSame( wp_get_attachment_url( self::$attachment_id ), get_the_post_thumbnail_url( self::$post ) );
- $GLOBALS['post'] = self::$post;
- $this->assertSame( wp_get_attachment_url( self::$attachment_id ), get_the_post_thumbnail_url() );
- }
- /**
- * @ticket 33070
- */
- function test_get_the_post_thumbnail_url_with_invalid_post() {
- set_post_thumbnail( self::$post, self::$attachment_id );
- $this->assertNotFalse( get_the_post_thumbnail_url( self::$post->ID ) );
- $deleted = wp_delete_post( self::$post->ID, true );
- $this->assertNotEmpty( $deleted );
- $this->assertFalse( get_the_post_thumbnail_url( self::$post->ID ) );
- }
- /**
- * @ticket 33070
- */
- function test_the_post_thumbnail_url() {
- $GLOBALS['post'] = self::$post;
- $this->expectOutputString( '' );
- the_post_thumbnail_url();
- set_post_thumbnail( self::$post, self::$attachment_id );
- $this->expectOutputString( wp_get_attachment_url( self::$attachment_id ) );
- the_post_thumbnail_url();
- }
- /**
- * @ticket 12922
- */
- function test__wp_preview_post_thumbnail_filter() {
- $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
- $GLOBALS['post'] = self::$post;
- $_REQUEST['_thumbnail_id'] = self::$attachment_id;
- $_REQUEST['preview_id'] = self::$post->ID;
- $result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
- // Clean up.
- $GLOBALS['post'] = $old_post;
- unset( $_REQUEST['_thumbnail_id'] );
- unset( $_REQUEST['preview_id'] );
- $this->assertEquals( self::$attachment_id, $result );
- }
- /**
- * @ticket 37697
- */
- function test__wp_preview_post_thumbnail_filter_secondary_post() {
- $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
- $secondary_post = self::factory()->post->create(
- array(
- 'post_stauts' => 'publish',
- )
- );
- $GLOBALS['post'] = self::$post;
- $_REQUEST['_thumbnail_id'] = self::$attachment_id;
- $_REQUEST['preview_id'] = $secondary_post;
- $result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
- // Clean up.
- $GLOBALS['post'] = $old_post;
- unset( $_REQUEST['_thumbnail_id'] );
- unset( $_REQUEST['preview_id'] );
- $this->assertEmpty( $result );
- }
- /**
- * @ticket 12922
- */
- function test_insert_post_with_post_thumbnail() {
- $post_id = wp_insert_post(
- array(
- 'ID' => self::$post->ID,
- 'post_status' => 'publish',
- 'post_content' => 'Post content',
- 'post_title' => 'Post Title',
- '_thumbnail_id' => self::$attachment_id,
- )
- );
- $thumbnail_id = get_post_thumbnail_id( $post_id );
- $this->assertSame( self::$attachment_id, $thumbnail_id );
- $post_id = wp_insert_post(
- array(
- 'ID' => $post_id,
- 'post_status' => 'publish',
- 'post_content' => 'Post content',
- 'post_title' => 'Post Title',
- '_thumbnail_id' => - 1, // -1 removes post thumbnail.
- )
- );
- $thumbnail_id = get_post_thumbnail_id( $post_id );
- $this->assertEmpty( $thumbnail_id );
- }
- /**
- * @ticket 37658
- */
- function test_insert_attachment_with_post_thumbnail() {
- // Audio files support featured images.
- $post_id = wp_insert_post(
- array(
- 'post_type' => 'attachment',
- 'post_status' => 'inherit',
- 'post_content' => 'Post content',
- 'post_title' => 'Post Title',
- 'post_mime_type' => 'audio/mpeg',
- 'post_parent' => 0,
- 'file' => DIR_TESTDATA . '/audio/test-noise.mp3', // File does not exist, but does not matter here.
- '_thumbnail_id' => self::$attachment_id,
- )
- );
- $thumbnail_id = get_post_thumbnail_id( $post_id );
- $this->assertSame( self::$attachment_id, $thumbnail_id );
- // Images do not support featured images.
- $post_id = wp_insert_post(
- array(
- 'post_type' => 'attachment',
- 'post_status' => 'inherit',
- 'post_content' => 'Post content',
- 'post_title' => 'Post Title',
- 'post_mime_type' => 'image/jpeg',
- 'post_parent' => 0,
- 'file' => DIR_TESTDATA . '/images/canola.jpg',
- '_thumbnail_id' => self::$attachment_id,
- )
- );
- $thumbnail_id = get_post_thumbnail_id( $post_id );
- $this->assertEmpty( $thumbnail_id );
- }
- /**
- * @ticket 39030
- */
- function test_post_thumbnail_size_filter_simple() {
- $this->current_size_filter_data = 'medium';
- add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
- // This filter is used to capture the $size result.
- add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
- get_the_post_thumbnail( self::$post );
- $result = $this->current_size_filter_result;
- $this->current_size_filter_data = null;
- $this->current_size_filter_result = null;
- $this->assertSame( 'medium', $result );
- }
- /**
- * @ticket 39030
- * @dataProvider data_post_thumbnail_size_filter_complex
- */
- function test_post_thumbnail_size_filter_complex( $which_post, $expected ) {
- $this->current_size_filter_data = array(
- self::$post->ID => 'medium',
- self::$different_post->ID => 'thumbnail',
- );
- $post = 1 === $which_post ? self::$different_post : self::$post;
- add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
- // This filter is used to capture the $size result.
- add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
- get_the_post_thumbnail( $post );
- $result = $this->current_size_filter_result;
- $this->current_size_filter_data = null;
- $this->current_size_filter_result = null;
- $this->assertSame( $expected, $result );
- }
- function data_post_thumbnail_size_filter_complex() {
- return array(
- array( 0, 'medium' ),
- array( 1, 'thumbnail' ),
- );
- }
- function filter_post_thumbnail_size( $size, $post_id ) {
- if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) {
- return $this->current_size_filter_data[ $post_id ];
- }
- if ( is_string( $this->current_size_filter_data ) ) {
- return $this->current_size_filter_data;
- }
- return $size;
- }
- function filter_set_post_thumbnail_size_result( $html, $post_id, $post_thumbnail_id, $size ) {
- $this->current_size_filter_result = $size;
- return $html;
- }
- }
|