Make WordPress Core

Ticket #36571: 36571.4.diff

File 36571.4.diff, 5.5 KB (added by DrewAPicture, 9 years ago)

Tests

  • src/wp-includes/comment-template.php

     
    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 = 0 ) {
     150        if ( $link = get_comment_author_email_link( $linktext, $before, $after, $comment ) ) {
    149151                echo $link;
     152        }
    150153}
    151154
    152155/**
     
    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 = 0 ) {
     175        $comment = get_comment( $comment );
     176
    171177        /**
    172178         * Filter the comment author's email for display.
    173179         *
     
    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;
  • tests/phpunit/tests/comment/getCommentAuthorEmailLink.php

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