WordPress.org

Make WordPress Core

Ticket #33723: 33723.3.diff

File 33723.3.diff, 6.0 KB (added by swissspidy, 6 years ago)

Unit tests, now for real

  • src/wp-includes/post-thumbnail-template.php

    diff --git src/wp-includes/post-thumbnail-template.php src/wp-includes/post-thumbnail-template.php
    index 7b8d1a2..045bbb3 100644
     
    1313 * Check if post has an image attached.
    1414 *
    1515 * @since 2.9.0
     16 * @since 4.4.0 Added the ability to pass a post object to `$post`.
    1617 *
    17  * @param int $post_id Optional. Post ID.
     18 * @param int|WP_Post $post Optional. Post ID or post object.
    1819 * @return bool Whether post has an image attached.
    1920 */
    20 function has_post_thumbnail( $post_id = null ) {
    21         return (bool) get_post_thumbnail_id( $post_id );
     21function has_post_thumbnail( $post = null ) {
     22        return (bool) get_post_thumbnail_id( $post );
    2223}
    2324
    2425/**
    2526 * Retrieve Post Thumbnail ID.
    2627 *
    2728 * @since 2.9.0
     29 * @since 4.4.0 Added the ability to pass a post object to `$post`.
    2830 *
    29  * @param int|null $post_id Optional. Post ID.
     31 * @param int|null|WP_Post $post Optional. Post ID or object
    3032 * @return mixed
    3133 */
    32 function get_post_thumbnail_id( $post_id = null ) {
    33         $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
     34function get_post_thumbnail_id( $post = null ) {
     35        $post = get_post( $post );
     36        $post_id = is_a( $post, 'WP_Post' ) ? $post->ID : null;
    3437        return get_post_meta( $post_id, '_thumbnail_id', true );
    3538}
    3639
    function update_post_thumbnail_cache( $wp_query = null ) { 
    9699 * size is used by default, though a different size can be specified instead as needed.
    97100 *
    98101 * @since 2.9.0
     102 * @since 4.4.0 Added the ability to pass a post object to `$post`.
    99103 *
    100  * @param int $post_id       Post ID. Default is the ID of the `$post` global.
     104 * @param int $post          Post ID or post object. Default to the current post.
    101105 * @param string|array $size Optional. Registered image size to use, or flat array of height
    102106 *                           and width values. Default 'post-thumbnail'.
    103107 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
    104108 * @return string
    105109 */
    106 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
    107         $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    108         $post_thumbnail_id = get_post_thumbnail_id( $post_id );
     110function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
     111        $post = get_post( $post );
     112        $post_id = is_a( $post, 'WP_Post' ) ? $post->ID : null;
     113        $post_thumbnail_id = get_post_thumbnail_id( $post );
    109114
    110115        /**
    111116         * Filter the post thumbnail size.
  • new file tests/phpunit/tests/post/post-thumbnail-template.php

    diff --git tests/phpunit/tests/post/post-thumbnail-template.php tests/phpunit/tests/post/post-thumbnail-template.php
    new file mode 100644
    index 0000000..ed212fa
    - +  
     1<?php
     2
     3/**
     4 * @group post
     5 * @group media
     6 */
     7class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
     8        protected $post;
     9        protected $attachment_id;
     10
     11        function setUp() {
     12                parent::setUp();
     13
     14                $this->post          = $this->factory->post->create_and_get();
     15                $file                = DIR_TESTDATA . '/images/canola.jpg';
     16                $this->attachment_id = $this->factory->attachment->create_object( $file, $this->post->ID, array(
     17                        'post_mime_type' => 'image/jpeg',
     18                ) );
     19        }
     20
     21        function test_has_post_thumbnail() {
     22                $this->assertFalse( has_post_thumbnail( $this->post ) );
     23                $this->assertFalse( has_post_thumbnail( $this->post->ID ) );
     24                $this->assertFalse( has_post_thumbnail() );
     25
     26                $GLOBALS['post'] = $this->post;
     27
     28                $this->assertFalse( has_post_thumbnail() );
     29
     30                unset( $GLOBALS['post'] );
     31
     32                set_post_thumbnail( $this->post, $this->attachment_id );
     33
     34                $this->assertTrue( has_post_thumbnail( $this->post ) );
     35                $this->assertTrue( has_post_thumbnail( $this->post->ID ) );
     36                $this->assertFalse( has_post_thumbnail() );
     37
     38                $GLOBALS['post'] = $this->post;
     39
     40                $this->assertTrue( has_post_thumbnail() );
     41        }
     42
     43        function test_get_post_thumbnail_id() {
     44                $this->assertEmpty( get_post_thumbnail_id( $this->post ) );
     45                $this->assertEmpty( get_post_thumbnail_id( $this->post->ID ) );
     46                $this->assertEmpty( get_post_thumbnail_id() );
     47
     48                set_post_thumbnail( $this->post, $this->attachment_id );
     49
     50                $this->assertEquals( $this->attachment_id, get_post_thumbnail_id( $this->post ) );
     51                $this->assertEquals( $this->attachment_id, get_post_thumbnail_id( $this->post->ID ) );
     52
     53                $GLOBALS['post'] = $this->post;
     54
     55                $this->assertEquals( $this->attachment_id, get_post_thumbnail_id() );
     56        }
     57
     58        function test_update_post_thumbnail_cache() {
     59                set_post_thumbnail( $this->post, $this->attachment_id );
     60
     61                $WP_Query = new WP_Query( array(
     62                        'post_type' => 'any',
     63                        'post__in'  => array( $this->post->ID ),
     64                        'orderby'   => 'post__in',
     65                ) );
     66
     67                $this->assertFalse( $WP_Query->thumbnails_cached );
     68
     69                update_post_thumbnail_cache( $WP_Query );
     70
     71                $this->assertTrue( $WP_Query->thumbnails_cached );
     72        }
     73
     74        function test_get_the_post_thumbnail() {
     75                $this->assertEquals( '', get_the_post_thumbnail() );
     76                $this->assertEquals( '', get_the_post_thumbnail( $this->post ) );
     77                set_post_thumbnail( $this->post, $this->attachment_id );
     78
     79                $expected = wp_get_attachment_image( $this->attachment_id, 'post-thumbnail', false, array( 'class' => 'attachment-post-thumbnail wp-post-image' ) );
     80
     81                $this->assertEquals( $expected, get_the_post_thumbnail( $this->post ) );
     82
     83                $GLOBALS['post'] = $this->post;
     84
     85                $this->assertEquals( $expected, get_the_post_thumbnail() );
     86        }
     87
     88        function test_the_post_thumbnail() {
     89                ob_start();
     90                the_post_thumbnail();
     91                $actual = ob_get_clean();
     92
     93                $this->assertEquals( '', $actual );
     94
     95                $GLOBALS['post'] = $this->post;
     96
     97                ob_start();
     98                the_post_thumbnail();
     99                $actual = ob_get_clean();
     100
     101                $this->assertEquals( '', $actual );
     102
     103                set_post_thumbnail( $this->post, $this->attachment_id );
     104
     105                $expected = wp_get_attachment_image( $this->attachment_id, 'post-thumbnail', false, array( 'class' => 'attachment-post-thumbnail wp-post-image' ) );
     106
     107                ob_start();
     108                the_post_thumbnail();
     109                $actual = ob_get_clean();
     110
     111                $this->assertEquals( $expected, $actual );
     112        }
     113}