Make WordPress Core

Ticket #36571: 36571.5.diff

File 36571.5.diff, 5.8 KB (added by boonebgorges, 7 years ago)
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index 967778a..3f177bd 100644
    function comment_author_email( $comment_ID = 0 ) { 
    138138 * address and use it for their own means good and bad.
    139139 *
    140140 * @since 0.71
     141 * @since 4.6.0 The `$comment` parameter was added.
    141142 *
    142143 * @param string $linktext Optional. Text to display instead of the comment author's email address.
    143144 *                         Default empty.
    144145 * @param string $before   Optional. Text or HTML to display before the email link. Default empty.
    145146 * @param string $after    Optional. Text or HTML to display after the email link. Default empty.
     147 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
    146148 */
    147 function comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
    148         if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
     149function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
     150        if ( $link = get_comment_author_email_link( $linktext, $before, $after, $comment ) ) {
    149151                echo $link;
     152        }
    150153}
    151154
    152155/**
    function comment_author_email_link( $linktext = '', $before = '', $after = '' ) 
    159162 * address and use it for their own means good and bad.
    160163 *
    161164 * @since 2.7.0
     165 * @since 4.6.0 The `$comment` parameter was added.
    162166 *
    163167 * @param string $linktext Optional. Text to display instead of the comment author's email address.
    164168 *                         Default empty.
    165169 * @param string $before   Optional. Text or HTML to display before the email link. Default empty.
    166170 * @param string $after    Optional. Text or HTML to display after the email link. Default empty.
     171 * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
    167172 * @return string
    168173 */
    169 function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
    170         $comment = get_comment();
     174function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
     175        $comment = get_comment( $comment );
     176
    171177        /**
    172178         * Filter the comment author's email for display.
    173179         *
    function get_comment_author_email_link( $linktext = '', $before = '', $after = ' 
    181187         * @param WP_Comment $comment              The comment object.
    182188         */
    183189        $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
     190
    184191        if ((!empty($email)) && ($email != '@')) {
    185192        $display = ($linktext != '') ? $linktext : $email;
    186193                $return  = $before;
  • new file tests/phpunit/tests/comment/getCommentAuthorEmailLink.php

    diff --git tests/phpunit/tests/comment/getCommentAuthorEmailLink.php tests/phpunit/tests/comment/getCommentAuthorEmailLink.php
    new file mode 100644
    index 0000000..82a7307
    - +  
     1<?php
     2/**
     3 * @group comment
     4 */
     5class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase {
     6        public static $comment;
     7
     8        public function setUp() {
     9                parent::setUp();
     10
     11                // Fake the 'comment' global.
     12                $GLOBALS['comment'] = self::$comment;
     13
     14                // Remove obfuscation for testing purposes.
     15                remove_filter( 'comment_email', 'antispambot' );
     16        }
     17
     18        public function tearDown() {
     19                unset( $GLOBALS['comment'] );
     20                parent::tearDown();
     21
     22                add_filter( 'comment_email', 'antispambot' );
     23        }
     24
     25        public static function wpSetUpBeforeClass( $factory ) {
     26                self::$comment = $factory->comment->create_and_get( array(
     27                        'comment_author_email' => 'foo@example.org'
     28                ) );
     29        }
     30
     31        public static function wpTearDownAfterClass() {
     32                wp_delete_comment( self::$comment->comment_ID, true );
     33        }
     34
     35        public function test_global_comment_with_default_parameters() {
     36                $expected = '<a href="mailto:foo@example.org">foo@example.org</a>';
     37
     38                $this->assertEquals( $expected, get_comment_author_email_link() );
     39        }
     40
     41        /**
     42         * @ticket 36571
     43         */
     44        public function test_all_parameters() {
     45                unset( $GLOBALS['comment'] );
     46
     47                $linktext = rand_str( 10 );
     48                $before   = rand_str( 5 );
     49                $after    = rand_str( 6 );
     50                $comment  = self::factory()->comment->create_and_get( array(
     51                        'comment_author_email' => $email = 'baz@example.org'
     52                ) );
     53
     54                $expected = sprintf( '%1$s<a href="mailto:%2$s">%3$s</a>%4$s', $before, $email, $linktext, $after );
     55
     56                $this->assertEquals( $expected, get_comment_author_email_link( $linktext, $before, $after, $comment ) );
     57        }
     58
     59        public function test_all_parameters_with_global_comment() {
     60                $linktext = rand_str( 10 );
     61                $before   = rand_str( 5 );
     62                $after    = rand_str( 6 );
     63
     64                $expected = sprintf( '%1$s<a href="mailto:foo@example.org">%2$s</a>%3$s', $before, $linktext, $after );
     65
     66                $this->assertEquals( $expected, get_comment_author_email_link( $linktext, $before, $after ) );
     67        }
     68
     69        public function test_linktext() {
     70                $expected = sprintf( '<a href="mailto:foo@example.org">%1$s</a>', $linktext = rand_str( 12 ) );
     71
     72                $this->assertEquals( $expected, get_comment_author_email_link( $linktext ) );
     73        }
     74
     75        public function test_before() {
     76                $expected = sprintf( '%1$s<a href="mailto:foo@example.org">foo@example.org</a>', $before = rand_str( 5 ) );
     77
     78                $this->assertEquals( $expected, get_comment_author_email_link( '', $before ) );
     79        }
     80
     81        public function test_after() {
     82                $expected = sprintf( '<a href="mailto:foo@example.org">foo@example.org</a>%1$s', $after = rand_str( 5 ) );
     83
     84                $this->assertEquals( $expected, get_comment_author_email_link( '', '', $after ) );
     85        }
     86
     87        /**
     88         * @ticket 36571
     89         */
     90        public function test_comment_param_should_override_global() {
     91                $comment = self::factory()->comment->create_and_get( array(
     92                        'comment_author_email' => $email = 'bar@example.org'
     93                ) );
     94
     95                $expected = sprintf( '<a href="mailto:%1$s">%2$s</a>', $email, $email );
     96
     97                $this->assertEquals( $expected, get_comment_author_email_link( '', '', '', $comment ) );
     98        }
     99}