WordPress.org

Make WordPress Core

Ticket #39955: 39955.4.diff

File 39955.4.diff, 1.6 KB (added by mikeschroder, 2 years ago)

Add tests

  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 6843c61b2..19eba00a6 100644
    function wp_prepare_attachment_for_js( $attachment ) { 
    30983098        );
    30993099
    31003100        $author = new WP_User( $attachment->post_author );
    3101         $response['authorName'] = $author->display_name;
     3101        if ( $author->exists() ) {
     3102                $response['authorName'] = html_entity_decode( $author->display_name, ENT_QUOTES, get_bloginfo( 'charset' ) );
     3103        } else {
     3104                $response['authorName'] = __( '(no author)' );
     3105        }
    31023106
    31033107        if ( $attachment->post_parent ) {
    31043108                $post_parent = get_post( $attachment->post_parent );
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 6b1ec9661..bccab12d3 100644
    https://w.org</a>' 
    267267                $this->assertEquals( 'image', $prepped['mime'] );
    268268                $this->assertEquals( 'image', $prepped['type'] );
    269269                $this->assertEquals( '', $prepped['subtype'] );
     270
     271                // Test that if author is not found, we return "(no author)" as `display_name`.
     272                // The previously used test post contains no author, so we can reuse it.
     273                $this->assertEquals( '(no author)', $prepped['authorName'] );
     274
     275                // Test that if author has HTML entities in display_name, they're decoded correctly.
     276                $html_entity_author = self::factory()->user->create( array(
     277                        'display_name' => 'You &amp; Me',
     278                ) );
     279                $post->post_author = $html_entity_author;
     280                $prepped = wp_prepare_attachment_for_js( $post );
     281                $this->assertEquals( 'You & Me', $prepped['authorName'] );
    270282        }
    271283
    272284        /**